1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package ml.docilealligator.infinityforreddit.asynctasks;
import android.content.SharedPreferences;
import android.os.Handler;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.account.AccountDao;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class AccountManagement {
public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase,
SharedPreferences currentAccountSharedPreferences, Executor executor,
Handler handler, String newAccountName,
SwitchAccountListener switchAccountListener) {
executor.execute(() -> {
redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent();
redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName);
Account account = redditDataRoomDatabase.accountDao().getCurrentAccount();
currentAccountSharedPreferences.edit()
.putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken())
.putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getAccountName())
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply();
currentAccountSharedPreferences.edit().remove(SharedPreferencesUtils.SUBSCRIBED_THINGS_SYNC_TIME).apply();
handler.post(() -> switchAccountListener.switched(account));
});
}
public static void switchToAnonymousMode(RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences currentAccountSharedPreferences,
Executor executor, Handler handler, boolean removeCurrentAccount,
SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener) {
executor.execute(() -> {
AccountDao accountDao = redditDataRoomDatabase.accountDao();
if (removeCurrentAccount) {
accountDao.deleteCurrentAccount();
}
accountDao.markAllAccountsNonCurrent();
String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "");
currentAccountSharedPreferences.edit().clear().apply();
currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply();
handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess);
});
}
public static void removeAccount(RedditDataRoomDatabase redditDataRoomDatabase,
Executor executor, String accoutName) {
executor.execute(() -> {
redditDataRoomDatabase.accountDao().deleteAccount(accoutName);
});
}
public interface SwitchToAnonymousAccountAsyncTaskListener {
void logoutSuccess();
}
public interface SwitchAccountListener {
void switched(Account account);
}
}
|