1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +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.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 getAllAccounts(): List<Account> = accountDao.getAllAccounts()
@ -31,19 +31,14 @@ class AccountDataSource private constructor(private val accountDao: AccountDao)
suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts()
companion object {
private lateinit var INSTANCE: AccountDataSource
@Volatile
private var INSTANCE: AccountDataSource? = null
@JvmStatic
fun getInstance(context: Context): AccountDataSource {
if (!this::INSTANCE.isInitialized) {
synchronized(AccountDataSource::class.java) {
if (!this::INSTANCE.isInitialized) {
val database = AppDatabase.getDatabase(context)
INSTANCE = AccountDataSource(database.accountDao())
}
}
return INSTANCE ?: synchronized(this) {
val dao: AccountDao = AppDatabase.getDatabase(context).accountDao()
AccountDataSource(dao).also { INSTANCE = it }
}
return INSTANCE
}
}
}

View File

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

View File

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

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.AccountDataSource;
import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.AccountRepository;
@ -341,7 +340,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
final Context context = getContext();
if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));
accountRepository = AccountRepository.Companion.getInstance(context);
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
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.MainActivity;
import awais.instagrabber.databinding.PrefAccountSwitcherBinding;
import awais.instagrabber.db.datasources.AccountDataSource;
import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.dialogs.AccountSwitcherDialogFragment;
import awais.instagrabber.utils.AppExecutors;
@ -74,7 +73,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
// screen.addPreference(new MoreHeaderPreference(getContext()));
final Context context = getContext();
if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));
accountRepository = AccountRepository.Companion.getInstance(context);
final PreferenceCategory accountCategory = new PreferenceCategory(context);
accountCategory.setTitle(R.string.account);
accountCategory.setIconSpaceReserved(false);

View File

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

View File

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