Finish MainActivity if clicking a notification for another user.

This commit is contained in:
Alex Ning 2019-08-19 17:57:04 +08:00
parent 5bf002629d
commit 556047aa42
14 changed files with 199 additions and 111 deletions

View File

@ -37,6 +37,9 @@ public interface AccountDao {
@Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1")
Account getCurrentAccount(); Account getCurrentAccount();
@Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1")
LiveData<Account> getCurrentAccountLiveData();
@Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " + @Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " +
"karma = :karma WHERE username = :username") "karma = :karma WHERE username = :username")
void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma);

View File

@ -10,19 +10,13 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountRepository { public class AccountRepository {
private AccountDao mAccountDao; private AccountDao mAccountDao;
private LiveData<Account> mAccountLiveData;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData; private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) {
mAccountDao = redditDataRoomDatabase.accountDao(); mAccountDao = redditDataRoomDatabase.accountDao();
mAccountLiveData = mAccountDao.getAccountLiveData(username);
mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(); mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData();
} }
LiveData<Account> getAccountLiveData() {
return mAccountLiveData;
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() { public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData; return mAccountsExceptCurrentAccountLiveData;
} }

View File

@ -14,20 +14,14 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountViewModel extends AndroidViewModel { public class AccountViewModel extends AndroidViewModel {
private AccountRepository mAccountRepository; private AccountRepository mAccountRepository;
private LiveData<Account> mAccountLiveData;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData; private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) {
super(application); super(application);
mAccountRepository = new AccountRepository(redditDataRoomDatabase, id); mAccountRepository = new AccountRepository(redditDataRoomDatabase, id);
mAccountLiveData = mAccountRepository.getAccountLiveData();
mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData(); mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData();
} }
public LiveData<Account> getAccountLiveData() {
return mAccountLiveData;
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() { public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData; return mAccountsExceptCurrentAccountLiveData;
} }

View File

@ -44,6 +44,9 @@ import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
@ -85,9 +88,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.all_drawer_items_linear_layout_main_activity) LinearLayout allDrawerItemsLinearLayout; @BindView(R.id.all_drawer_items_linear_layout_main_activity) LinearLayout allDrawerItemsLinearLayout;
@BindView(R.id.account_label_main_activity) TextView accountLabelTextView;
@BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout; @BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout;
@BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout; @BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout;
@BindView(R.id.inbox_linear_layout_main_activity) LinearLayout inboxLinearLayout; @BindView(R.id.inbox_linear_layout_main_activity) LinearLayout inboxLinearLayout;
@BindView(R.id.post_label_main_activity) TextView postLabelTextView;
@BindView(R.id.upvoted_linear_layout_main_activity) LinearLayout upvotedLinearLayout; @BindView(R.id.upvoted_linear_layout_main_activity) LinearLayout upvotedLinearLayout;
@BindView(R.id.downvoted_linear_layout_main_activity) LinearLayout downvotedLinearLayout; @BindView(R.id.downvoted_linear_layout_main_activity) LinearLayout downvotedLinearLayout;
@BindView(R.id.hidden_linear_layout_main_activity) LinearLayout hiddenLinearLayout; @BindView(R.id.hidden_linear_layout_main_activity) LinearLayout hiddenLinearLayout;
@ -146,6 +151,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -241,36 +248,57 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} }
private void getCurrentAccountAndBindView() { private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> { if(mNewAccountName != null) {
mNewAccountName = null; if(account == null || !account.getUsername().equals(mNewAccountName)) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if(account == null) { mNewAccountName = null;
mNullAccessToken = true; if(newAccount == null) {
} else { mNullAccessToken = true;
mAccessToken = account.getAccessToken(); } else {
mAccountName = account.getUsername(); mAccessToken = newAccount.getAccessToken();
mProfileImageUrl = account.getProfileImageUrl(); mAccountName = newAccount.getUsername();
mBannerImageUrl = account.getBannerImageUrl(); mProfileImageUrl = newAccount.getProfileImageUrl();
mKarma = account.getKarma(); mBannerImageUrl = newAccount.getBannerImageUrl();
mKarma = newAccount.getKarma();
Constraints constraints = new Constraints.Builder() Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build(); .build();
PeriodicWorkRequest pullNotificationRequest = PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS) new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
}
bindView();
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mProfileImageUrl = account.getProfileImageUrl();
mBannerImageUrl = account.getBannerImageUrl();
mKarma = account.getKarma();
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
}
bindView(); bindView();
}).execute(); }
}).execute(); } else {
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
@ -292,12 +320,14 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
} }
bindView(); bindView();
}).execute(); }
} }).execute();
} }
private void bindView() { private void bindView() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(2);
@ -328,7 +358,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
new AccountRecyclerViewAdapter.ItemSelectedListener() { new AccountRecyclerViewAdapter.ItemSelectedListener() {
@Override @Override
public void accountSelected(Account account) { public void accountSelected(Account account) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), newAccount -> {
Intent intent = new Intent(MainActivity.this, MainActivity.class); Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent); startActivity(intent);
finish(); finish();
@ -436,9 +466,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} else { } else {
mKarmaTextView.setText(R.string.press_here_to_login); mKarmaTextView.setText(R.string.press_here_to_login);
mAccountNameTextView.setText(R.string.anonymous_account); mAccountNameTextView.setText(R.string.anonymous_account);
accountLabelTextView.setVisibility(View.GONE);
profileLinearLayout.setVisibility(View.GONE); profileLinearLayout.setVisibility(View.GONE);
subscriptionLinearLayout.setVisibility(View.GONE); subscriptionLinearLayout.setVisibility(View.GONE);
inboxLinearLayout.setVisibility(View.GONE); inboxLinearLayout.setVisibility(View.GONE);
postLabelTextView.setVisibility(View.GONE);
upvotedLinearLayout.setVisibility(View.GONE); upvotedLinearLayout.setVisibility(View.GONE);
downvotedLinearLayout.setVisibility(View.GONE); downvotedLinearLayout.setVisibility(View.GONE);
hiddenLinearLayout.setVisibility(View.GONE); hiddenLinearLayout.setVisibility(View.GONE);
@ -666,6 +698,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override @Override
public void sortTypeSelected(String sortType) { public void sortTypeSelected(String sortType) {
sectionsPagerAdapter.changeSortType(sortType); sectionsPagerAdapter.changeSortType(sortType);
@ -693,6 +731,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} }
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {
private PostFragment frontPagePostFragment; private PostFragment frontPagePostFragment;
private PostFragment popularPostFragment; private PostFragment popularPostFragment;

View File

@ -213,6 +213,7 @@ class ParseComment {
} }
static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException { static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException {
Log.i("adfasdf", singleCommentData.toString());
String id = singleCommentData.getString(JSONUtils.ID_KEY); String id = singleCommentData.getString(JSONUtils.ID_KEY);
String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); String fullName = singleCommentData.getString(JSONUtils.NAME_KEY);
String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY);

View File

@ -60,8 +60,6 @@ public class PullNotificationWorker extends Worker {
Account account = accounts.get(accountIndex); Account account = accounts.get(accountIndex);
String accountName = account.getUsername(); String accountName = account.getUsername();
Log.i("workmanager", accountName + " " + account.getAccessToken());
Response<String> response = fetchMessages(account); Response<String> response = fetchMessages(account);
if(response != null && response.isSuccessful()) { if(response != null && response.isSuccessful()) {

View File

@ -2,13 +2,16 @@ package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask; import android.os.AsyncTask;
import Account.Account;
class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> { class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> {
interface SwitchAccountAsyncTaskListener { interface SwitchAccountAsyncTaskListener {
void switched(); void switched(Account account);
} }
private RedditDataRoomDatabase redditDataRoomDatabase; private RedditDataRoomDatabase redditDataRoomDatabase;
private String newAccountName; private String newAccountName;
private Account account;
private SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener; private SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener;
SwitchAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String newAccountName, SwitchAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String newAccountName,
@ -22,11 +25,12 @@ class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent(); redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent();
redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName); redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName);
account = redditDataRoomDatabase.accountDao().getCurrentAccount();
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
switchAccountAsyncTaskListener.switched(); switchAccountAsyncTaskListener.switched(account);
} }
} }

View File

@ -0,0 +1,5 @@
package ml.docilealligator.infinityforreddit;
public class SwitchAccountEvent {
public void accountSwitched(){}
}

View File

@ -28,6 +28,8 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -146,30 +148,34 @@ public class ViewMessageActivity extends AppCompatActivity {
} }
private void getCurrentAccountAndFetchMessage() { private void getCurrentAccountAndFetchMessage() {
if(mNewAccountName != null) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> { if(mNewAccountName != null) {
mNewAccountName = null; if(account == null || !account.getUsername().equals(mNewAccountName)) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if(account == null) { EventBus.getDefault().post(new SwitchAccountEvent());
mNullAccessToken = true; mNewAccountName = null;
} else { if(newAccount == null) {
mAccessToken = account.getAccessToken(); mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
}
bindView(); bindView();
} }).execute();
}).execute(); } else {
}).execute(); mAccessToken = account.getAccessToken();
} else { bindView();
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { }
} else {
if(account == null) { if(account == null) {
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
mAccessToken = account.getAccessToken(); mAccessToken = account.getAccessToken();
bindView();
} }
}).execute();
} bindView();
}
}).execute();
} }
private void bindView() { private void bindView() {

View File

@ -218,22 +218,27 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} }
private void getCurrentAccountAndBindView() { private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> { if(mNewAccountName != null) {
mNewAccountName = null; if(account == null || !account.getUsername().equals(mNewAccountName)) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if(account == null) { EventBus.getDefault().post(new SwitchAccountEvent());
mNullAccessToken = true; mNewAccountName = null;
} else { if(newAccount == null) {
mAccessToken = account.getAccessToken(); mNullAccessToken = true;
mAccountName = account.getUsername(); } else {
} mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
bindView();
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
bindView(); bindView();
}).execute(); }
}).execute(); } else {
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
@ -242,8 +247,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} }
bindView(); bindView();
}).execute(); }
} }).execute();
} }
private void bindView() { private void bindView() {

View File

@ -31,6 +31,8 @@ import com.google.android.material.chip.Chip;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -267,30 +269,37 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
} }
private void getCurrentAccountAndBindView() { private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> { if(mNewAccountName != null) {
mNewAccountName = null; if(account == null || !account.getUsername().equals(mNewAccountName)) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if(account == null) { EventBus.getDefault().post(new SwitchAccountEvent());
mNullAccessToken = true; mNewAccountName = null;
} else { if(newAccount == null) {
mAccessToken = account.getAccessToken(); mNullAccessToken = true;
mAccountName = account.getUsername(); } else {
} mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
bindView(true);
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
bindView(true); bindView(true);
}).execute(); }
}).execute(); } else {
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
mAccessToken = account.getAccessToken(); mAccessToken = account.getAccessToken();
mAccountName = account.getUsername(); mAccountName = account.getUsername();
} }
bindView(true); bindView(true);
}).execute(); }
} }).execute();
} }
private void fetchSubredditData() { private void fetchSubredditData() {

View File

@ -35,6 +35,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -347,30 +349,37 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
} }
private void getCurrentAccountAndInitializeViewPager() { private void getCurrentAccountAndInitializeViewPager() {
if(mNewAccountName != null) { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> { if(mNewAccountName != null) {
mNewAccountName = null; if(account == null || !account.getUsername().equals(mNewAccountName)) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if (account == null) { EventBus.getDefault().post(new SwitchAccountEvent());
mNullAccessToken = true; mNewAccountName = null;
} else { if(newAccount == null) {
mAccessToken = account.getAccessToken(); mNullAccessToken = true;
mAccountName = account.getUsername(); } else {
} mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
initializeViewPager();
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
initializeViewPager(); initializeViewPager();
}).execute(); }
}).execute(); } else {
} else { if(account == null) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if (account == null) {
mNullAccessToken = true; mNullAccessToken = true;
} else { } else {
mAccessToken = account.getAccessToken(); mAccessToken = account.getAccessToken();
mAccountName = account.getUsername(); mAccountName = account.getUsername();
} }
initializeViewPager(); initializeViewPager();
}).execute(); }
} }).execute();
} }
private void initializeViewPager() { private void initializeViewPager() {

View File

@ -38,6 +38,13 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<TextView
android:id="@+id/account_label_main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/label_account" />
<LinearLayout <LinearLayout
android:id="@+id/profile_linear_layout_main_activity" android:id="@+id/profile_linear_layout_main_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -116,6 +123,13 @@
</LinearLayout> </LinearLayout>
<TextView
android:id="@+id/post_label_main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/label_post" />
<LinearLayout <LinearLayout
android:id="@+id/upvoted_linear_layout_main_activity" android:id="@+id/upvoted_linear_layout_main_activity"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -231,4 +231,7 @@
<string name="notification_summary_subreddit">Subreddit</string> <string name="notification_summary_subreddit">Subreddit</string>
<string name="notification_summary_award">Award</string> <string name="notification_summary_award">Award</string>
<string name="notification_new_messages">%1$d New Messages</string> <string name="notification_new_messages">%1$d New Messages</string>
<string name="label_account">Account</string>
<string name="label_post">Post</string>
</resources> </resources>