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

Update how AccountDataSource and AccountRepository get initialised

This commit is contained in:
Ammar Githam 2021-06-12 13:02:15 +09:00
parent dad2260ea2
commit 06508ca78d
7 changed files with 22 additions and 30 deletions

View File

@ -5,7 +5,7 @@ import awais.instagrabber.db.AppDatabase
import awais.instagrabber.db.dao.AccountDao import awais.instagrabber.db.dao.AccountDao
import awais.instagrabber.db.entities.Account import awais.instagrabber.db.entities.Account
class AccountDataSource private constructor(private val accountDao: AccountDao) { class AccountDataSource(private val accountDao: AccountDao) {
suspend fun getAccount(uid: String): Account? = accountDao.findAccountByUid(uid) suspend fun getAccount(uid: String): Account? = accountDao.findAccountByUid(uid)
suspend fun getAllAccounts(): List<Account> = accountDao.getAllAccounts() suspend fun getAllAccounts(): List<Account> = accountDao.getAllAccounts()
@ -31,19 +31,14 @@ class AccountDataSource private constructor(private val accountDao: AccountDao)
suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts() suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts()
companion object { companion object {
private lateinit var INSTANCE: AccountDataSource @Volatile
private var INSTANCE: AccountDataSource? = null
@JvmStatic
fun getInstance(context: Context): AccountDataSource { fun getInstance(context: Context): AccountDataSource {
if (!this::INSTANCE.isInitialized) { return INSTANCE ?: synchronized(this) {
synchronized(AccountDataSource::class.java) { val dao: AccountDao = AppDatabase.getDatabase(context).accountDao()
if (!this::INSTANCE.isInitialized) { AccountDataSource(dao).also { INSTANCE = it }
val database = AppDatabase.getDatabase(context)
INSTANCE = AccountDataSource(database.accountDao())
}
}
} }
return INSTANCE
} }
} }
} }

View File

@ -1,9 +1,10 @@
package awais.instagrabber.db.repositories package awais.instagrabber.db.repositories
import android.content.Context
import awais.instagrabber.db.datasources.AccountDataSource import awais.instagrabber.db.datasources.AccountDataSource
import awais.instagrabber.db.entities.Account import awais.instagrabber.db.entities.Account
class AccountRepository private constructor(private val accountDataSource: AccountDataSource) { class AccountRepository(private val accountDataSource: AccountDataSource) {
suspend fun getAccount(uid: Long): Account? = accountDataSource.getAccount(uid.toString()) suspend fun getAccount(uid: Long): Account? = accountDataSource.getAccount(uid.toString())
suspend fun getAllAccounts(): List<Account> = accountDataSource.getAllAccounts() suspend fun getAllAccounts(): List<Account> = accountDataSource.getAllAccounts()
@ -36,14 +37,14 @@ class AccountRepository private constructor(private val accountDataSource: Accou
suspend fun deleteAllAccounts() = accountDataSource.deleteAllAccounts() suspend fun deleteAllAccounts() = accountDataSource.deleteAllAccounts()
companion object { companion object {
private lateinit var instance: AccountRepository @Volatile
private var INSTANCE: AccountRepository? = null
@JvmStatic fun getInstance(context: Context): AccountRepository {
fun getInstance(accountDataSource: AccountDataSource): AccountRepository { return INSTANCE ?: synchronized(this) {
if (!this::instance.isInitialized) { val dataSource: AccountDataSource = AccountDataSource.getInstance(context)
instance = AccountRepository(accountDataSource) AccountRepository(dataSource).also { INSTANCE = it }
} }
return instance
} }
} }
} }

View File

@ -22,7 +22,6 @@ import java.util.List;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.adapters.AccountSwitcherAdapter; import awais.instagrabber.adapters.AccountSwitcherAdapter;
import awais.instagrabber.databinding.DialogAccountSwitcherBinding; import awais.instagrabber.databinding.DialogAccountSwitcherBinding;
import awais.instagrabber.db.datasources.AccountDataSource;
import awais.instagrabber.db.entities.Account; import awais.instagrabber.db.entities.Account;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.AppExecutors;
@ -115,7 +114,7 @@ public class AccountSwitcherDialogFragment extends DialogFragment {
@Override @Override
public void onAttach(@NonNull final Context context) { public void onAttach(@NonNull final Context context) {
super.onAttach(context); super.onAttach(context);
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); accountRepository = AccountRepository.Companion.getInstance(context);
} }
@Override @Override

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.AccountDataSource;
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.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
@ -341,7 +340,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance(); graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); accountRepository = AccountRepository.Companion.getInstance(context);
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.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(

View File

@ -28,7 +28,6 @@ import awais.instagrabber.R;
import awais.instagrabber.activities.Login; import awais.instagrabber.activities.Login;
import awais.instagrabber.activities.MainActivity; import awais.instagrabber.activities.MainActivity;
import awais.instagrabber.databinding.PrefAccountSwitcherBinding; import awais.instagrabber.databinding.PrefAccountSwitcherBinding;
import awais.instagrabber.db.datasources.AccountDataSource;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.dialogs.AccountSwitcherDialogFragment; import awais.instagrabber.dialogs.AccountSwitcherDialogFragment;
import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.AppExecutors;
@ -74,7 +73,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
// screen.addPreference(new MoreHeaderPreference(getContext())); // screen.addPreference(new MoreHeaderPreference(getContext()));
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); accountRepository = AccountRepository.Companion.getInstance(context);
final PreferenceCategory accountCategory = new PreferenceCategory(context); final PreferenceCategory accountCategory = new PreferenceCategory(context);
accountCategory.setTitle(R.string.account); accountCategory.setTitle(R.string.account);
accountCategory.setIconSpaceReserved(false); accountCategory.setIconSpaceReserved(false);

View File

@ -5,7 +5,6 @@ package awais.instagrabber.utils
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import android.webkit.CookieManager import android.webkit.CookieManager
import awais.instagrabber.db.datasources.AccountDataSource
import awais.instagrabber.db.repositories.AccountRepository import awais.instagrabber.db.repositories.AccountRepository
import java.net.CookiePolicy import java.net.CookiePolicy
import java.net.HttpCookie import java.net.HttpCookie
@ -49,7 +48,7 @@ fun setupCookies(cookieRaw: String) {
suspend fun removeAllAccounts(context: Context) { suspend fun removeAllAccounts(context: Context) {
NET_COOKIE_MANAGER.cookieStore.removeAll() NET_COOKIE_MANAGER.cookieStore.removeAll()
AccountRepository.getInstance(AccountDataSource.getInstance(context)).deleteAllAccounts() AccountRepository.getInstance(context).deleteAllAccounts()
} }
fun getUserIdFromCookie(cookies: String?): Long { fun getUserIdFromCookie(cookies: String?): Long {

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.AccountDataSource;
import awais.instagrabber.db.datasources.FavoriteDataSource; 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;
@ -199,8 +198,9 @@ public final class ExportImportUtils {
Log.e(TAG, "importAccounts: Error parsing json", e); Log.e(TAG, "importAccounts: Error parsing json", e);
return; return;
} }
AccountRepository.getInstance(AccountDataSource.getInstance(context)) AccountRepository.Companion
.insertOrUpdateAccounts(accounts, CoroutineUtilsKt.getContinuation((unit, throwable) -> {}, Dispatchers.getIO())); .getInstance(context)
.insertOrUpdateAccounts(accounts, CoroutineUtilsKt.getContinuation((unit, throwable) -> {}, Dispatchers.getIO()));
} }
private static void importSettings(final JSONObject jsonObject) { private static void importSettings(final JSONObject jsonObject) {
@ -397,7 +397,7 @@ public final class ExportImportUtils {
private static ListenableFuture<JSONArray> getCookies(final Context context) { private static ListenableFuture<JSONArray> getCookies(final Context context) {
final SettableFuture<JSONArray> future = SettableFuture.create(); final SettableFuture<JSONArray> future = SettableFuture.create();
final AccountRepository accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); final AccountRepository accountRepository = AccountRepository.Companion.getInstance(context);
accountRepository.getAllAccounts( accountRepository.getAllAccounts(
CoroutineUtilsKt.getContinuation((accounts, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { CoroutineUtilsKt.getContinuation((accounts, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
if (throwable != null) { if (throwable != null) {