From 17fc426ecb9c3b5c7cf8ada5231ab150248fbb21 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 9 Aug 2019 15:28:22 +0800 Subject: Anonymous mode is available but it is not useable. --- app/src/main/java/Account/AccountDao.java | 3 + .../AccessTokenAuthenticator.java | 3 + .../infinityforreddit/MainActivity.java | 183 ++++++++++++++------- .../infinityforreddit/PostDataSourceFactory.java | 9 + .../infinityforreddit/PostFragment.java | 4 + .../infinityforreddit/PostViewModel.java | 45 ++++- .../SwitchToAnonymousAccountAsyncTask.java | 37 +++++ 7 files changed, 221 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java (limited to 'app/src/main') diff --git a/app/src/main/java/Account/AccountDao.java b/app/src/main/java/Account/AccountDao.java index b3672bb5..aa7b7f19 100644 --- a/app/src/main/java/Account/AccountDao.java +++ b/app/src/main/java/Account/AccountDao.java @@ -19,6 +19,9 @@ public interface AccountDao { @Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1") void markAllAccountsNonCurrent(); + @Query("DELETE FROM accounts WHERE is_current_user = 1") + void deleteCurrentAccount(); + @Query("DELETE FROM accounts") void deleteAllAccounts(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 0c568a06..8d460c5e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -37,6 +37,9 @@ class AccessTokenAuthenticator implements Authenticator { String accessToken = response.request().header(RedditUtils.AUTHORIZATION_KEY).substring(RedditUtils.AUTHORIZATION_BASE.length()); synchronized (this) { Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount(); + if(account == null) { + return null; + } String accessTokenFromDatabase = account.getAccessToken(); if (accessToken.equals(accessTokenFromDatabase)) { String newAccessToken = refreshAccessToken(account); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 29a89500..7fb79cc9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -152,39 +152,46 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE); if(!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); + getCurrentAccountAndBindView(false); } else { - bindView(); + bindView(false); } } else { - getCurrentAccountAndBindView(); + getCurrentAccountAndBindView(false); } fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag())); } - private void getCurrentAccountAndBindView() { + private void getCurrentAccountAndBindView(boolean afterAccountSwitch) { + mNullAccessToken = true; new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { mNullAccessToken = true; - Intent loginIntent = new Intent(this, LoginActivity.class); - startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE); + mAccessToken = null; + mAccountName = null; + mProfileImageUrl = null; + mBannerImageUrl = null; + mKarma = 0; } else { + mNullAccessToken = false; mAccessToken = account.getAccessToken(); mAccountName = account.getUsername(); mProfileImageUrl = account.getProfileImageUrl(); mBannerImageUrl = account.getBannerImageUrl(); mKarma = account.getKarma(); - bindView(); } + bindView(afterAccountSwitch); }).execute(); } - private void bindView() { - sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - viewPager.setAdapter(sectionsPagerAdapter); - viewPager.setOffscreenPageLimit(2); - tabLayout.setupWithViewPager(viewPager); + private void bindView(boolean afterAccountSwitch) { + if(!afterAccountSwitch) { + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + viewPager.setAdapter(sectionsPagerAdapter); + viewPager.setOffscreenPageLimit(2); + tabLayout.setupWithViewPager(viewPager); + } glide = Glide.with(this); @@ -192,7 +199,21 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe new AccountRecyclerViewAdapter.ItemSelectedListener() { @Override public void accountSelected(Account account) { - new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> relaunchActivity()).execute(); + new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> { + if(mAccessToken == null) { + Intent intent = new Intent(MainActivity.this, MainActivity.class); + startActivity(intent); + finish(); + } else { + getCurrentAccountAndBindView(true); + sectionsPagerAdapter.changeAccessToken(account.getAccessToken()); + drawer.closeDrawers(); + mDrawerOnAccountSwitch = false; + mDropIconImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_arrow_drop_down_24px)); + accountRecyclerView.setVisibility(View.GONE); + allDrawerItemsLinearLayout.setVisibility(View.VISIBLE); + } + }).execute(); } @Override @@ -203,12 +224,22 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Override public void anonymousSelected() { - + new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, false, + () -> { + Intent intent = new Intent(MainActivity.this, MainActivity.class); + startActivity(intent); + finish(); + }).execute(); } @Override public void logoutSelected() { - + new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, true, + () -> { + Intent intent = new Intent(MainActivity.this, MainActivity.class); + startActivity(intent); + finish(); + }).execute(); } @Override @@ -289,15 +320,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe Intent intent = new Intent(this, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName); startActivity(intent); + drawer.closeDrawers(); }); subscriptionLinearLayout.setOnClickListener(view -> { Intent intent = new Intent(this, SubscribedThingListingActivity.class); startActivity(intent); + drawer.closeDrawers(); }); settingsLinearLayout.setOnClickListener(view -> { - + drawer.closeDrawers(); }); } @@ -350,17 +383,13 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } } - private void relaunchActivity() { - Intent intent = getIntent(); - finish(); - startActivity(intent); - overridePendingTransition(0, 0); - } - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) { - relaunchActivity(); + Intent intent = getIntent(); + finish(); + startActivity(intent); + overridePendingTransition(0, 0); } super.onActivityResult(requestCode, resultCode, data); @@ -400,7 +429,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe startActivity(intent); return true; case R.id.action_refresh_main_activity: - sectionsPagerAdapter.refresh(viewPager.getCurrentItem()); + sectionsPagerAdapter.refresh(); mFetchUserInfoSuccess = false; loadUserData(); return true; @@ -450,7 +479,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Override public void sortTypeSelected(String sortType) { - sectionsPagerAdapter.changeSortType(sortType, viewPager.getCurrentItem()); + sectionsPagerAdapter.changeSortType(sortType); } @Override @@ -520,19 +549,32 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } @Override - public int getCount() { + public int getCount() + { + if(mAccessToken == null) { + return 2; + } return 3; } @Override public CharSequence getPageTitle(int position) { - switch (position) { - case 0: - return "Best"; - case 1: - return "Popular"; - case 2: - return "All"; + if(mAccessToken == null) { + switch (position) { + case 0: + return "Popular"; + case 1: + return "All"; + } + } else { + switch (position) { + case 0: + return "Best"; + case 1: + return "Popular"; + case 2: + return "All"; + } } return null; } @@ -541,21 +583,43 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); - switch (position) { - case 0: - frontPagePostFragment = (PostFragment) fragment; - break; - case 1: - popularPostFragment = (PostFragment) fragment; - break; - case 2: - allPostFragment = (PostFragment) fragment; + if(mAccessToken == null) { + switch (position) { + case 0: + popularPostFragment = (PostFragment) fragment; + break; + case 1: + allPostFragment = (PostFragment) fragment; + } + } else { + switch (position) { + case 0: + frontPagePostFragment = (PostFragment) fragment; + break; + case 1: + popularPostFragment = (PostFragment) fragment; + break; + case 2: + allPostFragment = (PostFragment) fragment; + } } return fragment; } - void changeSortType(String sortType, int fragmentPosition) { - switch (fragmentPosition) { + void changeAccessToken(String accessToken) { + if(frontPagePostFragment != null) { + frontPagePostFragment.changeAccessToken(accessToken); + } + if(popularPostFragment != null) { + popularPostFragment.changeAccessToken(accessToken); + } + if(allPostFragment != null) { + allPostFragment.changeAccessToken(accessToken); + } + } + + void changeSortType(String sortType) { + switch (viewPager.getCurrentItem()) { case 0: frontPagePostFragment.changeSortType(sortType); break; @@ -567,19 +631,22 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } } - public void refresh(int fragmentPosition) { - if(fragmentPosition == 0) { - if(frontPagePostFragment != null) { - ((FragmentCommunicator) frontPagePostFragment).refresh(); - } - } else if(fragmentPosition == 1) { - if(popularPostFragment != null) { - ((FragmentCommunicator) popularPostFragment).refresh(); - } - } else { - if(allPostFragment != null) { - ((FragmentCommunicator) allPostFragment).refresh(); - } + public void refresh() { + switch (viewPager.getCurrentItem()) { + case 0: + if(frontPagePostFragment != null) { + ((FragmentCommunicator) frontPagePostFragment).refresh(); + } + break; + case 1: + if(popularPostFragment != null) { + ((FragmentCommunicator) popularPostFragment).refresh(); + } + break; + case 2: + if(allPostFragment != null) { + ((FragmentCommunicator) allPostFragment).refresh(); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 8eb98be4..8c14ace6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -95,7 +95,16 @@ class PostDataSourceFactory extends DataSource.Factory { return postDataSource; } + void changeAccessToken(String accessToken) { + this.accessToken = accessToken; + } + void changeSortType(String sortType) { this.sortType = sortType; } + + void changeAccessTokenAndSortType(String accessToken, String sortType) { + this.accessToken = accessToken; + this.sortType = sortType; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 811c318b..3baa0c59 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -308,6 +308,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return rootView; } + void changeAccessToken(String accessToken) { + mPostViewModel.changeAccessToken(accessToken); + } + void changeSortType(String sortType) { mPostViewModel.changeSortType(sortType); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 4a464f08..62ec3e7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -1,7 +1,9 @@ package ml.docilealligator.infinityforreddit; import androidx.annotation.NonNull; +import androidx.core.util.Pair; import androidx.lifecycle.LiveData; +import androidx.lifecycle.MediatorLiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Transformations; import androidx.lifecycle.ViewModel; @@ -19,7 +21,9 @@ public class PostViewModel extends ViewModel { private LiveData initialLoadingState; private LiveData hasPostLiveData; private LiveData> posts; + private MutableLiveData accessTokenLiveData; private MutableLiveData sortTypeLiveData; + private AccessTokenAndSortTypeLiveData accessTokenAndSortTypeLiveData; public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, int filter) { @@ -32,17 +36,21 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); + accessTokenLiveData = new MutableLiveData<>(); + accessTokenLiveData.postValue(accessToken); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); + accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) .setPageSize(25) .build(); - posts = Transformations.switchMap(sortTypeLiveData, sort -> { - postDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); + posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> { + postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } @@ -59,17 +67,21 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); + accessTokenLiveData = new MutableLiveData<>(); + accessTokenLiveData.postValue(accessToken); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); + accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) .setPageSize(25) .build(); - posts = Transformations.switchMap(sortTypeLiveData, sort -> { - postDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); + posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> { + postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } @@ -86,6 +98,9 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); + accessTokenLiveData = new MutableLiveData<>(); + accessTokenLiveData.postValue(accessToken); + PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) @@ -93,6 +108,11 @@ public class PostViewModel extends ViewModel { .build(); posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + + posts = Transformations.switchMap(accessTokenLiveData, newAccessToken -> { + postDataSourceFactory.changeAccessToken(accessTokenLiveData.getValue()); + return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); + }); } public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, @@ -107,9 +127,13 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); + accessTokenLiveData = new MutableLiveData<>(); + accessTokenLiveData.postValue(accessToken); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); + accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) @@ -117,7 +141,7 @@ public class PostViewModel extends ViewModel { .build(); posts = Transformations.switchMap(sortTypeLiveData, sort -> { - postDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); + postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } @@ -154,6 +178,10 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.postValue(sortType); } + void changeAccessToken(String accessToken) { + accessTokenLiveData.postValue(accessToken); + } + public static class Factory extends ViewModelProvider.NewInstanceFactory { private Retrofit retrofit; private String accessToken; @@ -221,4 +249,11 @@ public class PostViewModel extends ViewModel { } } } + + private static class AccessTokenAndSortTypeLiveData extends MediatorLiveData> { + public AccessTokenAndSortTypeLiveData(LiveData accessToken, LiveData sortType) { + addSource(accessToken, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); + addSource(sortType, sortType1 -> setValue(Pair.create(accessToken.getValue(), sortType1))); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java new file mode 100644 index 00000000..af65d5f8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java @@ -0,0 +1,37 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import Account.AccountDao; + +class SwitchToAnonymousAccountAsyncTask extends AsyncTask { + interface SwitchToAnonymousAccountAsyncTaskListener { + void logoutSuccess(); + } + + private RedditDataRoomDatabase redditDataRoomDatabase; + private boolean removeCurrentAccount; + private SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener; + + SwitchToAnonymousAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, boolean removeCurrentAccount, + SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.removeCurrentAccount = removeCurrentAccount; + this.switchToAnonymousAccountAsyncTaskListener = switchToAnonymousAccountAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + AccountDao accountDao = redditDataRoomDatabase.accountDao(); + if(removeCurrentAccount) { + accountDao.deleteCurrentAccount(); + } + accountDao.markAllAccountsNonCurrent(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + switchToAnonymousAccountAsyncTaskListener.logoutSuccess(); + } +} -- cgit v1.2.3