From e539e426dff50fe575bf62d136fcec339c739727 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 24 Dec 2020 14:11:49 -0500 Subject: [PATCH] story read improvement (WIP) --- .../adapters/FeedStoriesAdapter.java | 5 ++- .../viewholder/FeedStoryViewHolder.java | 8 +++-- .../fragments/StoryViewerFragment.java | 33 ++++++++++++++----- .../fragments/main/FeedFragment.java | 24 +++++++++++--- .../instagrabber/models/FeedStoryModel.java | 10 ++++-- .../instagrabber/utils/ResponseBodyUtils.java | 2 +- .../viewmodels/FeedStoriesViewModel.java | 1 + .../webservices/StoriesService.java | 25 +------------- 8 files changed, 65 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java index c1759893..224eeec6 100755 --- a/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java @@ -1,5 +1,6 @@ package awais.instagrabber.adapters; +import android.util.Log; import android.view.LayoutInflater; import android.view.ViewGroup; @@ -22,7 +23,7 @@ public final class FeedStoriesAdapter extends ListAdapter { + if (listener != null) listener.onFeedStoryLongClick(model, position); + return true; + }); final ProfileModel profileModel = model.getProfileModel(); binding.title.setText(profileModel.getUsername()); - binding.title.setAlpha(model.getFullyRead() ? 0.5F : 1.0F); + binding.title.setAlpha(model.isFullyRead() ? 0.5F : 1.0F); binding.icon.setImageURI(profileModel.getSdProfilePic()); - binding.icon.setAlpha(model.getFullyRead() ? 0.5F : 1.0F); + binding.icon.setAlpha(model.isFullyRead() ? 0.5F : 1.0F); } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java index 6c1fd576..3a4953b0 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java @@ -60,6 +60,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.NumberFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; @@ -315,9 +316,6 @@ public class StoryViewerFragment extends Fragment { final boolean swipingBeyondCurrentStories = (endOfCurrentStories && isLeftSwipe) || (slidePos == 0 && isRightSwipe); if (swipingBeyondCurrentStories && hasFeedStories) { final int index = currentFeedStoryIndex; - if (settingsHelper.getBoolean(MARK_AS_SEEN)) { - new SeenAction(cookie, currentStory).execute(); - } if ((isRightSwipe && index == 0) || (isLeftSwipe && index == finalModels.size() - 1)) { Toast.makeText(context, R.string.no_more_stories, Toast.LENGTH_SHORT).show(); return; @@ -325,7 +323,7 @@ public class StoryViewerFragment extends Fragment { final Object feedStoryModel = isRightSwipe ? finalModels.get(index - 1) : finalModels.size() == index + 1 ? null : finalModels.get(index + 1); - paginateStories(feedStoryModel, context, isRightSwipe, currentFeedStoryIndex == finalModels.size() - 2); + paginateStories(feedStoryModel, finalModels.get(index), context, isRightSwipe, currentFeedStoryIndex == finalModels.size() - 2); return; } if (isRightSwipe) { @@ -364,8 +362,12 @@ public class StoryViewerFragment extends Fragment { if (hasFeedStories) { binding.btnBackward.setVisibility(currentFeedStoryIndex == 0 ? View.INVISIBLE : View.VISIBLE); binding.btnForward.setVisibility(currentFeedStoryIndex == finalModels.size() - 1 ? View.INVISIBLE : View.VISIBLE); - binding.btnBackward.setOnClickListener(v -> paginateStories(finalModels.get(currentFeedStoryIndex - 1), context, true, false)); - binding.btnForward.setOnClickListener(v -> paginateStories(finalModels.get(currentFeedStoryIndex + 1), context, false, + binding.btnBackward.setOnClickListener(v -> paginateStories(finalModels.get(currentFeedStoryIndex - 1), + finalModels.get(currentFeedStoryIndex), + context, true, false)); + binding.btnForward.setOnClickListener(v -> paginateStories(finalModels.get(currentFeedStoryIndex + 1), + finalModels.get(currentFeedStoryIndex), + context, false, currentFeedStoryIndex == finalModels.size() - 2)); } @@ -893,12 +895,27 @@ public class StoryViewerFragment extends Fragment { player = null; } - private void paginateStories(Object feedStory, Context context, boolean backward, boolean last) { - if (feedStory != null) { + private void paginateStories(Object newFeedStory, Object oldFeedStory, Context context, boolean backward, boolean last) { + if (newFeedStory != null) { if (fetching) { Toast.makeText(context, R.string.be_patient, Toast.LENGTH_SHORT).show(); return; } +// if (settingsHelper.getBoolean(MARK_AS_SEEN) +// && oldFeedStory != null +// && oldFeedStory instanceof FeedStoryModel +// && viewModel instanceof FeedStoriesViewModel) { +// final FeedStoriesViewModel feedStoriesViewModel = (FeedStoriesViewModel) viewModel; +// final FeedStoryModel oldFeedStoryModel = (FeedStoryModel) oldFeedStory; +// if (oldFeedStoryModel.isFullyRead()) { +// oldFeedStoryModel.setFullyRead(false); +// final List models = feedStoriesViewModel.getList().getValue(); +// final List modelsCopy = models == null ? new ArrayList<>() : new ArrayList<>(models); +// Log.d("austin_debug", oldFeedStoryModel.getProfileModel().getUsername() + ", v " + models.get(currentFeedStoryIndex).isFullyRead() + " l " + oldFeedStoryModel.isFullyRead()); +// modelsCopy.set(currentFeedStoryIndex, oldFeedStoryModel); +// feedStoriesViewModel.getList().setValue(models); +// } +// } fetching = true; binding.btnBackward.setVisibility(currentFeedStoryIndex == 1 && backward ? View.INVISIBLE : View.VISIBLE); binding.btnForward.setVisibility(last ? View.INVISIBLE : View.VISIBLE); diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index f562770a..f5af3ff4 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -354,10 +354,21 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre private void setupFeedStories() { feedStoriesViewModel = new ViewModelProvider(fragmentActivity).get(FeedStoriesViewModel.class); - final FeedStoriesAdapter feedStoriesAdapter = new FeedStoriesAdapter((model, position) -> { - final NavDirections action = FeedFragmentDirections.actionFeedFragmentToStoryViewerFragment(position, null, false, false, null, null); - NavHostFragment.findNavController(this).navigate(action); - }); + final FeedStoriesAdapter feedStoriesAdapter = new FeedStoriesAdapter( + new FeedStoriesAdapter.OnFeedStoryClickListener() { + @Override + public void onFeedStoryClick(FeedStoryModel model, int position) { + Log.d("austin_debug", "read status is "+model.isFullyRead()); + final NavDirections action = FeedFragmentDirections.actionFeedFragmentToStoryViewerFragment(position, null, false, false, null, null); + NavHostFragment.findNavController(FeedFragment.this).navigate(action); + } + + @Override + public void onFeedStoryLongClick(FeedStoryModel model, int position) { + navigateToProfile("@" + model.getProfileModel().getUsername()); + } + } + ); final Context context = getContext(); if (context == null) return; storiesRecyclerView = new RecyclerView(context); @@ -369,7 +380,10 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre storiesRecyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)); storiesRecyclerView.setAdapter(feedStoriesAdapter); fragmentActivity.setCollapsingView(storiesRecyclerView); - feedStoriesViewModel.getList().observe(fragmentActivity, feedStoriesAdapter::submitList); + feedStoriesViewModel.getList().observe(fragmentActivity, list -> { + Log.d("austin_debug", "observed"); + feedStoriesAdapter.submitList(list); + }); fetchStories(); } diff --git a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java index e2d9fc77..de9eea2f 100755 --- a/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java +++ b/app/src/main/java/awais/instagrabber/models/FeedStoryModel.java @@ -1,12 +1,14 @@ package awais.instagrabber.models; +import android.util.Log; + import java.io.Serializable; public final class FeedStoryModel implements Serializable { private final String storyMediaId; private final ProfileModel profileModel; private StoryModel[] storyModels; - private boolean fullyRead; + private Boolean fullyRead; public FeedStoryModel(final String storyMediaId, final ProfileModel profileModel, final boolean fullyRead) { this.storyMediaId = storyMediaId; @@ -30,7 +32,11 @@ public final class FeedStoryModel implements Serializable { return storyModels; } - public boolean getFullyRead() { + public Boolean isFullyRead() { return fullyRead; } + + public void setFullyRead(final boolean fullyRead) { + this.fullyRead = fullyRead; + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java index bfb33129..97248ba1 100644 --- a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java @@ -644,7 +644,7 @@ public final class ResponseBodyUtils { .setPostId(itemJson.getString(Constants.EXTRAS_ID)) .setThumbnailUrl(mediaType != MediaItemType.MEDIA_TYPE_SLIDER ? ResponseBodyUtils.getLowQualityImage(itemJson) : null) .setShortCode(itemJson.getString("code")) - .setPostCaption(captionJson != null ? captionJson.optString("text") : null) + .setPostCaption(captionJson != null ? captionJson.optString("text") : "") .setCaptionId(captionJson != null ? captionJson.optString("pk") : null) .setCommentsCount(itemJson.optInt("comment_count")) .setTimestamp(itemJson.optLong("taken_at", -1)) diff --git a/app/src/main/java/awais/instagrabber/viewmodels/FeedStoriesViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/FeedStoriesViewModel.java index fd61557d..f4bd3b41 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/FeedStoriesViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/FeedStoriesViewModel.java @@ -3,6 +3,7 @@ package awais.instagrabber.viewmodels; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import java.util.ArrayList; import java.util.List; import awais.instagrabber.models.FeedStoryModel; diff --git a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java index a0d93d3c..b5a76fc0 100644 --- a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java @@ -64,30 +64,6 @@ public class StoriesService extends BaseService { } public void getFeedStories(final String csrfToken, final ServiceCallback> callback) { - if (loadFromMock) { - final Handler handler = new Handler(); - handler.postDelayed(() -> { - final ClassLoader classLoader = getClass().getClassLoader(); - if (classLoader == null) { - Log.e(TAG, "getFeedStories: classLoader is null!"); - return; - } - try (InputStream resourceAsStream = classLoader.getResourceAsStream("stories_response.json"); - Reader in = new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8)) { - final int bufferSize = 1024; - final char[] buffer = new char[bufferSize]; - final StringBuilder out = new StringBuilder(); - int charsRead; - while ((charsRead = in.read(buffer, 0, buffer.length)) > 0) { - out.append(buffer, 0, charsRead); - } - parseStoriesBody(out.toString(), callback); - } catch (IOException e) { - Log.e(TAG, "getFeedStories: ", e); - } - }, 1000); - return; - } final Map form = new HashMap<>(4); form.put("reason", "cold_start"); form.put("_csrftoken", csrfToken); @@ -115,6 +91,7 @@ public class StoriesService extends BaseService { private void parseStoriesBody(final String body, final ServiceCallback> callback) { try { + Log.d("austin_debug", body); final List feedStoryModels = new ArrayList<>(); final JSONArray feedStoriesReel = new JSONObject(body).getJSONArray("tray"); for (int i = 0; i < feedStoriesReel.length(); ++i) {