diff options
Diffstat (limited to 'app/src/main/java')
12 files changed, 185 insertions, 114 deletions
diff --git a/app/src/main/java/Account/AccountDao.java b/app/src/main/java/Account/AccountDao.java index c05e5552..67caf71d 100644 --- a/app/src/main/java/Account/AccountDao.java +++ b/app/src/main/java/Account/AccountDao.java @@ -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); diff --git a/app/src/main/java/Account/AccountRepository.java b/app/src/main/java/Account/AccountRepository.java index 7aa77b51..573f00f8 100644 --- a/app/src/main/java/Account/AccountRepository.java +++ b/app/src/main/java/Account/AccountRepository.java @@ -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; } diff --git a/app/src/main/java/Account/AccountViewModel.java b/app/src/main/java/Account/AccountViewModel.java index 15fe6143..53fb0013 100644 --- a/app/src/main/java/Account/AccountViewModel.java +++ b/app/src/main/java/Account/AccountViewModel.java @@ -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; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 66899f0e..ee0f4792 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -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(); - - 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.KEEP, pullNotificationRequest); - } + 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(); + + 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); + 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); @@ -667,6 +699,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } @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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index c77e8a37..1ea058e6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index b9be2633..66d1b3f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -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()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java index f5b9c37b..516d0b55 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java @@ -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); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java new file mode 100644 index 00000000..0395f913 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java @@ -0,0 +1,5 @@ +package ml.docilealligator.infinityforreddit; + +public class SwitchAccountEvent { + public void accountSwitched(){} +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java index 2ea81af6..5bf78cd2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java @@ -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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index ba5ef9a8..aba1aa99 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 57169f00..c7ea29a5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 50f3fdf2..6a0e3649 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -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() { |