From 3d2dee0d94003cdb6962ab80cb39fa059d91fed8 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 29 Dec 2020 13:41:38 -0500 Subject: [PATCH] custom story sort, #122 point 1 --- .../adapters/FeedStoriesAdapter.java | 45 ++++++++++++++++++- .../settings/SettingsPreferencesFragment.java | 20 +++++++++ .../instagrabber/models/FeedStoryModel.java | 23 ++++++---- .../awais/instagrabber/utils/Constants.java | 1 + .../instagrabber/utils/SettingsHelper.java | 3 +- .../webservices/StoriesService.java | 41 ++++++++++++----- app/src/main/res/values/arrays.xml | 9 ++-- app/src/main/res/values/strings.xml | 1 + 8 files changed, 118 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java index 224eeec6..496acff8 100755 --- a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java @@ -1,16 +1,22 @@ package awais.instagrabber.adapters; -import android.util.Log; import android.view.LayoutInflater; import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ListAdapter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import awais.instagrabber.adapters.viewholder.FeedStoryViewHolder; import awais.instagrabber.databinding.ItemHighlightBinding; import awais.instagrabber.models.FeedStoryModel; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.Utils; public final class FeedStoriesAdapter extends ListAdapter { private final OnFeedStoryClickListener listener; @@ -46,6 +52,43 @@ public final class FeedStoriesAdapter extends ListAdapter list, @Nullable final Runnable commitCallback) { + if (list == null) { + super.submitList(null, commitCallback); + return; + } + super.submitList(sort(list), commitCallback); + } + + @Override + public void submitList(@Nullable final List list) { + if (list == null) { + super.submitList(null); + return; + } + super.submitList(sort(list)); + } + + private List sort(final List list) { + final List listCopy = new ArrayList<>(list); + Collections.sort(listCopy, (o1, o2) -> { + int result; + switch (Utils.settingsHelper.getString(Constants.STORY_SORT)) { + case "1": + result = o1.getTimestamp() > o2.getTimestamp() ? -1 : (o1.getTimestamp() == o2.getTimestamp() ? 0 : 1); + break; + case "2": + result = o1.getTimestamp() > o2.getTimestamp() ? 1 : (o1.getTimestamp() == o2.getTimestamp() ? 0 : -1); + break; + default: + result = 0; + } + return result; + }); + return listCopy; + } + public interface OnFeedStoryClickListener { void onFeedStoryClick(FeedStoryModel model, int position); diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java index 0f567583..24e935cf 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java @@ -79,6 +79,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { screen.addPreference(loggedInUsersPreferenceCategory); loggedInUsersPreferenceCategory.setIconSpaceReserved(false); loggedInUsersPreferenceCategory.setTitle(R.string.login_settings); + loggedInUsersPreferenceCategory.addPreference(getStorySortPreference()); loggedInUsersPreferenceCategory.addPreference(getMarkStoriesSeenPreference()); loggedInUsersPreferenceCategory.addPreference(getMarkDMSeenPreference()); loggedInUsersPreferenceCategory.addPreference(getEnableActivityNotificationsPreference()); @@ -204,6 +205,25 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { return preference; } + private Preference getStorySortPreference() { + final Context context = getContext(); + if (context == null) return null; + final ListPreference preference = new ListPreference(context); + preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance()); + final int length = getResources().getStringArray(R.array.story_sorts).length; + final String[] values = new String[length]; + for (int i = 0; i < length; i++) { + values[i] = String.valueOf(i); + } + preference.setKey(Constants.STORY_SORT); + preference.setTitle(R.string.story_sort_setting); + preference.setDialogTitle(R.string.story_sort_setting); + preference.setEntries(R.array.story_sorts); + preference.setIconSpaceReserved(false); + preference.setEntryValues(values); + return preference; + } + private Preference getMarkStoriesSeenPreference() { final Context context = getContext(); if (context == null) return null; diff --git a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java index de9eea2f..6273b020 100755 --- a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java +++ b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java @@ -7,30 +7,37 @@ import java.io.Serializable; public final class FeedStoryModel implements Serializable { private final String storyMediaId; private final ProfileModel profileModel; - private StoryModel[] storyModels; +// private StoryModel[] storyModels; private Boolean fullyRead; + private final long timestamp; - public FeedStoryModel(final String storyMediaId, final ProfileModel profileModel, final boolean fullyRead) { + public FeedStoryModel(final String storyMediaId, final ProfileModel profileModel, + final boolean fullyRead, final long timestamp) { this.storyMediaId = storyMediaId; this.profileModel = profileModel; this.fullyRead = fullyRead; + this.timestamp = timestamp; } public String getStoryMediaId() { return storyMediaId; } + public long getTimestamp() { + return timestamp; + } + public ProfileModel getProfileModel() { return profileModel; } - public void setStoryModels(final StoryModel[] storyModels) { - this.storyModels = storyModels; - } +// public void setStoryModels(final StoryModel[] storyModels) { +// this.storyModels = storyModels; +// } - public StoryModel[] getStoryModels() { - return storyModels; - } +// public StoryModel[] getStoryModels() { +// return storyModels; +// } public Boolean isFullyRead() { return fullyRead; diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index 3f6a2f25..e64041c2 100644 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -8,6 +8,7 @@ public final class Constants { public static final String CUSTOM_DATE_TIME_FORMAT = "date_time_custom_format"; public static final String APP_THEME = "app_theme_v19"; public static final String APP_LANGUAGE = "app_language_v19"; + public static final String STORY_SORT = "story_sort"; // int prefs public static final String PREV_INSTALL_VERSION = "prevVersion"; // boolean prefs diff --git a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java index f42c7cd7..d95a66ac 100755 --- a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java +++ b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java @@ -40,6 +40,7 @@ import static awais.instagrabber.utils.Constants.PREF_TOPIC_POSTS_LAYOUT; import static awais.instagrabber.utils.Constants.PREV_INSTALL_VERSION; import static awais.instagrabber.utils.Constants.SHOW_QUICK_ACCESS_DIALOG; import static awais.instagrabber.utils.Constants.SKIPPED_VERSION; +import static awais.instagrabber.utils.Constants.STORY_SORT; import static awais.instagrabber.utils.Constants.SWAP_DATE_TIME_FORMAT_ENABLED; public final class SettingsHelper { @@ -122,7 +123,7 @@ public final class SettingsHelper { {APP_LANGUAGE, APP_THEME, COOKIE, FOLDER_PATH, DATE_TIME_FORMAT, DATE_TIME_SELECTION, CUSTOM_DATE_TIME_FORMAT, DEVICE_UUID, SKIPPED_VERSION, DEFAULT_TAB, PREF_DARK_THEME, PREF_LIGHT_THEME, PREF_POSTS_LAYOUT, PREF_PROFILE_POSTS_LAYOUT, PREF_TOPIC_POSTS_LAYOUT, PREF_HASHTAG_POSTS_LAYOUT, PREF_LOCATION_POSTS_LAYOUT, - PREF_LIKED_POSTS_LAYOUT, PREF_TAGGED_POSTS_LAYOUT, PREF_SAVED_POSTS_LAYOUT}) + PREF_LIKED_POSTS_LAYOUT, PREF_TAGGED_POSTS_LAYOUT, PREF_SAVED_POSTS_LAYOUT, STORY_SORT}) public @interface StringSettings {} @StringDef({DOWNLOAD_USER_FOLDER, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS, diff --git a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java index 8d2bf5d3..a9a9e1ef 100644 --- a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java @@ -18,12 +18,6 @@ import awais.instagrabber.models.FeedStoryModel; import awais.instagrabber.models.HighlightModel; import awais.instagrabber.models.ProfileModel; import awais.instagrabber.models.StoryModel; -import awais.instagrabber.models.enums.MediaItemType; -import awais.instagrabber.models.stickers.PollModel; -import awais.instagrabber.models.stickers.QuestionModel; -import awais.instagrabber.models.stickers.QuizModel; -import awais.instagrabber.models.stickers.SliderModel; -import awais.instagrabber.models.stickers.SwipeUpModel; import awais.instagrabber.repositories.StoriesRepository; import awais.instagrabber.repositories.responses.StoryStickerResponse; import awais.instagrabber.utils.Constants; @@ -128,8 +122,9 @@ public class StoriesService extends BaseService { user.getString("profile_pic_url"), null, 0, 0, 0, false, false, false, false, false); final String id = node.getString("id"); - final boolean fullyRead = !node.isNull("seen") && node.getLong("seen") == node.getLong("latest_reel_media"); - feedStoryModels.add(new FeedStoryModel(id, profileModel, fullyRead)); + final long timestamp = node.getLong("latest_reel_media"); + final boolean fullyRead = !node.isNull("seen") && node.getLong("seen") == timestamp; + feedStoryModels.add(new FeedStoryModel(id, profileModel, fullyRead, timestamp)); } callback.onSuccess(feedStoryModels); } catch (JSONException e) { @@ -156,7 +151,7 @@ public class StoriesService extends BaseService { final int length = highlightsReel.length(); final List highlightModels = new ArrayList<>(); - // final String[] highlightIds = new String[length]; + for (int i = 0; i < length; ++i) { final JSONObject highlightNode = highlightsReel.getJSONObject(i); highlightModels.add(new HighlightModel( @@ -164,7 +159,8 @@ public class StoriesService extends BaseService { highlightNode.getString(Constants.EXTRAS_ID), highlightNode.getJSONObject("cover_media") .getJSONObject("cropped_image_version") - .getString("url") + .getString("url"), + highlightNode.getLong("latest_reel_media") )); } callback.onSuccess(highlightModels); @@ -337,4 +333,29 @@ public class StoriesService extends BaseService { } return builder.toString(); } + + public class ArchiveFetchResponse { + private List archives; + private final boolean hasNextPage; + private final String nextCursor; + + public ArchiveFetchResponse(final List highlightModels, final boolean hasNextPage, final String nextCursor) { + this.archives = archives; + this.hasNextPage = hasNextPage; + this.nextCursor = nextCursor; + } + + public List getArchives() { + return archives; + } + + public boolean hasNextPage() { + return hasNextPage; + } + + public String getNextCursor() { + return nextCursor; + } + } + } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index f2314d2a..f4ca34c0 100755 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -27,11 +27,10 @@ Dark Light - - 0 - 1 - 2 - 3 + + Instagram default (Unread then read) + From newest to oldest + From oldest to newest None diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b0519374..ef1714e4 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,6 +35,7 @@ Mark DM as seen after viewing Other members will know you viewed it Enable activity notifications + Feed stories sort Error loading profile!\nTry logging in and search again. Error creating Download folder(s). Save to custom folder