From 0398ac249f636a61cb1e79c93571684a58bdb703 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 21 Jan 2017 19:39:04 +0200 Subject: [PATCH] Fix for bookmark functionality and letters getting chopped in TextViews. --- .../activities/board/BoardActivity.java | 5 +- .../activities/topic/TopicActivity.java | 14 +- .../gr/thmmy/mthmmy/base/BaseActivity.java | 138 ++++++------------ .../java/gr/thmmy/mthmmy/model/Bookmark.java | 57 +++++++- .../thmmy/mthmmy/utils/ObjectSerializer.java | 69 --------- .../res/layout/activity_board_sub_board.xml | 3 +- .../main/res/layout/activity_board_topic.xml | 127 ++++++++-------- .../res/layout/activity_downloads_row.xml | 2 + 8 files changed, 177 insertions(+), 238 deletions(-) delete mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java 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 2289dcac..77067287 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 @@ -91,8 +91,9 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo getSupportActionBar().setDisplayShowHomeEnabled(true); } - setBoardBookmark((ImageButton) findViewById(R.id.bookmark), - new Bookmark(boardTitle, boardUrl)); + thisPageBookmark = new Bookmark(boardTitle, boardUrl); + thisPageBookmarkButton = (ImageButton) findViewById(R.id.bookmark); + setBoardBookmark(); 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 043a5a6c..4948474e 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 @@ -9,6 +9,7 @@ import android.support.design.widget.FloatingActionButton; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.util.SparseArray; import android.view.MotionEvent; import android.view.View; @@ -118,8 +119,9 @@ public class TopicActivity extends BaseActivity { getSupportActionBar().setDisplayShowHomeEnabled(true); } - setTopicBookmark((ImageButton) findViewById(R.id.bookmark), - new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl))); + thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl)); + thisPageBookmarkButton = (ImageButton) findViewById(R.id.bookmark); + setTopicBookmark(); createDrawer(); progressBar = (MaterialProgressBar) findViewById(R.id.progressBar); @@ -450,7 +452,7 @@ public class TopicActivity extends BaseActivity { } if (Integer.parseInt(newPageUrl.substring(base_url.length() + 1)) / 15 + 1 == thisPage) return SAME_PAGE; - } else topicTitle = null; + } else if (!Objects.equals(loadedPageUrl, "")) topicTitle = null; loadedPageUrl = newPageUrl; Request request = new Request.Builder() @@ -481,8 +483,10 @@ public class TopicActivity extends BaseActivity { switch (parseResult) { case SUCCESS: - setTopicBookmark((ImageButton) findViewById(R.id.bookmark), - new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl))); + if (topicTitle == null || Objects.equals(topicTitle, "")) { + thisPageBookmark = new Bookmark(parsedTitle, ThmmyPage.getTopicId(loadedPageUrl)); + setTopicBookmark(); + } progressBar.setVisibility(ProgressBar.INVISIBLE); topicAdapter.customNotifyDataSetChanged(new TopicTask()); 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 de2b5a7e..b3af480e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -29,9 +29,7 @@ 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; @@ -44,7 +42,6 @@ import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.services.DownloadService; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.FileManager.ThmmyFile; -import gr.thmmy.mthmmy.utils.ObjectSerializer; import okhttp3.OkHttpClient; 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 BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; - protected static SharedPreferences bookmarksFile; - protected static ArrayList topicsBookmarked; - protected static ArrayList boardsBookmarked; + protected Bookmark thisPageBookmark; + protected ImageButton thisPageBookmarkButton; + protected SharedPreferences bookmarksFile; + protected ArrayList topicsBookmarked; + protected ArrayList boardsBookmarked; protected static Drawable bookmarked; protected static Drawable notBookmarked; @@ -409,46 +408,45 @@ public abstract class BaseActivity extends AppCompatActivity { return topicsBookmarked; } - protected void setTopicBookmark(ImageButton bookmarkView, final Bookmark bookmark) { - if (matchExists(bookmark, topicsBookmarked)) { - bookmarkView.setImageDrawable(bookmarked); + protected void setTopicBookmark() { + if (thisPageBookmark.matchExists(topicsBookmarked)) { + thisPageBookmarkButton.setImageDrawable(bookmarked); } else { - bookmarkView.setImageDrawable(notBookmarked); + thisPageBookmarkButton.setImageDrawable(notBookmarked); } - bookmarkView.setOnClickListener(new View.OnClickListener() { + thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if (matchExists(bookmark, topicsBookmarked)) { - ((ImageButton) view).setImageDrawable(notBookmarked); - toggleTopicToBookmarks(bookmark); + if (thisPageBookmark.matchExists(topicsBookmarked)) { + thisPageBookmarkButton.setImageDrawable(notBookmarked); + toggleTopicToBookmarks(thisPageBookmark); Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); } else { - ((ImageButton) view).setImageDrawable(bookmarked); - toggleTopicToBookmarks(bookmark); + thisPageBookmarkButton.setImageDrawable(bookmarked); + toggleTopicToBookmarks(thisPageBookmark); 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); + protected void setBoardBookmark() { + if (thisPageBookmark.matchExists(boardsBookmarked)) { + thisPageBookmarkButton.setImageDrawable(bookmarked); } else { - bookmarkView.setImageDrawable(notBookmarked); + thisPageBookmarkButton.setImageDrawable(notBookmarked); } - bookmarkView.setOnClickListener(new View.OnClickListener() { + thisPageBookmarkButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if (matchExists(bookmark, boardsBookmarked)) { - ((ImageButton) view).setImageDrawable(notBookmarked); - toggleBoardToBookmarks(bookmark); + if (thisPageBookmark.matchExists(boardsBookmarked)) { + thisPageBookmarkButton.setImageDrawable(notBookmarked); Toast.makeText(BaseActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); } else { - ((ImageButton) view).setImageDrawable(bookmarked); - toggleBoardToBookmarks(bookmark); + thisPageBookmarkButton.setImageDrawable(bookmarked); 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() { String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); if (tmpString != null) - try { - topicsBookmarked = (ArrayList) ObjectSerializer.deserialize(tmpString); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); - } + topicsBookmarked = Bookmark.arrayFromString(tmpString); else { topicsBookmarked = new ArrayList<>(); - topicsBookmarked.add(null); } tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); if (tmpString != null) - try { - boardsBookmarked = (ArrayList) ObjectSerializer.deserialize(tmpString); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); - } + boardsBookmarked = Bookmark.arrayFromString(tmpString); 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); + if (bookmark.matchExists(boardsBookmarked)) { + boardsBookmarked.remove(bookmark.findIndex(boardsBookmarked)); + } else boardsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); updateBoardBookmarks(); } private void toggleTopicToBookmarks(Bookmark bookmark) { if (topicsBookmarked == null) return; - if (matchExists(bookmark, topicsBookmarked)) - topicsBookmarked.remove(findIndex(bookmark, topicsBookmarked)); - else topicsBookmarked.add(bookmark); + if (bookmark.matchExists(topicsBookmarked)) { + topicsBookmarked.remove(bookmark.findIndex(topicsBookmarked)); + } else { + topicsBookmarked.add(new Bookmark(bookmark.getTitle(), bookmark.getId())); + } 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(); - } - } + String tmpString; + tmpString = Bookmark.arrayToString(boardsBookmarked); 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(); - } - } + String tmpString; + tmpString = Bookmark.arrayToString(topicsBookmarked); SharedPreferences.Editor editor = bookmarksFile.edit(); editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply(); } - - private boolean matchExists(Bookmark bookmark, ArrayList 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 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------------------------------------------ //-------PERMS--------- @@ -570,8 +523,8 @@ public abstract class BaseActivity extends AppCompatActivity { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; - requestPermissions(PERMISSIONS_STORAGE, PERMISSIONS_REQUEST_CODE); - } + requestPermissions(PERMISSIONS_STORAGE, PERMISSIONS_REQUEST_CODE); + } } @@ -588,16 +541,14 @@ public abstract class BaseActivity extends AppCompatActivity { //----------------------------------DOWNLOAD------------------ private ThmmyFile tempThmmyFile; + public void launchDownloadService(ThmmyFile thmmyFile) { - if(checkPerms()) - { + if (checkPerms()) { Intent i = new Intent(this, DownloadService.class); i.setAction(ACTION_DOWNLOAD); i.putExtra(EXTRA_DOWNLOAD_URL, thmmyFile.getFileUrl().toString()); startService(i); - } - else - { + } else { tempThmmyFile = thmmyFile; requestPerms(); } @@ -605,8 +556,7 @@ public abstract class BaseActivity extends AppCompatActivity { //Uses temp file - called after permission grant public void launchDownloadService() { - if(checkPerms()) - { + if (checkPerms()) { Intent i = new Intent(this, DownloadService.class); i.setAction(ACTION_DOWNLOAD); i.putExtra(EXTRA_DOWNLOAD_URL, tempThmmyFile.getFileUrl().toString()); diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java index 88cc97d5..e9753046 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java @@ -1,6 +1,12 @@ package gr.thmmy.mthmmy.model; -public class Bookmark { +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Objects; + +public class Bookmark implements java.io.Serializable { private final String title, id; public Bookmark(String title, String id) { @@ -15,4 +21,53 @@ public class Bookmark { public String getId() { return id; } + + public boolean matchExists(ArrayList array) { + if (array != null && !array.isEmpty()) { + for (Bookmark bookmark : array) { + if (bookmark != null) { + if (Objects.equals(bookmark.getId(), this.id) + && Objects.equals(bookmark.getTitle(), this.title)) + return true; + } + } + } + return false; + } + + public int findIndex(ArrayList array) { + if (array != null && !array.isEmpty()) { + for (int i = 0; i < array.size(); ++i) { + if (array.get(i) != null && Objects.equals(array.get(i).getId(), this.id) + && Objects.equals(array.get(i).getTitle(), this.title)) + return i; + } + } + return -1; + } + + @Nullable + public static String arrayToString(@NonNull ArrayList arrayList) { + String returnString = ""; + for (Bookmark bookmark : arrayList) { + if (bookmark != null) { + returnString += (bookmark.getId() + "\t"); + returnString += (bookmark.getTitle() + "\n"); + } + } + if (!Objects.equals(returnString, "")) return returnString; + else return null; + } + + public static ArrayList arrayFromString(@NonNull String string) { + ArrayList returnArray = new ArrayList<>(); + String[] lines = string.split("\n"); + for (String line : lines) { + if (line == null || line.isEmpty() || Objects.equals(line, "")) break; + String[] parameters = line.split("\t"); + if (parameters.length != 2) break; + returnArray.add(new Bookmark(parameters[1], parameters[0])); + } + return returnArray; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java deleted file mode 100644 index ce3dbda6..00000000 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java +++ /dev/null @@ -1,69 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_board_sub_board.xml b/app/src/main/res/layout/activity_board_sub_board.xml index 3dcd5d6e..3b202022 100644 --- a/app/src/main/res/layout/activity_board_sub_board.xml +++ b/app/src/main/res/layout/activity_board_sub_board.xml @@ -28,8 +28,9 @@ android:id="@+id/child_board_title" android:layout_width="0dp" android:layout_height="wrap_content" + android:layout_gravity="center_vertical" android:layout_weight="1" - android:gravity="center_vertical" + android:paddingBottom="2dp" android:text="@string/child_board_title" android:textColor="@color/accent" android:textSize="22sp"/> diff --git a/app/src/main/res/layout/activity_board_topic.xml b/app/src/main/res/layout/activity_board_topic.xml index 7ce361e5..d041cc46 100644 --- a/app/src/main/res/layout/activity_board_topic.xml +++ b/app/src/main/res/layout/activity_board_topic.xml @@ -1,85 +1,80 @@ + android:background="?attr/selectableItemBackground" + android:clickable="true" + android:focusable="true" + android:orientation="vertical" + android:paddingLeft="16dp" + android:paddingRight="16dp"> + android:layout_marginBottom="5dp" + android:layout_marginTop="5dp" + android:orientation="horizontal"> - + android:layout_gravity="center_vertical" + android:layout_weight="1" + android:paddingBottom="2dp" + android:text="@string/topic_subject" + android:textColor="@color/primary_text" + android:textSize="18sp"/> - + + - - + - - - + android:layout_marginBottom="1dp" + android:layout_marginTop="1dp" + android:text="@string/topic_started_by" + android:textColor="@color/secondary_text" + android:textSize="12sp"/> - + - - + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_downloads_row.xml b/app/src/main/res/layout/activity_downloads_row.xml index 3206e64f..d6c664c9 100644 --- a/app/src/main/res/layout/activity_downloads_row.xml +++ b/app/src/main/res/layout/activity_downloads_row.xml @@ -29,9 +29,11 @@ android:id="@+id/download_title" android:layout_width="0dp" android:layout_height="wrap_content" + android:layout_gravity="center_vertical" android:layout_marginBottom="2dp" android:layout_marginTop="2dp" android:layout_weight="1" + android:paddingBottom="2dp" android:textColor="@color/accent" android:textSize="18sp"/>