Toot actions

This commit is contained in:
Grishka 2022-02-03 09:00:56 +03:00
parent 102fbeee1a
commit a1dee1fc88
18 changed files with 284 additions and 24 deletions

View File

@ -0,0 +1,99 @@
package org.joinmastodon.android.api;
import android.os.Looper;
import org.joinmastodon.android.E;
import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.api.requests.statuses.SetStatusFavorited;
import org.joinmastodon.android.api.requests.statuses.SetStatusReblogged;
import org.joinmastodon.android.events.StatusCountersUpdatedEvent;
import org.joinmastodon.android.model.Status;
import java.util.HashMap;
import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
public class StatusInteractionController{
private final String accountID;
private final HashMap<String, SetStatusFavorited> runningFavoriteRequests=new HashMap<>();
private final HashMap<String, SetStatusReblogged> runningReblogRequests=new HashMap<>();
public StatusInteractionController(String accountID){
this.accountID=accountID;
}
public void setFavorited(Status status, boolean favorited){
if(!Looper.getMainLooper().isCurrentThread())
throw new IllegalStateException("Can only be called from main thread");
SetStatusFavorited current=runningFavoriteRequests.remove(status.id);
if(current!=null){
current.cancel();
}
SetStatusFavorited req=(SetStatusFavorited) new SetStatusFavorited(status.id, favorited)
.setCallback(new Callback<>(){
@Override
public void onSuccess(Status result){
runningFavoriteRequests.remove(status.id);
E.post(new StatusCountersUpdatedEvent(result));
}
@Override
public void onError(ErrorResponse error){
runningFavoriteRequests.remove(status.id);
error.showToast(MastodonApp.context);
status.favourited=!favorited;
if(favorited)
status.favouritesCount--;
else
status.favouritesCount++;
E.post(new StatusCountersUpdatedEvent(status));
}
})
.exec(accountID);
runningFavoriteRequests.put(status.id, req);
status.favourited=favorited;
if(favorited)
status.favouritesCount++;
else
status.favouritesCount--;
}
public void setReblogged(Status status, boolean reblogged){
if(!Looper.getMainLooper().isCurrentThread())
throw new IllegalStateException("Can only be called from main thread");
SetStatusReblogged current=runningReblogRequests.remove(status.id);
if(current!=null){
current.cancel();
}
SetStatusReblogged req=(SetStatusReblogged) new SetStatusReblogged(status.id, reblogged)
.setCallback(new Callback<>(){
@Override
public void onSuccess(Status result){
runningReblogRequests.remove(status.id);
E.post(new StatusCountersUpdatedEvent(result));
}
@Override
public void onError(ErrorResponse error){
runningReblogRequests.remove(status.id);
error.showToast(MastodonApp.context);
status.reblogged=!reblogged;
if(reblogged)
status.reblogsCount--;
else
status.reblogsCount++;
E.post(new StatusCountersUpdatedEvent(status));
}
})
.exec(accountID);
runningReblogRequests.put(status.id, req);
status.reblogged=reblogged;
if(reblogged)
status.reblogsCount++;
else
status.reblogsCount--;
}
}

View File

@ -0,0 +1,11 @@
package org.joinmastodon.android.api.requests.statuses;
import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.Status;
public class SetStatusFavorited extends MastodonAPIRequest<Status>{
public SetStatusFavorited(String id, boolean favorited){
super(HttpMethod.POST, "/statuses/"+id+"/"+(favorited ? "favourite" : "unfavourite"), Status.class);
setRequestBody(new Object());
}
}

View File

@ -0,0 +1,11 @@
package org.joinmastodon.android.api.requests.statuses;
import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.Status;
public class SetStatusReblogged extends MastodonAPIRequest<Status>{
public SetStatusReblogged(String id, boolean reblogged){
super(HttpMethod.POST, "/statuses/"+id+"/"+(reblogged ? "reblog" : "unreblog"), Status.class);
setRequestBody(new Object());
}
}

View File

@ -1,6 +1,7 @@
package org.joinmastodon.android.api.session;
import org.joinmastodon.android.api.MastodonAPIController;
import org.joinmastodon.android.api.StatusInteractionController;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Application;
import org.joinmastodon.android.model.Token;
@ -13,6 +14,7 @@ public class AccountSession{
public Application app;
public long infoLastUpdated;
private transient MastodonAPIController apiController;
private transient StatusInteractionController statusInteractionController;
AccountSession(Token token, Account self, Application app, String domain, int tootCharLimit){
this.token=token;
@ -34,4 +36,10 @@ public class AccountSession{
apiController=new MastodonAPIController(this);
return apiController;
}
public StatusInteractionController getStatusInteractionController(){
if(statusInteractionController==null)
statusInteractionController=new StatusInteractionController(getID());
return statusInteractionController;
}
}

View File

@ -0,0 +1,17 @@
package org.joinmastodon.android.events;
import org.joinmastodon.android.model.Status;
public class StatusCountersUpdatedEvent{
public String id;
public int favorites, reblogs;
public boolean favorited, reblogged;
public StatusCountersUpdatedEvent(Status s){
id=s.id;
favorites=s.favouritesCount;
reblogs=s.reblogsCount;
favorited=s.favourited;
reblogged=s.reblogged;
}
}

View File

@ -79,18 +79,6 @@ public class HomeTimelineFragment extends StatusListFragment{
return true;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
E.register(this);
}
@Override
public void onDestroy(){
super.onDestroy();
E.unregister(this);
}
@Subscribe
public void onStatusCreated(StatusCreatedEvent ev){
prependItems(Collections.singletonList(ev.status));

View File

@ -1,12 +1,56 @@
package org.joinmastodon.android.fragments;
import android.os.Bundle;
import com.squareup.otto.Subscribe;
import org.joinmastodon.android.E;
import org.joinmastodon.android.events.StatusCountersUpdatedEvent;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.ui.displayitems.FooterStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.StatusDisplayItem;
import java.util.List;
import androidx.recyclerview.widget.RecyclerView;
public abstract class StatusListFragment extends BaseStatusListFragment<Status>{
protected List<StatusDisplayItem> buildDisplayItems(Status s){
return StatusDisplayItem.buildItems(this, s, accountID, s);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
E.register(this);
}
@Override
public void onDestroy(){
super.onDestroy();
E.unregister(this);
}
@Subscribe
public void onStatusCountersUpdated(StatusCountersUpdatedEvent ev){
for(Status s:data){
if(s.getContentStatus().id.equals(ev.id)){
s.update(ev);
for(int i=0;i<list.getChildCount();i++){
RecyclerView.ViewHolder holder=list.getChildViewHolder(list.getChildAt(i));
if(holder instanceof FooterStatusDisplayItem.Holder && ((FooterStatusDisplayItem.Holder) holder).getItem().status==s.getContentStatus()){
((FooterStatusDisplayItem.Holder) holder).rebind();
return;
}
}
return;
}
}
for(Status s:preloadedData){
if(s.id.equals(ev.id)){
s.update(ev);
return;
}
}
}
}

View File

@ -2,6 +2,7 @@ package org.joinmastodon.android.model;
import org.joinmastodon.android.api.ObjectValidationException;
import org.joinmastodon.android.api.RequiredField;
import org.joinmastodon.android.events.StatusCountersUpdatedEvent;
import java.time.Instant;
import java.util.List;
@ -111,4 +112,15 @@ public class Status extends BaseModel implements DisplayItemsParent{
public String getID(){
return id;
}
public void update(StatusCountersUpdatedEvent ev){
favouritesCount=ev.favorites;
reblogsCount=ev.reblogs;
favourited=ev.favorited;
reblogged=ev.reblogged;
}
public Status getContentStatus(){
return reblog!=null ? reblog : this;
}
}

View File

@ -1,14 +1,18 @@
package org.joinmastodon.android.ui.displayitems;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.joinmastodon.android.R;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.model.StatusPrivacy;
import org.joinmastodon.android.ui.utils.UiUtils;
import java.text.DecimalFormat;
@ -17,7 +21,7 @@ import me.grishka.appkit.utils.BindableViewHolder;
import me.grishka.appkit.utils.V;
public class FooterStatusDisplayItem extends StatusDisplayItem{
private final Status status;
public final Status status;
private final String accountID;
public FooterStatusDisplayItem(String parentID, Status status, String accountID){
@ -43,9 +47,13 @@ public class FooterStatusDisplayItem extends StatusDisplayItem{
share=findViewById(R.id.share);
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.N){
UiUtils.fixCompoundDrawableTintOnAndroid6(reply, R.color.text_secondary);
UiUtils.fixCompoundDrawableTintOnAndroid6(boost, R.color.text_secondary);
UiUtils.fixCompoundDrawableTintOnAndroid6(favorite, R.color.text_secondary);
UiUtils.fixCompoundDrawableTintOnAndroid6(boost, R.color.boost_icon);
UiUtils.fixCompoundDrawableTintOnAndroid6(favorite, R.color.favorite_icon);
}
reply.setOnClickListener(this::onReplyClick);
boost.setOnClickListener(this::onBoostClick);
favorite.setOnClickListener(this::onFavoriteClick);
share.setOnClickListener(this::onShareClick);
}
@Override
@ -53,6 +61,10 @@ public class FooterStatusDisplayItem extends StatusDisplayItem{
bindButton(reply, item.status.repliesCount);
bindButton(boost, item.status.reblogsCount);
bindButton(favorite, item.status.favouritesCount);
boost.setSelected(item.status.reblogged);
favorite.setSelected(item.status.favourited);
boost.setEnabled(item.status.visibility==StatusPrivacy.PUBLIC || item.status.visibility==StatusPrivacy.UNLISTED
|| (item.status.visibility==StatusPrivacy.PRIVATE && item.status.account.id.equals(AccountSessionManager.getInstance().getAccount(item.accountID).self.id)));
}
private void bindButton(TextView btn, int count){
@ -64,5 +76,28 @@ public class FooterStatusDisplayItem extends StatusDisplayItem{
btn.setCompoundDrawablePadding(0);
}
}
private void onReplyClick(View v){
}
private void onBoostClick(View v){
AccountSessionManager.getInstance().getAccount(item.accountID).getStatusInteractionController().setReblogged(item.status, !item.status.reblogged);
boost.setSelected(item.status.reblogged);
bindButton(boost, item.status.reblogsCount);
}
private void onFavoriteClick(View v){
AccountSessionManager.getInstance().getAccount(item.accountID).getStatusInteractionController().setFavorited(item.status, !item.status.favourited);
favorite.setSelected(item.status.favourited);
bindButton(favorite, item.status.favouritesCount);
}
private void onShareClick(View v){
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, item.status.url);
v.getContext().startActivity(Intent.createChooser(intent, v.getContext().getString(R.string.share_toot_title)));
}
}
}

View File

@ -47,7 +47,7 @@ public abstract class StatusDisplayItem{
public static ArrayList<StatusDisplayItem> buildItems(Fragment fragment, Status status, String accountID, DisplayItemsParent parentObject){
String parentID=parentObject.getID();
ArrayList<StatusDisplayItem> items=new ArrayList<>();
Status statusForContent=status.reblog==null ? status : status.reblog;
Status statusForContent=status.getContentStatus();
if(status.reblog!=null){
items.add(new ReblogOrReplyLineStatusDisplayItem(parentID, fragment.getString(R.string.user_boosted, status.account.displayName)));
}
@ -67,7 +67,7 @@ public abstract class StatusDisplayItem{
photoIndex++;
}
}
items.add(new FooterStatusDisplayItem(parentID, status, accountID));
items.add(new FooterStatusDisplayItem(parentID, statusForContent, accountID));
return items;
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/boost_selected" android:state_selected="true"/>
<item android:color="@color/text_secondary" android:state_enabled="true"/>
<item android:color="@color/text_secondary_alpha50"/>
</selector>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/favorite_selected" android:state_selected="true"/>
<item android:color="@color/text_secondary"/>
</selector>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/ic_fluent_arrow_repeat_all_24_regular"/>
<item android:drawable="@drawable/ic_fluent_arrow_repeat_all_off_24_regular"/>
</selector>

View File

@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:pathData="M3.196 2.147L3.28 2.22l18.5 18.5c0.293 0.293 0.293 0.767 0 1.06-0.266 0.267-0.683 0.29-0.976 0.073L20.72 21.78l-3.509-3.508c-0.436 0.12-0.89 0.194-1.358 0.219L15.5 18.5H8.564l1.9 1.9 0.066 0.076c0.224 0.294 0.202 0.715-0.067 0.984-0.266 0.266-0.683 0.29-0.976 0.072L9.403 21.46 6.22 18.278 6.154 18.2c-0.202-0.265-0.204-0.632-0.006-0.899l0.073-0.085 3.182-3.182 0.076-0.067c0.265-0.201 0.633-0.203 0.9-0.006l0.084 0.073 0.068 0.077c0.2 0.264 0.203 0.632 0.005 0.899l-0.073 0.085L8.558 17H15.5c0.142 0 0.283-0.006 0.421-0.017L6.402 7.46C4.687 8.254 3.5 9.988 3.5 12c0 1.296 0.493 2.477 1.302 3.365C4.925 15.497 5 15.675 5 15.872c0 0.414-0.336 0.75-0.75 0.75-0.222 0-0.421-0.097-0.559-0.25C2.641 15.22 2 13.684 2 12c0-2.421 1.324-4.533 3.287-5.652L2.22 3.28c-0.293-0.293-0.293-0.767 0-1.06 0.266-0.267 0.683-0.29 0.976-0.073zM19.75 7.378c0.22 0 0.416 0.094 0.553 0.244C21.357 8.775 22 10.312 22 12c0 2.057-0.955 3.89-2.446 5.081l-1.069-1.07C19.708 15.1 20.5 13.644 20.5 12c0-1.306-0.5-2.495-1.32-3.386-0.112-0.13-0.18-0.3-0.18-0.486 0-0.414 0.336-0.75 0.75-0.75zm-5.217-4.976L14.61 2.47l3.182 3.182c0.269 0.268 0.291 0.69 0.067 0.984l-0.067 0.076-3.182 3.182c-0.293 0.293-0.768 0.293-1.06 0-0.269-0.268-0.291-0.69-0.068-0.984l0.068-0.076L15.38 7H9.473l-1.48-1.48c0.091-0.007 0.182-0.013 0.274-0.016L8.5 5.5h7.021L13.55 3.53c-0.268-0.268-0.29-0.69-0.067-0.983L13.55 2.47c0.268-0.269 0.69-0.291 0.983-0.068z" android:fillColor="@color/fluent_default_icon_tint"/>
</vector>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!--~ Copyright (c) 2022. ~ Microsoft Corporation. All rights reserved.-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_fluent_star_24_filled" android:state_activated="true"/>
<item android:drawable="@drawable/ic_fluent_star_24_filled" android:state_checked="true"/>
<item android:drawable="@drawable/ic_fluent_star_24_filled" android:state_selected="true"/>
<item android:drawable="@drawable/ic_fluent_star_24_regular"/>
</selector>

View File

@ -12,7 +12,7 @@
android:layout_width="wrap_content"
android:layout_height="24dp"
android:minWidth="56dp">
<CheckedTextView
<TextView
android:id="@+id/reply"
android:layout_width="wrap_content"
android:layout_height="24dp"
@ -34,14 +34,15 @@
android:layout_width="wrap_content"
android:layout_height="24dp"
android:minWidth="56dp">
<CheckedTextView
<TextView
android:id="@+id/boost"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_gravity="center_horizontal"
android:drawableStart="@drawable/ic_fluent_arrow_repeat_all_24_regular"
android:drawableStart="@drawable/ic_boost"
android:drawablePadding="8dp"
android:drawableTint="@color/text_secondary"
android:drawableTint="@color/boost_icon"
android:textColor="@color/boost_icon"
android:gravity="center_vertical"
android:textAppearance="@style/m3_label_large"
tools:text="123"/>
@ -56,14 +57,15 @@
android:layout_width="wrap_content"
android:layout_height="24dp"
android:minWidth="56dp">
<CheckedTextView
<TextView
android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_gravity="center_horizontal"
android:drawableStart="@drawable/ic_fluent_star_24_regular"
android:drawableStart="@drawable/ic_fluent_star_24_selector"
android:drawablePadding="8dp"
android:drawableTint="@color/text_secondary"
android:drawableTint="@color/favorite_icon"
android:textColor="@color/favorite_icon"
android:gravity="center_vertical"
android:textAppearance="@style/m3_label_large"
tools:text="123"/>

View File

@ -20,4 +20,8 @@
<color name="text_secondary">@color/gray_500</color>
<color name="secondary">#E9EDF2</color>
<color name="base">#282C37</color>
<color name="text_secondary_alpha50">#80667085</color>
<color name="favorite_selected">#FF9F0A</color>
<color name="boost_selected">#79BD9A</color>
</resources>

View File

@ -25,4 +25,6 @@
<string name="time_minutes">%dm</string>
<string name="time_hours">%dh</string>
<string name="time_days">%dd</string>
<string name="share_toot_title">Share toot</string>
</resources>