Fixed lazy mode in anonymous mode in MainActivity.

This commit is contained in:
Alex Ning 2019-09-01 23:43:28 +08:00
parent c9c4b7fc58
commit 870b217174
2 changed files with 105 additions and 52 deletions

View File

@ -940,70 +940,123 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} }
boolean startLazyMode() { boolean startLazyMode() {
switch (viewPager.getCurrentItem()) { if(mAccessToken == null) {
case 0: switch (viewPager.getCurrentItem()) {
return ((FragmentCommunicator) frontPagePostFragment).startLazyMode(); case 0:
case 1: return ((FragmentCommunicator) popularPostFragment).startLazyMode();
return ((FragmentCommunicator) popularPostFragment).startLazyMode(); case 1:
case 2: return ((FragmentCommunicator) allPostFragment).startLazyMode();
return ((FragmentCommunicator) allPostFragment).startLazyMode(); }
} else {
switch (viewPager.getCurrentItem()) {
case 0:
return ((FragmentCommunicator) frontPagePostFragment).startLazyMode();
case 1:
return ((FragmentCommunicator) popularPostFragment).startLazyMode();
case 2:
return ((FragmentCommunicator) allPostFragment).startLazyMode();
}
} }
return false; return false;
} }
void stopLazyMode() { void stopLazyMode() {
switch (getCurrentLazyModeFragmentPosition()) { if(mAccessToken == null) {
case 0: switch (getCurrentLazyModeFragmentPosition()) {
((FragmentCommunicator) frontPagePostFragment).stopLazyMode(); case 0:
break; ((FragmentCommunicator) popularPostFragment).stopLazyMode();
case 1: break;
((FragmentCommunicator) popularPostFragment).stopLazyMode(); case 1:
break; ((FragmentCommunicator) allPostFragment).stopLazyMode();
case 2: break;
((FragmentCommunicator) allPostFragment).stopLazyMode(); }
break; } else {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).stopLazyMode();
break;
case 1:
((FragmentCommunicator) popularPostFragment).stopLazyMode();
break;
case 2:
((FragmentCommunicator) allPostFragment).stopLazyMode();
break;
}
} }
} }
void resumeLazyMode() { void resumeLazyMode() {
switch (getCurrentLazyModeFragmentPosition()) { if(mAccessToken == null) {
case 0: switch (getCurrentLazyModeFragmentPosition()) {
((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false); case 0:
break; ((FragmentCommunicator) popularPostFragment).resumeLazyMode(false);
case 1: break;
((FragmentCommunicator) popularPostFragment).resumeLazyMode(false); case 1:
break; ((FragmentCommunicator) allPostFragment).resumeLazyMode(false);
case 2: break;
((FragmentCommunicator) allPostFragment).resumeLazyMode(false); }
break; } else {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false);
break;
case 1:
((FragmentCommunicator) popularPostFragment).resumeLazyMode(false);
break;
case 2:
((FragmentCommunicator) allPostFragment).resumeLazyMode(false);
break;
}
} }
} }
void pauseLazyMode() { void pauseLazyMode() {
switch (getCurrentLazyModeFragmentPosition()) { if(mAccessToken == null) {
case 0: switch (getCurrentLazyModeFragmentPosition()) {
((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false); case 0:
break; ((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
case 1: break;
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false); case 1:
break; ((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
case 2: }
((FragmentCommunicator) allPostFragment).pauseLazyMode(false); } else {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false);
break;
case 1:
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
break;
case 2:
((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
}
} }
} }
int getCurrentLazyModeFragmentPosition() { int getCurrentLazyModeFragmentPosition() {
if(!isInLazyMode) { if(mAccessToken == null) {
return -1; if(!isInLazyMode) {
} else if(frontPagePostFragment != null && ((FragmentCommunicator) frontPagePostFragment).isInLazyMode()) { return -1;
return 0; } else if(popularPostFragment != null && ((FragmentCommunicator) popularPostFragment).isInLazyMode()) {
} else if(popularPostFragment != null && ((FragmentCommunicator) popularPostFragment).isInLazyMode()) { return 0;
return 1; } else if(allPostFragment != null && ((FragmentCommunicator) allPostFragment).isInLazyMode()) {
} else if(allPostFragment != null && ((FragmentCommunicator) allPostFragment).isInLazyMode()) { return 1;
return 2; } else {
return -1;
}
} else { } else {
return -1; if(!isInLazyMode) {
return -1;
} else if(frontPagePostFragment != null && ((FragmentCommunicator) frontPagePostFragment).isInLazyMode()) {
return 0;
} else if(popularPostFragment != null && ((FragmentCommunicator) popularPostFragment).isInLazyMode()) {
return 1;
} else if(allPostFragment != null && ((FragmentCommunicator) allPostFragment).isInLazyMode()) {
return 2;
} else {
return -1;
}
} }
} }

View File

@ -23,7 +23,7 @@ public class PostViewModel extends ViewModel {
private LiveData<PagedList<Post>> posts; private LiveData<PagedList<Post>> posts;
private MutableLiveData<Boolean> nsfwLiveData; private MutableLiveData<Boolean> nsfwLiveData;
private MutableLiveData<String> sortTypeLiveData; private MutableLiveData<String> sortTypeLiveData;
private nsfwAndSortTypeLiveData NSFWAndSortTypeLiveData; private PostViewModel.NSFWAndSortTypeLiveData NSFWAndSortTypeLiveData;
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, boolean nsfw) { int filter, boolean nsfw) {
@ -42,7 +42,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
@ -73,7 +73,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
@ -104,7 +104,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
@ -135,7 +135,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
@ -261,8 +261,8 @@ public class PostViewModel extends ViewModel {
} }
} }
private static class nsfwAndSortTypeLiveData extends MediatorLiveData<Pair<Boolean, String>> { private static class NSFWAndSortTypeLiveData extends MediatorLiveData<Pair<Boolean, String>> {
public nsfwAndSortTypeLiveData(LiveData<Boolean> nsfw, LiveData<String> sortType) { public NSFWAndSortTypeLiveData(LiveData<Boolean> nsfw, LiveData<String> sortType) {
addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue())));
addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1))); addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1)));
} }