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 f7a0a222..f7e10635 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java @@ -5,6 +5,7 @@ import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.AlertDialog; import android.content.ClipData; import android.content.Intent; import android.content.res.Configuration; @@ -1120,12 +1121,15 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr private void showLanguageAlert(){ Preferences prefs=AccountSessionManager.getInstance().getAccount(accountID).preferences; ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), prefs!=null ? prefs.postingDefaultLanguage : null, postLang, mainEditText.getText().toString()); - new M3AlertDialogBuilder(getActivity()) + final AlertDialog dlg=new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.language) .setView(vc.getView()) - .setPositiveButton(R.string.ok, (dialog, which)->setPostLanguage(vc.getSelectedOption())) - .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.cancel, null) .show(); + vc.setSelectionListener(opt->{ + setPostLanguage(opt); + dlg.dismiss(); + }); } private void setPostLanguage(ComposeLanguageAlertViewController.SelectedOption language){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/EditFilterFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/EditFilterFragment.java index bee767c1..49a96320 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/EditFilterFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/EditFilterFragment.java @@ -125,41 +125,40 @@ public class EditFilterFragment extends BaseSettingsFragment implements On ArrayList options=Arrays.stream(durationOptions).mapToObj(d->UiUtils.formatDuration(getActivity(), d)).collect(Collectors.toCollection(ArrayList::new)); options.add(0, getString(R.string.filter_duration_forever)); options.add(getString(R.string.filter_duration_custom)); - Instant[] newEnd={null}; - boolean[] isCustom={false}; AlertDialog alert=new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.settings_filter_duration_title) .setSupportingText(endsAt==null ? null : getString(R.string.settings_filter_ends, UiUtils.formatRelativeTimestampAsMinutesAgo(getActivity(), endsAt, false))) .setSingleChoiceItems(options.toArray(new String[0]), -1, (dlg, item)->{ AlertDialog a=(AlertDialog) dlg; if(item==options.size()-1){ // custom - showCustomDurationAlert(isCustom[0] ? newEnd[0] : null, date->{ + showCustomDurationAlert(null, date->{ if(date==null){ a.getListView().setItemChecked(item, false); }else{ - isCustom[0]=true; - newEnd[0]=date; - a.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); + Instant newEnd=date; + if(!Objects.equals(endsAt, newEnd)){ + endsAt=newEnd; + updateDurationItem(); + dirty=true; + } + a.dismiss(); } }); }else{ - isCustom[0]=false; + Instant newEnd; if(item==0){ - newEnd[0]=null; + newEnd=null; }else{ - newEnd[0]=Instant.now().plusSeconds(durationOptions[item-1]); + newEnd=Instant.now().plusSeconds(durationOptions[item-1]); } - a.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); + if(!Objects.equals(endsAt, newEnd)){ + endsAt=newEnd; + updateDurationItem(); + dirty=true; + } + a.dismiss(); } }) - .setPositiveButton(R.string.ok, (dlg, item)->{ - if(!Objects.equals(endsAt, newEnd[0])){ - endsAt=newEnd[0]; - updateDurationItem(); - dirty=true; - } - }) - .setNegativeButton(R.string.cancel, null) .show(); alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBehaviorFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBehaviorFragment.java index f7afda3d..df84ef5e 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBehaviorFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBehaviorFragment.java @@ -1,5 +1,6 @@ package org.joinmastodon.android.fragments.settings; +import android.app.AlertDialog; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; @@ -57,20 +58,20 @@ public class SettingsBehaviorFragment extends BaseSettingsFragment{ protected void doLoadData(int offset, int count){} private void onDefaultLanguageClick(ListItem item){ - ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), null, new ComposeLanguageAlertViewController.SelectedOption(-1, postLanguage, null), null); - new M3AlertDialogBuilder(getActivity()) + ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), null, newPostLanguage==null ? new ComposeLanguageAlertViewController.SelectedOption(-1, postLanguage, null) : newPostLanguage, null); + AlertDialog dlg=new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.default_post_language) .setView(vc.getView()) - .setPositiveButton(R.string.ok, (dlg, which)->{ - ComposeLanguageAlertViewController.SelectedOption opt=vc.getSelectedOption(); - if(!opt.locale.equals(postLanguage)){ - newPostLanguage=opt; - languageItem.subtitle=newPostLanguage.locale.getDisplayLanguage(Locale.getDefault()); - rebindItem(languageItem); - } - }) - .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.cancel, null) .show(); + vc.setSelectionListener(opt->{ + if(!opt.locale.equals(postLanguage)){ + newPostLanguage=opt; + languageItem.subtitle=newPostLanguage.locale.getDisplayLanguage(Locale.getDefault()); + rebindItem(languageItem); + } + dlg.dismiss(); + }); } private void onCustomTabsClick(ListItem item){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsDisplayFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsDisplayFragment.java index f50c9188..2e85c7ce 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsDisplayFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsDisplayFragment.java @@ -86,28 +86,26 @@ public class SettingsDisplayFragment extends BaseSettingsFragment{ case DARK -> 1; case AUTO -> 2; }; - int[] newSelected={selected}; new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.settings_theme) .setSingleChoiceItems((String[])IntStream.of(R.string.theme_light, R.string.theme_dark, R.string.theme_auto).mapToObj(this::getString).toArray(String[]::new), - selected, (dlg, item)->newSelected[0]=item) - .setPositiveButton(R.string.ok, (dlg, item)->{ - GlobalUserPreferences.ThemePreference pref=switch(newSelected[0]){ - case 0 -> GlobalUserPreferences.ThemePreference.LIGHT; - case 1 -> GlobalUserPreferences.ThemePreference.DARK; - case 2 -> GlobalUserPreferences.ThemePreference.AUTO; - default -> throw new IllegalStateException("Unexpected value: "+newSelected[0]); - }; - if(pref!=GlobalUserPreferences.theme){ - GlobalUserPreferences.ThemePreference prev=GlobalUserPreferences.theme; - GlobalUserPreferences.theme=pref; - GlobalUserPreferences.save(); - themeItem.subtitleRes=getAppearanceValue(); - rebindItem(themeItem); - maybeApplyNewThemeRightNow(prev); - } - }) - .setNegativeButton(R.string.cancel, null) + selected, (dlg, item)->{ + GlobalUserPreferences.ThemePreference pref=switch(item){ + case 0 -> GlobalUserPreferences.ThemePreference.LIGHT; + case 1 -> GlobalUserPreferences.ThemePreference.DARK; + case 2 -> GlobalUserPreferences.ThemePreference.AUTO; + default -> throw new IllegalStateException("Unexpected value: "+item); + }; + if(pref!=GlobalUserPreferences.theme){ + GlobalUserPreferences.ThemePreference prev=GlobalUserPreferences.theme; + GlobalUserPreferences.theme=pref; + GlobalUserPreferences.save(); + themeItem.subtitleRes=getAppearanceValue(); + rebindItem(themeItem); + maybeApplyNewThemeRightNow(prev); + } + dlg.dismiss(); + }) .show(); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java index deced530..be0d0837 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java @@ -192,18 +192,13 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment{ 3*24*3600, 7*24*3600 }; - int[] selectedOption={0}; AlertDialog alert=new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.pause_all_notifications_title) .setSupportingText(time>System.currentTimeMillis() ? getString(R.string.pause_notifications_ends, UiUtils.formatRelativeTimestampAsMinutesAgo(getActivity(), Instant.ofEpochMilli(time), false)) : null) .setSingleChoiceItems((String[])Arrays.stream(durationOptions).mapToObj(d->UiUtils.formatDuration(getActivity(), d)).toArray(String[]::new), -1, (dlg, item)->{ - if(selectedOption[0]==0){ - ((AlertDialog)dlg).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); - } - selectedOption[0]=durationOptions[item]; + AccountSessionManager.get(accountID).getLocalPreferences().setNotificationsPauseEndTime(System.currentTimeMillis()+durationOptions[item]*1000L); + dlg.dismiss(); }) - .setPositiveButton(R.string.ok, (dlg, item)->AccountSessionManager.get(accountID).getLocalPreferences().setNotificationsPauseEndTime(System.currentTimeMillis()+selectedOption[0]*1000L)) - .setNegativeButton(R.string.cancel, null) .show(); alert.setOnDismissListener(dialog->updatePauseItem()); alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); @@ -216,20 +211,18 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment{ R.string.notifications_policy_follower, R.string.notifications_policy_no_one ).map(this::getString).toArray(String[]::new); - int[] selectedItem={getPushSubscription().policy.ordinal()}; new M3AlertDialogBuilder(getActivity()) .setTitle(R.string.settings_notifications_policy) - .setSingleChoiceItems(items, selectedItem[0], (dlg, which)->selectedItem[0]=which) - .setPositiveButton(R.string.ok, (dlg, which)->{ + .setSingleChoiceItems(items, getPushSubscription().policy.ordinal(), (dlg, which)->{ + dlg.dismiss(); PushSubscription.Policy prevValue=getPushSubscription().policy; - PushSubscription.Policy newValue=PushSubscription.Policy.values()[selectedItem[0]]; + PushSubscription.Policy newValue=PushSubscription.Policy.values()[which]; if(prevValue==newValue) return; getPushSubscription().policy=newValue; updatePolicyItem(prevValue); needUpdateNotificationSettings=true; }) - .setNegativeButton(R.string.cancel, null) .show(); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeLanguageAlertViewController.java b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeLanguageAlertViewController.java index 3e12f4d7..3e114030 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeLanguageAlertViewController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeLanguageAlertViewController.java @@ -28,6 +28,7 @@ import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.function.Consumer; import java.util.stream.Collectors; import androidx.annotation.NonNull; @@ -47,6 +48,7 @@ public class ComposeLanguageAlertViewController{ private int selectedIndex=0; private Locale selectedLocale; private Locale detectedLocale; + private Consumer selectionListener; public ComposeLanguageAlertViewController(Context context, String preferred, SelectedOption previouslySelected, String postText){ this.context=context; @@ -221,6 +223,12 @@ public class ComposeLanguageAlertViewController{ if(holder!=null && holder.itemView instanceof Checkable checkable) checkable.setChecked(true); selectedIndex=index; + if(selectionListener!=null) + selectionListener.accept(getSelectedOption()); + } + + public void setSelectionListener(Consumer selectionListener){ + this.selectionListener=selectionListener; } private class AllLocalesAdapter extends RecyclerView.Adapter{ @@ -264,8 +272,8 @@ public class ComposeLanguageAlertViewController{ @Override public void onClick(){ - selectItem(getAbsoluteAdapterPosition()); selectedLocale=item.locale; + selectItem(getAbsoluteAdapterPosition()); } } @@ -321,8 +329,8 @@ public class ComposeLanguageAlertViewController{ @Override public void onClick(){ - selectItem(getAbsoluteAdapterPosition()); selectedLocale=item.locale; + selectItem(getAbsoluteAdapterPosition()); } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposePollViewController.java b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposePollViewController.java index 983d90b8..ebd85907 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposePollViewController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposePollViewController.java @@ -190,28 +190,20 @@ public class ComposePollViewController{ if(l==pollDuration) selectedOption=i; } - int[] chosenOption={0}; new M3AlertDialogBuilder(fragment.getActivity()) - .setSingleChoiceItems(options, selectedOption, (dialog, which)->chosenOption[0]=which) - .setTitle(R.string.poll_length) - .setPositiveButton(R.string.ok, (dialog, which)->{ - pollDuration=POLL_LENGTH_OPTIONS[chosenOption[0]]; + .setSingleChoiceItems(options, selectedOption, (dialog, which)->{ + pollDuration=POLL_LENGTH_OPTIONS[which]; pollDurationValue.setText(UiUtils.formatDuration(fragment.getContext(), pollDuration)); + dialog.dismiss(); }) - .setNegativeButton(R.string.cancel, null) + .setTitle(R.string.poll_length) .show(); } private void showPollStyleAlert(){ - final int[] option={pollIsMultipleChoice ? R.id.multiple_choice : R.id.single_choice}; AlertDialog alert=new M3AlertDialogBuilder(fragment.getActivity()) .setView(R.layout.poll_style) .setTitle(R.string.poll_style_title) - .setPositiveButton(R.string.ok, (dlg, which)->{ - pollIsMultipleChoice=option[0]==R.id.multiple_choice; - pollStyleValue.setText(pollIsMultipleChoice ? R.string.compose_poll_multiple_choice : R.string.compose_poll_single_choice); - }) - .setNegativeButton(R.string.cancel, null) .show(); CheckableLinearLayout multiple=alert.findViewById(R.id.multiple_choice); CheckableLinearLayout single=alert.findViewById(R.id.single_choice); @@ -219,11 +211,9 @@ public class ComposePollViewController{ multiple.setChecked(pollIsMultipleChoice); View.OnClickListener listener=v->{ int id=v.getId(); - if(id==option[0]) - return; - ((Checkable) alert.findViewById(option[0])).setChecked(false); - ((Checkable) v).setChecked(true); - option[0]=id; + pollIsMultipleChoice=id==R.id.multiple_choice; + pollStyleValue.setText(pollIsMultipleChoice ? R.string.compose_poll_multiple_choice : R.string.compose_poll_single_choice); + alert.dismiss(); }; single.setOnClickListener(listener); multiple.setOnClickListener(listener);