mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-02-04 13:54:44 +01:00
Anonymous mode is available but it is not useable.
This commit is contained in:
parent
a4583f1ecf
commit
17fc426ecb
@ -19,6 +19,9 @@ public interface AccountDao {
|
|||||||
@Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1")
|
@Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1")
|
||||||
void markAllAccountsNonCurrent();
|
void markAllAccountsNonCurrent();
|
||||||
|
|
||||||
|
@Query("DELETE FROM accounts WHERE is_current_user = 1")
|
||||||
|
void deleteCurrentAccount();
|
||||||
|
|
||||||
@Query("DELETE FROM accounts")
|
@Query("DELETE FROM accounts")
|
||||||
void deleteAllAccounts();
|
void deleteAllAccounts();
|
||||||
|
|
||||||
|
@ -37,6 +37,9 @@ class AccessTokenAuthenticator implements Authenticator {
|
|||||||
String accessToken = response.request().header(RedditUtils.AUTHORIZATION_KEY).substring(RedditUtils.AUTHORIZATION_BASE.length());
|
String accessToken = response.request().header(RedditUtils.AUTHORIZATION_KEY).substring(RedditUtils.AUTHORIZATION_BASE.length());
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount();
|
Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount();
|
||||||
|
if(account == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
String accessTokenFromDatabase = account.getAccessToken();
|
String accessTokenFromDatabase = account.getAccessToken();
|
||||||
if (accessToken.equals(accessTokenFromDatabase)) {
|
if (accessToken.equals(accessTokenFromDatabase)) {
|
||||||
String newAccessToken = refreshAccessToken(account);
|
String newAccessToken = refreshAccessToken(account);
|
||||||
|
@ -152,39 +152,46 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
|
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
|
||||||
|
|
||||||
if(!mNullAccessToken && mAccessToken == null) {
|
if(!mNullAccessToken && mAccessToken == null) {
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView(false);
|
||||||
} else {
|
} else {
|
||||||
bindView();
|
bindView(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
getCurrentAccountAndBindView();
|
getCurrentAccountAndBindView(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()));
|
fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentAccountAndBindView() {
|
private void getCurrentAccountAndBindView(boolean afterAccountSwitch) {
|
||||||
|
mNullAccessToken = true;
|
||||||
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
|
||||||
if(account == null) {
|
if(account == null) {
|
||||||
mNullAccessToken = true;
|
mNullAccessToken = true;
|
||||||
Intent loginIntent = new Intent(this, LoginActivity.class);
|
mAccessToken = null;
|
||||||
startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
|
mAccountName = null;
|
||||||
|
mProfileImageUrl = null;
|
||||||
|
mBannerImageUrl = null;
|
||||||
|
mKarma = 0;
|
||||||
} else {
|
} else {
|
||||||
|
mNullAccessToken = false;
|
||||||
mAccessToken = account.getAccessToken();
|
mAccessToken = account.getAccessToken();
|
||||||
mAccountName = account.getUsername();
|
mAccountName = account.getUsername();
|
||||||
mProfileImageUrl = account.getProfileImageUrl();
|
mProfileImageUrl = account.getProfileImageUrl();
|
||||||
mBannerImageUrl = account.getBannerImageUrl();
|
mBannerImageUrl = account.getBannerImageUrl();
|
||||||
mKarma = account.getKarma();
|
mKarma = account.getKarma();
|
||||||
bindView();
|
|
||||||
}
|
}
|
||||||
|
bindView(afterAccountSwitch);
|
||||||
}).execute();
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindView() {
|
private void bindView(boolean afterAccountSwitch) {
|
||||||
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
if(!afterAccountSwitch) {
|
||||||
viewPager.setAdapter(sectionsPagerAdapter);
|
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
||||||
viewPager.setOffscreenPageLimit(2);
|
viewPager.setAdapter(sectionsPagerAdapter);
|
||||||
tabLayout.setupWithViewPager(viewPager);
|
viewPager.setOffscreenPageLimit(2);
|
||||||
|
tabLayout.setupWithViewPager(viewPager);
|
||||||
|
}
|
||||||
|
|
||||||
glide = Glide.with(this);
|
glide = Glide.with(this);
|
||||||
|
|
||||||
@ -192,7 +199,21 @@ 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(), () -> 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
|
@Override
|
||||||
@ -203,12 +224,22 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void anonymousSelected() {
|
public void anonymousSelected() {
|
||||||
|
new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, false,
|
||||||
|
() -> {
|
||||||
|
Intent intent = new Intent(MainActivity.this, MainActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void logoutSelected() {
|
public void logoutSelected() {
|
||||||
|
new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, true,
|
||||||
|
() -> {
|
||||||
|
Intent intent = new Intent(MainActivity.this, MainActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -289,15 +320,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
Intent intent = new Intent(this, ViewUserDetailActivity.class);
|
Intent intent = new Intent(this, ViewUserDetailActivity.class);
|
||||||
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName);
|
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
drawer.closeDrawers();
|
||||||
});
|
});
|
||||||
|
|
||||||
subscriptionLinearLayout.setOnClickListener(view -> {
|
subscriptionLinearLayout.setOnClickListener(view -> {
|
||||||
Intent intent = new Intent(this, SubscribedThingListingActivity.class);
|
Intent intent = new Intent(this, SubscribedThingListingActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
drawer.closeDrawers();
|
||||||
});
|
});
|
||||||
|
|
||||||
settingsLinearLayout.setOnClickListener(view -> {
|
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
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
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);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
@ -400,7 +429,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_refresh_main_activity:
|
case R.id.action_refresh_main_activity:
|
||||||
sectionsPagerAdapter.refresh(viewPager.getCurrentItem());
|
sectionsPagerAdapter.refresh();
|
||||||
mFetchUserInfoSuccess = false;
|
mFetchUserInfoSuccess = false;
|
||||||
loadUserData();
|
loadUserData();
|
||||||
return true;
|
return true;
|
||||||
@ -450,7 +479,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sortTypeSelected(String sortType) {
|
public void sortTypeSelected(String sortType) {
|
||||||
sectionsPagerAdapter.changeSortType(sortType, viewPager.getCurrentItem());
|
sectionsPagerAdapter.changeSortType(sortType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -520,19 +549,32 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount()
|
||||||
|
{
|
||||||
|
if(mAccessToken == null) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getPageTitle(int position) {
|
public CharSequence getPageTitle(int position) {
|
||||||
switch (position) {
|
if(mAccessToken == null) {
|
||||||
case 0:
|
switch (position) {
|
||||||
return "Best";
|
case 0:
|
||||||
case 1:
|
return "Popular";
|
||||||
return "Popular";
|
case 1:
|
||||||
case 2:
|
return "All";
|
||||||
return "All";
|
}
|
||||||
|
} else {
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
return "Best";
|
||||||
|
case 1:
|
||||||
|
return "Popular";
|
||||||
|
case 2:
|
||||||
|
return "All";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -541,21 +583,43 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
@Override
|
@Override
|
||||||
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
||||||
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
||||||
switch (position) {
|
if(mAccessToken == null) {
|
||||||
case 0:
|
switch (position) {
|
||||||
frontPagePostFragment = (PostFragment) fragment;
|
case 0:
|
||||||
break;
|
popularPostFragment = (PostFragment) fragment;
|
||||||
case 1:
|
break;
|
||||||
popularPostFragment = (PostFragment) fragment;
|
case 1:
|
||||||
break;
|
allPostFragment = (PostFragment) fragment;
|
||||||
case 2:
|
}
|
||||||
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;
|
return fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeSortType(String sortType, int fragmentPosition) {
|
void changeAccessToken(String accessToken) {
|
||||||
switch (fragmentPosition) {
|
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:
|
case 0:
|
||||||
frontPagePostFragment.changeSortType(sortType);
|
frontPagePostFragment.changeSortType(sortType);
|
||||||
break;
|
break;
|
||||||
@ -567,19 +631,22 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refresh(int fragmentPosition) {
|
public void refresh() {
|
||||||
if(fragmentPosition == 0) {
|
switch (viewPager.getCurrentItem()) {
|
||||||
if(frontPagePostFragment != null) {
|
case 0:
|
||||||
((FragmentCommunicator) frontPagePostFragment).refresh();
|
if(frontPagePostFragment != null) {
|
||||||
}
|
((FragmentCommunicator) frontPagePostFragment).refresh();
|
||||||
} else if(fragmentPosition == 1) {
|
}
|
||||||
if(popularPostFragment != null) {
|
break;
|
||||||
((FragmentCommunicator) popularPostFragment).refresh();
|
case 1:
|
||||||
}
|
if(popularPostFragment != null) {
|
||||||
} else {
|
((FragmentCommunicator) popularPostFragment).refresh();
|
||||||
if(allPostFragment != null) {
|
}
|
||||||
((FragmentCommunicator) allPostFragment).refresh();
|
break;
|
||||||
}
|
case 2:
|
||||||
|
if(allPostFragment != null) {
|
||||||
|
((FragmentCommunicator) allPostFragment).refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,16 @@ class PostDataSourceFactory extends DataSource.Factory {
|
|||||||
return postDataSource;
|
return postDataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeAccessToken(String accessToken) {
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
void changeSortType(String sortType) {
|
void changeSortType(String sortType) {
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeAccessTokenAndSortType(String accessToken, String sortType) {
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.sortType = sortType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,6 +308,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
|||||||
return rootView;
|
return rootView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeAccessToken(String accessToken) {
|
||||||
|
mPostViewModel.changeAccessToken(accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
void changeSortType(String sortType) {
|
void changeSortType(String sortType) {
|
||||||
mPostViewModel.changeSortType(sortType);
|
mPostViewModel.changeSortType(sortType);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.util.Pair;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MediatorLiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
import androidx.lifecycle.Transformations;
|
import androidx.lifecycle.Transformations;
|
||||||
import androidx.lifecycle.ViewModel;
|
import androidx.lifecycle.ViewModel;
|
||||||
@ -19,7 +21,9 @@ public class PostViewModel extends ViewModel {
|
|||||||
private LiveData<NetworkState> initialLoadingState;
|
private LiveData<NetworkState> initialLoadingState;
|
||||||
private LiveData<Boolean> hasPostLiveData;
|
private LiveData<Boolean> hasPostLiveData;
|
||||||
private LiveData<PagedList<Post>> posts;
|
private LiveData<PagedList<Post>> posts;
|
||||||
|
private MutableLiveData<String> accessTokenLiveData;
|
||||||
private MutableLiveData<String> sortTypeLiveData;
|
private MutableLiveData<String> sortTypeLiveData;
|
||||||
|
private AccessTokenAndSortTypeLiveData accessTokenAndSortTypeLiveData;
|
||||||
|
|
||||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
||||||
int filter) {
|
int filter) {
|
||||||
@ -32,17 +36,21 @@ public class PostViewModel extends ViewModel {
|
|||||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||||
PostDataSource::hasPostLiveData);
|
PostDataSource::hasPostLiveData);
|
||||||
|
|
||||||
|
accessTokenLiveData = new MutableLiveData<>();
|
||||||
|
accessTokenLiveData.postValue(accessToken);
|
||||||
sortTypeLiveData = new MutableLiveData<>();
|
sortTypeLiveData = new MutableLiveData<>();
|
||||||
sortTypeLiveData.postValue(sortType);
|
sortTypeLiveData.postValue(sortType);
|
||||||
|
|
||||||
|
accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData);
|
||||||
|
|
||||||
PagedList.Config pagedListConfig =
|
PagedList.Config pagedListConfig =
|
||||||
(new PagedList.Config.Builder())
|
(new PagedList.Config.Builder())
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
.setPageSize(25)
|
.setPageSize(25)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
posts = Transformations.switchMap(sortTypeLiveData, sort -> {
|
posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> {
|
||||||
postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue());
|
||||||
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -59,17 +67,21 @@ public class PostViewModel extends ViewModel {
|
|||||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||||
PostDataSource::hasPostLiveData);
|
PostDataSource::hasPostLiveData);
|
||||||
|
|
||||||
|
accessTokenLiveData = new MutableLiveData<>();
|
||||||
|
accessTokenLiveData.postValue(accessToken);
|
||||||
sortTypeLiveData = new MutableLiveData<>();
|
sortTypeLiveData = new MutableLiveData<>();
|
||||||
sortTypeLiveData.postValue(sortType);
|
sortTypeLiveData.postValue(sortType);
|
||||||
|
|
||||||
|
accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData);
|
||||||
|
|
||||||
PagedList.Config pagedListConfig =
|
PagedList.Config pagedListConfig =
|
||||||
(new PagedList.Config.Builder())
|
(new PagedList.Config.Builder())
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
.setPageSize(25)
|
.setPageSize(25)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
posts = Transformations.switchMap(sortTypeLiveData, sort -> {
|
posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> {
|
||||||
postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue());
|
||||||
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -86,6 +98,9 @@ public class PostViewModel extends ViewModel {
|
|||||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||||
PostDataSource::hasPostLiveData);
|
PostDataSource::hasPostLiveData);
|
||||||
|
|
||||||
|
accessTokenLiveData = new MutableLiveData<>();
|
||||||
|
accessTokenLiveData.postValue(accessToken);
|
||||||
|
|
||||||
PagedList.Config pagedListConfig =
|
PagedList.Config pagedListConfig =
|
||||||
(new PagedList.Config.Builder())
|
(new PagedList.Config.Builder())
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
@ -93,6 +108,11 @@ public class PostViewModel extends ViewModel {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).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,
|
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(),
|
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||||
PostDataSource::hasPostLiveData);
|
PostDataSource::hasPostLiveData);
|
||||||
|
|
||||||
|
accessTokenLiveData = new MutableLiveData<>();
|
||||||
|
accessTokenLiveData.postValue(accessToken);
|
||||||
sortTypeLiveData = new MutableLiveData<>();
|
sortTypeLiveData = new MutableLiveData<>();
|
||||||
sortTypeLiveData.postValue(sortType);
|
sortTypeLiveData.postValue(sortType);
|
||||||
|
|
||||||
|
accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData);
|
||||||
|
|
||||||
PagedList.Config pagedListConfig =
|
PagedList.Config pagedListConfig =
|
||||||
(new PagedList.Config.Builder())
|
(new PagedList.Config.Builder())
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
@ -117,7 +141,7 @@ public class PostViewModel extends ViewModel {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
posts = Transformations.switchMap(sortTypeLiveData, sort -> {
|
posts = Transformations.switchMap(sortTypeLiveData, sort -> {
|
||||||
postDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue());
|
||||||
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -154,6 +178,10 @@ public class PostViewModel extends ViewModel {
|
|||||||
sortTypeLiveData.postValue(sortType);
|
sortTypeLiveData.postValue(sortType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeAccessToken(String accessToken) {
|
||||||
|
accessTokenLiveData.postValue(accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private String accessToken;
|
private String accessToken;
|
||||||
@ -221,4 +249,11 @@ public class PostViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class AccessTokenAndSortTypeLiveData extends MediatorLiveData<Pair<String, String>> {
|
||||||
|
public AccessTokenAndSortTypeLiveData(LiveData<String> accessToken, LiveData<String> sortType) {
|
||||||
|
addSource(accessToken, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue())));
|
||||||
|
addSource(sortType, sortType1 -> setValue(Pair.create(accessToken.getValue(), sortType1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
|
||||||
|
import Account.AccountDao;
|
||||||
|
|
||||||
|
class SwitchToAnonymousAccountAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user