Browse Source

Fix for bookmark functionality and letters getting chopped in TextViews.

pull/24/head
Apostolos Fanakis 8 years ago
parent
commit
0398ac249f
  1. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  2. 14
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  3. 138
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  4. 57
      app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java
  5. 69
      app/src/main/java/gr/thmmy/mthmmy/utils/ObjectSerializer.java
  6. 3
      app/src/main/res/layout/activity_board_sub_board.xml
  7. 127
      app/src/main/res/layout/activity_board_topic.xml
  8. 2
      app/src/main/res/layout/activity_downloads_row.xml

5
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);

14
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());

138
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<Bookmark> topicsBookmarked;
protected static ArrayList<Bookmark> boardsBookmarked;
protected Bookmark thisPageBookmark;
protected ImageButton thisPageBookmarkButton;
protected SharedPreferences bookmarksFile;
protected ArrayList<Bookmark> topicsBookmarked;
protected ArrayList<Bookmark> 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<Bookmark>) 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<Bookmark>) 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<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------------------------------------------
//-------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());

57
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<Bookmark> 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<Bookmark> 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<Bookmark> 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<Bookmark> arrayFromString(@NonNull String string) {
ArrayList<Bookmark> 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;
}
}

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

@ -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;
}
}

3
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"/>

127
app/src/main/res/layout/activity_board_topic.xml

@ -1,85 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topic_row_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<LinearLayout
android:id="@+id/topic_row_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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">
<LinearLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/topic_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
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"/>
<TextView
android:id="@+id/topic_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@string/topic_subject"
android:textColor="@color/primary_text"
android:textSize="18sp"/>
<ImageButton
android:id="@+id/topic_expand_collapse_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription="@string/child_board_button"
android:src="@drawable/ic_arrow_drop_down"/>
</LinearLayout>
<ImageButton
android:id="@+id/topic_expand_collapse_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription="@string/child_board_button"
android:src="@drawable/ic_arrow_drop_down"/>
</LinearLayout>
<LinearLayout
android:id="@+id/topic_expandable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:id="@+id/topic_expandable"
android:layout_width="match_parent"
<TextView
android:id="@+id/topic_started_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="@+id/topic_started_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:text="@string/topic_started_by"
android:textColor="@color/secondary_text"
android:textSize="12sp"/>
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:text="@string/topic_started_by"
android:textColor="@color/secondary_text"
android:textSize="12sp"/>
<TextView
android:id="@+id/topic_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:text="@string/topic_stats"
android:textColor="@color/secondary_text"
android:textSize="12sp"/>
<TextView
android:id="@+id/topic_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:text="@string/topic_stats"
android:textColor="@color/secondary_text"
android:textSize="12sp"/>
<TextView
android:id="@+id/topic_last_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:text="@string/topic_last_post"
android:textColor="@color/primary_text"
android:textSize="12sp"/>
</LinearLayout>
<TextView
android:id="@+id/topic_last_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:text="@string/topic_last_post"
android:textColor="@color/primary_text"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>

2
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"/>

Loading…
Cancel
Save