From 9bed06fb7c23bbb8df2a278636b64b6a1b1bc237 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Thu, 11 Oct 2018 20:32:37 +0300 Subject: [PATCH] add refresh label --- .../main/shoutbox/ShoutboxFragment.java | 50 +++++++++++++++++++ .../res/drawable/refresh_label_background.xml | 16 ++++++ app/src/main/res/layout/fragment_shoutbox.xml | 34 ++++++++++--- app/src/main/res/values/dimens.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable/refresh_label_background.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java index 8a373375..ceff90e5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java @@ -1,5 +1,6 @@ package gr.thmmy.mthmmy.activities.main.shoutbox; +import android.animation.ValueAnimator; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; @@ -7,11 +8,14 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.interpolator.view.animation.FastOutSlowInInterpolator; import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.editorview.EditorView; @@ -29,11 +33,13 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj private static final String TAG = "ShoutboxFragment"; private MaterialProgressBar progressBar; + private TextView refreshLabel; private ShoutboxTask shoutboxTask; private ShoutAdapter shoutAdapter; private EmojiKeyboard emojiKeyboard; private EditorView editorView; private Shoutbox shoutbox; + private ValueAnimator animator; public static ShoutboxFragment newInstance(int sectionNumber) { ShoutboxFragment fragment = new ShoutboxFragment(); @@ -46,6 +52,7 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj private void onShoutboxTaskSarted() { Timber.i("Starting shoutbox task..."); + hideRefreshLabel(); progressBar.setVisibility(View.VISIBLE); } @@ -58,6 +65,7 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj editorView.setAlpha(1f); editorView.setEnabled(true); progressBar.setVisibility(View.INVISIBLE); + showRefreshLabel(); if (resultCode == NetworkResultCodes.SUCCESSFUL) { Timber.i("Shout was sent successfully"); editorView.getEditText().getText().clear(); @@ -90,6 +98,12 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); + refreshLabel = rootView.findViewById(R.id.refresh_label); + refreshLabel.setOnClickListener(v -> { + shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); + shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + }); + progressBar = rootView.findViewById(R.id.progressBar); CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); @@ -103,6 +117,16 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj imm.hideSoftInputFromWindow(editorView.getWindowToken(), 0); return false; }); + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + if (dy > 0) { + showRefreshLabel(); + } else { + hideRefreshLabel(); + } + } + }); shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); @@ -136,6 +160,32 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj return rootView; } + private void hideRefreshLabel() { + if (refreshLabel.getVisibility() == View.GONE) return; + if (animator != null) animator.cancel(); + animator = getRefreshLabelAnimation(); + animator.start(); + } + + private void showRefreshLabel() { + if (refreshLabel.getVisibility() == View.VISIBLE) return; + if (animator != null) animator.cancel(); + animator = getRefreshLabelAnimation(); + animator.reverse(); + } + + private ValueAnimator getRefreshLabelAnimation() { + ValueAnimator animator = ValueAnimator.ofFloat(-200, 0); + animator.addUpdateListener(valueAnimator -> { + if (((Float) valueAnimator.getAnimatedValue()).intValue() == 1) refreshLabel.setVisibility(View.VISIBLE); + if (((Float) valueAnimator.getAnimatedValue()).intValue() == -199) refreshLabel.setVisibility(View.GONE); + refreshLabel.setTranslationY((float) valueAnimator.getAnimatedValue()); + }); + animator.setInterpolator(new FastOutSlowInInterpolator()); + animator.setDuration(200); + return animator; + } + @Override public void setEmojiKeyboardVisible(boolean visible) { emojiKeyboard.setVisibility(visible ? View.VISIBLE : View.GONE); diff --git a/app/src/main/res/drawable/refresh_label_background.xml b/app/src/main/res/drawable/refresh_label_background.xml new file mode 100644 index 00000000..14379c28 --- /dev/null +++ b/app/src/main/res/drawable/refresh_label_background.xml @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_shoutbox.xml b/app/src/main/res/layout/fragment_shoutbox.xml index 625c2b88..b52a034f 100644 --- a/app/src/main/res/layout/fragment_shoutbox.xml +++ b/app/src/main/res/layout/fragment_shoutbox.xml @@ -17,12 +17,33 @@ app:mpb_indeterminateTint="@color/accent" app:mpb_progressStyle="horizontal" /> - + android:layout_weight="1"> + + + + + + + android:paddingTop="8dp" /> 24sp 24dp 6dp + 12dp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21c96257..7f2a5619 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,6 +20,7 @@ Forum Unread Shoutbox + REFRESH thmmy.gr