You can now follow and unfollow accounts. Revolutionary.

This commit is contained in:
Grishka 2022-02-13 02:33:41 +03:00
parent 0615aa34f2
commit 9232428232
5 changed files with 121 additions and 13 deletions

View File

@ -0,0 +1,11 @@
package org.joinmastodon.android.api.requests.accounts;
import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.Relationship;
public class SetAccountFollowed extends MastodonAPIRequest<Relationship>{
public SetAccountFollowed(String id, boolean followed){
super(HttpMethod.POST, "/accounts/"+id+"/"+(followed ? "follow" : "unfollow"), Relationship.class);
setRequestBody(new Object());
}
}

View File

@ -27,6 +27,7 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toolbar;
@ -35,6 +36,7 @@ import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships;
import org.joinmastodon.android.api.requests.accounts.GetAccountStatuses;
import org.joinmastodon.android.api.requests.accounts.GetOwnAccount;
import org.joinmastodon.android.api.requests.accounts.SetAccountFollowed;
import org.joinmastodon.android.api.requests.accounts.UpdateAccountCredentials;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.Account;
@ -48,6 +50,7 @@ import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.views.CoverImageView;
import org.joinmastodon.android.ui.views.NestedRecyclerScrollView;
import org.joinmastodon.android.ui.views.ProgressBarButton;
import org.parceler.Parcels;
import java.time.LocalDateTime;
@ -80,7 +83,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
private CoverImageView cover;
private View avatarBorder;
private TextView name, username, bio, followersCount, followersLabel, followingCount, followingLabel, postsCount, postsLabel;
private Button actionButton;
private ProgressBarButton actionButton;
private ViewPager2 pager;
private NestedRecyclerScrollView scrollView;
private AccountTimelineFragment postsFragment, postsWithRepliesFragment, mediaFragment;
@ -91,6 +94,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
private float titleTransY;
private View postsBtn, followersBtn, followingBtn;
private EditText nameEdit, bioEdit;
private ProgressBar actionProgress;
private Account account;
private String accountID;
@ -141,6 +145,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
refreshLayout=content.findViewById(R.id.refresh_layout);
nameEdit=content.findViewById(R.id.name_edit);
bioEdit=content.findViewById(R.id.bio_edit);
actionProgress=content.findViewById(R.id.action_progress);
avatar.setOutlineProvider(new ViewOutlineProvider(){
@Override
@ -389,9 +394,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
@Override
public void onSuccess(List<Relationship> result){
relationship=result.get(0);
invalidateOptionsMenu();
actionButton.setVisibility(View.VISIBLE);
actionButton.setText(relationship.following ? R.string.button_following : R.string.button_follow);
updateRelationship();
}
@Override
@ -402,6 +405,12 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
.exec(accountID);
}
private void updateRelationship(){
invalidateOptionsMenu();
actionButton.setVisibility(View.VISIBLE);
actionButton.setText(relationship.following ? R.string.button_following : R.string.button_follow);
}
private void onScrollChanged(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY){
int topBarsH=getToolbar().getHeight()+statusBarHeight;
if(scrollY>avatar.getTop()-topBarsH){
@ -453,23 +462,33 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
loadAccountInfoAndEnterEditMode();
else
saveAndExitEditMode();
}else{
toggleFollowing();
}
}
private void setActionProgressVisible(boolean visible){
actionButton.setTextVisible(!visible);
actionProgress.setVisibility(visible ? View.VISIBLE : View.GONE);
actionButton.setClickable(!visible);
}
private void loadAccountInfoAndEnterEditMode(){
setActionProgressVisible(true);
new GetOwnAccount()
.setCallback(new Callback<>(){
@Override
public void onSuccess(Account result){
enterEditMode(result);
setActionProgressVisible(false);
}
@Override
public void onError(ErrorResponse error){
error.showToast(getActivity());
setActionProgressVisible(false);
}
})
.wrapProgress(getActivity(), R.string.loading, true)
.exec(accountID);
}
@ -561,6 +580,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
private void saveAndExitEditMode(){
if(!isInEditMode)
throw new IllegalStateException();
setActionProgressVisible(true);
new UpdateAccountCredentials(nameEdit.getText().toString(), bioEdit.getText().toString(), editNewAvatar, editNewCover, aboutFragment.getFields())
.setCallback(new Callback<>(){
@Override
@ -568,14 +588,35 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
account=result;
AccountSessionManager.getInstance().updateAccountInfo(accountID, account);
exitEditMode();
setActionProgressVisible(false);
}
@Override
public void onError(ErrorResponse error){
error.showToast(getActivity());
setActionProgressVisible(false);
}
})
.exec(accountID);
}
private void toggleFollowing(){
setActionProgressVisible(true);
new SetAccountFollowed(account.id, !relationship.following)
.setCallback(new Callback<>(){
@Override
public void onSuccess(Relationship result){
relationship=result;
updateRelationship();
setActionProgressVisible(false);
}
@Override
public void onError(ErrorResponse error){
error.showToast(getActivity());
setActionProgressVisible(false);
}
})
.wrapProgress(getActivity(), R.string.saving, false)
.exec(accountID);
}

View File

@ -0,0 +1,38 @@
package org.joinmastodon.android.ui.views;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;
public class ProgressBarButton extends Button{
private boolean textVisible=true;
public ProgressBarButton(Context context){
super(context);
}
public ProgressBarButton(Context context, AttributeSet attrs){
super(context, attrs);
}
public ProgressBarButton(Context context, AttributeSet attrs, int defStyleAttr){
super(context, attrs, defStyleAttr);
}
public void setTextVisible(boolean textVisible){
this.textVisible=textVisible;
invalidate();
}
public boolean isTextVisible(){
return textVisible;
}
@Override
protected void onDraw(Canvas canvas){
if(textVisible){
super.onDraw(canvas);
}
}
}

View File

@ -125,14 +125,31 @@
tools:text="following" />
</LinearLayout>
<Button
android:id="@+id/profile_action_btn"
<FrameLayout
android:id="@+id/profile_action_btn_wrap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@id/following_btn"
android:layout_margin="16dp"
tools:text="Edit Profile"/>
android:padding="16dp"
android:clipToPadding="false">
<org.joinmastodon.android.ui.views.ProgressBarButton
android:id="@+id/profile_action_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Edit Profile"/>
<ProgressBar
android:id="@+id/action_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
style="?android:progressBarStyleSmall"
android:elevation="10dp"
android:outlineProvider="none"
android:indeterminateTint="@color/text_button"
android:visibility="gone"/>
</FrameLayout>
<TextView
android:id="@+id/name"
@ -142,7 +159,7 @@
android:layout_alignParentStart="true"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toStartOf="@id/profile_action_btn"
android:layout_toStartOf="@id/profile_action_btn_wrap"
android:textAppearance="@style/m3_headline_small"
tools:text="Eugen" />
@ -152,7 +169,7 @@
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginStart="16dp"
android:layout_toStartOf="@id/profile_action_btn"
android:layout_toStartOf="@id/profile_action_btn_wrap"
android:textAppearance="@style/m3_title_medium"
android:textColor="@color/light_ui_action_button"
tools:text="\@Gargron"/>
@ -176,7 +193,7 @@
android:layout_alignParentStart="true"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toStartOf="@id/profile_action_btn"
android:layout_toStartOf="@id/profile_action_btn_wrap"
android:textAppearance="@style/m3_body_large"
android:background="@drawable/edit_text_border"
android:inputType="textPersonName|textCapWords"

View File

@ -34,6 +34,7 @@
<color name="actionbar_bg">#FAFBFC</color>
<color name="navigation_bar_bg">#000</color>
<color name="highlight_over_dark">#80FFFFFF</color>
<color name="text_button">@color/gray_50</color>
<color name="favorite_selected">#FF9F0A</color>
<color name="boost_selected">#79BD9A</color>