From 665f14388fee4dd7e96ccecc5c1b6987563996e3 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 6 Dec 2017 16:45:07 +0100 Subject: [PATCH] Prepares fetch more toots with bookmark --- .../mastodon/activities/BaseMainActivity.java | 12 +++++ .../mastodon/client/Entities/Status.java | 9 ++++ .../mastodon/drawers/StatusListAdapter.java | 30 ++++++++++-- .../fragments/DisplayStatusFragment.java | 47 ++++++++++++------- .../res/drawable-anydpi/ic_expand_more.xml | 9 ++++ app/src/main/res/layout/drawer_status.xml | 16 +++++++ app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-tr/strings.xml | 4 +- app/src/main/res/values/strings.xml | 2 + 9 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/drawable-anydpi/ic_expand_more.xml diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index f6afe0e2f..69bdf7bac 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -141,6 +141,8 @@ public abstract class BaseMainActivity extends AppCompatActivity String show_filtered; private AppBarLayout appBar; private static boolean activityPaused; + private String bookmark; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -572,6 +574,8 @@ public abstract class BaseMainActivity extends AppCompatActivity }); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + //Get the previous bookmark value + bookmark = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null); Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId); if( account == null){ Helper.logout(getApplicationContext()); @@ -1394,6 +1398,14 @@ public abstract class BaseMainActivity extends AppCompatActivity Helper.canPin = (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)); } + public String getBookmark() { + return bookmark; + } + + public void setBookmark(String bookmark) { + this.bookmark = bookmark; + } + /** * Page Adapter for settings diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index d38264c69..38a9c6c25 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -93,6 +93,7 @@ public class Status implements Parcelable { private boolean isNew = false; private boolean isTakingScreenShot = false; private boolean isVisible = true; + private boolean fetchMore = false; private Status status; private String content, contentCW, contentTranslated; private SpannableString contentSpan, contentSpanCW, contentSpanTranslated; @@ -732,4 +733,12 @@ public class Status implements Parcelable { public void setEmojiTranslateFound(boolean emojiTranslateFound) { isEmojiTranslateFound = emojiTranslateFound; } + + public boolean isFetchMore() { + return fetchMore; + } + + public void setFetchMore(boolean fetchMore) { + this.fetchMore = fetchMore; + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 5ae4f0a16..74c0378ed 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -71,6 +71,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.BaseMainActivity; import fr.gouv.etalab.mastodon.activities.MediaActivity; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; @@ -114,6 +115,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct private final int DISPLAYED_STATUS = 1; private List pins; private int conversationPosition; + private String bookmark = null; public StatusListAdapter(Context context, RetrieveFeedsAsyncTask.Type type, String targetedId, boolean isOnWifi, int behaviorWithAttachments, int translator, List statuses){ super(); @@ -127,6 +129,9 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct this.targetedId = targetedId; this.translator = translator; pins = new ArrayList<>(); + if( context instanceof BaseMainActivity){ + bookmark = ((BaseMainActivity) context).getBookmark(); + } } public StatusListAdapter(Context context, int position, String targetedId, boolean isOnWifi, int behaviorWithAttachments, int translator, List statuses){ @@ -141,6 +146,9 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct this.targetedId = targetedId; this.translator = translator; pins = new ArrayList<>(); + if( context instanceof BaseMainActivity){ + bookmark = ((BaseMainActivity) context).getBookmark(); + } } @Override @@ -218,7 +226,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct LinearLayout status_replies; LinearLayout status_replies_profile_pictures; ProgressBar loader_replies; - + Button fetch_more; ImageView new_element; public View getView(){ @@ -228,6 +236,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct ViewHolder(View itemView) { super(itemView); loader_replies = itemView.findViewById(R.id.loader_replies); + fetch_more = itemView.findViewById(R.id.fetch_more); status_document_container = itemView.findViewById(R.id.status_document_container); status_content = itemView.findViewById(R.id.status_content); status_content_translated = itemView.findViewById(R.id.status_content_translated); @@ -558,7 +567,11 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //-------- END -> Change the color in gray for accounts in DARK Theme only - + if( status.isFetchMore()) { + holder.fetch_more.setVisibility(View.VISIBLE); + holder.fetch_more.setEnabled(true); + }else + holder.fetch_more.setVisibility(View.GONE); if( status.getReblog() == null) holder.status_favorite_count.setText(String.valueOf(status.getFavourites_count())); @@ -1145,7 +1158,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct - private void loadAttachments(final Status status, ViewHolder holder){ + private void loadAttachments(final Status status, final ViewHolder holder){ List attachments = status.getMedia_attachments(); if( attachments != null && attachments.size() > 0){ int i = 0; @@ -1231,6 +1244,16 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct holder.status_document_container.setVisibility(View.GONE); } holder.status_show_more.setVisibility(View.GONE); + + holder.fetch_more.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(status.isFetchMore()) { + status.setFetchMore(false); + holder.fetch_more.setEnabled(false); + } + } + }); } @@ -1388,4 +1411,5 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct public void onRetrieveSearchEmoji(List emojis) { } + } \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 1f4d1daa0..0942d8423 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.BaseMainActivity; import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; @@ -68,10 +69,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn private String tag; private boolean swiped; private RecyclerView lv_status; - private boolean isOnWifi; - private int behaviorWithAttachments; private boolean showMediaOnly, showPinned; - private int positionSpinnerTrans; private String lastReadStatus; private Intent streamingFederatedIntent, streamingLocalIntent; LinearLayoutManager mLayoutManager; @@ -101,10 +99,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn assert context != null; final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - isOnWifi = Helper.isOnWIFI(context); - positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX); + boolean isOnWifi = Helper.isOnWIFI(context); + int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX); swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); - behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); + int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); if( type == RetrieveFeedsAsyncTask.Type.HOME) lastReadStatus = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null); @@ -156,11 +154,11 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn swiped = true; MainActivity.countNewStatus = 0; if( type == RetrieveFeedsAsyncTask.Type.USER) - asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, null, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else if( type == RetrieveFeedsAsyncTask.Type.TAG) - asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else - asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, @@ -253,20 +251,37 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn } if( statuses != null && statuses.size() > 0) { - if( type == RetrieveFeedsAsyncTask.Type.FAVOURITES ){ - this.statuses.addAll(statuses); - }else { + if (type == RetrieveFeedsAsyncTask.Type.HOME) { + String bookmark = null; + if( context instanceof BaseMainActivity){ + bookmark = ((BaseMainActivity) context).getBookmark(); + } + int inc = 0; for (Status tmpStatus : statuses) { - if (this.statuses.size() == 0 || Long.parseLong(tmpStatus.getId()) < Long.parseLong(this.statuses.get(this.statuses.size() - 1).getId())) { - if (type == RetrieveFeedsAsyncTask.Type.HOME && firstLoad && lastReadStatus != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)) { + if (bookmark != null){ + if (Long.parseLong(tmpStatus.getId()) > Long.parseLong(bookmark)) { tmpStatus.setNew(true); MainActivity.countNewStatus++; - } else { - tmpStatus.setNew(false); } + } + if( this.statuses != null && this.statuses.size() > 0) { + for (Status status : this.statuses) { + if (Long.parseLong(tmpStatus.getId()) == Long.parseLong(status.getId())) { + break; //This toot was already added (security, should not happen) + }else if (Long.parseLong(tmpStatus.getId()) < Long.parseLong(status.getId())) { + this.statuses.add(tmpStatus); + } + if( inc == statuses.size() -1 && bookmark != null && Long.parseLong(statuses.get(inc).getId()) > Long.parseLong(bookmark) ){ + tmpStatus.setFetchMore(true); + } + } + }else { this.statuses.add(tmpStatus); } + inc++; } + }else { + this.statuses.addAll(statuses); } if( firstLoad && type == RetrieveFeedsAsyncTask.Type.HOME && statuses.size() > 0) { //Update the id of the last toot retrieved diff --git a/app/src/main/res/drawable-anydpi/ic_expand_more.xml b/app/src/main/res/drawable-anydpi/ic_expand_more.xml new file mode 100644 index 000000000..fd3ce4a46 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_expand_more.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/drawer_status.xml b/app/src/main/res/layout/drawer_status.xml index 6fbdc4227..a4bde9e66 100644 --- a/app/src/main/res/layout/drawer_status.xml +++ b/app/src/main/res/layout/drawer_status.xml @@ -433,4 +433,20 @@ android:layout_height="wrap_content" android:layout_weight="1"/> +