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

Commit

Permalink
implement editing lists
Browse files Browse the repository at this point in the history
re: #30
  • Loading branch information
sk22 committed Jan 10, 2023
1 parent 54c29fd commit d47797b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@
import android.widget.ImageButton;

import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.lists.CreateList;
import org.joinmastodon.android.api.requests.lists.UpdateList;
import org.joinmastodon.android.api.requests.timelines.GetListTimeline;
import org.joinmastodon.android.model.ListTimeline;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.views.ListTimelineEditor;

import java.util.List;

import me.grishka.appkit.Nav;
import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
import me.grishka.appkit.api.SimpleCallback;
import me.grishka.appkit.utils.V;


public class ListTimelineFragment extends StatusListFragment {
private String listID;
private String listTitle;
private ListTimeline.RepliesPolicy repliesPolicy;
private ImageButton fab;

public ListTimelineFragment() {
Expand All @@ -33,8 +41,11 @@ public ListTimelineFragment() {
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
listID=getArguments().getString("listID");
listTitle=getArguments().getString("listTitle");
Bundle args = getArguments();
listID = args.getString("listID");
listTitle = args.getString("listTitle");
repliesPolicy = ListTimeline.RepliesPolicy.values()[args.getInt("repliesPolicy", 0)];

setTitle(listTitle);
setHasOptionsMenu(true);
}
Expand All @@ -47,12 +58,37 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Bundle args = new Bundle();
args.putString("listID", listID);
if (item.getItemId() == R.id.edit) {

ListTimelineEditor editor = new ListTimelineEditor(getContext());
editor.applyList(listTitle, repliesPolicy);
new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.sk_edit_list_title)
.setView(editor)
.setPositiveButton(R.string.save, (d, which) -> {
new UpdateList(listID, editor.getTitle(), editor.getRepliesPolicy()).setCallback(new Callback<>() {
@Override
public void onSuccess(ListTimeline list) {
setTitle(list.title);
listTitle = list.title;
repliesPolicy = list.repliesPolicy;
args.putString("listTitle", listTitle);
args.putInt("repliesPolicy", repliesPolicy.ordinal());
setResult(true, args);
}

@Override
public void onError(ErrorResponse error) {
error.showToast(getContext());
}
}).exec(accountID);
})
.setNegativeButton(R.string.cancel, (d, which) -> {})
.show();
} else if (item.getItemId() == R.id.delete) {
UiUtils.confirmDeleteList(getActivity(), accountID, listID, () -> {
Bundle args = new Bundle();
args.putString("listID", listID);
args.putBoolean("deleted", true);
setResult(true, args);
Nav.finish(this);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import me.grishka.appkit.views.UsableRecyclerView;

public class ListTimelinesFragment extends BaseRecyclerFragment<ListTimeline> implements ScrollableToTop {
private static final int LIST_DELETED_RESULT = 987;
private static final int LIST_CHANGED_RESULT = 987;

private String accountId;
private String profileAccountId;
Expand Down Expand Up @@ -147,14 +147,20 @@ public void onSuccess(List<ListTimeline> allLists) {
}

@Override
public void onFragmentResult(int reqCode, boolean listDeleted, Bundle result){
if (reqCode == LIST_DELETED_RESULT && listDeleted) {
public void onFragmentResult(int reqCode, boolean listChanged, Bundle result){
if (reqCode == LIST_CHANGED_RESULT && listChanged) {
String listID = result.getString("listID");

for (int i = 0; i < data.size(); i++) {
if (data.get(i).id.equals(listID)) {
data.remove(i);
adapter.notifyItemRemoved(i);
ListTimeline item = data.get(i);
if (item.id.equals(listID)) {
if (result.getBoolean("deleted")) {
data.remove(i);
adapter.notifyItemRemoved(i);
} else {
item.title = result.getString("listTitle", item.title);
item.repliesPolicy = ListTimeline.RepliesPolicy.values()[result.getInt("repliesPolicy")];
adapter.notifyItemChanged(i);
}
break;
}
}
Expand Down Expand Up @@ -223,7 +229,8 @@ public void onClick() {
args.putString("account", accountId);
args.putString("listID", item.id);
args.putString("listTitle", item.title);
Nav.goForResult(getActivity(), ListTimelineFragment.class, args, LIST_DELETED_RESULT, ListTimelinesFragment.this);
args.putInt("repliesPolicy", item.repliesPolicy.ordinal());
Nav.goForResult(getActivity(), ListTimelineFragment.class, args, LIST_CHANGED_RESULT, ListTimelinesFragment.this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

public class ListTimelineEditor extends LinearLayout {
private ListTimeline.RepliesPolicy policy = null;
private TextInputFrameLayout input;
private Button button;
private final TextInputFrameLayout input;
private final Button button;

@SuppressLint("ClickableViewAccessibility")
public ListTimelineEditor(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Expand All @@ -37,10 +37,9 @@ public ListTimelineEditor(Context context, AttributeSet attrs, int defStyleAttr,
setRepliesPolicy(ListTimeline.RepliesPolicy.LIST);
}

public void applyList(ListTimeline list) {
policy = list.repliesPolicy;
input.getEditText().setText(list.title);
setRepliesPolicy(list.repliesPolicy);
public void applyList(String title, ListTimeline.RepliesPolicy policy) {
input.getEditText().setText(title);
setRepliesPolicy(policy);
}

public String getTitle() {
Expand Down
10 changes: 4 additions & 6 deletions mastodon/src/main/res/layout/list_timeline_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.joinmastodon.android.ui.views.AutoOrientationLinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="24dp">
<TextView
android:id="@+id/text"
android:text="@string/sk_list_replies_policy"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:paddingVertical="8dp"
android:singleLine="true"
android:ellipsize="none"
android:gravity="center_vertical"
android:textColor="?android:textColorPrimary"
android:textSize="16sp" />
Expand All @@ -25,11 +25,9 @@
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_weight="0"
android:maxWidth="140dp"
android:background="@drawable/bg_inline_button"
android:elevation="0dp"
android:ellipsize="middle"
android:ellipsize="none"
android:fontFamily="sans-serif-medium"
android:singleLine="true"
android:stateListAnimator="@null"
Expand Down
7 changes: 4 additions & 3 deletions mastodon/src/main/res/values/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@
<string name="sk_create_list_title">Create list</string>
<string name="sk_list_name_hint">List name</string>
<string name="sk_list_replies_policy">Show replies to</string>
<string name="sk_list_replies_policy_list">List members</string>
<string name="sk_list_replies_policy_followed">Any followed user</string>
<string name="sk_list_replies_policy_none">No one</string>
<string name="sk_list_replies_policy_list">list members</string>
<string name="sk_list_replies_policy_followed">followed users</string>
<string name="sk_list_replies_policy_none">no one</string>
<string name="sk_delete_list">Delete list</string>
<string name="sk_delete_list_confirm">Are you sure you want to delete this list?</string>
<string name="sk_edit_list_title">Edit list</string>
</resources>

0 comments on commit d47797b

Please sign in to comment.