1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Update how FavoriteDataSource and FavoriteRepository get initialised

This commit is contained in:
Ammar Githam 2021-06-12 13:19:25 +09:00
parent dd227751af
commit 77868838b9
8 changed files with 22 additions and 34 deletions

View File

@ -6,7 +6,7 @@ import awais.instagrabber.db.dao.FavoriteDao
import awais.instagrabber.db.entities.Favorite import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.models.enums.FavoriteType import awais.instagrabber.models.enums.FavoriteType
class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDao) { class FavoriteDataSource(private val favoriteDao: FavoriteDao) {
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDao.findFavoriteByQueryAndType(query, type) suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDao.findFavoriteByQueryAndType(query, type)
suspend fun getAllFavorites(): List<Favorite> = favoriteDao.getAllFavorites() suspend fun getAllFavorites(): List<Favorite> = favoriteDao.getAllFavorites()
@ -26,20 +26,14 @@ class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDa
} }
companion object { companion object {
private lateinit var INSTANCE: FavoriteDataSource @Volatile
private var INSTANCE: FavoriteDataSource? = null
@JvmStatic
@Synchronized
fun getInstance(context: Context): FavoriteDataSource { fun getInstance(context: Context): FavoriteDataSource {
if (!this::INSTANCE.isInitialized) { return INSTANCE ?: synchronized(this) {
synchronized(FavoriteDataSource::class.java) { val dao: FavoriteDao = AppDatabase.getDatabase(context).favoriteDao()
if (!this::INSTANCE.isInitialized) { FavoriteDataSource(dao).also { INSTANCE = it }
val database = AppDatabase.getDatabase(context)
INSTANCE = FavoriteDataSource(database.favoriteDao())
} }
} }
} }
return INSTANCE
}
}
} }

View File

@ -1,10 +1,11 @@
package awais.instagrabber.db.repositories package awais.instagrabber.db.repositories
import android.content.Context
import awais.instagrabber.db.datasources.FavoriteDataSource import awais.instagrabber.db.datasources.FavoriteDataSource
import awais.instagrabber.db.entities.Favorite import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.models.enums.FavoriteType import awais.instagrabber.models.enums.FavoriteType
class FavoriteRepository private constructor(private val favoriteDataSource: FavoriteDataSource) { class FavoriteRepository(private val favoriteDataSource: FavoriteDataSource) {
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDataSource.getFavorite(query, type) suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDataSource.getFavorite(query, type)
@ -15,14 +16,14 @@ class FavoriteRepository private constructor(private val favoriteDataSource: Fav
suspend fun deleteFavorite(query: String?, type: FavoriteType?) = favoriteDataSource.deleteFavorite(query, type) suspend fun deleteFavorite(query: String?, type: FavoriteType?) = favoriteDataSource.deleteFavorite(query, type)
companion object { companion object {
private lateinit var instance: FavoriteRepository @Volatile
private var INSTANCE: FavoriteRepository? = null
@JvmStatic fun getInstance(context: Context): FavoriteRepository {
fun getInstance(favoriteDataSource: FavoriteDataSource): FavoriteRepository { return INSTANCE ?: synchronized(this) {
if (!this::instance.isInitialized) { val dataSource: FavoriteDataSource = FavoriteDataSource.getInstance(context)
instance = FavoriteRepository(favoriteDataSource) FavoriteRepository(dataSource).also { INSTANCE = it }
} }
return instance
} }
} }
} }

View File

@ -48,7 +48,6 @@ import awais.instagrabber.asyncs.HashtagPostFetchService;
import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.PrimaryActionModeCallback;
import awais.instagrabber.databinding.FragmentHashtagBinding; import awais.instagrabber.databinding.FragmentHashtagBinding;
import awais.instagrabber.databinding.LayoutHashtagDetailsBinding; import awais.instagrabber.databinding.LayoutHashtagDetailsBinding;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.db.repositories.FavoriteRepository;
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
@ -478,7 +477,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
hashtagDetailsBinding.favChip.setVisibility(View.VISIBLE); hashtagDetailsBinding.favChip.setVisibility(View.VISIBLE);
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context);
favoriteRepository.getFavorite( favoriteRepository.getFavorite(
hashtag, hashtag,
FavoriteType.HASHTAG, FavoriteType.HASHTAG,

View File

@ -46,7 +46,6 @@ import awais.instagrabber.asyncs.LocationPostFetchService;
import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.PrimaryActionModeCallback;
import awais.instagrabber.databinding.FragmentLocationBinding; import awais.instagrabber.databinding.FragmentLocationBinding;
import awais.instagrabber.databinding.LayoutLocationDetailsBinding; import awais.instagrabber.databinding.LayoutLocationDetailsBinding;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.db.repositories.FavoriteRepository;
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
@ -489,8 +488,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR
locationDetailsBinding.btnMap.setOnClickListener(null); locationDetailsBinding.btnMap.setOnClickListener(null);
} }
final FavoriteDataSource dataSource = FavoriteDataSource.getInstance(context); final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context);
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(dataSource);
locationDetailsBinding.favChip.setVisibility(View.VISIBLE); locationDetailsBinding.favChip.setVisibility(View.VISIBLE);
favoriteRepository.getFavorite( favoriteRepository.getFavorite(
String.valueOf(locationId), String.valueOf(locationId),

View File

@ -60,7 +60,6 @@ import awais.instagrabber.customviews.PrimaryActionModeCallback;
import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper; import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper;
import awais.instagrabber.databinding.FragmentProfileBinding; import awais.instagrabber.databinding.FragmentProfileBinding;
import awais.instagrabber.databinding.LayoutProfileDetailsBinding; import awais.instagrabber.databinding.LayoutProfileDetailsBinding;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.db.repositories.FavoriteRepository;
@ -341,7 +340,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
accountRepository = AccountRepository.Companion.getInstance(context); accountRepository = AccountRepository.Companion.getInstance(context);
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); favoriteRepository = FavoriteRepository.Companion.getInstance(context);
appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class); appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
viewModel = new ViewModelProvider(this, new ProfileFragmentViewModelFactory( viewModel = new ViewModelProvider(this, new ProfileFragmentViewModelFactory(
UserRepository.Companion.getInstance(), UserRepository.Companion.getInstance(),
@ -709,7 +708,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
profileDetailsBinding.isPrivate.setVisibility(profileModel.isPrivate() ? View.VISIBLE : View.GONE); profileDetailsBinding.isPrivate.setVisibility(profileModel.isPrivate() ? View.VISIBLE : View.GONE);
setupButtons(profileId); setupButtons(profileId);
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(getContext());
favoriteRepository.getFavorite( favoriteRepository.getFavorite(
profileModel.getUsername(), profileModel.getUsername(),
FavoriteType.USER, FavoriteType.USER,

View File

@ -32,7 +32,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import awais.instagrabber.BuildConfig; import awais.instagrabber.BuildConfig;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Account; import awais.instagrabber.db.entities.Account;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
@ -158,7 +157,7 @@ public final class ExportImportUtils {
LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault()) LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault())
); );
// Log.d(TAG, "importJson: favoriteModel: " + favoriteModel); // Log.d(TAG, "importJson: favoriteModel: " + favoriteModel);
final FavoriteRepository favRepo = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); final FavoriteRepository favRepo = FavoriteRepository.Companion.getInstance(context);
favRepo.getFavorite( favRepo.getFavorite(
query, query,
favoriteType, favoriteType,
@ -365,7 +364,7 @@ public final class ExportImportUtils {
private static ListenableFuture<JSONArray> getFavorites(final Context context) { private static ListenableFuture<JSONArray> getFavorites(final Context context) {
final SettableFuture<JSONArray> future = SettableFuture.create(); final SettableFuture<JSONArray> future = SettableFuture.create();
final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context);
favoriteRepository.getAllFavorites( favoriteRepository.getAllFavorites(
CoroutineUtilsKt.getContinuation((favorites, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { CoroutineUtilsKt.getContinuation((favorites, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
if (throwable != null) { if (throwable != null) {

View File

@ -6,7 +6,6 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import awais.instagrabber.db.datasources.FavoriteDataSource
import awais.instagrabber.db.entities.Favorite import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.db.repositories.FavoriteRepository import awais.instagrabber.db.repositories.FavoriteRepository
import awais.instagrabber.utils.extensions.TAG import awais.instagrabber.utils.extensions.TAG
@ -18,7 +17,7 @@ class FavoritesViewModel(application: Application) : AndroidViewModel(applicatio
private val _list = MutableLiveData<List<Favorite>>() private val _list = MutableLiveData<List<Favorite>>()
val list: LiveData<List<Favorite>> = _list val list: LiveData<List<Favorite>> = _list
private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application)) private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(application)
init { init {
fetch() fetch()

View File

@ -19,7 +19,6 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.datasources.RecentSearchDataSource; import awais.instagrabber.db.datasources.RecentSearchDataSource;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.entities.RecentSearch; import awais.instagrabber.db.entities.RecentSearch;
@ -83,7 +82,7 @@ public class SearchFragmentViewModel extends AppStateViewModel {
distinctQuery = distinctUntilChanged(query); distinctQuery = distinctUntilChanged(query);
searchService = SearchService.getInstance(); searchService = SearchService.getInstance();
recentSearchRepository = RecentSearchRepository.getInstance(RecentSearchDataSource.getInstance(application)); recentSearchRepository = RecentSearchRepository.getInstance(RecentSearchDataSource.getInstance(application));
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application)); favoriteRepository = FavoriteRepository.Companion.getInstance(application);
} }
public LiveData<String> getQuery() { public LiveData<String> getQuery() {