change behavior when adding reactions

closes sk22#769
This commit is contained in:
sk 2023-08-24 13:00:16 +02:00
parent 47fde1e08b
commit d44df2c23c
3 changed files with 51 additions and 21 deletions

View File

@ -106,7 +106,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
vh.btn.setClickable(!visible); vh.btn.setClickable(!visible);
} }
private MastodonAPIRequest<?> createRequest(String name, int count, boolean delete, Holder.EmojiReactionViewHolder vh, Runnable cb){ private MastodonAPIRequest<?> createRequest(String name, int count, boolean delete, Holder.EmojiReactionViewHolder vh, Runnable cb, Runnable err){
setActionProgressVisible(vh, true); setActionProgressVisible(vh, true);
boolean ak=parentFragment.isInstanceAkkoma(); boolean ak=parentFragment.isInstanceAkkoma();
boolean keepSpinning=delete && count == 1; boolean keepSpinning=delete && count == 1;
@ -124,6 +124,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
public void onError(ErrorResponse error){ public void onError(ErrorResponse error){
setActionProgressVisible(vh, false); setActionProgressVisible(vh, false);
error.showToast(parentFragment.getContext()); error.showToast(parentFragment.getContext());
if(err!=null) err.run();
} }
}); });
}else{ }else{
@ -140,6 +141,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
public void onError(ErrorResponse error){ public void onError(ErrorResponse error){
setActionProgressVisible(vh, false); setActionProgressVisible(vh, false);
error.showToast(parentFragment.getContext()); error.showToast(parentFragment.getContext());
if(err!=null) err.run();
} }
}); });
} }
@ -151,6 +153,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
private CustomEmojiPopupKeyboard emojiKeyboard; private CustomEmojiPopupKeyboard emojiKeyboard;
private final View space; private final View space;
private final ImageButton addButton; private final ImageButton addButton;
private final ProgressBar progress;
private final EmojiReactionsAdapter adapter; private final EmojiReactionsAdapter adapter;
private final ListImageLoaderWrapper imgLoader; private final ListImageLoaderWrapper imgLoader;
@ -162,6 +165,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null); imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null);
list.setAdapter(adapter=new EmojiReactionsAdapter(this, imgLoader)); list.setAdapter(adapter=new EmojiReactionsAdapter(this, imgLoader));
addButton=findViewById(R.id.add_btn); addButton=findViewById(R.id.add_btn);
progress=findViewById(R.id.progress);
addButton.setOnClickListener(this::onReactClick); addButton.setOnClickListener(this::onReactClick);
space=findViewById(R.id.space); space=findViewById(R.id.space);
list.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)); list.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
@ -213,24 +217,36 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
private void addEmojiReaction(String emoji, Emoji info) { private void addEmojiReaction(String emoji, Emoji info) {
if(item.status.reactions.stream().filter(r->r.name.equals(emoji) && r.me).findAny().isPresent()) return; if(item.status.reactions.stream().filter(r->r.name.equals(emoji) && r.me).findAny().isPresent()) return;
progress.setVisibility(View.VISIBLE);
addButton.setClickable(false);
addButton.setAlpha(0.55f);
Runnable resetBtn=()->{
progress.setVisibility(View.GONE);
addButton.setClickable(true);
addButton.setAlpha(1f);
};
Account me=AccountSessionManager.get(item.accountID).self; Account me=AccountSessionManager.get(item.accountID).self;
EmojiReaction existing=null; EmojiReaction existing=null;
for(int i=0; i<item.status.reactions.size(); i++){ for(int i=0; i<item.status.reactions.size(); i++){
EmojiReaction r=item.status.reactions.get(i); EmojiReaction r=item.status.reactions.get(i);
if(r.name.equals(emoji)){ if(r.name.equals(emoji)){
existing=r; existing=r;
r.add(me);
adapter.notifyItemChanged(i);
break; break;
} }
} }
if(existing==null){ EmojiReaction finalExisting=existing;
item.status.reactions.add(0, info!=null ? EmojiReaction.of(info, me) : EmojiReaction.of(emoji, me)); item.createRequest(emoji, existing==null ? 1 : existing.count, false, null, ()->{
adapter.notifyItemRangeInserted(0, 1); resetBtn.run();
} if(finalExisting==null){
E.post(new StatusCountersUpdatedEvent(item.status, adapter.parentHolder)); item.status.reactions.add(0, info!=null ? EmojiReaction.of(info, me) : EmojiReaction.of(emoji, me));
item.createRequest(emoji, existing==null ? 1 : existing.count, false, null, ()->{}).exec(item.accountID); adapter.notifyItemRangeInserted(0, 1);
}else{
finalExisting.add(me);
adapter.notifyItemChanged(item.status.reactions.indexOf(finalExisting));
}
E.post(new StatusCountersUpdatedEvent(item.status, adapter.parentHolder));
}, resetBtn).exec(item.accountID);
} }
@Override @Override
@ -362,7 +378,7 @@ public class EmojiReactionsStatusDisplayItem extends StatusDisplayItem {
E.post(new StatusCountersUpdatedEvent(parent.status, adapter.parentHolder)); E.post(new StatusCountersUpdatedEvent(parent.status, adapter.parentHolder));
adapter.parentHolder.imgLoader.updateImages(); adapter.parentHolder.imgLoader.updateImages();
}).exec(parent.parentFragment.getAccountID()); }, null).exec(parent.parentFragment.getAccountID());
}); });
if (parent.parentFragment.isInstanceAkkoma()) { if (parent.parentFragment.isInstanceAkkoma()) {

View File

@ -9,17 +9,32 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<ImageButton <FrameLayout
android:id="@+id/add_btn" android:id="@+id/add_btn_wrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="2dp" android:layout_width="wrap_content"
android:minHeight="48dp" android:layout_marginStart="2dp">
android:minWidth="48dp" <ProgressBar
android:background="@drawable/bg_button_m3_tonal_circle" android:id="@+id/progress"
android:tooltipText="@string/sk_button_react" style="?android:progressBarStyleSmall"
android:contentDescription="@string/sk_button_react" android:layout_width="wrap_content"
android:src="@drawable/ic_fluent_add_24_filled" /> android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="10dp"
android:indeterminate="true"
android:outlineProvider="none"
android:visibility="gone"/>
<ImageButton
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:minWidth="48dp"
android:background="@drawable/bg_button_m3_tonal_circle"
android:tooltipText="@string/sk_button_react"
android:contentDescription="@string/sk_button_react"
android:src="@drawable/ic_fluent_add_24_filled" />
</FrameLayout>
<org.joinmastodon.android.ui.views.EmojiReactionsRecyclerView <org.joinmastodon.android.ui.views.EmojiReactionsRecyclerView
android:id="@+id/list" android:id="@+id/list"

View File

@ -3,7 +3,6 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
tools:ignore="RtlSymmetry"> tools:ignore="RtlSymmetry">
<ProgressBar <ProgressBar
android:id="@+id/progress" android:id="@+id/progress"