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

Convert AccountDao, AccountDataSource and AccountRepository to kotlin

This commit is contained in:
Ammar Githam 2021-06-06 20:59:56 +09:00
parent 4a099ef20a
commit c647e22293
9 changed files with 252 additions and 374 deletions

View File

@ -195,6 +195,7 @@ dependencies {
def room_version = "2.3.0" def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-guava:$room_version" implementation "androidx.room:room-guava:$room_version"
implementation "androidx.room:room-ktx:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version"
// CameraX // CameraX

View File

@ -1,34 +1,25 @@
package awais.instagrabber.db.dao; package awais.instagrabber.db.dao
import androidx.room.Dao; import androidx.room.*
import androidx.room.Delete; import awais.instagrabber.db.entities.Account
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
import awais.instagrabber.db.entities.Account;
@Dao @Dao
public interface AccountDao { interface AccountDao {
@Query("SELECT * FROM accounts") @Query("SELECT * FROM accounts")
List<Account> getAllAccounts(); suspend fun getAllAccounts(): List<Account>
@Query("SELECT * FROM accounts WHERE uid = :uid") @Query("SELECT * FROM accounts WHERE uid = :uid")
Account findAccountByUid(String uid); suspend fun findAccountByUid(uid: String): Account?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
List<Long> insertAccounts(Account... accounts); suspend fun insertAccounts(vararg accounts: Account): List<Long>
@Update @Update
void updateAccounts(Account... accounts); suspend fun updateAccounts(vararg accounts: Account)
@Delete @Delete
void deleteAccounts(Account... accounts); suspend fun deleteAccounts(vararg accounts: Account)
@Query("DELETE from accounts") @Query("DELETE from accounts")
void deleteAllAccounts(); suspend fun deleteAllAccounts()
} }

View File

@ -1,68 +1,49 @@
package awais.instagrabber.db.datasources; package awais.instagrabber.db.datasources
import android.content.Context; import android.content.Context
import awais.instagrabber.db.AppDatabase
import awais.instagrabber.db.dao.AccountDao
import awais.instagrabber.db.entities.Account
import androidx.annotation.NonNull; class AccountDataSource private constructor(private val accountDao: AccountDao) {
import androidx.annotation.Nullable; suspend fun getAccount(uid: String): Account? = accountDao.findAccountByUid(uid)
import java.util.List; suspend fun getAllAccounts(): List<Account> = accountDao.getAllAccounts()
import awais.instagrabber.db.AppDatabase; suspend fun insertOrUpdateAccount(
import awais.instagrabber.db.dao.AccountDao; uid: String,
import awais.instagrabber.db.entities.Account; username: String,
cookie: String,
public class AccountDataSource { fullName: String,
private static final String TAG = AccountDataSource.class.getSimpleName(); profilePicUrl: String?,
) {
private static AccountDataSource INSTANCE; val account = getAccount(uid)
val toUpdate = Account(account?.id ?: 0, uid, username, cookie, fullName, profilePicUrl)
private final AccountDao accountDao;
private AccountDataSource(final AccountDao accountDao) {
this.accountDao = accountDao;
}
public static AccountDataSource getInstance(@NonNull Context context) {
if (INSTANCE == null) {
synchronized (AccountDataSource.class) {
if (INSTANCE == null) {
final AppDatabase database = AppDatabase.getDatabase(context);
INSTANCE = new AccountDataSource(database.accountDao());
}
}
}
return INSTANCE;
}
@Nullable
public final Account getAccount(final String uid) {
return accountDao.findAccountByUid(uid);
}
@NonNull
public final List<Account> getAllAccounts() {
return accountDao.getAllAccounts();
}
public final void insertOrUpdateAccount(final String uid,
final String username,
final String cookie,
final String fullName,
final String profilePicUrl) {
final Account account = getAccount(uid);
final Account toUpdate = new Account(account == null ? 0 : account.getId(), uid, username, cookie, fullName, profilePicUrl);
if (account != null) { if (account != null) {
accountDao.updateAccounts(toUpdate); accountDao.updateAccounts(toUpdate)
return; return
} }
accountDao.insertAccounts(toUpdate); accountDao.insertAccounts(toUpdate)
} }
public final void deleteAccount(@NonNull final Account account) { suspend fun deleteAccount(account: Account) = accountDao.deleteAccounts(account)
accountDao.deleteAccounts(account);
}
public final void deleteAllAccounts() { suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts()
accountDao.deleteAllAccounts();
companion object {
private lateinit var INSTANCE: AccountDataSource
@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
}
} }
} }

View File

@ -1,131 +1,49 @@
package awais.instagrabber.db.repositories; package awais.instagrabber.db.repositories
import java.util.List; import awais.instagrabber.db.datasources.AccountDataSource
import awais.instagrabber.db.entities.Account
import awais.instagrabber.db.datasources.AccountDataSource; class AccountRepository private constructor(private val accountDataSource: AccountDataSource) {
import awais.instagrabber.db.entities.Account; suspend fun getAccount(uid: Long): Account? = accountDataSource.getAccount(uid.toString())
import awais.instagrabber.utils.AppExecutors;
public class AccountRepository { suspend fun getAllAccounts(): List<Account> = accountDataSource.getAllAccounts()
private static final String TAG = AccountRepository.class.getSimpleName();
private static AccountRepository instance; suspend fun insertOrUpdateAccounts(accounts: List<Account>) {
for (account in accounts) {
private final AppExecutors appExecutors; accountDataSource.insertOrUpdateAccount(
private final AccountDataSource accountDataSource; account.uid,
account.username,
// private List<Account> cachedAccounts; account.cookie,
account.fullName,
private AccountRepository(final AppExecutors appExecutors, final AccountDataSource accountDataSource) { account.profilePic
this.appExecutors = appExecutors; )
this.accountDataSource = accountDataSource; }
} }
public static AccountRepository getInstance(final AccountDataSource accountDataSource) { suspend fun insertOrUpdateAccount(
if (instance == null) { uid: Long,
instance = new AccountRepository(AppExecutors.INSTANCE, accountDataSource); username: String,
} cookie: String,
return instance; fullName: String,
profilePicUrl: String?,
): Account? {
accountDataSource.insertOrUpdateAccount(uid.toString(), username, cookie, fullName, profilePicUrl)
return accountDataSource.getAccount(uid.toString())
} }
public void getAccount(final long uid, suspend fun deleteAccount(account: Account) = accountDataSource.deleteAccount(account)
final RepositoryCallback<Account> callback) {
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
final Account account = accountDataSource.getAccount(String.valueOf(uid));
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
if (account == null) {
callback.onDataNotAvailable();
return;
}
callback.onSuccess(account);
});
});
}
public void getAllAccounts(final RepositoryCallback<List<Account>> callback) { suspend fun deleteAllAccounts() = accountDataSource.deleteAllAccounts()
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
final List<Account> accounts = accountDataSource.getAllAccounts();
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
if (accounts == null) {
callback.onDataNotAvailable();
return;
}
// cachedAccounts = accounts;
callback.onSuccess(accounts);
});
});
}
public void insertOrUpdateAccounts(final List<Account> accounts, companion object {
final RepositoryCallback<Void> callback) { private lateinit var instance: AccountRepository
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
for (final Account account : accounts) {
accountDataSource.insertOrUpdateAccount(account.getUid(),
account.getUsername(),
account.getCookie(),
account.getFullName(),
account.getProfilePic());
}
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
callback.onSuccess(null);
});
});
}
public void insertOrUpdateAccount(final long uid, @JvmStatic
final String username, fun getInstance(accountDataSource: AccountDataSource): AccountRepository {
final String cookie, if (!this::instance.isInitialized) {
final String fullName, instance = AccountRepository(accountDataSource)
final String profilePicUrl,
final RepositoryCallback<Account> callback) {
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
accountDataSource.insertOrUpdateAccount(String.valueOf(uid), username, cookie, fullName, profilePicUrl);
final Account updated = accountDataSource.getAccount(String.valueOf(uid));
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
if (updated == null) {
callback.onDataNotAvailable();
return;
} }
callback.onSuccess(updated); return instance
});
});
} }
public void deleteAccount(final Account account,
final RepositoryCallback<Void> callback) {
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
accountDataSource.deleteAccount(account);
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
callback.onSuccess(null);
});
});
} }
public void deleteAllAccounts(final RepositoryCallback<Void> callback) {
// request on the I/O thread
appExecutors.getDiskIO().execute(() -> {
accountDataSource.deleteAllAccounts();
// notify on the main thread
appExecutors.getMainThread().execute(() -> {
if (callback == null) return;
callback.onSuccess(null);
});
});
}
} }

View File

@ -3,6 +3,7 @@ package awais.instagrabber.dialogs;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -14,6 +15,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -23,30 +25,29 @@ import awais.instagrabber.databinding.DialogAccountSwitcherBinding;
import awais.instagrabber.db.datasources.AccountDataSource; 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.db.repositories.RepositoryCallback;
import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CookieUtils;
import awais.instagrabber.utils.CoroutineUtilsKt;
import awais.instagrabber.utils.ProcessPhoenix; import awais.instagrabber.utils.ProcessPhoenix;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import kotlinx.coroutines.Dispatchers;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
public class AccountSwitcherDialogFragment extends DialogFragment { public class AccountSwitcherDialogFragment extends DialogFragment {
private static final String TAG = AccountSwitcherDialogFragment.class.getSimpleName();
private AccountRepository accountRepository; private AccountRepository accountRepository;
private OnAddAccountClickListener onAddAccountClickListener; private OnAddAccountClickListener onAddAccountClickListener;
private DialogAccountSwitcherBinding binding; private DialogAccountSwitcherBinding binding;
public AccountSwitcherDialogFragment() { public AccountSwitcherDialogFragment() {}
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext()));
}
public AccountSwitcherDialogFragment(final OnAddAccountClickListener onAddAccountClickListener) { public AccountSwitcherDialogFragment(final OnAddAccountClickListener onAddAccountClickListener) {
this.onAddAccountClickListener = onAddAccountClickListener; this.onAddAccountClickListener = onAddAccountClickListener;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext()));
} }
private final AccountSwitcherAdapter.OnAccountClickListener accountClickListener = (model, isCurrent) -> { private final AccountSwitcherAdapter.OnAccountClickListener accountClickListener = (model, isCurrent) -> {
@ -80,17 +81,15 @@ public class AccountSwitcherDialogFragment extends DialogFragment {
.setMessage(getString(R.string.quick_access_confirm_delete, model.getUsername())) .setMessage(getString(R.string.quick_access_confirm_delete, model.getUsername()))
.setPositiveButton(R.string.yes, (dialog, which) -> { .setPositiveButton(R.string.yes, (dialog, which) -> {
if (accountRepository == null) return; if (accountRepository == null) return;
accountRepository.deleteAccount(model, new RepositoryCallback<Void>() { accountRepository.deleteAccount(
@Override model,
public void onSuccess(final Void result) { CoroutineUtilsKt.getContinuation((unit, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
dismiss(); dismiss();
if (throwable != null) {
Log.e(TAG, "deleteAccount: ", throwable);
} }
}), Dispatchers.getIO())
@Override );
public void onDataNotAvailable() {
dismiss();
}
});
}) })
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.show(); .show();
@ -113,6 +112,12 @@ public class AccountSwitcherDialogFragment extends DialogFragment {
init(); init();
} }
@Override
public void onAttach(@NonNull final Context context) {
super.onAttach(context);
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));
}
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -129,18 +134,19 @@ public class AccountSwitcherDialogFragment extends DialogFragment {
final AccountSwitcherAdapter adapter = new AccountSwitcherAdapter(accountClickListener, accountLongClickListener); final AccountSwitcherAdapter adapter = new AccountSwitcherAdapter(accountClickListener, accountLongClickListener);
binding.accounts.setAdapter(adapter); binding.accounts.setAdapter(adapter);
if (accountRepository == null) return; if (accountRepository == null) return;
accountRepository.getAllAccounts(new RepositoryCallback<List<Account>>() { accountRepository.getAllAccounts(
@Override CoroutineUtilsKt.getContinuation((accounts, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
public void onSuccess(final List<Account> accounts) { if (throwable != null) {
Log.e(TAG, "init: ", throwable);
return;
}
if (accounts == null) return; if (accounts == null) return;
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
sortUserList(cookie, accounts); final List<Account> copy = new ArrayList<>(accounts);
adapter.submitList(accounts); sortUserList(cookie, copy);
} adapter.submitList(copy);
}), Dispatchers.getIO())
@Override );
public void onDataNotAvailable() {}
});
binding.addAccountBtn.setOnClickListener(v -> { binding.addAccountBtn.setOnClickListener(v -> {
if (onAddAccountClickListener == null) return; if (onAddAccountClickListener == null) return;
onAddAccountClickListener.onAddAccountClick(this); onAddAccountClickListener.onAddAccountClick(this);

View File

@ -62,7 +62,6 @@ 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.AccountDataSource;
import awais.instagrabber.db.datasources.FavoriteDataSource; import awais.instagrabber.db.datasources.FavoriteDataSource;
import awais.instagrabber.db.entities.Account;
import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.entities.Favorite;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.db.repositories.FavoriteRepository;
@ -1027,17 +1026,14 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
cookie, cookie,
profileModel.getFullName(), profileModel.getFullName(),
profileModel.getProfilePicUrl(), profileModel.getProfilePicUrl(),
new RepositoryCallback<Account>() { CoroutineUtilsKt.getContinuation((account, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
@Override if (throwable != null) {
public void onSuccess(final Account result) { Log.e(TAG, "updateAccountInfo: ", throwable);
return;
}
accountIsUpdated = true; accountIsUpdated = true;
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {
Log.e(TAG, "onDataNotAvailable: insert failed");
}
});
} }
private void fetchStoryAndHighlights(final long profileId) { private void fetchStoryAndHighlights(final long profileId) {

View File

@ -11,7 +11,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -24,17 +23,13 @@ import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import awais.instagrabber.BuildConfig; import awais.instagrabber.BuildConfig;
import awais.instagrabber.R; 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.datasources.AccountDataSource;
import awais.instagrabber.db.entities.Account;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
import awais.instagrabber.db.repositories.RepositoryCallback;
import awais.instagrabber.dialogs.AccountSwitcherDialogFragment; import awais.instagrabber.dialogs.AccountSwitcherDialogFragment;
import awais.instagrabber.utils.AppExecutors; import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
@ -98,9 +93,19 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
return true; return true;
})); }));
} }
accountRepository.getAllAccounts(new RepositoryCallback<List<Account>>() { accountRepository.getAllAccounts(
@Override CoroutineUtilsKt.getContinuation((accounts, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
public void onSuccess(@NonNull final List<Account> accounts) { if (throwable != null) {
Log.d(TAG, "getAllAccounts", throwable);
if (!isLoggedIn) {
// Need to show something to trigger login activity
accountCategory.addPreference(getPreference(R.string.add_account, R.drawable.ic_add, preference -> {
startActivityForResult(new Intent(getContext(), Login.class), Constants.LOGIN_RESULT_CODE);
return true;
}));
}
return;
}
if (!isLoggedIn) { if (!isLoggedIn) {
if (accounts.size() > 0) { if (accounts.size() > 0) {
final Context context1 = getContext(); final Context context1 = getContext();
@ -131,20 +136,24 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
.setPositiveButton(R.string.yes, (dialog, which) -> { .setPositiveButton(R.string.yes, (dialog, which) -> {
final Context context1 = getContext(); final Context context1 = getContext();
if (context1 == null) return; if (context1 == null) return;
CookieUtils.removeAllAccounts(context1, new RepositoryCallback<Void>() { CookieUtils.removeAllAccounts(
@Override context1,
public void onSuccess(final Void result) { CoroutineUtilsKt.getContinuation(
// shouldRecreate(); (unit, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
final Context context1 = getContext(); if (throwable1 != null) {
if (context1 == null) return; return;
Toast.makeText(context1, R.string.logout_success, Toast.LENGTH_SHORT).show();
settingsHelper.putString(Constants.COOKIE, "");
AppExecutors.INSTANCE.getMainThread().execute(() -> ProcessPhoenix.triggerRebirth(context1), 200);
} }
final Context context2 = getContext();
@Override if (context2 == null) return;
public void onDataNotAvailable() {} Toast.makeText(context2, R.string.logout_success, Toast.LENGTH_SHORT).show();
}); settingsHelper.putString(Constants.COOKIE, "");
AppExecutors.INSTANCE
.getMainThread()
.execute(() -> ProcessPhoenix.triggerRebirth(context1), 200);
}),
Dispatchers.getIO()
)
);
}) })
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.show(); .show();
@ -153,20 +162,8 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
if (preference1 == null) return; if (preference1 == null) return;
accountCategory.addPreference(preference1); accountCategory.addPreference(preference1);
} }
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {
Log.d(TAG, "onDataNotAvailable");
if (!isLoggedIn) {
// Need to show something to trigger login activity
accountCategory.addPreference(getPreference(R.string.add_account, R.drawable.ic_add, preference -> {
startActivityForResult(new Intent(getContext(), Login.class), Constants.LOGIN_RESULT_CODE);
return true;
}));
}
}
});
// final PreferenceCategory generalCategory = new PreferenceCategory(context); // final PreferenceCategory generalCategory = new PreferenceCategory(context);
// generalCategory.setTitle(R.string.pref_category_general); // generalCategory.setTitle(R.string.pref_category_general);
@ -295,31 +292,24 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
return; return;
} }
if (user != null) { if (user != null) {
// Log.d(TAG, "adding userInfo: " + result);
accountRepository.insertOrUpdateAccount( accountRepository.insertOrUpdateAccount(
uid, uid,
user.getUsername(), user.getUsername(),
cookie, cookie,
user.getFullName(), user.getFullName(),
user.getProfilePicUrl(), user.getProfilePicUrl(),
new RepositoryCallback<Account>() { CoroutineUtilsKt.getContinuation((account, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
@Override if (throwable1 != null) {
public void onSuccess(final Account result) { Log.e(TAG, "onActivityResult: ", throwable1);
// final FragmentActivity activity = getActivity(); return;
// if (activity == null) return; }
// activity.recreate();
AppExecutors.INSTANCE.getMainThread().execute(() -> { AppExecutors.INSTANCE.getMainThread().execute(() -> {
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
ProcessPhoenix.triggerRebirth(context); ProcessPhoenix.triggerRebirth(context);
}, 200); }, 200);
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {
Log.e(TAG, "onDataNotAvailable: insert failed");
}
});
} }
}), Dispatchers.getIO())); }), Dispatchers.getIO()));
} }
@ -415,20 +405,21 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
final PrefAccountSwitcherBinding binding = PrefAccountSwitcherBinding.bind(root); final PrefAccountSwitcherBinding binding = PrefAccountSwitcherBinding.bind(root);
final long uid = CookieUtils.getUserIdFromCookie(cookie); final long uid = CookieUtils.getUserIdFromCookie(cookie);
if (uid <= 0) return; if (uid <= 0) return;
accountRepository.getAccount(uid, new RepositoryCallback<Account>() { accountRepository.getAccount(
@Override uid,
public void onSuccess(final Account account) { CoroutineUtilsKt.getContinuation((account, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
if (throwable != null) {
Log.e(TAG, "onBindViewHolder: ", throwable);
return;
}
binding.getRoot().post(() -> { binding.getRoot().post(() -> {
binding.fullName.setText(account.getFullName()); binding.fullName.setText(account.getFullName());
binding.username.setText("@" + account.getUsername()); binding.username.setText("@" + account.getUsername());
binding.profilePic.setImageURI(account.getProfilePic()); binding.profilePic.setImageURI(account.getProfilePic());
binding.getRoot().requestLayout(); binding.getRoot().requestLayout();
}); });
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {}
});
} }
} }
} }

View File

@ -7,7 +7,6 @@ import android.util.Log
import android.webkit.CookieManager import android.webkit.CookieManager
import awais.instagrabber.db.datasources.AccountDataSource import awais.instagrabber.db.datasources.AccountDataSource
import awais.instagrabber.db.repositories.AccountRepository import awais.instagrabber.db.repositories.AccountRepository
import awais.instagrabber.db.repositories.RepositoryCallback
import java.net.CookiePolicy import java.net.CookiePolicy
import java.net.HttpCookie import java.net.HttpCookie
import java.net.URI import java.net.URI
@ -48,14 +47,9 @@ fun setupCookies(cookieRaw: String) {
} }
} }
fun removeAllAccounts(context: Context, callback: RepositoryCallback<Void?>?) { suspend fun removeAllAccounts(context: Context) {
NET_COOKIE_MANAGER.cookieStore.removeAll() NET_COOKIE_MANAGER.cookieStore.removeAll()
try { AccountRepository.getInstance(AccountDataSource.getInstance(context)).deleteAllAccounts()
AccountRepository.getInstance(AccountDataSource.getInstance(context))
.deleteAllAccounts(callback)
} catch (e: Exception) {
Log.e(TAG, "setupCookies", e)
}
} }
fun getUserIdFromCookie(cookies: String?): Long { fun getUserIdFromCookie(cookies: String?): Long {

View File

@ -42,6 +42,7 @@ import awais.instagrabber.db.repositories.RepositoryCallback;
import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.enums.FavoriteType; import awais.instagrabber.models.enums.FavoriteType;
import awais.instagrabber.utils.PasswordUtils.IncorrectPasswordException; import awais.instagrabber.utils.PasswordUtils.IncorrectPasswordException;
import kotlinx.coroutines.Dispatchers;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
@ -396,9 +397,13 @@ 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.getInstance(AccountDataSource.getInstance(context));
accountRepository.getAllAccounts(new RepositoryCallback<List<Account>>() { accountRepository.getAllAccounts(
@Override CoroutineUtilsKt.getContinuation((accounts, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
public void onSuccess(final List<Account> accounts) { if (throwable != null) {
Log.e(TAG, "getCookies: ", throwable);
future.set(new JSONArray());
return;
}
final JSONArray jsonArray = new JSONArray(); final JSONArray jsonArray = new JSONArray();
try { try {
for (final Account cookie : accounts) { for (final Account cookie : accounts) {
@ -416,13 +421,8 @@ public final class ExportImportUtils {
} }
} }
future.set(jsonArray); future.set(jsonArray);
} }), Dispatchers.getIO())
);
@Override
public void onDataNotAvailable() {
future.set(new JSONArray());
}
});
return future; return future;
} }