Browse Source

story read improvement (WIP)

renovate/org.robolectric-robolectric-4.x
Austin Huang 4 years ago
parent
commit
e539e426df
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 5
      app/src/main/java/awais/instagrabber/adapters/FeedStoriesAdapter.java
  2. 8
      app/src/main/java/awais/instagrabber/adapters/viewholder/FeedStoryViewHolder.java
  3. 33
      app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java
  4. 22
      app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java
  5. 10
      app/src/main/java/awais/instagrabber/models/FeedStoryModel.java
  6. 2
      app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java
  7. 1
      app/src/main/java/awais/instagrabber/viewmodels/FeedStoriesViewModel.java
  8. 25
      app/src/main/java/awais/instagrabber/webservices/StoriesService.java

5
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<FeedStoryModel, FeedSt
@Override
public boolean areContentsTheSame(@NonNull final FeedStoryModel oldItem, @NonNull final FeedStoryModel newItem) {
return oldItem.getStoryMediaId().equals(newItem.getStoryMediaId());
return oldItem.getStoryMediaId().equals(newItem.getStoryMediaId()) && oldItem.isFullyRead().equals(newItem.isFullyRead());
}
};
@ -47,5 +48,7 @@ public final class FeedStoriesAdapter extends ListAdapter<FeedStoryModel, FeedSt
public interface OnFeedStoryClickListener {
void onFeedStoryClick(FeedStoryModel model, int position);
void onFeedStoryLongClick(FeedStoryModel model, int position);
}
}

8
app/src/main/java/awais/instagrabber/adapters/viewholder/FeedStoryViewHolder.java

@ -24,10 +24,14 @@ public final class FeedStoryViewHolder extends RecyclerView.ViewHolder {
if (listener == null) return;
listener.onFeedStoryClick(model, position);
});
binding.getRoot().setOnLongClickListener(v -> {
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);
}
}

33
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<FeedStoryModel> models = feedStoriesViewModel.getList().getValue();
// final List<FeedStoryModel> 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);

22
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 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(this).navigate(action);
});
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();
}

10
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;
}
}

2
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))

1
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;

25
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<List<FeedStoryModel>> 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<String, Object> 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<List<FeedStoryModel>> callback) {
try {
Log.d("austin_debug", body);
final List<FeedStoryModel> feedStoryModels = new ArrayList<>();
final JSONArray feedStoriesReel = new JSONObject(body).getJSONArray("tray");
for (int i = 0; i < feedStoriesReel.length(); ++i) {

Loading…
Cancel
Save