Update poll compose dialog

This commit is contained in:
0xd9a 2023-01-17 16:59:41 +05:30
parent f8641a953a
commit 89c30f16f0
8 changed files with 163 additions and 116 deletions

View file

@ -72,6 +72,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vanniktech.emoji.EmojiManager;
@ -1705,7 +1706,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
* @param position - int position
*/
private void displayPollPopup(ComposeViewHolder holder, Status statusDraft, int position) {
AlertDialog.Builder alertPoll = new AlertDialog.Builder(context, Helper.dialogStyle());
AlertDialog.Builder alertPoll = new MaterialAlertDialogBuilder(context, Helper.dialogStyle());
alertPoll.setTitle(R.string.create_poll);
ComposePollBinding composePollBinding = ComposePollBinding.inflate(LayoutInflater.from(context), new LinearLayout(context), false);
alertPoll.setView(composePollBinding.getRoot());
@ -1723,17 +1724,19 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(max_length);
composePollBinding.option1.text.setFilters(fArray);
composePollBinding.option1.text.setHint(context.getString(R.string.poll_choice_s, 1));
composePollBinding.option1.textLayout.setHint(context.getString(R.string.poll_choice_s, 1));
composePollBinding.option2.text.setFilters(fArray);
composePollBinding.option2.text.setHint(context.getString(R.string.poll_choice_s, 2));
composePollBinding.option2.textLayout.setHint(context.getString(R.string.poll_choice_s, 2));
composePollBinding.option1.buttonRemove.setVisibility(View.GONE);
composePollBinding.option2.buttonRemove.setVisibility(View.GONE);
int finalMax_entry = max_entry;
composePollBinding.buttonAddOption.setOnClickListener(v -> {
if (pollCountItem[0] < finalMax_entry) {
ComposePollItemBinding composePollItemBinding = ComposePollItemBinding.inflate(LayoutInflater.from(context), new LinearLayout(context), false);
ComposePollItemBinding composePollItemBinding = ComposePollItemBinding.inflate(LayoutInflater.from(composePollBinding.optionsList.getContext()), composePollBinding.optionsList, false);
if (composePollBinding.pollType.getCheckedButtonId() == R.id.poll_type_multiple)
composePollItemBinding.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_multiple);
composePollItemBinding.text.setFilters(fArray);
composePollItemBinding.text.setHint(context.getString(R.string.poll_choice_s, (pollCountItem[0] + 1)));
composePollItemBinding.textLayout.setHint(context.getString(R.string.poll_choice_s, (pollCountItem[0] + 1)));
LinearLayoutCompat viewItem = composePollItemBinding.getRoot();
composePollBinding.optionsList.addView(composePollItemBinding.getRoot());
composePollItemBinding.buttonRemove.setOnClickListener(view -> {
@ -1764,27 +1767,31 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
ArrayAdapter<CharSequence> pollduration = ArrayAdapter.createFromResource(context,
R.array.poll_duration, android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<CharSequence> pollchoice = ArrayAdapter.createFromResource(context,
R.array.poll_choice_type, android.R.layout.simple_spinner_dropdown_item);
composePollBinding.pollType.setAdapter(pollchoice);
composePollBinding.pollDuration.setAdapter(pollduration);
composePollBinding.pollDuration.setSelection(4);
composePollBinding.pollType.setSelection(0);
if (statusDraft != null && statusDraft.poll != null && statusDraft.poll.options != null) {
int i = 1;
for (Poll.PollItem pollItem : statusDraft.poll.options) {
if (i == 1) {
if (statusDraft.poll.multiple)
composePollBinding.option1.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_multiple);
if (pollItem.title != null)
composePollBinding.option1.text.setText(pollItem.title);
} else if (i == 2) {
if (statusDraft.poll.multiple)
composePollBinding.option2.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_multiple);
if (pollItem.title != null)
composePollBinding.option2.text.setText(pollItem.title);
} else {
ComposePollItemBinding composePollItemBinding = ComposePollItemBinding.inflate(LayoutInflater.from(context), new LinearLayout(context), false);
if (composePollBinding.pollType.getCheckedButtonId() == R.id.poll_type_multiple)
composePollItemBinding.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_multiple);
else
composePollItemBinding.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_single);
composePollItemBinding.text.setFilters(fArray);
composePollItemBinding.text.setHint(context.getString(R.string.poll_choice_s, (pollCountItem[0] + 1)));
composePollItemBinding.textLayout.setHint(context.getString(R.string.poll_choice_s, (pollCountItem[0] + 1)));
composePollItemBinding.text.setText(pollItem.title);
composePollBinding.optionsList.addView(composePollItemBinding.getRoot());
composePollItemBinding.buttonRemove.setOnClickListener(view -> {
@ -1822,9 +1829,9 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
break;
}
if (statusDraft.poll.multiple)
composePollBinding.pollType.setSelection(1);
composePollBinding.pollType.check(R.id.poll_type_multiple);
else
composePollBinding.pollType.setSelection(0);
composePollBinding.pollType.check(R.id.poll_type_single);
}
@ -1834,15 +1841,32 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
dialog.dismiss();
notifyItemChanged(position);
});
alertPoll.setPositiveButton(R.string.validate, null);
alertPoll.setPositiveButton(R.string.save, null);
final AlertDialog alertPollDiaslog = alertPoll.create();
alertPollDiaslog.setOnShowListener(dialog -> {
composePollBinding.pollType.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
if (isChecked) {
if (checkedId == R.id.poll_type_single) {
if (statusDraft != null && statusDraft.poll != null) statusDraft.poll.multiple = false;
for (int i = 0; i < composePollBinding.optionsList.getChildCount(); i++) {
ComposePollItemBinding child = ComposePollItemBinding.bind(composePollBinding.optionsList.getChildAt(i));
child.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_single);
}
} else if (checkedId == R.id.poll_type_multiple) {
if (statusDraft != null && statusDraft.poll != null) statusDraft.poll.multiple = true;
for (int i = 0; i < composePollBinding.optionsList.getChildCount(); i++) {
ComposePollItemBinding child = ComposePollItemBinding.bind(composePollBinding.optionsList.getChildAt(i));
child.typeIndicator.setImageResource(R.drawable.ic_compose_poll_option_mark_multiple);
}
}
}
});
Button b = alertPollDiaslog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(view1 -> {
int poll_duration_pos = composePollBinding.pollDuration.getSelectedItemPosition();
int poll_choice_pos = composePollBinding.pollType.getSelectedItemPosition();
int selected_poll_type_id = composePollBinding.pollType.getCheckedButtonId();
String choice1 = composePollBinding.option1.text.getText().toString().trim();
String choice2 = composePollBinding.option2.text.getText().toString().trim();
@ -1850,7 +1874,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
Toasty.error(context, context.getString(R.string.poll_invalid_choices), Toasty.LENGTH_SHORT).show();
} else if (statusDraft != null) {
statusDraft.poll = new Poll();
statusDraft.poll.multiple = (poll_choice_pos != 0);
statusDraft.poll.multiple = selected_poll_type_id == R.id.poll_type_multiple;
int expire;
switch (poll_duration_pos) {
case 0:

View file

@ -4,6 +4,6 @@
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#808080"
android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z" />
android:fillColor="@android:color/white"
android:pathData="M18,19L6,19c-0.55,0 -1,-0.45 -1,-1L5,6c0,-0.55 0.45,-1 1,-1h12c0.55,0 1,0.45 1,1v12c0,0.55 -0.45,1 -1,1zM19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z" />
</vector>

View file

@ -14,29 +14,22 @@
You should have received a copy of the GNU General Public License along with Fedilab; if not,
see <http://www.gnu.org/licenses>
-->
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
app:cardElevation="2dp">
<ScrollView
android:id="@+id/options_list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
android:paddingHorizontal="24dp"
android:paddingVertical="12dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/options_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical">
<include
@ -52,59 +45,65 @@
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatImageButton
<com.google.android.material.button.MaterialButton
android:id="@+id/button_add_option"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginTop="6dp"
android:contentDescription="@string/add_poll_item"
android:src="@drawable/ic_baseline_add_24" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:gravity="center_vertical">
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp"
android:text="@string/add_poll_item"
app:icon="@drawable/ic_baseline_add_24" />
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="24dp"
android:text="@string/poll_type"
android:textAlignment="viewEnd" />
android:textAppearance="@style/TextAppearance.Material3.LabelLarge" />
<androidx.appcompat.widget.AppCompatSpinner
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/poll_type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:gravity="center_vertical">
android:orientation="vertical"
app:checkedButton="@id/poll_type_single"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/poll_type_single"
style="@style/Fedilab.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/poll_type_single"
android:textAlignment="textStart"
app:icon="@drawable/ic_compose_poll_option_mark_single" />
<com.google.android.material.button.MaterialButton
android:id="@+id/poll_type_multiple"
style="@style/Fedilab.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/poll_type_multiple"
android:textAlignment="textStart"
app:icon="@drawable/ic_compose_poll_option_mark_multiple" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="24dp"
android:text="@string/poll_duration"
android:textAlignment="viewEnd" />
android:textAppearance="@style/TextAppearance.Material3.LabelLarge" />
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/poll_duration"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
</com.google.android.material.card.MaterialCardView>
</ScrollView>

View file

@ -15,6 +15,7 @@
see <http://www.gnu.org/licenses>
-->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
@ -23,23 +24,30 @@
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/type_indicator"
android:layout_width="36dp"
android:layout_height="36dp"
android:padding="4dp"
android:src="@drawable/ic_compose_poll_option_mark_multiple" />
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_compose_poll_option_mark_single" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/text"
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:layout_marginHorizontal="6dp"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatImageButton
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/button_remove"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="48dp"
android:layout_height="48dp"
android:backgroundTint="@color/errorColor"
android:src="@drawable/ic_baseline_close_24" />
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/ic_baseline_close_24" />
</androidx.appcompat.widget.LinearLayoutCompat>

View file

@ -100,7 +100,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="AppThemeAlertDialog" parent="Theme.Material3.Dark.Dialog.Alert">
<style name="AppThemeAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/md_theme_dark_primary</item>
<item name="colorPrimary">@color/md_theme_dark_primary</item>
<item name="colorOnPrimary">@color/md_theme_dark_onPrimary</item>
@ -223,7 +223,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="SolarizedAlertDialog" parent="Theme.Material3.Dark.Dialog.Alert">
<style name="SolarizedAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/solarized_md_theme_dark_onPrimaryContainer</item>
<item name="colorPrimary">@color/solarized_md_theme_dark_primary</item>
<item name="colorOnPrimary">@color/solarized_md_theme_dark_onPrimary</item>
@ -339,7 +339,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="BlackAlertDialog" parent="Theme.Material3.Dark.Dialog.Alert">
<style name="BlackAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/black_200</item>
<item name="colorPrimary">@color/dracula_comment</item>
<item name="colorOnPrimary">@color/white</item>
@ -454,7 +454,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="DraculaAlertDialog" parent="Theme.Material3.Dark.Dialog.Alert">
<style name="DraculaAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/dracula_pink</item>
<item name="colorPrimary">@color/dracula_comment</item>
<item name="colorOnPrimary">@color/dracula_foreground</item>

View file

@ -441,7 +441,7 @@
<string name="saving">Saving…</string>
<string name="image_saved">Image Saved Successfully!</string>
<string name="save_image_failed">Failed to save Image</string>
<string name="add_poll_item">Add a poll item</string>
<string name="add_poll_item">Add a choice</string>
<string name="mute_conversation">Mute conversation</string>
<string name="unmute_conversation">Unmute conversation</string>
<string name="toast_unmute_conversation">The conversation is no longer muted!</string>
@ -1538,6 +1538,8 @@
<string name="set_unfollow_validation_title">Confirm unfollows</string>
<string name="message_has_been_sent">Message has been sent!</string>
<string name="poll_type">Poll type:</string>
<string name="poll_type_single">Single choice</string>
<string name="poll_type_multiple">Multiple choices</string>
<string name="poll_duration">Poll duration:</string>
<string name="set_display_bookmark_indication">Always display bookmark button</string>
<string name="set_display_translate_indication">Always display translate button</string>

View file

@ -100,7 +100,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="AppThemeAlertDialog" parent="Theme.Material3.Light.Dialog.Alert">
<style name="AppThemeAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/md_theme_light_primary</item>
<item name="colorPrimary">@color/md_theme_light_primary</item>
<item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
@ -185,7 +185,7 @@
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="SolarizedAlertDialog" parent="Theme.Material3.Light.Dialog.Alert">
<style name="SolarizedAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="linkColor">@color/solarized_md_theme_light_primary</item>
<item name="colorPrimary">@color/solarized_md_theme_light_primary</item>
<item name="colorOnPrimary">@color/solarized_md_theme_light_onPrimary</item>
@ -261,4 +261,9 @@
<item name="android:layout_height">36dp</item>
<item name="android:padding">4dp</item>
</style>
<style name="Fedilab.OutlinedButton" parent="Widget.Material3.Button.OutlinedButton">
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
</style>
</resources>