From 5914ab0b7ba6ed5108f0901b551e4399f56f041d Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sun, 26 Feb 2017 00:26:00 +0200 Subject: [PATCH] Topic scrollable title and auto scroll, extra info. --- app/build.gradle | 10 +- .../activities/board/BoardActivity.java | 3 +- .../activities/topic/TopicActivity.java | 98 +++++++++++++++++-- .../gr/thmmy/mthmmy/base/BaseActivity.java | 50 +++++----- app/src/main/res/layout/activity_topic.xml | 20 ++-- app/src/main/res/layout/dialog_topic_info.xml | 33 +++++++ app/src/main/res/menu/topic_menu.xml | 16 +++ app/src/main/res/values/colors.xml | 1 + 8 files changed, 186 insertions(+), 45 deletions(-) create mode 100644 app/src/main/res/layout/dialog_topic_info.xml create mode 100644 app/src/main/res/menu/topic_menu.xml diff --git a/app/build.gradle b/app/build.gradle index bbf5b90d..2469aa31 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,11 +28,11 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:25.1.0' - compile 'com.android.support:design:25.1.0' - compile 'com.android.support:support-v4:25.1.0' - compile 'com.android.support:cardview-v7:25.1.0' - compile 'com.android.support:recyclerview-v7:25.1.0' + compile 'com.android.support:appcompat-v7:25.2.0' + compile 'com.android.support:design:25.2.0' + compile 'com.android.support:support-v4:25.2.0' + compile 'com.android.support:cardview-v7:25.2.0' + compile 'com.android.support:recyclerview-v7:25.2.0' compile 'com.google.firebase:firebase-crash:10.0.1' compile 'com.squareup.okhttp3:okhttp:3.5.0' compile 'com.squareup.picasso:picasso:2.5.2' diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index 77d2165d..c44afe1b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -92,8 +92,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo } thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl)); - thisPageBookmarkButton = (ImageButton) findViewById(R.id.bookmark); - setBoardBookmark(); + setBoardBookmark((ImageButton) findViewById(R.id.bookmark)); createDrawer(); progressBar = (MaterialProgressBar) findViewById(R.id.progressBar); 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 7f0393a9..82100a2d 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 @@ -6,10 +6,21 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; +import android.text.Html; +import android.text.SpannableString; +import android.text.SpannableStringBuilder; +import android.text.method.LinkMovementMethod; +import android.text.method.ScrollingMovementMethod; +import android.text.style.ClickableSpan; +import android.text.style.URLSpan; import android.util.SparseArray; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; @@ -92,6 +103,9 @@ public class TopicActivity extends BaseActivity { private TextView pageIndicator; private ImageButton nextPage; private ImageButton lastPage; + //Topic's info + SpannableStringBuilder topicTreeAndMods = new SpannableStringBuilder("Loading..."), + topicViewers = new SpannableStringBuilder("Loading..."); //Other variables private MaterialProgressBar progressBar; private static String base_url = ""; @@ -117,18 +131,22 @@ public class TopicActivity extends BaseActivity { finish(); } + thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl)); + //Initializes graphics toolbar = (Toolbar) findViewById(R.id.toolbar); - toolbar.setTitle(topicTitle); + TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); + toolbarTitle.setText(topicTitle); setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); } - thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl)); - thisPageBookmarkButton = (ImageButton) findViewById(R.id.bookmark); - setTopicBookmark(); + //Makes title scrollable + toolbarTitle.setHorizontallyScrolling(true); + toolbarTitle.setMovementMethod(new ScrollingMovementMethod()); + createDrawer(); progressBar = (MaterialProgressBar) findViewById(R.id.progressBar); @@ -179,6 +197,43 @@ public class TopicActivity extends BaseActivity { topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflates the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.topic_menu, menu); + setTopicBookmark(menu.getItem(0)); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle presses on the action bar items + switch (item.getItemId()) { + case R.id.menu_bookmark: + topicMenuBookmarkClick(); + return true; + case R.id.menu_info: + AlertDialog.Builder builder = new AlertDialog.Builder(this); + LayoutInflater inflater = this.getLayoutInflater(); + LinearLayout infoDialog = (LinearLayout) inflater.inflate(R.layout.dialog_topic_info + , null); + ((TextView) infoDialog.findViewById(R.id.dialog_title)).setText("Info"); + TextView treeAndMods = (TextView) infoDialog.findViewById(R.id.topic_tree_and_mods); + treeAndMods.setText(topicTreeAndMods); + treeAndMods.setMovementMethod(LinkMovementMethod.getInstance()); + TextView usersViewing = (TextView) infoDialog.findViewById(R.id.users_viewing); + usersViewing.setText(topicViewers); + usersViewing.setMovementMethod(LinkMovementMethod.getInstance()); + + builder.setView(infoDialog); + AlertDialog dialog = builder.create(); + dialog.show(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + @Override public void onBackPressed() { if (drawer.isDrawerOpen()) { @@ -476,7 +531,8 @@ public class TopicActivity extends BaseActivity { case SUCCESS: if (topicTitle == null || Objects.equals(topicTitle, "")) { thisPageBookmark = new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl)); - setTopicBookmark(); + invalidateOptionsMenu(); + //setTopicBookmark(menu.getItem(0)); } progressBar.setVisibility(ProgressBar.INVISIBLE); @@ -522,7 +578,13 @@ public class TopicActivity extends BaseActivity { private void parse(Document topic) { ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); - //Find reply page url + //Finds topic's tree, mods and users viewing + { + topicTreeAndMods = getSpannableFromHtml(topic.select("div.nav").first().html()); + topicViewers = getSpannableFromHtml(TopicParser.parseUsersViewingThisTopic(topic, language)); + } + + //Finds reply page url { Element replyButton = topic.select("a:has(img[alt=Reply])").first(); if (replyButton == null) @@ -558,6 +620,30 @@ public class TopicActivity extends BaseActivity { postsList.clear(); postsList.addAll(TopicParser.parseTopic(topic, language)); } + + private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) + { + int start = strBuilder.getSpanStart(span); + int end = strBuilder.getSpanEnd(span); + int flags = strBuilder.getSpanFlags(span); + ClickableSpan clickable = new ClickableSpan() { + public void onClick(View view) { + //TODO + } + }; + strBuilder.setSpan(clickable, start, end, flags); + strBuilder.removeSpan(span); + } + + private SpannableStringBuilder getSpannableFromHtml(String html) { + CharSequence sequence = Html.fromHtml(html); + SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence); + URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class); + for(URLSpan span : urls) { + makeLinkClickable(strBuilder, span); + } + return strBuilder; + } } class ReplyTask extends AsyncTask { diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 2147a1d3..9e2a3023 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -14,6 +14,7 @@ import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.view.MenuItem; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; @@ -63,7 +64,8 @@ public abstract class BaseActivity extends AppCompatActivity { private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; protected Bookmark thisPageBookmark; - protected ImageButton thisPageBookmarkButton; + protected MenuItem thisPageBookmarkMenuButton; + protected ImageButton thisPageBookmarkImageButton; private SharedPreferences bookmarksFile; private ArrayList topicsBookmarked; private ArrayList boardsBookmarked; @@ -408,42 +410,42 @@ public abstract class BaseActivity extends AppCompatActivity { return topicsBookmarked; } - protected void setTopicBookmark() { + protected void setTopicBookmark(MenuItem thisPageBookmarkMenuButton) { + this.thisPageBookmarkMenuButton = thisPageBookmarkMenuButton; if (thisPageBookmark.matchExists(topicsBookmarked)) { - thisPageBookmarkButton.setImageDrawable(bookmarked); + thisPageBookmarkMenuButton.setIcon(bookmarked); } else { - thisPageBookmarkButton.setImageDrawable(notBookmarked); + thisPageBookmarkMenuButton.setIcon(notBookmarked); + } + } + + protected void topicMenuBookmarkClick(){ + if (thisPageBookmark.matchExists(topicsBookmarked)) { + thisPageBookmarkMenuButton.setIcon(notBookmarked); + toggleTopicToBookmarks(thisPageBookmark); + Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); + } else { + thisPageBookmarkMenuButton.setIcon(bookmarked); + toggleTopicToBookmarks(thisPageBookmark); + Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); } - thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (thisPageBookmark.matchExists(topicsBookmarked)) { - thisPageBookmarkButton.setImageDrawable(notBookmarked); - toggleTopicToBookmarks(thisPageBookmark); - Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); - } else { - thisPageBookmarkButton.setImageDrawable(bookmarked); - toggleTopicToBookmarks(thisPageBookmark); - Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); - } - } - }); } - protected void setBoardBookmark() { + protected void setBoardBookmark(final ImageButton thisPageBookmarkImageButton) { + this.thisPageBookmarkImageButton = thisPageBookmarkImageButton; if (thisPageBookmark.matchExists(boardsBookmarked)) { - thisPageBookmarkButton.setImageDrawable(bookmarked); + thisPageBookmarkImageButton.setImageDrawable(bookmarked); } else { - thisPageBookmarkButton.setImageDrawable(notBookmarked); + thisPageBookmarkImageButton.setImageDrawable(notBookmarked); } - thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { + thisPageBookmarkImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (thisPageBookmark.matchExists(boardsBookmarked)) { - thisPageBookmarkButton.setImageDrawable(notBookmarked); + thisPageBookmarkImageButton.setImageDrawable(notBookmarked); Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); } else { - thisPageBookmarkButton.setImageDrawable(bookmarked); + thisPageBookmarkImageButton.setImageDrawable(bookmarked); Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); } toggleBoardToBookmarks(thisPageBookmark); diff --git a/app/src/main/res/layout/activity_topic.xml b/app/src/main/res/layout/activity_topic.xml index 23b12737..e4735bcc 100644 --- a/app/src/main/res/layout/activity_topic.xml +++ b/app/src/main/res/layout/activity_topic.xml @@ -23,15 +23,19 @@ android:background="?attr/colorPrimary" app:popupTheme="@style/ToolbarTheme"> - + android:ellipsize="marquee" + android:fillViewport="true" + android:focusable="true" + android:focusableInTouchMode="true" + android:marqueeRepeatLimit="marquee_forever" + android:maxLines="1" + android:scrollHorizontally="true" + android:textColor="@color/white" + /> diff --git a/app/src/main/res/layout/dialog_topic_info.xml b/app/src/main/res/layout/dialog_topic_info.xml new file mode 100644 index 00000000..47b8ec31 --- /dev/null +++ b/app/src/main/res/layout/dialog_topic_info.xml @@ -0,0 +1,33 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/topic_menu.xml b/app/src/main/res/menu/topic_menu.xml new file mode 100644 index 00000000..16c60e0a --- /dev/null +++ b/app/src/main/res/menu/topic_menu.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 45d9efda..7af0467a 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -16,6 +16,7 @@ #323232 #3C3F41 #8B8B8B + #4B0082 #FFFFFF #CCCCCC