Skip to content

Commit 98238ed

Browse files
Fill empty post requests with blank quotes,
Properly send a comment to make a vote, Returned play and track media icons, Load more comments
1 parent f513bce commit 98238ed

File tree

10 files changed

+249
-34
lines changed

10 files changed

+249
-34
lines changed

app/src/main/java/com/mrboomdev/awery/extensions/data/CatalogComment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class CatalogComment implements CatalogSearchResults<CatalogComment> {
4747
*/
4848
@Json(name = "votes")
4949
public Integer votes;
50-
public String date;
50+
public String date, id;
5151

5252
/**
5353
* Used only for the Frontend
@@ -56,7 +56,6 @@ public class CatalogComment implements CatalogSearchResults<CatalogComment> {
5656
*/
5757
@Json(ignore = true)
5858
public long visualId;
59-
@Json(ignore = true)
6059
public int voteState = VOTE_STATE_NONE;
6160

6261
@Override

app/src/main/java/com/mrboomdev/awery/extensions/support/anilist/query/AnilistSearchQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ public String getQuery() {
132132
{
133133
Page(page: __PAGE__, perPage: __ITEMS_PER_PAGE__) {
134134
media(__PARAMS__) {
135-
format duration
135+
duration
136136
countryOfOrigin
137137
id description bannerImage status
138138
genres averageScore episodes
139139
startDate { year month day }
140140
endDate { year month day }
141-
coverImage { extraLarge large color medium }
141+
coverImage { extraLarge large medium }
142142
tags { name }
143143
title { romaji(stylised: false) english(stylised: false) native(stylised: false) }
144144
}

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsBridge.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mrboomdev.awery.extensions.support.js;
22

33
import static com.mrboomdev.awery.app.AweryLifecycle.getAnyContext;
4+
import static com.mrboomdev.awery.util.NiceUtils.doIfNotNull;
45
import static com.mrboomdev.awery.util.NiceUtils.stream;
56

67
import android.util.Log;
@@ -128,8 +129,8 @@ public Object fetch(@NonNull ScriptableObject options) {
128129
}
129130
}
130131

131-
if(options.has("method", options)) {
132-
var method = options.get("method").toString().toLowerCase(Locale.ROOT);
132+
doIfNotNull(stringFromJs(options.get("method")), method -> {
133+
method = method.toLowerCase(Locale.ROOT);
133134

134135
request.setMethod(options.has("method", options) ? switch(method) {
135136
case "get" -> HttpClient.Method.GET;
@@ -139,7 +140,12 @@ public Object fetch(@NonNull ScriptableObject options) {
139140
case "patch" -> HttpClient.Method.PATCH;
140141
default -> throw new IllegalArgumentException("Unsupported method: " + method);
141142
} : null);
142-
}
143+
144+
if(method.equals("post") && !options.has("body", options)) {
145+
request.setBody("", MimeTypes.TEXT);
146+
147+
}
148+
});
143149

144150
request.callAsync(getAnyContext(), new HttpClient.HttpCallback() {
145151
@Override

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsComment.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected JsComment(@NonNull ScriptableObject o) {
3737
comments = o.has("comments", o) ? intFromJs(o.get("comments", o)) : CatalogComment.DISABLED;
3838
votes = o.has("votes", o) ? fromJs(o.get("votes", o), Integer.class) : null;
3939
voteState = intFromJs(o.get("voteState", o));
40+
id = stringFromJs(o.get("id", o));
4041

4142
canComment = booleanFromJs(o.get("canComment", o));
4243
hasNextPage = booleanFromJs(o.get("hasNextPage", o));
@@ -46,17 +47,13 @@ protected JsComment(@NonNull ScriptableObject o) {
4647

4748
public static Scriptable createJsComment(Context context, Scriptable scope, CatalogComment comment) {
4849
if(comment == null) return null;
49-
50-
if(comment instanceof JsComment jsComment) {
51-
return jsComment.customData;
52-
}
53-
5450
var o = context.newObject(scope);
5551

5652
o.put("authorName", o, comment.authorName);
5753
o.put("authorAvatar", o, comment.authorAvatar);
5854
o.put("text", o, comment.text);
5955
o.put("date", o, comment.date);
56+
o.put("id", o, comment.id);
6057

6158
o.put("likes", o, comment.likes);
6259
o.put("dislikes", o, comment.dislikes);

app/src/main/java/com/mrboomdev/awery/extensions/support/js/JsProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,16 @@ public void voteComment(CatalogComment comment, @NonNull ResponseCallback<Catalo
356356
manager.postRunnable(() -> {
357357
if(scope.get("aweryVoteComment") instanceof Function fun) {
358358
try {
359-
fun.call(context, scope, null, new Object[]{ comment, (Callback<NativeObject>) (o, e) -> {
360-
if(e != null) {
361-
callback.onFailure(new JsException(e));
362-
return;
363-
}
359+
fun.call(context, scope, null, new Object[]{
360+
JsComment.createJsComment(context, scope, comment),
361+
(Callback<NativeObject>) (o, e) -> {
362+
if(e != null) {
363+
callback.onFailure(new JsException(e));
364+
return;
365+
}
364366

365-
callback.onSuccess(new JsComment(o));
366-
}});
367+
callback.onSuccess(new JsComment(o));
368+
}});
367369
} catch(Throwable e) {
368370
callback.onFailure(e);
369371
}

app/src/main/java/com/mrboomdev/awery/ui/fragments/MediaCommentsFragment.java

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@
6262
import java9.util.Objects;
6363

6464
public class MediaCommentsFragment extends Fragment {
65+
private static final int LAST_PAGE = -1;
6566
private static final String TAG = "MediaCommentsFragment";
6667
private final SingleViewAdapter.BindingSingleViewAdapter<LayoutLoadingBinding> loadingAdapter;
6768
private final WeakHashMap<CatalogComment, Parcelable> scrollPositions = new WeakHashMap<>();
69+
private final WeakHashMap<CatalogComment, Integer> pages = new WeakHashMap<>();
6870
private final CommentsAdapter commentsAdapter = new CommentsAdapter();
6971
private final List<CatalogComment> currentCommentsPath = new ArrayList<>();
7072
private final Runnable backPressCallback;
73+
private boolean isLoading;
7174
private ExtensionProvider selectedProvider;
7275
private RecyclerView recycler;
7376
private Runnable onCloseRequestListener;
@@ -130,11 +133,11 @@ public void setMedia(CatalogMedia media) {
130133
this.media = media;
131134
if(adapter == null) return;
132135

133-
loadData(null, null);
136+
loadData(null, null, 0);
134137
}
135138

136139
private void setComment(@Nullable CatalogComment comment, CatalogComment reloadThis) {
137-
this.recycler.scrollToPosition(0);
140+
//this.recycler.scrollToPosition(0);
138141
this.comment = comment;
139142

140143
if(comment != null) {
@@ -167,9 +170,13 @@ private void setComment(@Nullable CatalogComment comment, CatalogComment reloadT
167170
var layoutManager = requireNonNull(recycler.getLayoutManager());
168171
layoutManager.onRestoreInstanceState(scrollPosition);
169172
}
173+
174+
if(comment != null && !comment.hasNextPage()) {
175+
reachedEnd();
176+
}
170177
}
171178

172-
private void loadData(CatalogComment parent, CatalogComment reloadThis) {
179+
private void loadData(CatalogComment parent, CatalogComment reloadThis, int page) {
173180
if(this.comment != null) {
174181
var layoutManager = requireNonNull(recycler.getLayoutManager());
175182
scrollPositions.put(this.comment, layoutManager.onSaveInstanceState());
@@ -199,16 +206,30 @@ private void loadData(CatalogComment parent, CatalogComment reloadThis) {
199206
}
200207

201208
var request = new ReadMediaCommentsRequest()
202-
.setPage(0)
209+
.setPage(page)
203210
.setParentComment(parent)
204211
.setMedia(media);
205212

213+
isLoading = true;
214+
206215
selectedProvider.readMediaComments(request, new ExtensionProvider.ResponseCallback<>() {
207216
@Override
208-
public void onSuccess(CatalogComment parent) {
217+
public void onSuccess(CatalogComment newComment) {
209218
runOnUiThread(() -> {
210219
if(getContext() == null) return;
211-
setComment(parent, reloadThis);
220+
isLoading = false;
221+
222+
if(page == 0) {
223+
pages.put(newComment, 0);
224+
setComment(newComment, reloadThis);
225+
return;
226+
}
227+
228+
commentsAdapter.addData(newComment);
229+
230+
if(!newComment.hasNextPage()) {
231+
reachedEnd();
232+
}
212233
}, recycler);
213234
}
214235

@@ -217,6 +238,7 @@ public void onFailure(Throwable e) {
217238
loadingAdapter.getBinding(binding -> runOnUiThread(() -> {
218239
if(getContext() == null) return;
219240
swipeRefresher.setRefreshing(false);
241+
isLoading = false;
220242

221243
if(parent != null && (reloadThis == null ||
222244
(e instanceof JsException jsE && Objects.equals(jsE.getErrorId(), JsException.ERROR_NOTHING_FOUND)))) {
@@ -242,6 +264,18 @@ public void onFailure(Throwable e) {
242264
});
243265
}
244266

267+
private void reachedEnd() {
268+
pages.put(comment, LAST_PAGE);
269+
270+
loadingAdapter.getBinding(binding -> {
271+
binding.info.setVisibility(View.VISIBLE);
272+
binding.progressBar.setVisibility(View.GONE);
273+
binding.title.setText(R.string.you_reached_end);
274+
binding.message.setText(R.string.you_reached_end_description);
275+
loadingAdapter.setEnabled(true);
276+
});
277+
}
278+
245279
@Override
246280
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
247281
providers = stream(ExtensionsFactory.getExtensions(Extension.FLAG_WORKING))
@@ -257,14 +291,21 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
257291
selectedProvider = providers.get(0);
258292
}
259293

294+
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
295+
@Override
296+
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
297+
tryLoadMore();
298+
}
299+
});
300+
260301
setMedia(media);
261302
}
262303

263304
@Nullable
264305
@Override
265306
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
266307
swipeRefresher = new SwipeRefreshLayout(inflater.getContext());
267-
swipeRefresher.setOnRefreshListener(() -> loadData(comment, comment));
308+
swipeRefresher.setOnRefreshListener(() -> loadData(comment, comment, 0));
268309

269310
var parentLayout = new LinearLayoutCompat(inflater.getContext());
270311
parentLayout.setOrientation(LinearLayoutCompat.VERTICAL);
@@ -284,7 +325,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
284325

285326
adapter = new ConcatAdapter(new ConcatAdapter.Config.Builder()
286327
.setStableIdMode(ConcatAdapter.Config.StableIdMode.ISOLATED_STABLE_IDS).build(),
287-
loadingAdapter, commentsAdapter);
328+
commentsAdapter, loadingAdapter);
288329

289330
recycler.setAdapter(adapter);
290331
parentLayout.addView(recycler, createLinearParams(MATCH_PARENT, 0, 1));
@@ -318,7 +359,7 @@ public void onSuccess(CatalogComment comment) {
318359
/* So apparently people wanna to see all comments even after you did post a new one.
319360
Weird... */
320361
loadData(MediaCommentsFragment.this.comment,
321-
MediaCommentsFragment.this.comment);
362+
MediaCommentsFragment.this.comment, 0);
322363

323364
sendBinding.loadingIndicator.setVisibility(View.GONE);
324365
sendBinding.sendButton.setVisibility(View.VISIBLE);
@@ -349,6 +390,25 @@ public void onFailure(Throwable e) {
349390
return swipeRefresher;
350391
}
351392

393+
private void tryLoadMore() {
394+
if(media == null) return;
395+
var page = pages.get(comment);
396+
397+
if(page == null) {
398+
throw new NullPointerException("Page not found!");
399+
}
400+
401+
if(!isLoading && page != LAST_PAGE) {
402+
var lastIndex = comment.size() - 1;
403+
404+
if(recycler.getLayoutManager() instanceof LinearLayoutManager manager
405+
&& manager.findLastVisibleItemPosition() >= lastIndex) {
406+
pages.put(comment, page + 1);
407+
loadData(comment, comment, page + 1);
408+
}
409+
}
410+
}
411+
352412
private class CommentsAdapter extends RecyclerView.Adapter<CommentViewHolder> {
353413
private final UniqueIdGenerator idGenerator = new UniqueIdGenerator();
354414
private CatalogComment root;
@@ -373,6 +433,18 @@ public void setData(@Nullable CatalogComment root) {
373433
notifyDataSetChanged();
374434
}
375435

436+
public void addData(@Nullable CatalogComment root) {
437+
if(root == null) return;
438+
439+
for(var item : root.items) {
440+
item.visualId = idGenerator.getLong();
441+
}
442+
443+
var wasSize = comment.size();
444+
this.root.items.addAll(root.items);
445+
notifyItemRangeInserted(wasSize, root.items.size());
446+
}
447+
376448
@NonNull
377449
@Override
378450
public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@@ -384,7 +456,7 @@ public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewT
384456
var comment = holder.getComment();
385457
if(comment == MediaCommentsFragment.this.comment) return;
386458

387-
loadData(comment, null);
459+
loadData(comment, null, 0);
388460
});
389461

390462
return holder;

app/src/main/java/com/mrboomdev/awery/util/NiceUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public static <A, B> A returnIfNotNull(B param, Callbacks.Result1<A, B> callback
2727
return param == null ? null : callback.run(param);
2828
}
2929

30+
public static <A> void doIfNotNull(A param, Callbacks.Callback1<A> callback) {
31+
if(param != null) {
32+
callback.run(param);
33+
}
34+
}
35+
3036
/**
3137
* @return The first item in the collection that matches the query
3238
* @author MrBoomDev

app/src/main/java/com/mrboomdev/awery/util/exceptions/ExceptionDescriptor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public ExceptionDescriptor(@NonNull Throwable t) {
3434
}
3535

3636
public static Throwable unwrap(Throwable t) {
37-
if(!isUnknownException(t)) {
38-
return t;
37+
if(t instanceof WrappedException wrappedException) {
38+
return unwrap(wrappedException.getWrappedException());
3939
}
4040

41-
while(t instanceof WrappedException e) {
42-
t = e.getWrappedException();
41+
if(!isUnknownException(t)) {
42+
return t;
4343
}
4444

4545
var firstCause = t.getCause();

0 commit comments

Comments
 (0)