Pause lazy mode if the user is swiping posts.

This commit is contained in:
Alex Ning
2019-06-09 14:59:39 +08:00
parent 7cfc4296a0
commit c124828352
4 changed files with 98 additions and 34 deletions

View File

@@ -40,11 +40,11 @@
<activity <activity
android:name=".ViewImageActivity" android:name=".ViewImageActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/Theme.AppCompat.Transparent" /> android:theme="@style/AppTheme.ActionBar.Transparent" />
<activity <activity
android:name=".ViewVideoActivity" android:name=".ViewVideoActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/Theme.AppCompat.Transparent" /> android:theme="@style/AppTheme.ActionBar.Transparent" />
<activity <activity
android:name=".ViewPostDetailActivity" android:name=".ViewPostDetailActivity"
android:parentActivityName=".MainActivity" /> android:parentActivityName=".MainActivity" />

View File

@@ -4,4 +4,6 @@ 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 pauseLazyMode() {};
} }

View File

@@ -1,11 +1,14 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
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;
@@ -53,11 +56,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@BindView(R.id.fetch_post_info_image_view_post_fragment) ImageView mFetchPostInfoImageView; @BindView(R.id.fetch_post_info_image_view_post_fragment) ImageView mFetchPostInfoImageView;
@BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView; @BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView;
private Activity activity;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private String mName; private String mName;
private int mPostType; private int mPostType;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private boolean isLazyModePaused = false;
private PostRecyclerViewAdapter mAdapter; private PostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller smoothScroller; private RecyclerView.SmoothScroller smoothScroller;
@@ -67,6 +72,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private Window window; private Window window;
private Handler lazyModeHandler; private Handler lazyModeHandler;
private Runnable lazyModeRunnable; private Runnable lazyModeRunnable;
private CountDownTimer resumeLazyModeCountDownTimer;
@Inject @Named("no_oauth") @Inject @Named("no_oauth")
Retrofit mRetrofit; Retrofit mRetrofit;
@@ -95,7 +101,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_post, container, false); View rootView = inflater.inflate(R.layout.fragment_post, container, false);
((Infinity) getActivity().getApplication()).getmAppComponent().inject(this); ((Infinity) activity.getApplication()).getmAppComponent().inject(this);
ButterKnife.bind(this, rootView); ButterKnife.bind(this, rootView);
@@ -103,28 +109,64 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
lazyModeHandler = new Handler(); lazyModeHandler = new Handler();
smoothScroller = new LinearSmoothScroller(getActivity()) { smoothScroller = new LinearSmoothScroller(activity) {
@Override @Override
protected int getVerticalSnapPreference() { protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START; return LinearSmoothScroller.SNAP_TO_START;
} }
}; };
window = getActivity().getWindow(); window = activity.getWindow();
lazyModeRunnable = new Runnable() {
@Override
public void run() {
if(isInLazyMode && !isLazyModePaused) {
int nPosts = mAdapter.getItemCount();
int firstVisiblePosition = mLinearLayoutManager.findFirstVisibleItemPosition();
if(firstVisiblePosition != RecyclerView.NO_POSITION && nPosts > firstVisiblePosition) {
smoothScroller.setTargetPosition(firstVisiblePosition + 1);
mLinearLayoutManager.startSmoothScroll(smoothScroller);
}
}
lazyModeHandler.postDelayed(this, 2500);
}
};
resumeLazyModeCountDownTimer = new CountDownTimer(2500, 2500) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
resumeLazyMode(true);
}
};
if(savedInstanceState != null) { if(savedInstanceState != null) {
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
if(isInLazyMode) { if(isInLazyMode) {
startLazyMode(); resumeLazyMode(false);
} }
} }
mLinearLayoutManager = new LinearLayoutManager(getActivity()); mLinearLayoutManager = new LinearLayoutManager(activity);
mPostRecyclerView.setLayoutManager(mLinearLayoutManager); mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mPostRecyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(isInLazyMode) {
pauseLazyMode();
}
return false;
}
});
mPostType = getArguments().getInt(POST_TYPE_KEY); mPostType = getArguments().getInt(POST_TYPE_KEY);
String accessToken = getActivity().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;
@@ -132,7 +174,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) { if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
mName = getArguments().getString(NAME_KEY); mName = getArguments().getString(NAME_KEY);
mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit, mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
@@ -151,7 +193,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
}); });
} else { } else {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
@@ -195,6 +237,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
return rootView; return rootView;
} }
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
this.activity = (Activity) context;
}
@Override @Override
public void onSaveInstanceState(@NonNull Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@@ -208,7 +256,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
if(getActivity() != null && isAdded()) { if(activity != null && isAdded()) {
mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchPostInfoTextView.setText(stringResId); mFetchPostInfoTextView.setText(stringResId);
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView);
@@ -217,32 +265,46 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void startLazyMode() { public void startLazyMode() {
Toast.makeText(getActivity(), getString(R.string.lazy_mode_start, 2.5), Toast.LENGTH_SHORT).show();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
isInLazyMode = true; isInLazyMode = true;
lazyModeRunnable = new Runnable() { isLazyModePaused = false;
@Override
public void run() {
int nPosts = mAdapter.getItemCount();
int firstVisiblePosition = mLinearLayoutManager.findFirstVisibleItemPosition();
if(firstVisiblePosition != RecyclerView.NO_POSITION && nPosts > firstVisiblePosition) {
smoothScroller.setTargetPosition(firstVisiblePosition + 1);
mLinearLayoutManager.startSmoothScroll(smoothScroller);
}
if(isInLazyMode) {
lazyModeHandler.postDelayed(this, 2500);
}
}
};
lazyModeHandler.postDelayed(lazyModeRunnable, 2500); lazyModeHandler.postDelayed(lazyModeRunnable, 2500);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Toast.makeText(activity, getString(R.string.lazy_mode_start, 2.5), Toast.LENGTH_SHORT).show();
} }
@Override @Override
public void stopLazyMode() { public void stopLazyMode() {
lazyModeHandler.removeCallbacks(lazyModeRunnable);
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
isInLazyMode = false; isInLazyMode = false;
Toast.makeText(getActivity(), getString(R.string.lazy_mode_stop), Toast.LENGTH_SHORT).show(); isLazyModePaused = false;
lazyModeHandler.removeCallbacks(lazyModeRunnable);
resumeLazyModeCountDownTimer.cancel();
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Toast.makeText(activity, getString(R.string.lazy_mode_stop), Toast.LENGTH_SHORT).show();
}
@Override
public void resumeLazyMode(boolean resumeNow) {
isInLazyMode = true;
isLazyModePaused = false;
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if(resumeNow) {
lazyModeHandler.post(lazyModeRunnable);
} else {
lazyModeHandler.postDelayed(lazyModeRunnable, 2500);
}
}
@Override
public void pauseLazyMode() {
isInLazyMode = true;
if(isLazyModePaused) {
resumeLazyModeCountDownTimer.cancel();
} else {
isLazyModePaused = true;
lazyModeHandler.removeCallbacks(lazyModeRunnable);
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
resumeLazyModeCountDownTimer.start();
} }
@Subscribe @Subscribe
@@ -260,7 +322,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
if(isInLazyMode) { if(isInLazyMode) {
startLazyMode(); resumeLazyMode(false);
} }
} }
@@ -268,9 +330,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
if(isInLazyMode) { if(isInLazyMode) {
stopLazyMode(); pauseLazyMode();
resumeLazyModeCountDownTimer.cancel();
} }
isInLazyMode = true;
} }
@Override @Override

View File

@@ -65,7 +65,7 @@
<string name="app_label">Infinity</string> <string name="app_label">Infinity</string>
<string name="search_hint">Search anything</string> <string name="search_hint">Search anything</string>
<string name="lazy_mode_start">Lazy Mode starts in %1$fs</string> <string name="lazy_mode_start">Lazy Mode starts in %1$.1fs</string>
<string name="lazy_mode_stop">Lazy Mode stopped</string> <string name="lazy_mode_stop">Lazy Mode stopped</string>
</resources> </resources>