From 870b21717403fc5a9304183816d0c9cef9f73d4d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 1 Sep 2019 23:43:28 +0800 Subject: [PATCH] Fixed lazy mode in anonymous mode in MainActivity. --- .../infinityforreddit/MainActivity.java | 143 ++++++++++++------ .../infinityforreddit/PostViewModel.java | 14 +- 2 files changed, 105 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 05061989..abe26221 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -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; + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index cae2bace..62e58547 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -23,7 +23,7 @@ public class PostViewModel extends ViewModel { private LiveData> posts; private MutableLiveData nsfwLiveData; private MutableLiveData 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> { - public nsfwAndSortTypeLiveData(LiveData nsfw, LiveData sortType) { + private static class NSFWAndSortTypeLiveData extends MediatorLiveData> { + public NSFWAndSortTypeLiveData(LiveData nsfw, LiveData sortType) { addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1))); }