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,6 +940,14 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} }
boolean startLazyMode() { boolean startLazyMode() {
if(mAccessToken == null) {
switch (viewPager.getCurrentItem()) {
case 0:
return ((FragmentCommunicator) popularPostFragment).startLazyMode();
case 1:
return ((FragmentCommunicator) allPostFragment).startLazyMode();
}
} else {
switch (viewPager.getCurrentItem()) { switch (viewPager.getCurrentItem()) {
case 0: case 0:
return ((FragmentCommunicator) frontPagePostFragment).startLazyMode(); return ((FragmentCommunicator) frontPagePostFragment).startLazyMode();
@ -948,11 +956,22 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
case 2: case 2:
return ((FragmentCommunicator) allPostFragment).startLazyMode(); return ((FragmentCommunicator) allPostFragment).startLazyMode();
} }
}
return false; return false;
} }
void stopLazyMode() { void stopLazyMode() {
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).stopLazyMode();
break;
case 1:
((FragmentCommunicator) allPostFragment).stopLazyMode();
break;
}
} else {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
((FragmentCommunicator) frontPagePostFragment).stopLazyMode(); ((FragmentCommunicator) frontPagePostFragment).stopLazyMode();
@ -965,8 +984,19 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
break; break;
} }
} }
}
void resumeLazyMode() { void resumeLazyMode() {
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).resumeLazyMode(false);
break;
case 1:
((FragmentCommunicator) allPostFragment).resumeLazyMode(false);
break;
}
} else {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false); ((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false);
@ -979,8 +1009,18 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
break; break;
} }
} }
}
void pauseLazyMode() { void pauseLazyMode() {
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
break;
case 1:
((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
}
} else {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false); ((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false);
@ -992,8 +1032,20 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
((FragmentCommunicator) allPostFragment).pauseLazyMode(false); ((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
} }
} }
}
int getCurrentLazyModeFragmentPosition() { int getCurrentLazyModeFragmentPosition() {
if(mAccessToken == null) {
if(!isInLazyMode) {
return -1;
} else if(popularPostFragment != null && ((FragmentCommunicator) popularPostFragment).isInLazyMode()) {
return 0;
} else if(allPostFragment != null && ((FragmentCommunicator) allPostFragment).isInLazyMode()) {
return 1;
} else {
return -1;
}
} else {
if(!isInLazyMode) { if(!isInLazyMode) {
return -1; return -1;
} else if(frontPagePostFragment != null && ((FragmentCommunicator) frontPagePostFragment).isInLazyMode()) { } else if(frontPagePostFragment != null && ((FragmentCommunicator) frontPagePostFragment).isInLazyMode()) {
@ -1006,6 +1058,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
return -1; return -1;
} }
} }
}
void changeSortType(String sortType) { void changeSortType(String sortType) {
if(mAccessToken == null) { if(mAccessToken == null) {

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)));
} }