Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
progress indicator for emoji reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
sk22 committed Aug 23, 2023
1 parent 4920bf6 commit 647e3e5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.joinmastodon.android.ui.displayitems;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
Expand All @@ -10,13 +11,13 @@
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.joinmastodon.android.E;
import org.joinmastodon.android.R;
Expand All @@ -40,6 +41,7 @@
import org.joinmastodon.android.ui.CustomEmojiPopupKeyboard;
import org.joinmastodon.android.ui.utils.TextDrawable;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.views.ProgressBarButton;

import me.grishka.appkit.Nav;
import me.grishka.appkit.api.Callback;
Expand Down Expand Up @@ -95,27 +97,50 @@ private void updateHidden(){
hidden=status.reactions.isEmpty() && hideAdd;
}

private MastodonAPIRequest<?> createRequest(String name, boolean delete, Runnable cb){
// borrowed from ProfileFragment
private void setActionProgressVisible(Holder.EmojiReactionViewHolder vh, boolean visible){
if(vh==null) return;
vh.progress.setVisibility(visible ? View.VISIBLE : View.GONE);
if(visible)
vh.progress.setIndeterminateTintList(vh.btn.getTextColors());
vh.btn.setClickable(!visible);
}

private MastodonAPIRequest<?> createRequest(String name, int count, boolean delete, Holder.EmojiReactionViewHolder vh, Runnable cb){
setActionProgressVisible(vh, true);
boolean ak=parentFragment.isInstanceAkkoma();
boolean keepSpinning=delete && count == 1;
if(forAnnouncement){
MastodonAPIRequest<Object> req=delete
? new DeleteAnnouncementReaction(status.id, name)
: new AddAnnouncementReaction(status.id, name);
return req.setCallback(new Callback<>(){
@Override
public void onSuccess(Object result){ cb.run(); }
public void onSuccess(Object result){
if(!keepSpinning) setActionProgressVisible(vh, false);
cb.run();
}
@Override
public void onError(ErrorResponse error){ error.showToast(parentFragment.getContext()); }
public void onError(ErrorResponse error){
setActionProgressVisible(vh, false);
error.showToast(parentFragment.getContext());
}
});
}else{
MastodonAPIRequest<Status> req=delete
? (ak ? new PleromaDeleteStatusReaction(status.id, name) : new DeleteStatusReaction(status.id, name))
: (ak ? new PleromaAddStatusReaction(status.id, name) : new AddStatusReaction(status.id, name));
return req.setCallback(new Callback<>(){
@Override
public void onSuccess(Status result){ cb.run(); }
public void onSuccess(Status result){
if(!keepSpinning) setActionProgressVisible(vh, false);
cb.run();
}
@Override
public void onError(ErrorResponse error){ error.showToast(parentFragment.getContext()); }
public void onError(ErrorResponse error){
setActionProgressVisible(vh, false);
error.showToast(parentFragment.getContext());
}
});
}
}
Expand Down Expand Up @@ -188,25 +213,24 @@ public void onEmojiSelected(String emoji){

private void addEmojiReaction(String emoji, Emoji info) {
if(item.status.reactions.stream().filter(r->r.name.equals(emoji) && r.me).findAny().isPresent()) return;

item.createRequest(emoji, false, ()->{
Account me=AccountSessionManager.get(item.accountID).self;
boolean found=false;
for(int i=0; i<item.status.reactions.size(); i++){
EmojiReaction r=item.status.reactions.get(i);
if(r.name.equals(emoji)){
found=true;
r.add(me);
adapter.notifyItemChanged(i);
break;
}
}
if(!found){
item.status.reactions.add(info!=null ? EmojiReaction.of(info, me) : EmojiReaction.of(emoji, me));
adapter.notifyItemRangeInserted(item.status.reactions.size() - 1, 1);

Account me=AccountSessionManager.get(item.accountID).self;
EmojiReaction existing=null;
for(int i=0; i<item.status.reactions.size(); i++){
EmojiReaction r=item.status.reactions.get(i);
if(r.name.equals(emoji)){
existing=r;
r.add(me);
adapter.notifyItemChanged(i);
break;
}
E.post(new StatusCountersUpdatedEvent(item.status, adapter.parentHolder));
}).exec(item.accountID);
}
if(existing==null){
item.status.reactions.add(0, info!=null ? EmojiReaction.of(info, me) : EmojiReaction.of(emoji, me));
adapter.notifyItemRangeInserted(0, 1);
}
E.post(new StatusCountersUpdatedEvent(item.status, adapter.parentHolder));
item.createRequest(emoji, existing==null ? 1 : existing.count, false, null, ()->{}).exec(item.accountID);
}

@Override
Expand Down Expand Up @@ -252,14 +276,14 @@ public EmojiReactionsAdapter(Holder parentHolder, ListImageLoaderWrapper imgLoad
@NonNull
@Override
public EmojiReactionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
FrameLayout frame=new FrameLayout(parent.getContext());
frame.setPaddingRelative(0, 0, V.dp(8), 0);
Button btn=new Button(parent.getContext(), null, 0, R.style.Widget_Mastodon_M3_Button_Outlined_Icon);
btn.setCompoundDrawableTintList(null);
btn.setBackgroundResource(R.drawable.bg_button_m3_tonal);
btn.setCompoundDrawables(item.placeholder, null, null, null);
frame.addView(btn);
return new EmojiReactionViewHolder(frame);
// FrameLayout frame=new FrameLayout(parent.getContext());
// frame.setPaddingRelative(0, 0, V.dp(8), 0);
// Button btn=new Button(parent.getContext(), null, 0, R.style.Widget_Mastodon_M3_Button_Outlined_Icon);
// btn.setCompoundDrawableTintList(null);
// btn.setBackgroundResource(R.drawable.bg_button_m3_tonal);
// btn.setCompoundDrawables(item.placeholder, null, null, null);
// frame.addView(btn);
return new EmojiReactionViewHolder(parent.getContext(), list);
}

@Override
Expand All @@ -285,11 +309,13 @@ public ImageLoaderRequest getImageRequest(int position, int image){
}

private static class EmojiReactionViewHolder extends BindableViewHolder<Pair<EmojiReactionsStatusDisplayItem, EmojiReaction>> implements ImageLoaderViewHolder{
private final Button btn;
private final ProgressBarButton btn;
private final ProgressBar progress;

public EmojiReactionViewHolder(@NonNull View itemView){
super(itemView);
btn=(Button) ((FrameLayout) itemView).getChildAt(0);
public EmojiReactionViewHolder(Context context, RecyclerView list){
super(context, R.layout.item_emoji_reaction, list);
btn=findViewById(R.id.btn);
progress=findViewById(R.id.progress);
}

@Override
Expand All @@ -306,6 +332,7 @@ public void clearImage(int index){

@Override
public void onBind(Pair<EmojiReactionsStatusDisplayItem, EmojiReaction> item){
item.first.setActionProgressVisible(this, false);
EmojiReactionsStatusDisplayItem parent=item.first;
EmojiReaction reaction=item.second;
btn.setText(UiUtils.abbreviateNumber(reaction.count));
Expand All @@ -322,9 +349,8 @@ public void onBind(Pair<EmojiReactionsStatusDisplayItem, EmojiReaction> item){
btn.setSelected(reaction.me);
btn.setOnClickListener(e->{
boolean deleting=reaction.me;
parent.createRequest(reaction.name, deleting, ()->{
parent.createRequest(reaction.name, reaction.count, deleting, this, ()->{
EmojiReactionsAdapter adapter = (EmojiReactionsAdapter) getBindingAdapter();

for(int i=0; i<parent.status.reactions.size(); i++){
EmojiReaction r=parent.status.reactions.get(i);
if(!r.name.equals(reaction.name)) continue;
Expand Down
27 changes: 27 additions & 0 deletions mastodon/src/main/res/layout/item_emoji_reaction.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:paddingEnd="8dp"
tools:ignore="RtlSymmetry">
<ProgressBar
android:id="@+id/progress"
style="?android:progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="10dp"
android:indeterminate="true"
android:outlineProvider="none"
android:visibility="gone"/>
<org.joinmastodon.android.ui.views.ProgressBarButton
android:id="@+id/btn"
style="@style/Widget.Mastodon.M3.Button.Outlined.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTint="@null"
android:drawableStart="@drawable/image_placeholder"
android:background="@drawable/bg_button_m3_tonal"/>
</FrameLayout>

0 comments on commit 647e3e5

Please sign in to comment.