diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-06-09 06:59:39 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-06-09 06:59:39 +0000 |
commit | c124828352a3cd67eeb87733f99988722b636cfa (patch) | |
tree | a84fb0fe6e5b317d9dcdc89d7459201bffde36d3 /app/src | |
parent | 7cfc4296a0c6e429a07a8cc8cdc32489220cb04b (diff) | |
download | infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar.gz infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar.bz2 infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar.lz infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar.xz infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.tar.zst infinity-for-reddit-c124828352a3cd67eeb87733f99988722b636cfa.zip |
Pause lazy mode if the user is swiping posts.
Diffstat (limited to '')
4 files changed, 97 insertions, 33 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0d43f71f..e1c3adf1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,11 +40,11 @@ <activity android:name=".ViewImageActivity" android:parentActivityName=".MainActivity" - android:theme="@style/Theme.AppCompat.Transparent" /> + android:theme="@style/AppTheme.ActionBar.Transparent" /> <activity android:name=".ViewVideoActivity" android:parentActivityName=".MainActivity" - android:theme="@style/Theme.AppCompat.Transparent" /> + android:theme="@style/AppTheme.ActionBar.Transparent" /> <activity android:name=".ViewPostDetailActivity" android:parentActivityName=".MainActivity" /> diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 58707c1a..d0ff1e4e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -4,4 +4,6 @@ interface FragmentCommunicator { void refresh(); default void startLazyMode() {}; default void stopLazyMode() {}; + default void resumeLazyMode(boolean resumeNow) {}; + default void pauseLazyMode() {}; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 50878fdd..cf26927c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -1,11 +1,14 @@ package ml.docilealligator.infinityforreddit; +import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.CountDownTimer; import android.os.Handler; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; 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_text_view_post_fragment) TextView mFetchPostInfoTextView; + private Activity activity; private LinearLayoutManager mLinearLayoutManager; private String mName; private int mPostType; private boolean isInLazyMode = false; + private boolean isLazyModePaused = false; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; @@ -67,6 +72,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private Window window; private Handler lazyModeHandler; private Runnable lazyModeRunnable; + private CountDownTimer resumeLazyModeCountDownTimer; @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -95,7 +101,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { // Inflate the layout for this fragment 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); @@ -103,28 +109,64 @@ public class PostFragment extends Fragment implements FragmentCommunicator { lazyModeHandler = new Handler(); - smoothScroller = new LinearSmoothScroller(getActivity()) { + smoothScroller = new LinearSmoothScroller(activity) { @Override protected int getVerticalSnapPreference() { 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) { isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); if(isInLazyMode) { - startLazyMode(); + resumeLazyMode(false); } } - mLinearLayoutManager = new LinearLayoutManager(getActivity()); + mLinearLayoutManager = new LinearLayoutManager(activity); 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); - 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, ""); PostViewModel.Factory factory; @@ -132,7 +174,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if(mPostType != PostDataSource.TYPE_FRONT_PAGE) { mName = getArguments().getString(NAME_KEY); - mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, @@ -151,7 +193,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); } else { - mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, @@ -196,6 +238,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + this.activity = (Activity) context; + } + + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); @@ -208,7 +256,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void showErrorView(int stringResId) { mProgressBar.setVisibility(View.GONE); - if(getActivity() != null && isAdded()) { + if(activity != null && isAdded()) { mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoTextView.setText(stringResId); Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); @@ -217,32 +265,46 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override 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; - lazyModeRunnable = new Runnable() { - @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); - } - } - }; + isLazyModePaused = false; 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 public void stopLazyMode() { + isInLazyMode = false; + isLazyModePaused = false; lazyModeHandler.removeCallbacks(lazyModeRunnable); + resumeLazyModeCountDownTimer.cancel(); window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - isInLazyMode = false; - Toast.makeText(getActivity(), getString(R.string.lazy_mode_stop), Toast.LENGTH_SHORT).show(); + 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 @@ -260,7 +322,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void onStart() { super.onStart(); if(isInLazyMode) { - startLazyMode(); + resumeLazyMode(false); } } @@ -268,9 +330,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void onStop() { super.onStop(); if(isInLazyMode) { - stopLazyMode(); + pauseLazyMode(); + resumeLazyModeCountDownTimer.cancel(); } - isInLazyMode = true; } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0d23c7ca..5dd09c72 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -65,7 +65,7 @@ <string name="app_label">Infinity</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> </resources> |