From 5b28468efd49387b4f8b83f142f3adf3104ca60c Mon Sep 17 00:00:00 2001 From: sk Date: Thu, 8 Dec 2022 20:53:34 +0100 Subject: [PATCH] add option to allow multiple poll choices --- .../android/fragments/ComposeFragment.java | 36 +++++++++++++++++++ .../ic_fluent_add_square_24_regular.xml | 3 ++ .../main/res/layout/compose_poll_option.xml | 1 + .../src/main/res/layout/fragment_compose.xml | 34 ++++++++++++++++++ mastodon/src/main/res/values/strings.xml | 1 + 5 files changed, 75 insertions(+) create mode 100644 mastodon/src/main/res/drawable/ic_fluent_add_square_24_regular.xml diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java index 406d74673..cdee35419 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java @@ -40,6 +40,7 @@ import android.view.WindowManager; import android.view.animation.LinearInterpolator; import android.view.inputmethod.InputMethodManager; import android.widget.Button; +import android.widget.CheckBox; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageButton; @@ -150,6 +151,8 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr private ReorderableLinearLayout pollOptionsView; private View pollWrap; private View addPollOptionBtn; + private View pollAllowMultipleItem; + private CheckBox pollAllowMultipleCheckbox; private TextView pollDurationView; private ArrayList pollOptions=new ArrayList<>(); @@ -288,6 +291,9 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr pollOptionsView=view.findViewById(R.id.poll_options); pollWrap=view.findViewById(R.id.poll_wrap); addPollOptionBtn=view.findViewById(R.id.add_poll_option); + pollAllowMultipleItem=view.findViewById(R.id.poll_allow_multiple); + pollAllowMultipleCheckbox=view.findViewById(R.id.poll_allow_multiple_checkbox); + pollAllowMultipleItem.setOnClickListener(v->this.togglePollAllowMultiple()); addPollOptionBtn.setOnClickListener(v->{ createDraftPollOption().edit.requestFocus(); @@ -302,6 +308,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr pollBtn.setSelected(true); mediaBtn.setEnabled(false); pollWrap.setVisibility(View.VISIBLE); + updatePollAllowMultiple(savedInstanceState.getBoolean("pollAllowMultiple", false)); for(String oldText:savedInstanceState.getStringArrayList("pollOptions")){ DraftPollOption opt=createDraftPollOption(); opt.edit.setText(oldText); @@ -312,6 +319,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr pollBtn.setSelected(true); mediaBtn.setEnabled(false); pollWrap.setVisibility(View.VISIBLE); + updatePollAllowMultiple(editingStatus.poll.multiple); for(Poll.Option eopt:editingStatus.poll.options){ DraftPollOption opt=createDraftPollOption(); opt.edit.setText(eopt.title); @@ -381,6 +389,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr outState.putStringArrayList("pollOptions", opts); outState.putInt("pollDuration", pollDuration); outState.putString("pollDurationStr", pollDurationStr); + outState.putBoolean("pollAllowMultiple", pollAllowMultipleItem.isSelected()); } outState.putBoolean("hasSpoiler", hasSpoiler); if(!attachments.isEmpty()){ @@ -658,6 +667,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr if(!pollOptions.isEmpty()){ req.poll=new CreateStatus.Request.Poll(); req.poll.expiresIn=pollDuration; + req.poll.multiple=pollAllowMultipleItem.isSelected(); for(DraftPollOption opt:pollOptions) req.poll.options.add(opt.edit.getText().toString()); } @@ -1158,6 +1168,11 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr option.view=LayoutInflater.from(getActivity()).inflate(R.layout.compose_poll_option, pollOptionsView, false); option.edit=option.view.findViewById(R.id.edit); option.dragger=option.view.findViewById(R.id.dragger_thingy); + ImageView icon = option.view.findViewById(R.id.icon); + icon.setImageDrawable(getContext().getDrawable(pollAllowMultipleItem.isSelected() ? + R.drawable.ic_poll_checkbox_regular_selector : + R.drawable.ic_poll_option_button + )); option.dragger.setOnLongClickListener(v->{ pollOptionsView.startDragging(option.view); @@ -1319,6 +1334,27 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr }); } + private void togglePollAllowMultiple() { + updatePollAllowMultiple(!pollAllowMultipleItem.isSelected()); + } + + private void updatePollAllowMultiple(boolean multiple){ + pollAllowMultipleItem.setSelected(multiple); + pollAllowMultipleCheckbox.setChecked(multiple); + ImageView btn = addPollOptionBtn.findViewById(R.id.add_poll_option_icon); + btn.setImageDrawable(getContext().getDrawable(multiple ? + R.drawable.ic_fluent_add_square_24_regular : + R.drawable.ic_fluent_add_circle_24_regular + )); + for (DraftPollOption opt:pollOptions) { + ImageView icon = opt.view.findViewById(R.id.icon); + icon.setImageDrawable(getContext().getDrawable(multiple ? + R.drawable.ic_poll_checkbox_regular_selector : + R.drawable.ic_poll_option_button + )); + } + } + @Override public void onSelectionChanged(int start, int end){ if(ignoreSelectionChanges) diff --git a/mastodon/src/main/res/drawable/ic_fluent_add_square_24_regular.xml b/mastodon/src/main/res/drawable/ic_fluent_add_square_24_regular.xml new file mode 100644 index 000000000..0aa232d6c --- /dev/null +++ b/mastodon/src/main/res/drawable/ic_fluent_add_square_24_regular.xml @@ -0,0 +1,3 @@ + + + diff --git a/mastodon/src/main/res/layout/compose_poll_option.xml b/mastodon/src/main/res/layout/compose_poll_option.xml index d806da362..d93a68014 100644 --- a/mastodon/src/main/res/layout/compose_poll_option.xml +++ b/mastodon/src/main/res/layout/compose_poll_option.xml @@ -15,6 +15,7 @@ android:outlineProvider="background" android:elevation="2dp"> + + + + + + + Welcome Back Log in with the server where you created your account. Server URL + Allow multiple choices \ No newline at end of file