Browse Source

Bookmarks init and fixes.

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
a78a432088
  1. 15
      app/src/main/AndroidManifest.xml
  2. 62
      app/src/main/java/gr/thmmy/mthmmy/activities/BookmarkActivity.java
  3. 15
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  4. 16
      app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java
  5. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  6. 38
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  7. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  8. 227
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  9. 18
      app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java
  10. 47
      app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java
  11. 69
      app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java
  12. BIN
      app/src/main/res/drawable-hdpi/ic_bookmark_false.png
  13. BIN
      app/src/main/res/drawable-hdpi/ic_bookmark_true.png
  14. BIN
      app/src/main/res/drawable-mdpi/ic_bookmark_false.png
  15. BIN
      app/src/main/res/drawable-mdpi/ic_bookmark_true.png
  16. BIN
      app/src/main/res/drawable-xhdpi/ic_bookmark_false.png
  17. BIN
      app/src/main/res/drawable-xhdpi/ic_bookmark_true.png
  18. BIN
      app/src/main/res/drawable-xxhdpi/ic_bookmark_false.png
  19. BIN
      app/src/main/res/drawable-xxhdpi/ic_bookmark_true.png
  20. BIN
      app/src/main/res/drawable-xxxhdpi/ic_bookmark_false.png
  21. BIN
      app/src/main/res/drawable-xxxhdpi/ic_bookmark_true.png
  22. 10
      app/src/main/res/layout/activity_board.xml
  23. 57
      app/src/main/res/layout/activity_bookmark.xml
  24. 10
      app/src/main/res/layout/activity_topic.xml
  25. 7
      app/src/main/res/values/strings.xml

15
app/src/main/AndroidManifest.xml

@ -15,7 +15,6 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.main.MainActivity"
android:configChanges="orientation|screenSize"
@ -28,7 +27,6 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
@ -37,7 +35,6 @@
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan">
</activity>
<activity
android:name=".activities.AboutActivity"
android:parentActivityName=".activities.main.MainActivity"
@ -46,7 +43,6 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity"/>
</activity>
<activity
android:name=".activities.topic.TopicActivity"
android:configChanges="orientation|screenSize"
@ -56,12 +52,10 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity"/>
</activity>
<activity
android:name=".activities.profile.ProfileActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".activities.board.BoardActivity"
android:configChanges="orientation|screenSize"
@ -71,7 +65,6 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity"/>
</activity>
<activity
android:name=".activities.downloads.DownloadsActivity"
android:parentActivityName=".activities.main.MainActivity"
@ -80,6 +73,14 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity"/>
</activity>
<activity
android:name=".activities.BookmarkActivity"
android:parentActivityName=".activities.main.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity"/>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"

62
app/src/main/java/gr/thmmy/mthmmy/activities/BookmarkActivity.java

@ -0,0 +1,62 @@
package gr.thmmy.mthmmy.activities;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Bookmark;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
public class BookmarkActivity extends BaseActivity {
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmark);
//Initialize toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Bookmarks");
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
createDrawer();
drawer.setSelection(BOOKMARKS_ID);
progressBar = (MaterialProgressBar) findViewById(R.id.progressBar);
LinearLayout bookmarksLinearView = (LinearLayout) findViewById(R.id.bookmarks_container);
TextView tmp = new TextView(this);
tmp.setText("Your bookmarked boards:");
bookmarksLinearView.addView(tmp);
for (Bookmark bookmarkedBoard : getBoardsBookmarked()) {
if (bookmarkedBoard != null && bookmarkedBoard.getTitle() != null) {
TextView bookmarkedBoardView = new TextView(this);
bookmarkedBoardView.setText(bookmarkedBoard.getTitle());
bookmarksLinearView.addView(bookmarkedBoardView);
}
}
tmp = new TextView(this);
tmp.setText("Your bookmarked topics:");
bookmarksLinearView.addView(tmp);
for (Bookmark bookmarkedTopic : getTopicsBookmarked()) {
if (bookmarkedTopic != null && bookmarkedTopic.getTitle() != null) {
TextView bookmarkedTopicView = new TextView(this);
bookmarkedTopicView.setText(bookmarkedTopic.getTitle());
bookmarksLinearView.addView(bookmarkedTopicView);
}
}
}
}

15
app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java

@ -1,17 +1,15 @@
package gr.thmmy.mthmmy.activities.board;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
@ -26,10 +24,10 @@ import java.util.Objects;
import javax.net.ssl.SSLHandshakeException;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Board;
import gr.thmmy.mthmmy.model.LinkTarget;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.Topic;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report;
@ -76,8 +74,8 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
Bundle extras = getIntent().getExtras();
boardTitle = extras.getString(BUNDLE_BOARD_TITLE);
boardUrl = extras.getString(BUNDLE_BOARD_URL);
LinkTarget.Target target = LinkTarget.resolveLinkTarget(Uri.parse(boardUrl));
if (!target.is(LinkTarget.Target.BOARD)) {
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(boardUrl));
if (!target.is(ThmmyPage.PageCategory.BOARD)) {
Report.e(TAG, "Bundle came with a non board url!\nUrl:\n" + boardUrl);
Toast.makeText(this, "An error has occurred\nAborting.", Toast.LENGTH_SHORT).show();
finish();
@ -93,7 +91,10 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
setBoardBookmark((ImageButton) findViewById(R.id.bookmark),
new Bookmark(boardTitle, boardUrl));
createDrawer();
progressBar = (MaterialProgressBar) findViewById(R.id.progressBar);
newTopicFAB = (FloatingActionButton) findViewById(R.id.board_fab);
newTopicFAB.setEnabled(false);

16
app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java

@ -25,7 +25,7 @@ import javax.net.ssl.SSLHandshakeException;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Download;
import gr.thmmy.mthmmy.model.LinkTarget;
import gr.thmmy.mthmmy.model.ThmmyPage;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report;
import okhttp3.Request;
@ -73,8 +73,8 @@ public class DownloadsActivity extends BaseActivity implements DownloadsAdapter.
downloadsTitle = "Downloads";
downloadsUrl = extras.getString(BUNDLE_DOWNLOADS_URL);
if (downloadsUrl != null && !Objects.equals(downloadsUrl, "")) {
LinkTarget.Target target = LinkTarget.resolveLinkTarget(Uri.parse(downloadsUrl));
if (!target.is(LinkTarget.Target.DOWNLOADS)) {
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(downloadsUrl));
if (!target.is(ThmmyPage.PageCategory.DOWNLOADS)) {
Report.e(TAG, "Bundle came with a non board url!\nUrl:\n" + downloadsUrl);
Toast.makeText(this, "An error has occurred\nAborting.", Toast.LENGTH_SHORT).show();
finish();
@ -85,8 +85,10 @@ public class DownloadsActivity extends BaseActivity implements DownloadsAdapter.
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(downloadsTitle);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
createDrawer();
drawer.setSelection(DOWNLOADS_ID);
@ -222,8 +224,8 @@ public class DownloadsActivity extends BaseActivity implements DownloadsAdapter.
}
Download.DownloadItemType type;
if (LinkTarget.resolveLinkTarget(Uri.parse(thisPageUrl)).is(LinkTarget.
Target.DOWNLOADS_CATEGORY)) type = Download.DownloadItemType.DOWNLOADS_CATEGORY;
if (ThmmyPage.resolvePageCategory(Uri.parse(thisPageUrl)).is(ThmmyPage.
PageCategory.DOWNLOADS_CATEGORY)) type = Download.DownloadItemType.DOWNLOADS_CATEGORY;
else type = Download.DownloadItemType.DOWNLOADS_FILE;
Elements pages = downloadPage.select("a.navPages");

13
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -1,6 +1,5 @@
package gr.thmmy.mthmmy.activities.profile;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
@ -12,7 +11,6 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
@ -33,14 +31,13 @@ import java.util.Objects;
import javax.net.ssl.SSLHandshakeException;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.activities.profile.latestPosts.LatestPostsFragment;
import gr.thmmy.mthmmy.activities.profile.stats.StatsFragment;
import gr.thmmy.mthmmy.activities.profile.summary.SummaryFragment;
import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.LinkTarget;
import gr.thmmy.mthmmy.model.PostSummary;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CircleTransform;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report;
@ -166,16 +163,16 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
});
}*/
LinkTarget.Target target = LinkTarget.resolveLinkTarget(Uri.parse(profileUrl));
if (!target.is(LinkTarget.Target.PROFILE)) {
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(Uri.parse(profileUrl));
if (!target.is(ThmmyPage.PageCategory.PROFILE)) {
Report.e(TAG, "Bundle came with a non profile url!\nUrl:\n" + profileUrl);
Toast.makeText(this, "An error has occurred\n Aborting.", Toast.LENGTH_SHORT).show();
finish();
}
if (target.is(LinkTarget.Target.PROFILE_STATS)) {
if (target.is(ThmmyPage.PageCategory.PROFILE_STATS)) {
profileUrl = profileUrl.substring(0, profileUrl.indexOf(";sa=statPanel"));
tabSelect = 2;
} else if (target.is(LinkTarget.Target.PROFILE_LATEST_POSTS)) {
} else if (target.is(ThmmyPage.PageCategory.PROFILE_LATEST_POSTS)) {
profileUrl = profileUrl.substring(0, profileUrl.indexOf(";sa=showPosts"));
tabSelect = 1;
}

38
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -29,8 +29,9 @@ import java.util.Objects;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.LinkTarget;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.ParseHelpers;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import mthmmy.utils.Report;
@ -70,7 +71,6 @@ public class TopicActivity extends BaseActivity {
public static final ArrayList<Integer> toQuoteList = new ArrayList<>();
//Topic's pages
private int thisPage = 1;
public static String base_url = "";
private int numberOfPages = 1;
private final SparseArray<String> pagesUrls = new SparseArray<>();
//Page select
@ -88,9 +88,10 @@ public class TopicActivity extends BaseActivity {
private ImageButton nextPage;
private ImageButton lastPage;
//Other variables
private FloatingActionButton replyFAB;
private MaterialProgressBar progressBar;
public static String base_url = "";
private String topicTitle;
private FloatingActionButton replyFAB;
private String parsedTitle;
private RecyclerView recyclerView;
private String loadedPageUrl = "";
@ -104,10 +105,11 @@ public class TopicActivity extends BaseActivity {
Bundle extras = getIntent().getExtras();
topicTitle = extras.getString(BUNDLE_TOPIC_TITLE);
LinkTarget.Target target = LinkTarget.resolveLinkTarget(
Uri.parse(extras.getString(BUNDLE_TOPIC_URL)));
if (!target.is(LinkTarget.Target.TOPIC)) {
Report.e(TAG, "Bundle came with a non topic url!\nUrl:\n" + extras.getString(BUNDLE_TOPIC_URL));
String topicPageUrl = extras.getString(BUNDLE_TOPIC_URL);
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(
Uri.parse(topicPageUrl));
if (!target.is(ThmmyPage.PageCategory.TOPIC)) {
Report.e(TAG, "Bundle came with a non topic url!\nUrl:\n" + topicPageUrl);
Toast.makeText(this, "An error has occurred\n Aborting.", Toast.LENGTH_SHORT).show();
finish();
}
@ -121,6 +123,8 @@ public class TopicActivity extends BaseActivity {
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
setTopicBookmark((ImageButton) findViewById(R.id.bookmark),
new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl)));
createDrawer();
progressBar = (MaterialProgressBar) findViewById(R.id.progressBar);
@ -458,14 +462,25 @@ public class TopicActivity extends BaseActivity {
}
//Checks if the page to be loaded is the one already shown
if (!Objects.equals(loadedPageUrl, "") && !loadedPageUrl.contains(base_url)) {
if (!Objects.equals(loadedPageUrl, "") && loadedPageUrl.contains(base_url)) {
if (newPageUrl.contains("topicseen#new"))
if (Integer.parseInt(loadedPageUrl.substring(base_url.length())) == numberOfPages)
if (thisPage == numberOfPages)
return SAME_PAGE;
if (newPageUrl.contains("msg")) {
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
if (tmpUrlSbstr.contains("msg"))
tmpUrlSbstr = tmpUrlSbstr.substring(0, tmpUrlSbstr.indexOf("msg") - 1);
int testAgainst = Integer.parseInt(tmpUrlSbstr);
for (Post post : postsList) {
if (post.getPostIndex() == testAgainst) {
return SAME_PAGE;
}
}
}
if (Objects.equals(loadedPageUrl.substring(base_url.length())
, newPageUrl.substring(base_url.length())))
return SAME_PAGE;
}
} else topicTitle = null;
loadedPageUrl = newPageUrl;
Request request = new Request.Builder()
@ -496,6 +511,9 @@ public class TopicActivity extends BaseActivity {
switch (parseResult) {
case SUCCESS:
setTopicBookmark((ImageButton) findViewById(R.id.bookmark),
new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl)));
progressBar.setVisibility(ProgressBar.INVISIBLE);
topicAdapter.customNotifyDataSetChanged(new TopicTask());
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(true);

10
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -43,8 +43,8 @@ import java.util.Objects;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.board.BoardActivity;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.model.LinkTarget;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CircleTransform;
import gr.thmmy.mthmmy.utils.FileManager.ThmmyFile;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -552,8 +552,8 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
private boolean handleUri(final Uri uri) {
final String uriString = uri.toString();
LinkTarget.Target target = LinkTarget.resolveLinkTarget(uri);
if (target.is(LinkTarget.Target.TOPIC)) {
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(uri);
if (target.is(ThmmyPage.PageCategory.TOPIC)) {
//This url points to a topic
//Checks if this is the current topic
if (Objects.equals(uriString.substring(0, uriString.lastIndexOf(".")), base_url)) {
@ -574,7 +574,7 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
}
topicTask.execute(uri.toString());
return true;
} else if (target.is(LinkTarget.Target.BOARD)) {
} else if (target.is(ThmmyPage.PageCategory.BOARD)) {
Intent intent = new Intent(context, BoardActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_BOARD_URL, uriString);
@ -583,7 +583,7 @@ class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MyViewHolder> {
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} else if (target.is(LinkTarget.Target.PROFILE)) {
} else if (target.is(ThmmyPage.PageCategory.PROFILE)) {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_PROFILE_URL, uriString);

227
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -1,13 +1,18 @@
package gr.thmmy.mthmmy.base;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
@ -21,13 +26,20 @@ import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.AboutActivity;
import gr.thmmy.mthmmy.activities.BookmarkActivity;
import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity;
import gr.thmmy.mthmmy.activities.main.MainActivity;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.ObjectSerializer;
import okhttp3.OkHttpClient;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
@ -44,6 +56,16 @@ public abstract class BaseActivity extends AppCompatActivity {
//SessionManager
protected static SessionManager sessionManager;
//Bookmarks
private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs";
private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey";
private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey";
protected static SharedPreferences bookmarksFile;
protected static ArrayList<Bookmark> topicsBookmarked;
protected static ArrayList<Bookmark> boardsBookmarked;
protected static Drawable bookmarked;
protected static Drawable notBookmarked;
//Common UI elements
protected Toolbar toolbar;
protected Drawer drawer;
@ -56,6 +78,25 @@ public abstract class BaseActivity extends AppCompatActivity {
// they become null when app restarts after crash
if (sessionManager == null)
sessionManager = BaseApplication.getInstance().getSessionManager();
if (sessionManager.isLoggedIn()) {
if (bookmarked == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
bookmarked = getResources().getDrawable(R.drawable.ic_bookmark_true, null);
} else //noinspection deprecation
bookmarked = getResources().getDrawable(R.drawable.ic_bookmark_true);
}
if (notBookmarked == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notBookmarked = getResources().getDrawable(R.drawable.ic_bookmark_false, null);
} else //noinspection deprecation
notBookmarked = getResources().getDrawable(R.drawable.ic_bookmark_false);
}
if (topicsBookmarked == null || boardsBookmarked == null) {
bookmarksFile = getSharedPreferences(BOOKMARKS_SHARED_PREFS, Context.MODE_PRIVATE);
loadSavedBookmarks();
}
}
}
@Override
@ -84,14 +125,16 @@ public abstract class BaseActivity extends AppCompatActivity {
//------------------------------------------DRAWER STUFF----------------------------------------
protected static final int HOME_ID = 0;
protected static final int DOWNLOADS_ID = 1;
protected static final int LOG_ID = 2;
protected static final int ABOUT_ID = 3;
protected static final int BOOKMARKS_ID = 2;
protected static final int LOG_ID = 3;
protected static final int ABOUT_ID = 4;
private AccountHeader accountHeader;
private ProfileDrawerItem profileDrawerItem;
private PrimaryDrawerItem homeItem, downloadsItem, loginLogoutItem, aboutItem;
private IconicsDrawable homeIcon, homeIconSelected, downloadsIcon, downloadsIconSelected, loginIcon, logoutIcon,
aboutIcon, aboutIconSelected;
private PrimaryDrawerItem homeItem, downloadsItem, bookmarksItem, loginLogoutItem, aboutItem;
private IconicsDrawable homeIcon, homeIconSelected, downloadsIcon, downloadsIconSelected,
bookmarksIcon, bookmarksIconSelected, loginIcon, logoutIcon, aboutIcon,
aboutIconSelected;
/**
* Call only after initializing Toolbar
@ -118,6 +161,14 @@ public abstract class BaseActivity extends AppCompatActivity {
.icon(FontAwesome.Icon.faw_download)
.color(selectedSecondaryColor);
bookmarksIcon = new IconicsDrawable(this)
.icon(FontAwesome.Icon.faw_bookmark)
.color(primaryColor);
bookmarksIconSelected = new IconicsDrawable(this)
.icon(FontAwesome.Icon.faw_bookmark)
.color(selectedSecondaryColor);
loginIcon = new IconicsDrawable(this)
.icon(FontAwesome.Icon.faw_sign_in)
.color(primaryColor);
@ -162,6 +213,14 @@ public abstract class BaseActivity extends AppCompatActivity {
.withName(R.string.downloads)
.withIcon(downloadsIcon)
.withSelectedIcon(downloadsIconSelected);
bookmarksItem = new PrimaryDrawerItem()
.withTextColor(primaryColor)
.withSelectedColor(selectedPrimaryColor)
.withSelectedTextColor(selectedSecondaryColor)
.withIdentifier(BOOKMARKS_ID)
.withName(R.string.bookmark)
.withIcon(bookmarksIcon)
.withSelectedIcon(bookmarksIconSelected);
} else
loginLogoutItem = new PrimaryDrawerItem()
.withTextColor(primaryColor)
@ -228,8 +287,7 @@ public abstract class BaseActivity extends AppCompatActivity {
startActivity(i);
}
} else if (drawerItem.equals(DOWNLOADS_ID)) {
if (!(BaseActivity.this instanceof DownloadsActivity)) //When logged out or if user is guest
{
if (!(BaseActivity.this instanceof DownloadsActivity)) {
Intent i = new Intent(BaseActivity.this, DownloadsActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_DOWNLOADS_URL, "");
@ -237,6 +295,11 @@ public abstract class BaseActivity extends AppCompatActivity {
i.putExtras(extras);
startActivity(i);
}
} else if (drawerItem.equals(BOOKMARKS_ID)) {
if (!(BaseActivity.this instanceof BookmarkActivity)) {
Intent i = new Intent(BaseActivity.this, BookmarkActivity.class);
startActivity(i);
}
} else if (drawerItem.equals(LOG_ID)) {
if (!sessionManager.isLoggedIn()) //When logged out or if user is guest
{
@ -260,7 +323,7 @@ public abstract class BaseActivity extends AppCompatActivity {
});
if (sessionManager.isLoggedIn())
drawerBuilder.addDrawerItems(homeItem, downloadsItem, loginLogoutItem, aboutItem);
drawerBuilder.addDrawerItems(homeItem, downloadsItem, bookmarksItem, loginLogoutItem, aboutItem);
else
drawerBuilder.addDrawerItems(homeItem, loginLogoutItem, aboutItem);
@ -298,7 +361,7 @@ public abstract class BaseActivity extends AppCompatActivity {
}
//-------------------------------------------LOGOUT-------------------------------------------------
//-------------------------------------------LOGOUT-------------------------------------------------
/**
* Result toast will always display a success, because when user chooses logout all data are
@ -328,5 +391,151 @@ public abstract class BaseActivity extends AppCompatActivity {
}
//-----------------------------------------LOGOUT END-----------------------------------------------
//---------------------------------------------BOOKMARKS--------------------------------------------
protected ArrayList<Bookmark> getBoardsBookmarked() {
return boardsBookmarked;
}
protected ArrayList<Bookmark> getTopicsBookmarked() {
return topicsBookmarked;
}
protected void setTopicBookmark(ImageButton bookmarkView, final Bookmark bookmark) {
if (matchExists(bookmark, topicsBookmarked)) {
bookmarkView.setImageDrawable(bookmarked);
} else {
bookmarkView.setImageDrawable(notBookmarked);
}
bookmarkView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matchExists(bookmark, topicsBookmarked)) {
((ImageButton) view).setImageDrawable(notBookmarked);
toggleTopicToBookmarks(bookmark);
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show();
} else {
((ImageButton) view).setImageDrawable(bookmarked);
toggleTopicToBookmarks(bookmark);
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show();
}
}
});
}
protected void setBoardBookmark(ImageButton bookmarkView, final Bookmark bookmark) {
if (matchExists(bookmark, boardsBookmarked)) {
bookmarkView.setImageDrawable(bookmarked);
} else {
bookmarkView.setImageDrawable(notBookmarked);
}
bookmarkView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matchExists(bookmark, boardsBookmarked)) {
((ImageButton) view).setImageDrawable(notBookmarked);
toggleBoardToBookmarks(bookmark);
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show();
} else {
((ImageButton) view).setImageDrawable(bookmarked);
toggleBoardToBookmarks(bookmark);
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show();
}
}
});
}
private void loadSavedBookmarks() {
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null);
if (tmpString != null)
try {
topicsBookmarked = (ArrayList<Bookmark>) ObjectSerializer.deserialize(tmpString);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
else {
topicsBookmarked = new ArrayList<>();
topicsBookmarked.add(null);
}
tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null);
if (tmpString != null)
try {
boardsBookmarked = (ArrayList<Bookmark>) ObjectSerializer.deserialize(tmpString);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
else {
boardsBookmarked = new ArrayList<>();
boardsBookmarked.add(null);
}
}
private void toggleBoardToBookmarks(Bookmark bookmark) {
if (boardsBookmarked == null) return;
if (matchExists(bookmark, boardsBookmarked)) {
if (boardsBookmarked.size() == 1) boardsBookmarked.set(0, null);
else boardsBookmarked.remove(findIndex(bookmark, boardsBookmarked));
} else boardsBookmarked.add(bookmark);
updateBoardBookmarks();
}
private void toggleTopicToBookmarks(Bookmark bookmark) {
if (topicsBookmarked == null) return;
if (matchExists(bookmark, topicsBookmarked))
topicsBookmarked.remove(findIndex(bookmark, topicsBookmarked));
else topicsBookmarked.add(bookmark);
updateTopicBookmarks();
}
private void updateBoardBookmarks() {
String tmpString = null;
if (!(boardsBookmarked.size() == 1 && boardsBookmarked.get(0) == null)) {
try {
tmpString = ObjectSerializer.serialize(boardsBookmarked);
} catch (IOException e) {
e.printStackTrace();
}
}
SharedPreferences.Editor editor = bookmarksFile.edit();
editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply();
}
private void updateTopicBookmarks() {
String tmpString = null;
if (!(topicsBookmarked.size() == 1 && topicsBookmarked.get(0) == null)) {
try {
tmpString = ObjectSerializer.serialize(topicsBookmarked);
} catch (IOException e) {
e.printStackTrace();
}
}
SharedPreferences.Editor editor = bookmarksFile.edit();
editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply();
}
private boolean matchExists(Bookmark bookmark, ArrayList<Bookmark> array) {
if (!array.isEmpty()) {
for (Bookmark b : array) {
if (b != null) {
return Objects.equals(b.getId(), bookmark.getId())
&& Objects.equals(b.getTitle(), bookmark.getTitle());
}
}
}
return false;
}
private int findIndex(Bookmark bookmark, ArrayList<Bookmark> array) {
if (array.size() == 1 && array.get(0) == null) return -1;
if (!array.isEmpty()) {
for (int i = 0; i < array.size(); ++i) {
if (array.get(i) != null && Objects.equals(array.get(i).getId(), bookmark.getId())
&& Objects.equals(array.get(i).getTitle(), bookmark.getTitle()))
return i;
}
}
return -1;
}
//-------------------------------------------BOOKMARKS END------------------------------------------
}

18
app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java

@ -0,0 +1,18 @@
package gr.thmmy.mthmmy.model;
public class Bookmark {
private final String title, id;
public Bookmark(String title, String id) {
this.title = title;
this.id = id;
}
public String getTitle() {
return title;
}
public String getId() {
return id;
}
}

47
app/src/main/java/gr/thmmy/mthmmy/model/LinkTarget.java → app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java

@ -11,7 +11,7 @@ import mthmmy.utils.Report;
* classes). It can be used to resolve link targets as to whether they are pointing to the forum and
* where in the forum they may point.
*/
public class LinkTarget {
public class ThmmyPage {
/**
* Debug Tag for logging debug output to LogCat
*/
@ -32,7 +32,7 @@ public class LinkTarget {
* <li>{@link #PROFILE}</li>
* </ul>
*/
public enum Target {
public enum PageCategory {
/**
* Link doesn't point to thmmy.
*/
@ -87,7 +87,7 @@ public class LinkTarget {
DOWNLOADS;
/**
* This method defines a custom equality check for {@link Target} enums. It does not check
* This method defines a custom equality check for {@link PageCategory} enums. It does not check
* whether a url is equal to another.
* <p>Method returns true if parameter's Target is the same as the object and in the specific
* cases described below, false otherwise.</p><ul>
@ -106,7 +106,7 @@ public class LinkTarget {
* @param other another Target
* @return true if <b>enums</b> are equal, false otherwise
*/
public boolean is(Target other) {
public boolean is(PageCategory other) {
return ((this == PROFILE_LATEST_POSTS || this == PROFILE_STATS || this == PROFILE_SUMMARY)
&& other == PROFILE)
|| ((this == DOWNLOADS_FILE || this == DOWNLOADS_CATEGORY) && other == DOWNLOADS)
@ -122,7 +122,7 @@ public class LinkTarget {
* @return true if url is pointing to thmmy, false otherwise
*/
public static boolean isThmmy(Uri uri) {
return resolveLinkTarget(uri) != Target.NOT_THMMY;
return resolvePageCategory(uri) != PageCategory.NOT_THMMY;
}
/**
@ -131,28 +131,43 @@ public class LinkTarget {
* @param uri url to resolve
* @return resolved target
*/
public static Target resolveLinkTarget(Uri uri) {
public static PageCategory resolvePageCategory(Uri uri) {
final String host = uri.getHost();
final String uriString = uri.toString();
if (Objects.equals(host, "www.thmmy.gr")) {
if (uriString.contains("topic=")) return Target.TOPIC;
else if (uriString.contains("board=")) return Target.BOARD;
if (uriString.contains("topic=")) return PageCategory.TOPIC;
else if (uriString.contains("board=")) return PageCategory.BOARD;
else if (uriString.contains("action=profile")) {
if (uriString.contains(";sa=showPosts"))
return Target.PROFILE_LATEST_POSTS;
return PageCategory.PROFILE_LATEST_POSTS;
else if (uriString.contains(";sa=statPanel"))
return Target.PROFILE_STATS;
else return Target.PROFILE_SUMMARY;
return PageCategory.PROFILE_STATS;
else return PageCategory.PROFILE_SUMMARY;
} else if (uriString.contains("action=unread"))
return Target.UNREAD_POSTS;
return PageCategory.UNREAD_POSTS;
else if (uriString.contains("action=tpmod;dl=item"))
return Target.DOWNLOADS_FILE;
return PageCategory.DOWNLOADS_FILE;
else if (uriString.contains("action=tpmod;dl"))
return Target.DOWNLOADS_CATEGORY;
return PageCategory.DOWNLOADS_CATEGORY;
Report.v(TAG, "Unknown thmmy link found, link: " + uriString);
return Target.UNKNOWN_THMMY;
return PageCategory.UNKNOWN_THMMY;
}
return Target.NOT_THMMY;
return PageCategory.NOT_THMMY;
}
public static String getBoardId(String boardUrl) {
if (resolvePageCategory(Uri.parse(boardUrl)) == PageCategory.BOARD) {
return boardUrl.substring(boardUrl.indexOf("board=") + 6, boardUrl.lastIndexOf("."));
}
return null;
}
public static String getTopicId(String topicUrl) {
if (resolvePageCategory(Uri.parse(topicUrl)) == PageCategory.TOPIC) {
String tmp = topicUrl.substring(topicUrl.indexOf("topic=") + 6);
return tmp.substring(0, tmp.indexOf("."));
}
return null;
}
}

69
app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java

@ -0,0 +1,69 @@
package gr.thmmy.mthmmy.utils;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//copied from http://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/ObjectSerializer.java
//package org.apache.pig.impl.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectSerializer {
public static String serialize(Serializable obj) throws IOException {
if (obj == null) return "";
ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
objStream.writeObject(obj);
objStream.close();
return encodeBytes(serialObj.toByteArray());
}
public static Object deserialize(String str) throws IOException, ClassNotFoundException {
if (str == null || str.length() == 0) return null;
ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
ObjectInputStream objStream = new ObjectInputStream(serialObj);
return objStream.readObject();
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
public static byte[] decodeBytes(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i += 2) {
char c = str.charAt(i);
bytes[i / 2] = (byte) ((c - 'a') << 4);
c = str.charAt(i + 1);
bytes[i / 2] += (c - 'a');
}
return bytes;
}
}

BIN
app/src/main/res/drawable-hdpi/ic_bookmark_false.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 B

BIN
app/src/main/res/drawable-hdpi/ic_bookmark_true.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 230 B

BIN
app/src/main/res/drawable-mdpi/ic_bookmark_false.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 B

BIN
app/src/main/res/drawable-mdpi/ic_bookmark_true.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

BIN
app/src/main/res/drawable-xhdpi/ic_bookmark_false.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 B

BIN
app/src/main/res/drawable-xhdpi/ic_bookmark_true.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 B

BIN
app/src/main/res/drawable-xxhdpi/ic_bookmark_false.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 B

BIN
app/src/main/res/drawable-xxhdpi/ic_bookmark_true.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 362 B

BIN
app/src/main/res/drawable-xxxhdpi/ic_bookmark_false.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 B

BIN
app/src/main/res/drawable-xxxhdpi/ic_bookmark_true.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 473 B

10
app/src/main/res/layout/activity_board.xml

@ -22,6 +22,16 @@
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ToolbarTheme">
<ImageButton
android:id="@+id/bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|end"
android:layout_marginEnd="4dp"
android:background="@null"
android:contentDescription="@string/bookmark"
android:src="@drawable/ic_bookmark_false"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

57
app/src/main/res/layout/activity_bookmark.xml

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.topic.TopicActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/ToolbarTheme">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ToolbarTheme">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bookmarks_nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|start"
android:layout_marginTop="64dp"
android:background="@color/background"
android:scrollbars="none">
<LinearLayout
android:id="@+id/bookmarks_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progressBar"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
android:layout_width="match_parent"
android:layout_height="@dimen/progress_bar_height"
android:indeterminate="true"
android:visibility="invisible"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|center"
app:mpb_indeterminateTint="@color/accent"
app:mpb_progressStyle="horizontal"/>
</android.support.design.widget.CoordinatorLayout>

10
app/src/main/res/layout/activity_topic.xml

@ -22,6 +22,16 @@
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ToolbarTheme">
<ImageButton
android:id="@+id/bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|end"
android:layout_marginEnd="4dp"
android:background="@null"
android:contentDescription="@string/bookmark"
android:src="@drawable/ic_bookmark_false"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

7
app/src/main/res/values/strings.xml

@ -5,7 +5,10 @@
<string name="login">Login</string>
<string name="login_spinner">Authenticating&#8230;</string>
<string name="logout">Logout</string>
<string name="downloads">Downloads</string>
<string name="about">About</string>
<string name="home">Home</string>
<string name="bookmark">Bookmarks</string>
<!--Login Activity-->
<string name="thmmy_img_description">thmmy.gr</string>
@ -50,11 +53,7 @@
<string name="most_popular_boards_by_posts_title">Most Popular Boards By Posts</string>
<string name="most_popular_boards_by_activity_title">Most Popular Boards By Activity</string>
<!--Downloads Activity-->
<string name="downloads">Downloads</string>
<!--About Activity-->
<string name="about">About</string>
<string name="version">v%1$s</string>
<string name="logo">Logo</string>
<string name="trollPic">You should watch a funny pic!</string>

Loading…
Cancel
Save