Browse Source

Update how AccountDataSource and AccountRepository get initialised

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
06508ca78d
  1. 17
      app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt
  2. 15
      app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt
  3. 3
      app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java
  4. 3
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
  5. 3
      app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
  6. 3
      app/src/main/java/awais/instagrabber/utils/CookieUtils.kt
  7. 6
      app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java

17
app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt

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

15
app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt

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

3
app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java

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

3
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.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(

3
app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java

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

3
app/src/main/java/awais/instagrabber/utils/CookieUtils.kt

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

6
app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java

@ -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,7 +198,8 @@ 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
.getInstance(context)
.insertOrUpdateAccounts(accounts, CoroutineUtilsKt.getContinuation((unit, throwable) -> {}, Dispatchers.getIO())); .insertOrUpdateAccounts(accounts, CoroutineUtilsKt.getContinuation((unit, throwable) -> {}, Dispatchers.getIO()));
} }
@ -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) {

Loading…
Cancel
Save