From 77868838b973baf144c64646a126f088b173df2d Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 12 Jun 2021 13:19:25 +0900 Subject: [PATCH] Update how FavoriteDataSource and FavoriteRepository get initialised --- .../db/datasources/FavoriteDataSource.kt | 18 ++++++------------ .../db/repositories/FavoriteRepository.kt | 15 ++++++++------- .../fragments/HashTagFragment.java | 3 +-- .../fragments/LocationFragment.java | 4 +--- .../fragments/main/ProfileFragment.java | 5 ++--- .../instagrabber/utils/ExportImportUtils.java | 5 ++--- .../viewmodels/FavoritesViewModel.kt | 3 +-- .../viewmodels/SearchFragmentViewModel.java | 3 +-- 8 files changed, 22 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt b/app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt index 44554b4d..7b01dc76 100644 --- a/app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt +++ b/app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt @@ -6,7 +6,7 @@ import awais.instagrabber.db.dao.FavoriteDao import awais.instagrabber.db.entities.Favorite 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 getAllFavorites(): List = favoriteDao.getAllFavorites() @@ -26,20 +26,14 @@ class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDa } companion object { - private lateinit var INSTANCE: FavoriteDataSource + @Volatile + private var INSTANCE: FavoriteDataSource? = null - @JvmStatic - @Synchronized fun getInstance(context: Context): FavoriteDataSource { - if (!this::INSTANCE.isInitialized) { - synchronized(FavoriteDataSource::class.java) { - if (!this::INSTANCE.isInitialized) { - val database = AppDatabase.getDatabase(context) - INSTANCE = FavoriteDataSource(database.favoriteDao()) - } - } + return INSTANCE ?: synchronized(this) { + val dao: FavoriteDao = AppDatabase.getDatabase(context).favoriteDao() + FavoriteDataSource(dao).also { INSTANCE = it } } - return INSTANCE } } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt b/app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt index cded8cf2..acec8e5c 100644 --- a/app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt +++ b/app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt @@ -1,10 +1,11 @@ package awais.instagrabber.db.repositories +import android.content.Context import awais.instagrabber.db.datasources.FavoriteDataSource import awais.instagrabber.db.entities.Favorite 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) @@ -15,14 +16,14 @@ class FavoriteRepository private constructor(private val favoriteDataSource: Fav suspend fun deleteFavorite(query: String?, type: FavoriteType?) = favoriteDataSource.deleteFavorite(query, type) companion object { - private lateinit var instance: FavoriteRepository + @Volatile + private var INSTANCE: FavoriteRepository? = null - @JvmStatic - fun getInstance(favoriteDataSource: FavoriteDataSource): FavoriteRepository { - if (!this::instance.isInitialized) { - instance = FavoriteRepository(favoriteDataSource) + fun getInstance(context: Context): FavoriteRepository { + return INSTANCE ?: synchronized(this) { + val dataSource: FavoriteDataSource = FavoriteDataSource.getInstance(context) + FavoriteRepository(dataSource).also { INSTANCE = it } } - return instance } } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index f9f68b91..185f186d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -48,7 +48,6 @@ import awais.instagrabber.asyncs.HashtagPostFetchService; import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.databinding.FragmentHashtagBinding; import awais.instagrabber.databinding.LayoutHashtagDetailsBinding; -import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; @@ -478,7 +477,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe hashtagDetailsBinding.favChip.setVisibility(View.VISIBLE); final Context context = getContext(); if (context == null) return; - final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); + final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context); favoriteRepository.getFavorite( hashtag, FavoriteType.HASHTAG, diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index 0f87318f..a8d1d6df 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -46,7 +46,6 @@ import awais.instagrabber.asyncs.LocationPostFetchService; import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.databinding.FragmentLocationBinding; import awais.instagrabber.databinding.LayoutLocationDetailsBinding; -import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; @@ -489,8 +488,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR locationDetailsBinding.btnMap.setOnClickListener(null); } - final FavoriteDataSource dataSource = FavoriteDataSource.getInstance(context); - final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(dataSource); + final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context); locationDetailsBinding.favChip.setVisibility(View.VISIBLE); favoriteRepository.getFavorite( String.valueOf(locationId), diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 56275695..e008387c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -60,7 +60,6 @@ import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper; import awais.instagrabber.databinding.FragmentProfileBinding; import awais.instagrabber.databinding.LayoutProfileDetailsBinding; -import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.FavoriteRepository; @@ -341,7 +340,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final Context context = getContext(); if (context == null) return; accountRepository = AccountRepository.Companion.getInstance(context); - favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); + favoriteRepository = FavoriteRepository.Companion.getInstance(context); appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class); viewModel = new ViewModelProvider(this, new ProfileFragmentViewModelFactory( UserRepository.Companion.getInstance(), @@ -709,7 +708,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe profileDetailsBinding.isPrivate.setVisibility(profileModel.isPrivate() ? View.VISIBLE : View.GONE); setupButtons(profileId); - final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); + final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(getContext()); favoriteRepository.getFavorite( profileModel.getUsername(), FavoriteType.USER, diff --git a/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java b/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java index 291f52ea..cc03534d 100755 --- a/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java @@ -32,7 +32,6 @@ import java.util.List; import java.util.Map; import awais.instagrabber.BuildConfig; -import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.entities.Account; import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.repositories.AccountRepository; @@ -158,7 +157,7 @@ public final class ExportImportUtils { LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault()) ); // Log.d(TAG, "importJson: favoriteModel: " + favoriteModel); - final FavoriteRepository favRepo = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); + final FavoriteRepository favRepo = FavoriteRepository.Companion.getInstance(context); favRepo.getFavorite( query, favoriteType, @@ -365,7 +364,7 @@ public final class ExportImportUtils { private static ListenableFuture getFavorites(final Context context) { final SettableFuture future = SettableFuture.create(); - final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); + final FavoriteRepository favoriteRepository = FavoriteRepository.Companion.getInstance(context); favoriteRepository.getAllFavorites( CoroutineUtilsKt.getContinuation((favorites, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { if (throwable != null) { diff --git a/app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt index bdf2381f..62a75ecb 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt @@ -6,7 +6,6 @@ import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope -import awais.instagrabber.db.datasources.FavoriteDataSource import awais.instagrabber.db.entities.Favorite import awais.instagrabber.db.repositories.FavoriteRepository import awais.instagrabber.utils.extensions.TAG @@ -18,7 +17,7 @@ class FavoritesViewModel(application: Application) : AndroidViewModel(applicatio private val _list = MutableLiveData>() val list: LiveData> = _list - private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application)) + private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(application) init { fetch() diff --git a/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java index 6ddb915e..8bdcfb08 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.datasources.RecentSearchDataSource; import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.RecentSearch; @@ -83,7 +82,7 @@ public class SearchFragmentViewModel extends AppStateViewModel { distinctQuery = distinctUntilChanged(query); searchService = SearchService.getInstance(); recentSearchRepository = RecentSearchRepository.getInstance(RecentSearchDataSource.getInstance(application)); - favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application)); + favoriteRepository = FavoriteRepository.Companion.getInstance(application); } public LiveData getQuery() {