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")
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, " +
"karma = :karma WHERE username = :username")
void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma);

View File

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

View File

@ -14,20 +14,14 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountViewModel extends AndroidViewModel {
private AccountRepository mAccountRepository;
private LiveData<Account> mAccountLiveData;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) {
super(application);
mAccountRepository = new AccountRepository(redditDataRoomDatabase, id);
mAccountLiveData = mAccountRepository.getAccountLiveData();
mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData();
}
public LiveData<Account> getAccountLiveData() {
return mAccountLiveData;
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
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.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.TimeUnit;
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.toolbar) Toolbar toolbar;
@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.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout;
@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.downvoted_linear_layout_main_activity) LinearLayout downvotedLinearLayout;
@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);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources();
@ -241,36 +248,57 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
}
private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
mNewAccountName = null;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mProfileImageUrl = account.getProfileImageUrl();
mBannerImageUrl = account.getBannerImageUrl();
mKarma = account.getKarma();
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
mNewAccountName = null;
if(newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
mProfileImageUrl = newAccount.getProfileImageUrl();
mBannerImageUrl = newAccount.getBannerImageUrl();
mKarma = newAccount.getKarma();
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS)
.setConstraints(constraints)
.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();
}).execute();
}).execute();
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
}
} else {
if(account == null) {
mNullAccessToken = true;
} else {
@ -292,12 +320,14 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
}
bindView();
}).execute();
}
}
}).execute();
}
private void bindView() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2);
@ -328,7 +358,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
new AccountRecyclerViewAdapter.ItemSelectedListener() {
@Override
public void accountSelected(Account account) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), newAccount -> {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
@ -436,9 +466,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} else {
mKarmaTextView.setText(R.string.press_here_to_login);
mAccountNameTextView.setText(R.string.anonymous_account);
accountLabelTextView.setVisibility(View.GONE);
profileLinearLayout.setVisibility(View.GONE);
subscriptionLinearLayout.setVisibility(View.GONE);
inboxLinearLayout.setVisibility(View.GONE);
postLabelTextView.setVisibility(View.GONE);
upvotedLinearLayout.setVisibility(View.GONE);
downvotedLinearLayout.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);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public void sortTypeSelected(String 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 PostFragment frontPagePostFragment;
private PostFragment popularPostFragment;

View File

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

View File

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

View File

@ -2,13 +2,16 @@ package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask;
import Account.Account;
class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> {
interface SwitchAccountAsyncTaskListener {
void switched();
void switched(Account account);
}
private RedditDataRoomDatabase redditDataRoomDatabase;
private String newAccountName;
private Account account;
private SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener;
SwitchAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String newAccountName,
@ -22,11 +25,12 @@ class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... voids) {
redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent();
redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName);
account = redditDataRoomDatabase.accountDao().getCurrentAccount();
return null;
}
@Override
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.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject;
import javax.inject.Named;
@ -146,30 +148,34 @@ public class ViewMessageActivity extends AppCompatActivity {
}
private void getCurrentAccountAndFetchMessage() {
if(mNewAccountName != null) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
mNewAccountName = null;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent());
mNewAccountName = null;
if(newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
}
bindView();
}
}).execute();
}).execute();
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
}).execute();
} else {
mAccessToken = account.getAccessToken();
bindView();
}
} else {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
bindView();
}
}).execute();
}
bindView();
}
}).execute();
}
private void bindView() {

View File

@ -218,22 +218,27 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
}
private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
mNewAccountName = null;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent());
mNewAccountName = null;
if(newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
bindView();
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
bindView();
}).execute();
}).execute();
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
}
} else {
if(account == null) {
mNullAccessToken = true;
} else {
@ -242,8 +247,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
}
bindView();
}).execute();
}
}
}).execute();
}
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.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject;
import javax.inject.Named;
@ -267,30 +269,37 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
}
private void getCurrentAccountAndBindView() {
if(mNewAccountName != null) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
mNewAccountName = null;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent());
mNewAccountName = null;
if(newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
bindView(true);
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
bindView(true);
}).execute();
}).execute();
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
}
} else {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
bindView(true);
}).execute();
}
}
}).execute();
}
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.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject;
import javax.inject.Named;
@ -347,30 +349,37 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
}
private void getCurrentAccountAndInitializeViewPager() {
if(mNewAccountName != null) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, () -> {
mNewAccountName = null;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if (account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent());
mNewAccountName = null;
if(newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
}
initializeViewPager();
}).execute();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
initializeViewPager();
}).execute();
}).execute();
} else {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if (account == null) {
}
} else {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
initializeViewPager();
}).execute();
}
}
}).execute();
}
private void initializeViewPager() {

View File

@ -38,6 +38,13 @@
android:layout_height="wrap_content"
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
android:id="@+id/profile_linear_layout_main_activity"
android:layout_width="match_parent"
@ -116,6 +123,13 @@
</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
android:id="@+id/upvoted_linear_layout_main_activity"
android:layout_width="match_parent"

View File

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