Browse Source

Add sync option to topic bookmarks

pull/24/head
Apostolos Fanakis 7 years ago
parent
commit
ca2c571f30
  1. 35
      app/src/main/java/gr/thmmy/mthmmy/activities/BookmarkActivity.java
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  4. 16
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  5. 19
      app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java
  6. 2
      app/src/main/res/layout/activity_bookmark_topic_row.xml

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

@ -2,12 +2,16 @@ package gr.thmmy.mthmmy.activities;
import android.content.Intent; import android.content.Intent;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.board.BoardActivity; import gr.thmmy.mthmmy.activities.board.BoardActivity;
@ -27,6 +31,9 @@ public class BookmarkActivity extends BaseActivity {
private TextView boardsTitle; private TextView boardsTitle;
private TextView topicsTitle; private TextView topicsTitle;
private static Drawable notificationsEnabledButtonImage;
private static Drawable notificationsDisabledButtonImage;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -44,6 +51,18 @@ public class BookmarkActivity extends BaseActivity {
createDrawer(); createDrawer();
drawer.setSelection(BOOKMARKS_ID); drawer.setSelection(BOOKMARKS_ID);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationsEnabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_on, null);
} else {
notificationsEnabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_on, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationsDisabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_off, null);
} else {
notificationsDisabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_off, null);
}
LinearLayout bookmarksLinearView = findViewById(R.id.bookmarks_container); LinearLayout bookmarksLinearView = findViewById(R.id.bookmarks_container);
LayoutInflater layoutInflater = getLayoutInflater(); LayoutInflater layoutInflater = getLayoutInflater();
@ -131,10 +150,21 @@ public class BookmarkActivity extends BaseActivity {
} }
}); });
((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedTopic.getTitle()); ((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedTopic.getTitle());
(row.findViewById(R.id.toggle_notification)).setOnClickListener(new View.OnClickListener() {
final ImageButton notificationsEnabledButton = row.findViewById(R.id.toggle_notification);
if (!bookmarkedTopic.isNotificationsEnabled()){
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
notificationsEnabledButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
//TODO if(toggleNotification(bookmarkedTopic)){
notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage);
} else {
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage);
}
} }
}); });
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() { (row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() {
@ -142,6 +172,7 @@ public class BookmarkActivity extends BaseActivity {
public void onClick(View view) { public void onClick(View view) {
removeBookmark(bookmarkedTopic); removeBookmark(bookmarkedTopic);
row.setVisibility(View.GONE); row.setVisibility(View.GONE);
Toast.makeText(BookmarkActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show();
updateTitles(); updateTitles();
} }
}); });

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

@ -89,7 +89,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);
} }
thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl)); thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl), false);
setBoardBookmark((ImageButton) findViewById(R.id.bookmark)); setBoardBookmark((ImageButton) findViewById(R.id.bookmark));
createDrawer(); createDrawer();
@ -304,7 +304,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
|| !Objects.equals(boardTitle, parsedTitle)) { || !Objects.equals(boardTitle, parsedTitle)) {
boardTitle = parsedTitle; boardTitle = parsedTitle;
toolbar.setTitle(boardTitle); toolbar.setTitle(boardTitle);
thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl)); thisPageBookmark = new Bookmark(boardTitle, ThmmyPage.getBoardId(boardUrl), false);
} }
//Parse was successful //Parse was successful

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

@ -204,7 +204,7 @@ public class TopicActivity extends BaseActivity {
finish(); finish();
} }
thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl)); thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true);
//Initializes graphics //Initializes graphics
toolbar = findViewById(R.id.toolbar); toolbar = findViewById(R.id.toolbar);
@ -631,7 +631,7 @@ public class TopicActivity extends BaseActivity {
|| !Objects.equals(topicTitle, parsedTitle)) { || !Objects.equals(topicTitle, parsedTitle)) {
toolbarTitle.setText(parsedTitle); toolbarTitle.setText(parsedTitle);
topicTitle = parsedTitle; topicTitle = parsedTitle;
thisPageBookmark = new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl)); thisPageBookmark = new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl), true);
invalidateOptionsMenu(); invalidateOptionsMenu();
} }

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

@ -514,7 +514,7 @@ public abstract class BaseActivity extends AppCompatActivity {
if (boardsBookmarked == null) return; if (boardsBookmarked == null) return;
if (bookmark.matchExists(boardsBookmarked)) { if (bookmark.matchExists(boardsBookmarked)) {
boardsBookmarked.remove(bookmark.findIndex(boardsBookmarked)); boardsBookmarked.remove(bookmark.findIndex(boardsBookmarked));
} else boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); } else boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId(), false));
updateBoardBookmarks(); updateBoardBookmarks();
} }
@ -523,7 +523,7 @@ public abstract class BaseActivity extends AppCompatActivity {
if (bookmark.matchExists(topicsBookmarked)) { if (bookmark.matchExists(topicsBookmarked)) {
topicsBookmarked.remove(bookmark.findIndex(topicsBookmarked)); topicsBookmarked.remove(bookmark.findIndex(topicsBookmarked));
} else { } else {
topicsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); topicsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId(), true));
} }
updateTopicBookmarks(); updateTopicBookmarks();
} }
@ -546,6 +546,18 @@ public abstract class BaseActivity extends AppCompatActivity {
if (bookmark.matchExists(boardsBookmarked)) toggleBoardToBookmarks(bookmark); if (bookmark.matchExists(boardsBookmarked)) toggleBoardToBookmarks(bookmark);
else if (bookmark.matchExists(topicsBookmarked)) toggleTopicToBookmarks(bookmark); else if (bookmark.matchExists(topicsBookmarked)) toggleTopicToBookmarks(bookmark);
} }
protected boolean toggleNotification(Bookmark bookmark){
if (bookmark.matchExists(topicsBookmarked)){
topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).toggleNotificationsEnabled();
updateTopicBookmarks();
//TODO toggle firebase here!
return topicsBookmarked.get(bookmark.findIndex(topicsBookmarked)).isNotificationsEnabled();
}
return false;
}
//-------------------------------------------BOOKMARKS END------------------------------------------ //-------------------------------------------BOOKMARKS END------------------------------------------
//-------PERMS--------- //-------PERMS---------

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

@ -8,10 +8,12 @@ import java.util.Objects;
public class Bookmark implements java.io.Serializable { public class Bookmark implements java.io.Serializable {
private final String title, id; private final String title, id;
private boolean isNotificationsEnabled;
public Bookmark(String title, String id) { public Bookmark(String title, String id, boolean isNotificationsEnabled) {
this.title = title; this.title = title;
this.id = id; this.id = id;
this.isNotificationsEnabled = isNotificationsEnabled;
} }
public String getTitle() { public String getTitle() {
@ -22,6 +24,14 @@ public class Bookmark implements java.io.Serializable {
return id; return id;
} }
public boolean isNotificationsEnabled() {
return isNotificationsEnabled;
}
public void toggleNotificationsEnabled(){
this.isNotificationsEnabled = !this.isNotificationsEnabled;
}
public boolean matchExists(ArrayList<Bookmark> array) { public boolean matchExists(ArrayList<Bookmark> array) {
if (array != null && !array.isEmpty()) { if (array != null && !array.isEmpty()) {
for (Bookmark bookmark : array) { for (Bookmark bookmark : array) {
@ -52,7 +62,8 @@ public class Bookmark implements java.io.Serializable {
for (Bookmark bookmark : arrayList) { for (Bookmark bookmark : arrayList) {
if (bookmark != null) { if (bookmark != null) {
returnString += (bookmark.getId() + "\t"); returnString += (bookmark.getId() + "\t");
returnString += (bookmark.getTitle() + "\n"); returnString += (bookmark.getTitle() + "\t");
returnString += (bookmark.isNotificationsEnabled() + "\n");
} }
} }
if (!Objects.equals(returnString, "")) return returnString; if (!Objects.equals(returnString, "")) return returnString;
@ -65,8 +76,8 @@ public class Bookmark implements java.io.Serializable {
for (String line : lines) { for (String line : lines) {
if (line == null || line.isEmpty() || Objects.equals(line, "")) break; if (line == null || line.isEmpty() || Objects.equals(line, "")) break;
String[] parameters = line.split("\t"); String[] parameters = line.split("\t");
if (parameters.length != 2) break; if (parameters.length != 3) break;
returnArray.add(new Bookmark(parameters[1], parameters[0])); returnArray.add(new Bookmark(parameters[1], parameters[0], Boolean.parseBoolean(parameters[2])));
} }
return returnArray; return returnArray;
} }

2
app/src/main/res/layout/activity_bookmark_topic_row.xml

@ -30,7 +30,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/toggle_notification" android:contentDescription="@string/toggle_notification"
app:srcCompat="@drawable/ic_notification_off"/> app:srcCompat="@drawable/ic_notification_on"/>
<ImageButton <ImageButton
android:id="@+id/remove_bookmark" android:id="@+id/remove_bookmark"

Loading…
Cancel
Save