Adds feature: Bookmarks #320

This commit is contained in:
stom79 2018-02-15 15:27:46 +01:00
parent b6751f8852
commit 8034072cd1
9 changed files with 247 additions and 19 deletions

View File

@ -86,6 +86,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.Version;
import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayBookmarksFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayDraftsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayFollowRequestSentFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayListsFragment;
@ -1315,7 +1316,7 @@ public abstract class BaseMainActivity extends BaseActivity
toolbarTitle.setVisibility(View.VISIBLE);
appBar.setExpanded(true);
if (id != R.id.nav_drafts) {
if (id != R.id.nav_drafts && id != R.id.nav_bookmarks ) {
delete_all.setVisibility(View.GONE);
}else{
delete_all.setVisibility(View.VISIBLE);
@ -1368,6 +1369,12 @@ public abstract class BaseMainActivity extends BaseActivity
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, displayDraftsFragment, fragmentTag).commit();
toot.setVisibility(View.GONE);
}else if (id == R.id.nav_bookmarks) {
DisplayBookmarksFragment displayBookmarksFragment = new DisplayBookmarksFragment();
fragmentTag = "BOOKMARKS";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, displayBookmarksFragment, fragmentTag).commit();
toot.setVisibility(View.GONE);
}else if (id == R.id.nav_search) {
DisplaySearchFragment displaySearchFragment = new DisplaySearchFragment();
fragmentTag = "SEARCH";

View File

@ -52,7 +52,8 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
FAVOURITES,
ONESTATUS,
CONTEXT,
TAG
TAG,
CACHE_BOOKMARKS
}
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){

View File

@ -1719,8 +1719,8 @@ public class API {
}
try{
status.setReblog(parseStatuses(context, resobj.getJSONObject("reblog")));
}catch (Exception ignored){ignored.printStackTrace();}
} catch (JSONException ignored) {ignored.printStackTrace();}
}catch (Exception ignored){}
} catch (JSONException ignored) {}
return status;
}

View File

@ -348,7 +348,9 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int HIDDEN_STATUS = 0;
String filter;
if( type == RetrieveFeedsAsyncTask.Type.HOME)
if( type == RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS)
return DISPLAYED_STATUS;
else if( type == RetrieveFeedsAsyncTask.Type.HOME)
filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_HOME, null);
else if( type == RetrieveFeedsAsyncTask.Type.LOCAL)
filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_LOCAL, null);
@ -470,6 +472,12 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.status_replies.setVisibility(View.GONE);
}
}
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Status statusBookmarked = new StatusCacheDAO(context, db).getStatus(status.getId());
if( statusBookmarked != null)
status.setBookmarked(true);
else
status.setBookmarked(false);
if( status.isBookmarked())
holder.status_bookmark.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_bookmark));
else
@ -606,21 +614,29 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.status_bookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if( status.isBookmarked()){
Status status1 = new StatusCacheDAO(context, db).getStatus(status.getId());
if( status1 == null)
if( type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) {
status.setBookmarked(!status.isBookmarked());
if (status.isBookmarked()) {
new StatusCacheDAO(context, db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, status);
else
new StatusCacheDAO(context, db).updateStatus(StatusCacheDAO.BOOKMARK_CACHE, status);
status.setBookmarked(true);
Toast.makeText(context, R.string.status_bookmarked, Toast.LENGTH_LONG).show();
Toast.makeText(context, R.string.status_bookmarked, Toast.LENGTH_LONG).show();
} else {
new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, status);
Toast.makeText(context, R.string.status_unbookmarked, Toast.LENGTH_LONG).show();
}
notifyStatusChanged(status);
}else {
new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, status);
status.setBookmarked(false);
Toast.makeText(context, R.string.status_unbookmarked, Toast.LENGTH_LONG).show();
int position = 0;
for (Status statustmp : statuses) {
if (statustmp.getId().equals(status.getId())) {
statuses.remove(status);
statusListAdapter.notifyItemRemoved(position);
new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, statustmp);
Toast.makeText(context, R.string.status_unbookmarked, Toast.LENGTH_LONG).show();
break;
}
position++;
}
}
notifyStatusChanged(status);
}
});
holder.status_content_translated.setMovementMethod(LinkMovementMethod.getInstance());

View File

@ -0,0 +1,137 @@
package fr.gouv.etalab.mastodon.fragments;
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
/**
* Created by Thomas on 15/02/2018.
* Fragment to display bookmarks
*/
public class DisplayBookmarksFragment extends Fragment {
private Context context;
private List<Status> statuses;
private StatusListAdapter statusListAdapter;
private RelativeLayout textviewNoAction;
private LinearLayoutManager mLayoutManager;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bookmarks, container, false);
context = getContext();
final RecyclerView lv_status = rootView.findViewById(R.id.lv_status);
RelativeLayout mainLoader = rootView.findViewById(R.id.loader);
textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
statuses = new StatusCacheDAO(context, db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
final boolean isOnWifi = Helper.isOnWIFI(context);
final int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
final int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
mLayoutManager = new LinearLayoutManager(context);
if( statuses != null && statuses.size() > 0) {
statusListAdapter = new StatusListAdapter(context, RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
lv_status.setLayoutManager(mLayoutManager);
}else {
textviewNoAction.setVisibility(View.VISIBLE);
}
mainLoader.setVisibility(View.GONE);
FloatingActionButton delete_all = null;
try {
delete_all = ((MainActivity) context).findViewById(R.id.delete_all);
}catch (Exception ignored){}
if( delete_all != null)
delete_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_all);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
new StatusCacheDAO(context, db).removeAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
statuses = new ArrayList<>();
statuses.clear();
statusListAdapter = new StatusListAdapter(context, RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses);
lv_status.setAdapter(statusListAdapter);
statusListAdapter.notifyDataSetChanged();
textviewNoAction.setVisibility(View.VISIBLE);
dialogConfirm.dismiss();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
dialogConfirm.dismiss();
}
})
.show();
}
});
return rootView;
}
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
}

View File

@ -61,6 +61,7 @@ public class StatusCacheDAO {
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_STATUS_ID, status.getId());
values.put(Sqlite.COL_URI, status.getUri());
values.put(Sqlite.COL_URL, status.getUrl());
values.put(Sqlite.COL_ACCOUNT, Helper.accountToStringStorage(status.getAccount()));
values.put(Sqlite.COL_IN_REPLY_TO_ID, status.getIn_reply_to_id());
values.put(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID, status.getIn_reply_to_account_id());
@ -154,7 +155,7 @@ public class StatusCacheDAO {
public Status getStatus(String id){
String instance = Helper.getLiveInstance(context);
try {
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_STATUS_ID + " = '" + id + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance, null, null, null, null, null);
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_STATUS_ID + " = '" + id + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance +"'", null, null, null, null, null);
return cursorToStoredStatus(c);
} catch (Exception e) {
return null;

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Thomas Schneider
This file is a part of Mastalab
This program is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with Mastalab; if not,
see <http://www.gnu.org/licenses>.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="@dimen/fab_margin"
android:paddingRight="@dimen/fab_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Listview status -->
<android.support.v7.widget.RecyclerView
android:id="@+id/lv_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
/>
<RelativeLayout
android:id="@+id/no_action"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="10dp"
android:gravity="center"
android:textSize="25sp"
android:layout_gravity="center"
android:textStyle="italic|bold"
android:typeface="serif"
android:text="@string/bookmarks_empty"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!-- Main Loader -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loader"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</RelativeLayout>

View File

@ -23,6 +23,10 @@
android:id="@+id/nav_drafts"
android:icon="@drawable/ic_save_white"
android:title="@string/drafts" />
<item
android:id="@+id/nav_bookmarks"
android:icon="@drawable/ic_bookmark"
android:title="@string/bookmarks" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search"

View File

@ -138,7 +138,8 @@
<item quantity="one">%d reply</item>
<item quantity="other">%d replies</item>
</plurals>
<string name="bookmarks">Bookmarks</string>
<string name="bookmarks_empty">No bookmarks to display</string>
<string name="status_bookmarked">Status has been added to bookmarks!</string>
<string name="status_unbookmarked">Status was removed from bookmarks!</string>