Add polls support to bookmarks and allow to refresh bookmark values.
This commit is contained in:
parent
af32fad854
commit
66ee4c3019
|
@ -20,6 +20,7 @@ import android.os.AsyncTask;
|
|||
import android.os.SystemClock;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import app.fedilab.android.client.API;
|
||||
|
@ -28,6 +29,8 @@ import app.fedilab.android.interfaces.OnSyncBookmarksInterface;
|
|||
import app.fedilab.android.sqlite.Sqlite;
|
||||
import app.fedilab.android.sqlite.StatusCacheDAO;
|
||||
|
||||
import static app.fedilab.android.sqlite.StatusCacheDAO.BOOKMARK_CACHE;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 15/11/2019.
|
||||
|
@ -40,13 +43,19 @@ public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
private OnSyncBookmarksInterface listener;
|
||||
private WeakReference<Context> contextReference;
|
||||
private sync type;
|
||||
private String statusId;
|
||||
|
||||
public SyncBookmarksAsyncTask(Context context, sync type, OnSyncBookmarksInterface onSyncBookmarksInterface) {
|
||||
this.contextReference = new WeakReference<>(context);
|
||||
this.type = type;
|
||||
this.listener = onSyncBookmarksInterface;
|
||||
}
|
||||
|
||||
public SyncBookmarksAsyncTask(Context context, String statusId, OnSyncBookmarksInterface onSyncBookmarksInterface) {
|
||||
this.contextReference = new WeakReference<>(context);
|
||||
this.type = sync.REFRESH;
|
||||
this.statusId = statusId;
|
||||
this.listener = onSyncBookmarksInterface;
|
||||
}
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
|
@ -57,9 +66,9 @@ public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
max_id = apiResponse.getMax_id();
|
||||
List<app.fedilab.android.client.Entities.Status> statuses = apiResponse.getStatuses();
|
||||
for (app.fedilab.android.client.Entities.Status tmpStatus : statuses) {
|
||||
app.fedilab.android.client.Entities.Status status = new StatusCacheDAO(contextReference.get(), db).getStatus(StatusCacheDAO.BOOKMARK_CACHE, tmpStatus.getId());
|
||||
app.fedilab.android.client.Entities.Status status = new StatusCacheDAO(contextReference.get(), db).getStatus(BOOKMARK_CACHE, tmpStatus.getId());
|
||||
if (status == null) {
|
||||
new StatusCacheDAO(contextReference.get(), db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, tmpStatus);
|
||||
new StatusCacheDAO(contextReference.get(), db).insertStatus(BOOKMARK_CACHE, tmpStatus);
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
@ -68,8 +77,9 @@ public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
SystemClock.sleep(200);
|
||||
}
|
||||
} while (max_id != null);
|
||||
} else {
|
||||
List<app.fedilab.android.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
|
||||
statusList = new StatusCacheDAO(contextReference.get(), db).getAllStatus(BOOKMARK_CACHE);
|
||||
} else if (type == sync.EXPORT){
|
||||
List<app.fedilab.android.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(BOOKMARK_CACHE);
|
||||
if (statuses != null) {
|
||||
for (app.fedilab.android.client.Entities.Status tmpStatus : statuses) {
|
||||
new API(contextReference.get()).postAction(API.StatusAction.BOOKMARK, tmpStatus.getId());
|
||||
|
@ -80,8 +90,15 @@ public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
}
|
||||
}
|
||||
}
|
||||
statusList = new StatusCacheDAO(contextReference.get(), db).getAllStatus(BOOKMARK_CACHE);
|
||||
}else {
|
||||
APIResponse apiResponse = new API(contextReference.get()).getStatusbyIdAndCache(statusId);
|
||||
app.fedilab.android.client.Entities.Status refreshedStatus = apiResponse.getStatuses().get(0);
|
||||
new StatusCacheDAO(contextReference.get().getApplicationContext(), db).updateStatus(BOOKMARK_CACHE, refreshedStatus);
|
||||
statusList = new ArrayList<>();
|
||||
statusList.add(refreshedStatus);
|
||||
}
|
||||
statusList = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -92,7 +109,8 @@ public class SyncBookmarksAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
|
||||
public enum sync {
|
||||
EXPORT,
|
||||
IMPORT
|
||||
IMPORT,
|
||||
REFRESH
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -133,6 +133,7 @@ import app.fedilab.android.asynctasks.PostActionAsyncTask;
|
|||
import app.fedilab.android.asynctasks.PostStatusAsyncTask;
|
||||
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
|
||||
import app.fedilab.android.asynctasks.RetrieveRelationshipQuickReplyAsyncTask;
|
||||
import app.fedilab.android.asynctasks.SyncBookmarksAsyncTask;
|
||||
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
|
||||
import app.fedilab.android.client.API;
|
||||
import app.fedilab.android.client.APIResponse;
|
||||
|
@ -171,6 +172,7 @@ import app.fedilab.android.interfaces.OnRetrieveRelationshipQuickReplyInterface;
|
|||
import app.fedilab.android.interfaces.OnRetrieveRepliesInterface;
|
||||
import app.fedilab.android.interfaces.OnRetrieveSearcAccountshInterface;
|
||||
import app.fedilab.android.interfaces.OnRetrieveSearchInterface;
|
||||
import app.fedilab.android.interfaces.OnSyncBookmarksInterface;
|
||||
import app.fedilab.android.jobs.ScheduledBoostsSyncJob;
|
||||
import app.fedilab.android.sqlite.AccountDAO;
|
||||
import app.fedilab.android.sqlite.CustomEmojiDAO;
|
||||
|
@ -194,7 +196,7 @@ import static app.fedilab.android.activities.BaseMainActivity.social;
|
|||
* Created by Thomas on 24/04/2017.
|
||||
* Adapter for Status
|
||||
*/
|
||||
public class StatusListAdapter extends RecyclerView.Adapter implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveImageInterface, OnRetrieveEmojiInterface, OnRetrieveRepliesInterface, OnRetrieveCardInterface, OnPollInterface, OnRefreshCachedStatusInterface, OnRetrieveSearcAccountshInterface, OnRetrieveSearchInterface, OnPostStatusActionInterface, OnRetrieveRelationshipQuickReplyInterface {
|
||||
public class StatusListAdapter extends RecyclerView.Adapter implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveImageInterface, OnRetrieveEmojiInterface, OnRetrieveRepliesInterface, OnRetrieveCardInterface, OnPollInterface, OnRefreshCachedStatusInterface, OnRetrieveSearcAccountshInterface, OnRetrieveSearchInterface, OnPostStatusActionInterface, OnRetrieveRelationshipQuickReplyInterface, OnSyncBookmarksInterface {
|
||||
|
||||
public static final int DISPLAYED_STATUS = 1;
|
||||
public static final int COMPACT_STATUS = 3;
|
||||
|
@ -1173,7 +1175,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
|
|||
|
||||
|
||||
if (holder.cached_status != null && holder.getItemViewType() == DISPLAYED_STATUS) {
|
||||
if (status.iscached()) {
|
||||
if (status.iscached() || type == RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) {
|
||||
holder.cached_status.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
holder.cached_status.setVisibility(View.GONE);
|
||||
|
@ -1212,7 +1214,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
|
|||
holder.fedilab_features_panel.setBackgroundColor(ContextCompat.getColor(context, R.color.custom_features_panel_background_light));
|
||||
}
|
||||
|
||||
if (!status.iscached()) {
|
||||
if (!status.iscached() && type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) {
|
||||
holder.custom_feature_cache.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
|
@ -1268,9 +1270,14 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
|
|||
|
||||
TooltipCompat.setTooltipText(holder.custom_feature_cache, context.getString(R.string.refresh_cache));
|
||||
holder.custom_feature_cache.setOnClickListener(view -> {
|
||||
new ManageCachedStatusAsyncTask(context, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
status.setCustomFeaturesDisplayed(false);
|
||||
notifyStatusChanged(status);
|
||||
if( type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) {
|
||||
new ManageCachedStatusAsyncTask(context, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
status.setCustomFeaturesDisplayed(false);
|
||||
notifyStatusChanged(status);
|
||||
}else{
|
||||
new SyncBookmarksAsyncTask(context, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -3847,6 +3854,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
|
|||
statuses.get(i).setFavourited(status.isFavourited());
|
||||
statuses.get(i).setReblogged(status.isReblogged());
|
||||
statuses.get(i).setReplies_count(status.getReplies_count());
|
||||
statuses.get(i).setPoll(status.getPoll());
|
||||
statusListAdapter.notifyItemChanged(i);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
@ -3981,6 +3989,13 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRetrieveBookmarks(List<Status> statuses) {
|
||||
if( statuses != null && statuses.size() > 0 ) {
|
||||
notifyStatusChanged(statuses.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
private static class ViewHolderEmpty extends RecyclerView.ViewHolder {
|
||||
ViewHolderEmpty(View itemView) {
|
||||
super(itemView);
|
||||
|
|
|
@ -207,6 +207,7 @@ import app.fedilab.android.client.Entities.Emojis;
|
|||
import app.fedilab.android.client.Entities.Filters;
|
||||
import app.fedilab.android.client.Entities.MainMenuItem;
|
||||
import app.fedilab.android.client.Entities.Mention;
|
||||
import app.fedilab.android.client.Entities.Poll;
|
||||
import app.fedilab.android.client.Entities.RemoteInstance;
|
||||
import app.fedilab.android.client.Entities.Status;
|
||||
import app.fedilab.android.client.Entities.Tag;
|
||||
|
@ -2698,15 +2699,32 @@ public class Helper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Serialized a List of Emojis class
|
||||
* Serialized a Poll class
|
||||
*
|
||||
* @param emojis Emojis List to serialize
|
||||
* @return String serialized List of Emojis
|
||||
* @param poll Poll to serialize
|
||||
* @return String serialized Poll
|
||||
*/
|
||||
public static String emojisToStringStorage(List<Emojis> emojis) {
|
||||
public static String pollToStringStorage(Poll poll) {
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
return gson.toJson(emojis);
|
||||
return gson.toJson(poll);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Unserialized a Poll
|
||||
*
|
||||
* @param serializedPoll String serialized poll
|
||||
* @return Poll
|
||||
*/
|
||||
public static Poll restorePollFromString(String serializedPoll) {
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
return gson.fromJson(serializedPoll, Poll.class);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
@ -2728,6 +2746,22 @@ public class Helper {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serialized a Poll class
|
||||
*
|
||||
* @param emojis Emojis List to serialize
|
||||
* @return String serialized List of Emojis
|
||||
*/
|
||||
public static String emojisToStringStorage(List<Emojis> emojis) {
|
||||
Gson gson = new Gson();
|
||||
try {
|
||||
return gson.toJson(emojis);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialized a List of a Attachment class
|
||||
*
|
||||
|
|
|
@ -48,7 +48,7 @@ import static android.content.Context.MODE_PRIVATE;
|
|||
|
||||
public class Sqlite extends SQLiteOpenHelper {
|
||||
|
||||
public static final int DB_VERSION = 37;
|
||||
public static final int DB_VERSION = 38;
|
||||
public static final String DB_NAME = "mastodon_etalab_db";
|
||||
//Table for custom emoji
|
||||
public static final String TABLE_CUSTOM_EMOJI = "CUSTOM_EMOJI";
|
||||
|
@ -184,6 +184,8 @@ public class Sqlite extends SQLiteOpenHelper {
|
|||
static final String COL_NAV_BLOCKED_DOMAINS = "NAV_BLOCKED_DOMAINS";
|
||||
static final String COL_NAV_HOWTO = "NAV_HOWTO";
|
||||
static final String COL_NAV_TRENDS = "NAV_TRENDS";
|
||||
static final String COL_POLL = "POLL";
|
||||
|
||||
private static final String TABLE_USER_ACCOUNT_TEMP = "USER_ACCOUNT_TEMP";
|
||||
private static final String CREATE_TABLE_USER_ACCOUNT = "CREATE TABLE " + TABLE_USER_ACCOUNT + " ("
|
||||
+ COL_USER_ID + " TEXT, " + COL_USERNAME + " TEXT NOT NULL, " + COL_ACCT + " TEXT NOT NULL, "
|
||||
|
@ -293,7 +295,7 @@ public class Sqlite extends SQLiteOpenHelper {
|
|||
+ COL_EMOJIS + " TEXT, " + COL_REBLOGS_COUNT + " INTEGER NOT NULL, " + COL_FAVOURITES_COUNT + " INTEGER NOT NULL, "
|
||||
+ COL_REBLOGGED + " INTEGER, " + COL_FAVOURITED + " INTEGER, " + COL_MUTED + " INTEGER, " + COL_SENSITIVE + " INTEGER, "
|
||||
+ COL_SPOILER_TEXT + " TEXT, " + COL_VISIBILITY + " TEXT NOT NULL, " + COL_MEDIA_ATTACHMENTS + " TEXT," + COL_CARD + " TEXT,"
|
||||
+ COL_MENTIONS + " TEXT, " + COL_TAGS + " TEXT, " + COL_APPLICATION + " TEXT,"
|
||||
+ COL_MENTIONS + " TEXT, " + COL_POLL + " TEXT, " + COL_TAGS + " TEXT, " + COL_APPLICATION + " TEXT,"
|
||||
+ COL_LANGUAGE + " TEXT," + COL_PINNED + " INTEGER)";
|
||||
private final String CREATE_UNIQUE_CACHE_INDEX = "CREATE UNIQUE INDEX instance_statusid on "
|
||||
+ TABLE_STATUSES_CACHE + "(" + COL_INSTANCE + "," + COL_STATUS_ID + "," + COL_CACHED_ACTION + ")";
|
||||
|
@ -539,6 +541,9 @@ public class Sqlite extends SQLiteOpenHelper {
|
|||
if (oldVersion > 33) {
|
||||
db.execSQL("ALTER TABLE " + TABLE_MAIN_MENU_ITEMS + " ADD COLUMN " + COL_NAV_TRENDS + " INTEGER DEFAULT 1");
|
||||
}
|
||||
case 37:
|
||||
if (oldVersion > 8)
|
||||
db.execSQL("ALTER TABLE " + TABLE_STATUSES_CACHE + " ADD COLUMN " + COL_POLL + " TEXT");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -81,6 +81,7 @@ public class StatusCacheDAO {
|
|||
values.put(Sqlite.COL_REBLOG, status.getReblog() != null ? Helper.statusToStringStorage(status.getReblog()) : null);
|
||||
values.put(Sqlite.COL_CONTENT, status.getContent());
|
||||
values.put(Sqlite.COL_EMOJIS, status.getEmojis() != null ? Helper.emojisToStringStorage(status.getEmojis()) : null);
|
||||
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
|
||||
values.put(Sqlite.COL_REBLOGS_COUNT, status.getReblogs_count());
|
||||
values.put(Sqlite.COL_FAVOURITES_COUNT, status.getFavourites_count());
|
||||
values.put(Sqlite.COL_REBLOGGED, status.isReblogged());
|
||||
|
@ -135,6 +136,7 @@ public class StatusCacheDAO {
|
|||
values.put(Sqlite.COL_REBLOG, status.getReblog() != null ? Helper.statusToStringStorage(status.getReblog()) : null);
|
||||
values.put(Sqlite.COL_CONTENT, status.getContent());
|
||||
values.put(Sqlite.COL_EMOJIS, status.getEmojis() != null ? Helper.emojisToStringStorage(status.getEmojis()) : null);
|
||||
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
|
||||
values.put(Sqlite.COL_REBLOGS_COUNT, status.getReblogs_count());
|
||||
values.put(Sqlite.COL_FAVOURITES_COUNT, status.getFavourites_count());
|
||||
values.put(Sqlite.COL_REBLOGGED, status.isReblogged());
|
||||
|
@ -180,8 +182,9 @@ public class StatusCacheDAO {
|
|||
values.put(Sqlite.COL_FAVOURITED, status.isFavourited());
|
||||
values.put(Sqlite.COL_MUTED, status.isMuted());
|
||||
values.put(Sqlite.COL_PINNED, status.isPinned());
|
||||
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
|
||||
return db.update(Sqlite.TABLE_STATUSES_CACHE,
|
||||
values, Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " = ? " + Sqlite.COL_CACHED_ACTION + " = ?",
|
||||
values, Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " = ? AND " + Sqlite.COL_CACHED_ACTION + " = ?",
|
||||
new String[]{String.valueOf(status.getId()), instance, String.valueOf(cacheType)});
|
||||
}
|
||||
|
||||
|
@ -768,6 +771,7 @@ public class StatusCacheDAO {
|
|||
status.setContent(context, c.getString(c.getColumnIndex(Sqlite.COL_CONTENT)));
|
||||
status.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
|
||||
status.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
|
||||
status.setPoll(Helper.restorePollFromString(c.getString(c.getColumnIndex(Sqlite.COL_POLL))));
|
||||
status.setReblogs_count(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGS_COUNT)));
|
||||
status.setFavourites_count(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITES_COUNT)));
|
||||
status.setReblogged(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGGED)) == 1);
|
||||
|
@ -815,6 +819,7 @@ public class StatusCacheDAO {
|
|||
status.setContent(context, c.getString(c.getColumnIndex(Sqlite.COL_CONTENT)));
|
||||
status.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
|
||||
status.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
|
||||
status.setPoll(Helper.restorePollFromString(c.getString(c.getColumnIndex(Sqlite.COL_POLL))));
|
||||
status.setReblogs_count(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGS_COUNT)));
|
||||
status.setFavourites_count(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITES_COUNT)));
|
||||
status.setReblogged(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGGED)) == 1);
|
||||
|
|
Loading…
Reference in New Issue