1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-12 17:57: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.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<Favorite> = 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
}
}
}

View File

@ -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
}
}
}

View File

@ -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,

View File

@ -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),

View File

@ -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,

View File

@ -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<JSONArray> getFavorites(final Context context) {
final SettableFuture<JSONArray> 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) {

View File

@ -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<List<Favorite>>()
val list: LiveData<List<Favorite>> = _list
private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application))
private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(application)
init {
fetch()

View File

@ -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<String> getQuery() {