Browse Source

Code cleanup and fixes.

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
46df521605
  1. 76
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 27
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareFABBehavior.java
  4. 75
      app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java

76
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. * The key to use when putting topic's title String to {@link TopicActivity}'s Bundle.
*/ */
public static final String EXTRAS_TOPIC_TITLE = "TOPIC_TITLE"; public static final String EXTRAS_TOPIC_TITLE = "TOPIC_TITLE";
static String PACKAGE_NAME;
private static TopicTask topicTask; private static TopicTask topicTask;
//About posts //About posts
private List<Post> postsList; private List<Post> postsList;
static final int NO_POST_FOCUS = -1; private static final int NO_POST_FOCUS = -1;
static int postFocus = NO_POST_FOCUS; private static int postFocus = NO_POST_FOCUS;
private static int postFocusPosition = 0;
//Quotes //Quotes
public static final ArrayList<Integer> toQuoteList = new ArrayList<>(); public static final ArrayList<Integer> toQuoteList = new ArrayList<>();
//Topic's pages //Topic's pages
@ -93,7 +93,7 @@ public class TopicActivity extends BaseActivity {
private FloatingActionButton replyFAB; private FloatingActionButton replyFAB;
private String parsedTitle; private String parsedTitle;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager; private String loadedPageUrl = "";
@Override @Override
@ -101,7 +101,6 @@ public class TopicActivity extends BaseActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic); setContentView(R.layout.activity_topic);
PACKAGE_NAME = getApplicationContext().getPackageName();
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
topicTitle = extras.getString("TOPIC_TITLE"); topicTitle = extras.getString("TOPIC_TITLE");
@ -118,13 +117,14 @@ public class TopicActivity extends BaseActivity {
progressBar = (MaterialProgressBar) findViewById(R.id.progressBar); progressBar = (MaterialProgressBar) findViewById(R.id.progressBar);
postsList = new ArrayList<>(); postsList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.topic_recycler_view); recyclerView = (RecyclerView) findViewById(R.id.topic_recycler_view);
recyclerView.setHasFixedSize(true); recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getApplicationContext()); LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager); 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 = (FloatingActionButton) findViewById(R.id.topic_fab);
replyFAB.setEnabled(false); replyFAB.setEnabled(false);
@ -336,6 +336,7 @@ public class TopicActivity extends BaseActivity {
private static final int SUCCESS = 0; private static final int SUCCESS = 0;
private static final int NETWORK_ERROR = 1; private static final int NETWORK_ERROR = 1;
private static final int OTHER_ERROR = 2; private static final int OTHER_ERROR = 2;
private static final int SAME_PAGE = 3;
protected void onPreExecute() { protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);
@ -345,13 +346,13 @@ public class TopicActivity extends BaseActivity {
protected Integer doInBackground(String... strings) { protected Integer doInBackground(String... strings) {
Document document; Document document;
base_url = strings[0].substring(0, strings[0].lastIndexOf(".")); //This topic's base url 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 //Finds message focus if present
{ {
postFocus = NO_POST_FOCUS; postFocus = NO_POST_FOCUS;
if (pageUrl.contains("msg")) { if (newPageUrl.contains("msg")) {
String tmp = pageUrl.substring(pageUrl.indexOf("msg") + 3); String tmp = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
if (tmp.contains(";")) if (tmp.contains(";"))
postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(";"))); postFocus = Integer.parseInt(tmp.substring(0, tmp.indexOf(";")));
else else
@ -359,21 +360,29 @@ public class TopicActivity extends BaseActivity {
} }
} }
Request request = new Request.Builder() if (!loadedPageUrl.contains(base_url)) {
.url(pageUrl) Request request = new Request.Builder()
.build(); .url(newPageUrl)
try { .build();
Response response = client.newCall(request).execute(); try {
document = Jsoup.parse(response.body().string()); Response response = client.newCall(request).execute();
parse(document); document = Jsoup.parse(response.body().string());
return SUCCESS; parse(document);
} catch (IOException e) { for (int i = 0; i < postsList.size(); ++i) {
Report.i(TAG, "IO Exception", e); if (postsList.get(i).getPostIndex() == postFocus) {
return NETWORK_ERROR; postFocusPosition = i;
} catch (Exception e) { break;
Report.e(TAG, "Exception", e); }
return OTHER_ERROR; }
} 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) { protected void onPostExecute(Integer parseResult) {
@ -381,16 +390,9 @@ public class TopicActivity extends BaseActivity {
case SUCCESS: case SUCCESS:
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList), false); recyclerView.swapAdapter(new TopicAdapter(getApplicationContext(), progressBar,
//Set post focus postsList, new TopicTask()), false);
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);
}
}
}
replyFAB.setEnabled(true); replyFAB.setEnabled(true);
//Set current page //Set current page
@ -408,6 +410,8 @@ public class TopicActivity extends BaseActivity {
case NETWORK_ERROR: case NETWORK_ERROR:
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show();
break; break;
case SAME_PAGE:
break;
default: default:
//Parse failed - should never happen //Parse failed - should never happen
Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Fatal Error", Toast.LENGTH_SHORT).show();

27
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 android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.EXTRAS_PROFILE_URL; 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.base_url;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.postFocus;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList;
/** /**
@ -71,10 +68,6 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
private static int THUMBNAIL_SIZE; private static int THUMBNAIL_SIZE;
private final Context context; private final Context context;
private final List<Post> postsList; private final List<Post> 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 * Used to hold the state of visibility and other attributes for views that are animated or
* otherwise changed. Used in combination with {@link #isPostDateAndNumberVisibile}, * otherwise changed. Used in combination with {@link #isPostDateAndNumberVisibile},
@ -96,6 +89,8 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
*/ */
private static final int isQuoteButtonChecked = 2; private static final int isQuoteButtonChecked = 2;
private final MaterialProgressBar progressBar; private final MaterialProgressBar progressBar;
private DownloadTask downloadTask;
private final TopicActivity.TopicTask topicTask;
/** /**
* Custom {@link RecyclerView.ViewHolder} implementation * Custom {@link RecyclerView.ViewHolder} implementation
@ -155,7 +150,8 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
* @param context the context of the {@link RecyclerView} * @param context the context of the {@link RecyclerView}
* @param postsList List of {@link Post} objects to use * @param postsList List of {@link Post} objects to use
*/ */
TopicAdapter(Context context, MaterialProgressBar progressBar, List<Post> postsList) { TopicAdapter(Context context, MaterialProgressBar progressBar, List<Post> postsList,
TopicActivity.TopicTask topicTask) {
this.context = context; this.context = context;
this.postsList = postsList; this.postsList = postsList;
@ -165,6 +161,8 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
viewProperties.add(new boolean[3]); viewProperties.add(new boolean[3]);
} }
this.progressBar = progressBar; this.progressBar = progressBar;
downloadTask = new DownloadTask();
this.topicTask = topicTask;
} }
@Override @Override
@ -240,7 +238,7 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
attached.setOnClickListener(new View.OnClickListener() { attached.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
DownloadTask downloadTask = new DownloadTask(); downloadTask = new DownloadTask();
downloadTask.execute(attachedFile); downloadTask.execute(attachedFile);
} }
}); });
@ -395,14 +393,6 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
}); });
//Also when post is clicked //Also when post is clicked
holder.post.setOnTouchListener(new CustomTouchListener(holder.post, holder.cardView, holder.quoteToggle)); 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 @Override
@ -542,8 +532,7 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
} }
} }
} }
//Restart activity with new data topicTask.execute(uri.toString());
//TODO
} }
return true; return true;
} }

2
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 * Extends FloatingActionButton's behavior so the button will hide when scrolling down and show
* otherwise. * otherwise.
*/ */
@SuppressWarnings("WeakerAccess")
public class ScrollAwareFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { public class ScrollAwareFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
@SuppressWarnings("UnusedParameters")
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super(); super();
} }

75
app/src/main/java/gr/thmmy/mthmmy/utils/ScrollAwareLinearBehavior.java

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

Loading…
Cancel
Save