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() {
switch (viewPager.getCurrentItem()) {
case 0:
return ((FragmentCommunicator) frontPagePostFragment).startLazyMode();
case 1:
return ((FragmentCommunicator) popularPostFragment).startLazyMode();
case 2:
return ((FragmentCommunicator) allPostFragment).startLazyMode();
if(mAccessToken == null) {
switch (viewPager.getCurrentItem()) {
case 0:
return ((FragmentCommunicator) popularPostFragment).startLazyMode();
case 1:
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;
}
void stopLazyMode() {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).stopLazyMode();
break;
case 1:
((FragmentCommunicator) popularPostFragment).stopLazyMode();
break;
case 2:
((FragmentCommunicator) allPostFragment).stopLazyMode();
break;
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).stopLazyMode();
break;
case 1:
((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() {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false);
break;
case 1:
((FragmentCommunicator) popularPostFragment).resumeLazyMode(false);
break;
case 2:
((FragmentCommunicator) allPostFragment).resumeLazyMode(false);
break;
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).resumeLazyMode(false);
break;
case 1:
((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() {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false);
break;
case 1:
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
break;
case 2:
((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
if(mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
break;
case 1:
((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() {
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;
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 {
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 MutableLiveData<Boolean> nsfwLiveData;
private MutableLiveData<String> sortTypeLiveData;
private nsfwAndSortTypeLiveData NSFWAndSortTypeLiveData;
private PostViewModel.NSFWAndSortTypeLiveData NSFWAndSortTypeLiveData;
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
int filter, boolean nsfw) {
@ -42,7 +42,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@ -73,7 +73,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@ -104,7 +104,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@ -135,7 +135,7 @@ public class PostViewModel extends ViewModel {
sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType);
NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
NSFWAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
@ -261,8 +261,8 @@ public class PostViewModel extends ViewModel {
}
}
private static class nsfwAndSortTypeLiveData extends MediatorLiveData<Pair<Boolean, String>> {
public nsfwAndSortTypeLiveData(LiveData<Boolean> nsfw, LiveData<String> sortType) {
private static class NSFWAndSortTypeLiveData extends MediatorLiveData<Pair<Boolean, String>> {
public NSFWAndSortTypeLiveData(LiveData<Boolean> nsfw, LiveData<String> sortType) {
addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue())));
addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1)));
}