From 46df521605225643ef2d210497ccceac7bff4bd7 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 31 Dec 2016 18:25:03 +0200 Subject: [PATCH] Code cleanup and fixes. --- .../activities/topic/TopicActivity.java | 76 ++++++++++--------- .../mthmmy/activities/topic/TopicAdapter.java | 27 ++----- .../mthmmy/utils/ScrollAwareFABBehavior.java | 2 + .../utils/ScrollAwareLinearBehavior.java | 75 +++++++----------- 4 files changed, 79 insertions(+), 101 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index a9456960..53849692 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -60,12 +60,12 @@ public class TopicActivity extends BaseActivity { * The key to use when putting topic's title String to {@link TopicActivity}'s Bundle. */ public static final String EXTRAS_TOPIC_TITLE = "TOPIC_TITLE"; - static String PACKAGE_NAME; private static TopicTask topicTask; //About posts private List postsList; - static final int NO_POST_FOCUS = -1; - static int postFocus = NO_POST_FOCUS; + private static final int NO_POST_FOCUS = -1; + private static int postFocus = NO_POST_FOCUS; + private static int postFocusPosition = 0; //Quotes public static final ArrayList toQuoteList = new ArrayList<>(); //Topic's pages @@ -93,7 +93,7 @@ public class TopicActivity extends BaseActivity { private FloatingActionButton replyFAB; private String parsedTitle; private RecyclerView recyclerView; - private RecyclerView.LayoutManager layoutManager; + private String loadedPageUrl = ""; @Override @@ -101,7 +101,6 @@ public class TopicActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_topic); - PACKAGE_NAME = getApplicationContext().getPackageName(); Bundle extras = getIntent().getExtras(); topicTitle = extras.getString("TOPIC_TITLE"); @@ -118,13 +117,14 @@ public class TopicActivity extends BaseActivity { progressBar = (MaterialProgressBar) findViewById(R.id.progressBar); - postsList = new ArrayList<>(); + postsList = new ArrayList<>(); recyclerView = (RecyclerView) findViewById(R.id.topic_recycler_view); recyclerView.setHasFixedSize(true); - layoutManager = new LinearLayoutManager(getApplicationContext()); + LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList)); + recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList, + new TopicTask())); replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); replyFAB.setEnabled(false); @@ -336,6 +336,7 @@ public class TopicActivity extends BaseActivity { private static final int SUCCESS = 0; private static final int NETWORK_ERROR = 1; private static final int OTHER_ERROR = 2; + private static final int SAME_PAGE = 3; protected void onPreExecute() { progressBar.setVisibility(ProgressBar.VISIBLE); @@ -345,13 +346,13 @@ public class TopicActivity extends BaseActivity { protected Integer doInBackground(String... strings) { Document document; base_url = strings[0].substring(0, strings[0].lastIndexOf(".")); //This topic's base url - String pageUrl = strings[0]; + String newPageUrl = strings[0]; //Finds message focus if present { postFocus = NO_POST_FOCUS; - if (pageUrl.contains("msg")) { - String tmp = pageUrl.substring(pageUrl.indexOf("msg") + 3); + if (newPageUrl.contains("msg")) { + String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); if (tmp.contains(";")) postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(";"))); else @@ -359,21 +360,29 @@ public class TopicActivity extends BaseActivity { } } - Request request = new Request.Builder() - .url(pageUrl) - .build(); - try { - Response response = client.newCall(request).execute(); - document = Jsoup.parse(response.body().string()); - parse(document); - return SUCCESS; - } catch (IOException e) { - Report.i(TAG, "IO Exception", e); - return NETWORK_ERROR; - } catch (Exception e) { - Report.e(TAG, "Exception", e); - return OTHER_ERROR; - } + if (!loadedPageUrl.contains(base_url)) { + Request request = new Request.Builder() + .url(newPageUrl) + .build(); + try { + Response response = client.newCall(request).execute(); + document = Jsoup.parse(response.body().string()); + parse(document); + for (int i = 0; i < postsList.size(); ++i) { + if (postsList.get(i).getPostIndex() == postFocus) { + postFocusPosition = i; + break; + } + } + return SUCCESS; + } catch (IOException e) { + Report.i(TAG, "IO Exception", e); + return NETWORK_ERROR; + } catch (Exception e) { + Report.e(TAG, "Exception", e); + return OTHER_ERROR; + } + } else return SAME_PAGE; } protected void onPostExecute(Integer parseResult) { @@ -381,16 +390,9 @@ public class TopicActivity extends BaseActivity { case SUCCESS: progressBar.setVisibility(ProgressBar.INVISIBLE); - recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList), false); - //Set post focus - if (postFocus != NO_POST_FOCUS) { - for (int i = postsList.size() - 1; i >= 0; --i) { - int currentPostIndex = postsList.get(i).getPostIndex(); - if (currentPostIndex == postFocus) { - layoutManager.scrollToPosition(i); - } - } - } + recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar, + postsList, new TopicTask()), false); + replyFAB.setEnabled(true); //Set current page @@ -408,6 +410,8 @@ public class TopicActivity extends BaseActivity { case NETWORK_ERROR: Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); break; + case SAME_PAGE: + break; default: //Parse failed - should never happen Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 892af27b..7cbce162 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -51,10 +51,7 @@ import mthmmy.utils.Report; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.EXTRAS_PROFILE_URL; -import static gr.thmmy.mthmmy.activities.topic.TopicActivity.NO_POST_FOCUS; -import static gr.thmmy.mthmmy.activities.topic.TopicActivity.PACKAGE_NAME; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.base_url; -import static gr.thmmy.mthmmy.activities.topic.TopicActivity.postFocus; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList; /** @@ -71,10 +68,6 @@ class TopicAdapter extends RecyclerView.Adapter { private static int THUMBNAIL_SIZE; private final Context context; private final List postsList; - /** - * True if there is a post to focus to, false otherwise - */ - private boolean foundPostFocus = false; /** * Used to hold the state of visibility and other attributes for views that are animated or * otherwise changed. Used in combination with {@link #isPostDateAndNumberVisibile}, @@ -96,6 +89,8 @@ class TopicAdapter extends RecyclerView.Adapter { */ private static final int isQuoteButtonChecked = 2; private final MaterialProgressBar progressBar; + private DownloadTask downloadTask; + private final TopicActivity.TopicTask topicTask; /** * Custom {@link RecyclerView.ViewHolder} implementation @@ -155,7 +150,8 @@ class TopicAdapter extends RecyclerView.Adapter { * @param context the context of the {@link RecyclerView} * @param postsList List of {@link Post} objects to use */ - TopicAdapter(Context context, MaterialProgressBar progressBar, List postsList) { + TopicAdapter(Context context, MaterialProgressBar progressBar, List postsList, + TopicActivity.TopicTask topicTask) { this.context = context; this.postsList = postsList; @@ -165,6 +161,8 @@ class TopicAdapter extends RecyclerView.Adapter { viewProperties.add(new boolean[3]); } this.progressBar = progressBar; + downloadTask = new DownloadTask(); + this.topicTask = topicTask; } @Override @@ -240,7 +238,7 @@ class TopicAdapter extends RecyclerView.Adapter { attached.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - DownloadTask downloadTask = new DownloadTask(); + downloadTask = new DownloadTask(); downloadTask.execute(attachedFile); } }); @@ -395,14 +393,6 @@ class TopicAdapter extends RecyclerView.Adapter { }); //Also when post is clicked holder.post.setOnTouchListener(new CustomTouchListener(holder.post, holder.cardView, holder.quoteToggle)); - - //Focuses - if (postFocus != NO_POST_FOCUS && !foundPostFocus) { - if (currentPost.getPostIndex() == postFocus) { - holder.cardView.requestFocus(); - foundPostFocus = true; - } - } } @Override @@ -542,8 +532,7 @@ class TopicAdapter extends RecyclerView.Adapter { } } } - //Restart activity with new data - //TODO + topicTask.execute(uri.toString()); } return true; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareFABBehavior.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareFABBehavior.java index 45e40871..8c818bc9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareFABBehavior.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareFABBehavior.java @@ -11,7 +11,9 @@ import android.view.View; * Extends FloatingActionButton's behavior so the button will hide when scrolling down and show * otherwise. */ +@SuppressWarnings("WeakerAccess") public class ScrollAwareFABBehavior extends CoordinatorLayout.Behavior { + @SuppressWarnings("UnusedParameters") public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { super(); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java index 0de9d16d..2c7b051f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java @@ -10,14 +10,13 @@ import android.view.View; import android.view.ViewPropertyAnimator; /** - * CoordinatorLayout Behavior for bottom navigation bar. + * Extends LinearLayout's behavior. Used for bottom navigation bar. *

When a nested ScrollView is scrolled down, the view will disappear. * When the ScrollView is scrolled back up, the view will reappear.

*/ +@SuppressWarnings("WeakerAccess") public class ScrollAwareLinearBehavior extends CoordinatorLayout.Behavior { - private int mDySinceDirectionChange; - private boolean mIsShowing; - private boolean mIsHiding; + private static final int ANIMATION_DURATION = 100; public ScrollAwareLinearBehavior(Context context, AttributeSet attrs) { super(context, attrs); @@ -29,32 +28,26 @@ public class ScrollAwareLinearBehavior extends CoordinatorLayout.Behavior } @Override - public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) { - if (dy > 0 && mDySinceDirectionChange < 0 - || dy < 0 && mDySinceDirectionChange > 0) { - child.animate().cancel(); - mDySinceDirectionChange = 0; - } - - mDySinceDirectionChange += dy; - - if (mDySinceDirectionChange > child.getHeight() - && child.getVisibility() == View.VISIBLE - && !mIsHiding) { - hide(child); - } else if (mDySinceDirectionChange < 0 - && child.getVisibility() == View.INVISIBLE - && !mIsShowing) { - show(child); + public void onNestedScroll (CoordinatorLayout coordinatorLayout, View bottomNavBar, View target, + int dxConsumed, int dyConsumed, + int dxUnconsumed, int dyUnconsumed){ + super.onNestedScroll(coordinatorLayout, bottomNavBar, target, dxConsumed, dyConsumed,dxUnconsumed, dyUnconsumed); + if (dyConsumed > 0 && bottomNavBar.getVisibility() == View.VISIBLE) { + hide(bottomNavBar); + } else if (dyConsumed < 0 && bottomNavBar.getVisibility() != View.VISIBLE) { + show(bottomNavBar); } } - private void hide(final View view) { - mIsHiding = true; - ViewPropertyAnimator animator = view.animate() - .translationY(view.getHeight()) + /** + * Animates the hiding of a bottom navigation bar. + * @param bottomNavBar bottom navigation bar View + */ + private void hide(final View bottomNavBar) { + ViewPropertyAnimator animator = bottomNavBar.animate() + .translationY(bottomNavBar.getHeight()) .setInterpolator(new FastOutSlowInInterpolator()) - .setDuration(100); + .setDuration(ANIMATION_DURATION); animator.setListener(new Animator.AnimatorListener() { @Override @@ -63,18 +56,11 @@ public class ScrollAwareLinearBehavior extends CoordinatorLayout.Behavior @Override public void onAnimationEnd(Animator animator) { - // Prevent drawing the View after it is gone - mIsHiding = false; - view.setVisibility(View.INVISIBLE); + bottomNavBar.setVisibility(View.INVISIBLE); } @Override public void onAnimationCancel(Animator animator) { - // Canceling a hide should show the view - mIsHiding = false; - if (!mIsShowing) { - show(view); - } } @Override @@ -85,31 +71,28 @@ public class ScrollAwareLinearBehavior extends CoordinatorLayout.Behavior animator.start(); } - private void show(final View view) { - mIsShowing = true; - ViewPropertyAnimator animator = view.animate() + /** + * Animates the showing of a bottom navigation bar. + * @param bottomNavBar bottom navigation bar View + */ + private void show(final View bottomNavBar) { + ViewPropertyAnimator animator = bottomNavBar.animate() .translationY(0) .setInterpolator(new FastOutSlowInInterpolator()) - .setDuration(100); + .setDuration(ANIMATION_DURATION); animator.setListener(new Animator.AnimatorListener() { @Override - public void onAnimationStart(Animator animator) { - view.setVisibility(View.VISIBLE); + public void onAnimationStart(Animator animator){ + bottomNavBar.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animator) { - mIsShowing = false; } @Override public void onAnimationCancel(Animator animator) { - // Canceling a show should hide the view - mIsShowing = false; - if (!mIsHiding) { - hide(view); - } } @Override