|
@ -29,9 +29,7 @@ import com.mikepenz.materialdrawer.model.ProfileDrawerItem; |
|
|
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; |
|
|
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; |
|
|
import com.mikepenz.materialdrawer.model.interfaces.IProfile; |
|
|
import com.mikepenz.materialdrawer.model.interfaces.IProfile; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.activities.AboutActivity; |
|
|
import gr.thmmy.mthmmy.activities.AboutActivity; |
|
@ -44,7 +42,6 @@ import gr.thmmy.mthmmy.model.Bookmark; |
|
|
import gr.thmmy.mthmmy.services.DownloadService; |
|
|
import gr.thmmy.mthmmy.services.DownloadService; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
import gr.thmmy.mthmmy.utils.FileManager.ThmmyFile; |
|
|
import gr.thmmy.mthmmy.utils.FileManager.ThmmyFile; |
|
|
import gr.thmmy.mthmmy.utils.ObjectSerializer; |
|
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
|
|
|
|
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
|
|
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
|
@ -67,9 +64,11 @@ public abstract class BaseActivity extends AppCompatActivity { |
|
|
private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; |
|
|
private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; |
|
|
private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; |
|
|
private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; |
|
|
private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; |
|
|
private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; |
|
|
protected static SharedPreferences bookmarksFile; |
|
|
protected Bookmark thisPageBookmark; |
|
|
protected static ArrayList<Bookmark> topicsBookmarked; |
|
|
protected ImageButton thisPageBookmarkButton; |
|
|
protected static ArrayList<Bookmark> boardsBookmarked; |
|
|
protected SharedPreferences bookmarksFile; |
|
|
|
|
|
protected ArrayList<Bookmark> topicsBookmarked; |
|
|
|
|
|
protected ArrayList<Bookmark> boardsBookmarked; |
|
|
protected static Drawable bookmarked; |
|
|
protected static Drawable bookmarked; |
|
|
protected static Drawable notBookmarked; |
|
|
protected static Drawable notBookmarked; |
|
|
|
|
|
|
|
@ -409,46 +408,45 @@ public abstract class BaseActivity extends AppCompatActivity { |
|
|
return topicsBookmarked; |
|
|
return topicsBookmarked; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void setTopicBookmark(ImageButton bookmarkView, final Bookmark bookmark) { |
|
|
protected void setTopicBookmark() { |
|
|
if (matchExists(bookmark, topicsBookmarked)) { |
|
|
if (thisPageBookmark.matchExists(topicsBookmarked)) { |
|
|
bookmarkView.setImageDrawable(bookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(bookmarked); |
|
|
} else { |
|
|
} else { |
|
|
bookmarkView.setImageDrawable(notBookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(notBookmarked); |
|
|
} |
|
|
} |
|
|
bookmarkView.setOnClickListener(new View.OnClickListener() { |
|
|
thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void onClick(View view) { |
|
|
public void onClick(View view) { |
|
|
if (matchExists(bookmark, topicsBookmarked)) { |
|
|
if (thisPageBookmark.matchExists(topicsBookmarked)) { |
|
|
((ImageButton) view).setImageDrawable(notBookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(notBookmarked); |
|
|
toggleTopicToBookmarks(bookmark); |
|
|
toggleTopicToBookmarks(thisPageBookmark); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|
|
} else { |
|
|
} else { |
|
|
((ImageButton) view).setImageDrawable(bookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(bookmarked); |
|
|
toggleTopicToBookmarks(bookmark); |
|
|
toggleTopicToBookmarks(thisPageBookmark); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void setBoardBookmark(ImageButton bookmarkView, final Bookmark bookmark) { |
|
|
protected void setBoardBookmark() { |
|
|
if (matchExists(bookmark, boardsBookmarked)) { |
|
|
if (thisPageBookmark.matchExists(boardsBookmarked)) { |
|
|
bookmarkView.setImageDrawable(bookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(bookmarked); |
|
|
} else { |
|
|
} else { |
|
|
bookmarkView.setImageDrawable(notBookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(notBookmarked); |
|
|
} |
|
|
} |
|
|
bookmarkView.setOnClickListener(new View.OnClickListener() { |
|
|
thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void onClick(View view) { |
|
|
public void onClick(View view) { |
|
|
if (matchExists(bookmark, boardsBookmarked)) { |
|
|
if (thisPageBookmark.matchExists(boardsBookmarked)) { |
|
|
((ImageButton) view).setImageDrawable(notBookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(notBookmarked); |
|
|
toggleBoardToBookmarks(bookmark); |
|
|
|
|
|
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|
|
} else { |
|
|
} else { |
|
|
((ImageButton) view).setImageDrawable(bookmarked); |
|
|
thisPageBookmarkButton.setImageDrawable(bookmarked); |
|
|
toggleBoardToBookmarks(bookmark); |
|
|
|
|
|
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(BaseActivity.this, "Bookmark added", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
} |
|
|
|
|
|
toggleBoardToBookmarks(thisPageBookmark); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -456,95 +454,50 @@ public abstract class BaseActivity extends AppCompatActivity { |
|
|
private void loadSavedBookmarks() { |
|
|
private void loadSavedBookmarks() { |
|
|
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); |
|
|
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); |
|
|
if (tmpString != null) |
|
|
if (tmpString != null) |
|
|
try { |
|
|
topicsBookmarked = Bookmark.arrayFromString(tmpString); |
|
|
topicsBookmarked = (ArrayList<Bookmark>) ObjectSerializer.deserialize(tmpString); |
|
|
|
|
|
} catch (IOException | ClassNotFoundException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
else { |
|
|
topicsBookmarked = new ArrayList<>(); |
|
|
topicsBookmarked = new ArrayList<>(); |
|
|
topicsBookmarked.add(null); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); |
|
|
tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); |
|
|
if (tmpString != null) |
|
|
if (tmpString != null) |
|
|
try { |
|
|
boardsBookmarked = Bookmark.arrayFromString(tmpString); |
|
|
boardsBookmarked = (ArrayList<Bookmark>) ObjectSerializer.deserialize(tmpString); |
|
|
|
|
|
} catch (IOException | ClassNotFoundException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
else { |
|
|
boardsBookmarked = new ArrayList<>(); |
|
|
boardsBookmarked = new ArrayList<>(); |
|
|
boardsBookmarked.add(null); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void toggleBoardToBookmarks(Bookmark bookmark) { |
|
|
private void toggleBoardToBookmarks(Bookmark bookmark) { |
|
|
if (boardsBookmarked == null) return; |
|
|
if (boardsBookmarked == null) return; |
|
|
if (matchExists(bookmark, boardsBookmarked)) { |
|
|
if (bookmark.matchExists(boardsBookmarked)) { |
|
|
if (boardsBookmarked.size() == 1) boardsBookmarked.set(0, null); |
|
|
boardsBookmarked.remove(bookmark.findIndex(boardsBookmarked)); |
|
|
else boardsBookmarked.remove(findIndex(bookmark, boardsBookmarked)); |
|
|
} else boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); |
|
|
} else boardsBookmarked.add(bookmark); |
|
|
|
|
|
updateBoardBookmarks(); |
|
|
updateBoardBookmarks(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void toggleTopicToBookmarks(Bookmark bookmark) { |
|
|
private void toggleTopicToBookmarks(Bookmark bookmark) { |
|
|
if (topicsBookmarked == null) return; |
|
|
if (topicsBookmarked == null) return; |
|
|
if (matchExists(bookmark, topicsBookmarked)) |
|
|
if (bookmark.matchExists(topicsBookmarked)) { |
|
|
topicsBookmarked.remove(findIndex(bookmark, topicsBookmarked)); |
|
|
topicsBookmarked.remove(bookmark.findIndex(topicsBookmarked)); |
|
|
else topicsBookmarked.add(bookmark); |
|
|
} else { |
|
|
|
|
|
topicsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); |
|
|
|
|
|
} |
|
|
updateTopicBookmarks(); |
|
|
updateTopicBookmarks(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void updateBoardBookmarks() { |
|
|
private void updateBoardBookmarks() { |
|
|
String tmpString = null; |
|
|
String tmpString; |
|
|
if (!(boardsBookmarked.size() == 1 && boardsBookmarked.get(0) == null)) { |
|
|
tmpString = Bookmark.arrayToString(boardsBookmarked); |
|
|
try { |
|
|
|
|
|
tmpString = ObjectSerializer.serialize(boardsBookmarked); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
SharedPreferences.Editor editor = bookmarksFile.edit(); |
|
|
SharedPreferences.Editor editor = bookmarksFile.edit(); |
|
|
editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply(); |
|
|
editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void updateTopicBookmarks() { |
|
|
private void updateTopicBookmarks() { |
|
|
String tmpString = null; |
|
|
String tmpString; |
|
|
if (!(topicsBookmarked.size() == 1 && topicsBookmarked.get(0) == null)) { |
|
|
tmpString = Bookmark.arrayToString(topicsBookmarked); |
|
|
try { |
|
|
|
|
|
tmpString = ObjectSerializer.serialize(topicsBookmarked); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
SharedPreferences.Editor editor = bookmarksFile.edit(); |
|
|
SharedPreferences.Editor editor = bookmarksFile.edit(); |
|
|
editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply(); |
|
|
editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private boolean matchExists(Bookmark bookmark, ArrayList<Bookmark> array) { |
|
|
|
|
|
if (array != null && !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------------------------------------------
|
|
|
//-------------------------------------------BOOKMARKS END------------------------------------------
|
|
|
|
|
|
|
|
|
//-------PERMS---------
|
|
|
//-------PERMS---------
|
|
@ -588,16 +541,14 @@ public abstract class BaseActivity extends AppCompatActivity { |
|
|
|
|
|
|
|
|
//----------------------------------DOWNLOAD------------------
|
|
|
//----------------------------------DOWNLOAD------------------
|
|
|
private ThmmyFile tempThmmyFile; |
|
|
private ThmmyFile tempThmmyFile; |
|
|
|
|
|
|
|
|
public void launchDownloadService(ThmmyFile thmmyFile) { |
|
|
public void launchDownloadService(ThmmyFile thmmyFile) { |
|
|
if(checkPerms()) |
|
|
if (checkPerms()) { |
|
|
{ |
|
|
|
|
|
Intent i = new Intent(this, DownloadService.class); |
|
|
Intent i = new Intent(this, DownloadService.class); |
|
|
i.setAction(ACTION_DOWNLOAD); |
|
|
i.setAction(ACTION_DOWNLOAD); |
|
|
i.putExtra(EXTRA_DOWNLOAD_URL, thmmyFile.getFileUrl().toString()); |
|
|
i.putExtra(EXTRA_DOWNLOAD_URL, thmmyFile.getFileUrl().toString()); |
|
|
startService(i); |
|
|
startService(i); |
|
|
} |
|
|
} else { |
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
tempThmmyFile = thmmyFile; |
|
|
tempThmmyFile = thmmyFile; |
|
|
requestPerms(); |
|
|
requestPerms(); |
|
|
} |
|
|
} |
|
@ -605,8 +556,7 @@ public abstract class BaseActivity extends AppCompatActivity { |
|
|
|
|
|
|
|
|
//Uses temp file - called after permission grant
|
|
|
//Uses temp file - called after permission grant
|
|
|
public void launchDownloadService() { |
|
|
public void launchDownloadService() { |
|
|
if(checkPerms()) |
|
|
if (checkPerms()) { |
|
|
{ |
|
|
|
|
|
Intent i = new Intent(this, DownloadService.class); |
|
|
Intent i = new Intent(this, DownloadService.class); |
|
|
i.setAction(ACTION_DOWNLOAD); |
|
|
i.setAction(ACTION_DOWNLOAD); |
|
|
i.putExtra(EXTRA_DOWNLOAD_URL, tempThmmyFile.getFileUrl().toString()); |
|
|
i.putExtra(EXTRA_DOWNLOAD_URL, tempThmmyFile.getFileUrl().toString()); |
|
|