Fixed lazy mode could not be paused properly.

This commit is contained in:
Alex Ning 2019-06-10 10:48:16 +08:00
parent c124828352
commit 076607a7ca
2 changed files with 32 additions and 38 deletions

View File

@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit;
interface FragmentCommunicator { interface FragmentCommunicator {
void refresh(); void refresh();
default void startLazyMode() {}; default void startLazyMode() {}
default void stopLazyMode() {}; default void stopLazyMode() {}
default void resumeLazyMode(boolean resumeNow) {}; default void resumeLazyMode(boolean resumeNow) {}
default void pauseLazyMode() {}; default void pauseLazyMode(boolean startTimer) {}
} }

View File

@ -8,7 +8,6 @@ import android.os.Bundle;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.Handler; import android.os.Handler;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
@ -59,8 +58,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private Activity activity; private Activity activity;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private String mName;
private int mPostType;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private boolean isLazyModePaused = false; private boolean isLazyModePaused = false;
@ -154,31 +151,28 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mLinearLayoutManager = new LinearLayoutManager(activity); mLinearLayoutManager = new LinearLayoutManager(activity);
mPostRecyclerView.setLayoutManager(mLinearLayoutManager); mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mPostRecyclerView.setOnTouchListener(new View.OnTouchListener() { mPostRecyclerView.setOnTouchListener((view, motionEvent) -> {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(isInLazyMode) { if(isInLazyMode) {
pauseLazyMode(); pauseLazyMode(true);
} }
return false; return false;
}
}); });
mPostType = getArguments().getInt(POST_TYPE_KEY); int postType = getArguments().getInt(POST_TYPE_KEY);
String accessToken = activity.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) String accessToken = activity.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
PostViewModel.Factory factory; PostViewModel.Factory factory;
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) { if(postType != PostDataSource.TYPE_FRONT_PAGE) {
mName = getArguments().getString(NAME_KEY); String name = getArguments().getString(NAME_KEY);
mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() { getResources().getConfiguration().locale, name, postType, new PostDataSource.OnPostFetchedCallback() {
@Override @Override
public void hasPost() { public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
@ -194,10 +188,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
} else { } else {
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); mSharedPreferences, postType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() { getResources().getConfiguration().locale, postType, new PostDataSource.OnPostFetchedCallback() {
@Override @Override
public void hasPost() { public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
@ -284,7 +278,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void resumeLazyMode(boolean resumeNow) { public void resumeLazyMode(boolean resumeNow) {
isInLazyMode = true; if(isInLazyMode) {
isLazyModePaused = false; isLazyModePaused = false;
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if(resumeNow) { if(resumeNow) {
@ -293,19 +287,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
lazyModeHandler.postDelayed(lazyModeRunnable, 2500); lazyModeHandler.postDelayed(lazyModeRunnable, 2500);
} }
} }
}
@Override @Override
public void pauseLazyMode() { public void pauseLazyMode(boolean startTimer) {
isInLazyMode = true;
if(isLazyModePaused) {
resumeLazyModeCountDownTimer.cancel(); resumeLazyModeCountDownTimer.cancel();
} else { isInLazyMode = true;
isLazyModePaused = true; isLazyModePaused = true;
lazyModeHandler.removeCallbacks(lazyModeRunnable); lazyModeHandler.removeCallbacks(lazyModeRunnable);
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
if(startTimer) {
resumeLazyModeCountDownTimer.start(); resumeLazyModeCountDownTimer.start();
} }
}
@Subscribe @Subscribe
public void onPostUpdateEvent(PostUpdateEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
@ -330,8 +325,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
if(isInLazyMode) { if(isInLazyMode) {
pauseLazyMode(); pauseLazyMode(false);
resumeLazyModeCountDownTimer.cancel();
} }
} }