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

Commit

Permalink
add option to allow multiple poll choices
Browse files Browse the repository at this point in the history
  • Loading branch information
sk22 committed Dec 8, 2022
1 parent 6fd58c9 commit 5b28468
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -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<DraftPollOption> pollOptions=new ArrayList<>();
Expand Down Expand Up @@ -288,6 +291,9 @@ public void onIconChanged(int icon){
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();
Expand All @@ -302,6 +308,7 @@ public void onIconChanged(int icon){
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);
Expand All @@ -312,6 +319,7 @@ public void onIconChanged(int icon){
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);
Expand Down Expand Up @@ -381,6 +389,7 @@ public void onSaveInstanceState(Bundle outState){
outState.putStringArrayList("pollOptions", opts);
outState.putInt("pollDuration", pollDuration);
outState.putString("pollDurationStr", pollDurationStr);
outState.putBoolean("pollAllowMultiple", pollAllowMultipleItem.isSelected());
}
outState.putBoolean("hasSpoiler", hasSpoiler);
if(!attachments.isEmpty()){
Expand Down Expand Up @@ -658,6 +667,7 @@ private void publish(){
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());
}
Expand Down Expand Up @@ -1158,6 +1168,11 @@ private DraftPollOption createDraftPollOption(){
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);
Expand Down Expand Up @@ -1319,6 +1334,27 @@ private void updateVisibilityIcon(){
});
}

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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="M12 7c0.414 0 0.75 0.336 0.75 0.75v3.5h3.5c0.414 0 0.75 0.336 0.75 0.75s-0.336 0.75-0.75 0.75h-3.5v3.5c0 0.414-0.336 0.75-0.75 0.75s-0.75-0.336-0.75-0.75v-3.5h-3.5C7.336 12.75 7 12.414 7 12s0.336-0.75 0.75-0.75h3.5v-3.5C11.25 7.336 11.586 7 12 7zM3 6.25C3 4.455 4.455 3 6.25 3h11.5C19.545 3 21 4.455 21 6.25v11.5c0 1.795-1.455 3.25-3.25 3.25H6.25C4.455 21 3 19.545 3 17.75V6.25zM6.25 4.5C5.284 4.5 4.5 5.284 4.5 6.25v11.5c0 0.966 0.784 1.75 1.75 1.75h11.5c0.966 0 1.75-0.784 1.75-1.75V6.25c0-0.966-0.784-1.75-1.75-1.75H6.25z" android:fillColor="@color/fluent_default_icon_tint"/>
</vector>
1 change: 1 addition & 0 deletions mastodon/src/main/res/layout/compose_poll_option.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:outlineProvider="background"
android:elevation="2dp">
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="16dp"
Expand Down
34 changes: 34 additions & 0 deletions mastodon/src/main/res/layout/fragment_compose.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
android:outlineProvider="background"
android:elevation="2dp">
<ImageView
android:id="@+id/add_poll_option_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="16dp"
Expand All @@ -145,6 +146,39 @@
android:textAppearance="@style/m3_label_large"
android:textColor="?android:textColorPrimary"
tools:text="Duration: 7 days"/>

<LinearLayout
android:id="@+id/poll_allow_multiple"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:layoutDirection="locale"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:background="?android:selectableItemBackground">

<CheckBox
android:id="@+id/poll_allow_multiple_checkbox"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginEnd="24dp"
android:duplicateParentState="true"
android:importantForAccessibility="no"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:singleLine="true"
android:text="@string/poll_allow_multiple" />
</LinearLayout>
</LinearLayout>

<org.joinmastodon.android.ui.views.ComposeMediaLayout
Expand Down
1 change: 1 addition & 0 deletions mastodon/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,5 @@
<string name="login_title">Welcome Back</string>
<string name="login_subtitle">Log in with the server where you created your account.</string>
<string name="server_url">Server URL</string>
<string name="poll_allow_multiple">Allow multiple choices</string>
</resources>

0 comments on commit 5b28468

Please sign in to comment.