diff --git a/app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt b/app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt index 46c144af..ecc5f56d 100644 --- a/app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt +++ b/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.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 = 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 } } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt b/app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt index 53d21f1f..174cf17d 100644 --- a/app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt +++ b/app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt @@ -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 = 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 } } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java index 23d89959..cf874b72 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java @@ -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 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 b1f239e2..d727e33d 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.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( diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java index b3962054..64546efc 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java +++ b/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.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); diff --git a/app/src/main/java/awais/instagrabber/utils/CookieUtils.kt b/app/src/main/java/awais/instagrabber/utils/CookieUtils.kt index 83738faf..620aedf1 100644 --- a/app/src/main/java/awais/instagrabber/utils/CookieUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/CookieUtils.kt @@ -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 { diff --git a/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java b/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java index b34c7260..291f52ea 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.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 getCookies(final Context context) { final SettableFuture 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) {