This commit is contained in:
Grishka
2024-02-24 03:04:35 +03:00
parent f2fbf55c53
commit 3f47497c12
7 changed files with 75 additions and 82 deletions

View File

@@ -5,6 +5,7 @@ import android.animation.AnimatorSet;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData; import android.content.ClipData;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
@@ -1120,12 +1121,15 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
private void showLanguageAlert(){ private void showLanguageAlert(){
Preferences prefs=AccountSessionManager.getInstance().getAccount(accountID).preferences; Preferences prefs=AccountSessionManager.getInstance().getAccount(accountID).preferences;
ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), prefs!=null ? prefs.postingDefaultLanguage : null, postLang, mainEditText.getText().toString()); 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) .setTitle(R.string.language)
.setView(vc.getView()) .setView(vc.getView())
.setPositiveButton(R.string.ok, (dialog, which)->setPostLanguage(vc.getSelectedOption())) .setPositiveButton(R.string.cancel, null)
.setNegativeButton(R.string.cancel, null)
.show(); .show();
vc.setSelectionListener(opt->{
setPostLanguage(opt);
dlg.dismiss();
});
} }
private void setPostLanguage(ComposeLanguageAlertViewController.SelectedOption language){ private void setPostLanguage(ComposeLanguageAlertViewController.SelectedOption language){

View File

@@ -125,41 +125,40 @@ public class EditFilterFragment extends BaseSettingsFragment<Void> implements On
ArrayList<String> options=Arrays.stream(durationOptions).mapToObj(d->UiUtils.formatDuration(getActivity(), d)).collect(Collectors.toCollection(ArrayList<String>::new)); ArrayList<String> options=Arrays.stream(durationOptions).mapToObj(d->UiUtils.formatDuration(getActivity(), d)).collect(Collectors.toCollection(ArrayList<String>::new));
options.add(0, getString(R.string.filter_duration_forever)); options.add(0, getString(R.string.filter_duration_forever));
options.add(getString(R.string.filter_duration_custom)); options.add(getString(R.string.filter_duration_custom));
Instant[] newEnd={null};
boolean[] isCustom={false};
AlertDialog alert=new M3AlertDialogBuilder(getActivity()) AlertDialog alert=new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.settings_filter_duration_title) .setTitle(R.string.settings_filter_duration_title)
.setSupportingText(endsAt==null ? null : getString(R.string.settings_filter_ends, UiUtils.formatRelativeTimestampAsMinutesAgo(getActivity(), endsAt, false))) .setSupportingText(endsAt==null ? null : getString(R.string.settings_filter_ends, UiUtils.formatRelativeTimestampAsMinutesAgo(getActivity(), endsAt, false)))
.setSingleChoiceItems(options.toArray(new String[0]), -1, (dlg, item)->{ .setSingleChoiceItems(options.toArray(new String[0]), -1, (dlg, item)->{
AlertDialog a=(AlertDialog) dlg; AlertDialog a=(AlertDialog) dlg;
if(item==options.size()-1){ // custom if(item==options.size()-1){ // custom
showCustomDurationAlert(isCustom[0] ? newEnd[0] : null, date->{ showCustomDurationAlert(null, date->{
if(date==null){ if(date==null){
a.getListView().setItemChecked(item, false); a.getListView().setItemChecked(item, false);
}else{ }else{
isCustom[0]=true; Instant newEnd=date;
newEnd[0]=date; if(!Objects.equals(endsAt, newEnd)){
a.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); endsAt=newEnd;
}
});
}else{
isCustom[0]=false;
if(item==0){
newEnd[0]=null;
}else{
newEnd[0]=Instant.now().plusSeconds(durationOptions[item-1]);
}
a.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
})
.setPositiveButton(R.string.ok, (dlg, item)->{
if(!Objects.equals(endsAt, newEnd[0])){
endsAt=newEnd[0];
updateDurationItem(); updateDurationItem();
dirty=true; dirty=true;
} }
a.dismiss();
}
});
}else{
Instant newEnd;
if(item==0){
newEnd=null;
}else{
newEnd=Instant.now().plusSeconds(durationOptions[item-1]);
}
if(!Objects.equals(endsAt, newEnd)){
endsAt=newEnd;
updateDurationItem();
dirty=true;
}
a.dismiss();
}
}) })
.setNegativeButton(R.string.cancel, null)
.show(); .show();
alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} }

View File

@@ -1,5 +1,6 @@
package org.joinmastodon.android.fragments.settings; package org.joinmastodon.android.fragments.settings;
import android.app.AlertDialog;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
@@ -57,20 +58,20 @@ public class SettingsBehaviorFragment extends BaseSettingsFragment<Void>{
protected void doLoadData(int offset, int count){} protected void doLoadData(int offset, int count){}
private void onDefaultLanguageClick(ListItem<?> item){ private void onDefaultLanguageClick(ListItem<?> item){
ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), null, new ComposeLanguageAlertViewController.SelectedOption(-1, postLanguage, null), null); ComposeLanguageAlertViewController vc=new ComposeLanguageAlertViewController(getActivity(), null, newPostLanguage==null ? new ComposeLanguageAlertViewController.SelectedOption(-1, postLanguage, null) : newPostLanguage, null);
new M3AlertDialogBuilder(getActivity()) AlertDialog dlg=new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.default_post_language) .setTitle(R.string.default_post_language)
.setView(vc.getView()) .setView(vc.getView())
.setPositiveButton(R.string.ok, (dlg, which)->{ .setPositiveButton(R.string.cancel, null)
ComposeLanguageAlertViewController.SelectedOption opt=vc.getSelectedOption(); .show();
vc.setSelectionListener(opt->{
if(!opt.locale.equals(postLanguage)){ if(!opt.locale.equals(postLanguage)){
newPostLanguage=opt; newPostLanguage=opt;
languageItem.subtitle=newPostLanguage.locale.getDisplayLanguage(Locale.getDefault()); languageItem.subtitle=newPostLanguage.locale.getDisplayLanguage(Locale.getDefault());
rebindItem(languageItem); rebindItem(languageItem);
} }
}) dlg.dismiss();
.setNegativeButton(R.string.cancel, null) });
.show();
} }
private void onCustomTabsClick(ListItem<?> item){ private void onCustomTabsClick(ListItem<?> item){

View File

@@ -86,17 +86,15 @@ public class SettingsDisplayFragment extends BaseSettingsFragment<Void>{
case DARK -> 1; case DARK -> 1;
case AUTO -> 2; case AUTO -> 2;
}; };
int[] newSelected={selected};
new M3AlertDialogBuilder(getActivity()) new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.settings_theme) .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), .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) selected, (dlg, item)->{
.setPositiveButton(R.string.ok, (dlg, item)->{ GlobalUserPreferences.ThemePreference pref=switch(item){
GlobalUserPreferences.ThemePreference pref=switch(newSelected[0]){
case 0 -> GlobalUserPreferences.ThemePreference.LIGHT; case 0 -> GlobalUserPreferences.ThemePreference.LIGHT;
case 1 -> GlobalUserPreferences.ThemePreference.DARK; case 1 -> GlobalUserPreferences.ThemePreference.DARK;
case 2 -> GlobalUserPreferences.ThemePreference.AUTO; case 2 -> GlobalUserPreferences.ThemePreference.AUTO;
default -> throw new IllegalStateException("Unexpected value: "+newSelected[0]); default -> throw new IllegalStateException("Unexpected value: "+item);
}; };
if(pref!=GlobalUserPreferences.theme){ if(pref!=GlobalUserPreferences.theme){
GlobalUserPreferences.ThemePreference prev=GlobalUserPreferences.theme; GlobalUserPreferences.ThemePreference prev=GlobalUserPreferences.theme;
@@ -106,8 +104,8 @@ public class SettingsDisplayFragment extends BaseSettingsFragment<Void>{
rebindItem(themeItem); rebindItem(themeItem);
maybeApplyNewThemeRightNow(prev); maybeApplyNewThemeRightNow(prev);
} }
dlg.dismiss();
}) })
.setNegativeButton(R.string.cancel, null)
.show(); .show();
} }

View File

@@ -192,18 +192,13 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{
3*24*3600, 3*24*3600,
7*24*3600 7*24*3600
}; };
int[] selectedOption={0};
AlertDialog alert=new M3AlertDialogBuilder(getActivity()) AlertDialog alert=new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.pause_all_notifications_title) .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) .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)->{ .setSingleChoiceItems((String[])Arrays.stream(durationOptions).mapToObj(d->UiUtils.formatDuration(getActivity(), d)).toArray(String[]::new), -1, (dlg, item)->{
if(selectedOption[0]==0){ AccountSessionManager.get(accountID).getLocalPreferences().setNotificationsPauseEndTime(System.currentTimeMillis()+durationOptions[item]*1000L);
((AlertDialog)dlg).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); dlg.dismiss();
}
selectedOption[0]=durationOptions[item];
}) })
.setPositiveButton(R.string.ok, (dlg, item)->AccountSessionManager.get(accountID).getLocalPreferences().setNotificationsPauseEndTime(System.currentTimeMillis()+selectedOption[0]*1000L))
.setNegativeButton(R.string.cancel, null)
.show(); .show();
alert.setOnDismissListener(dialog->updatePauseItem()); alert.setOnDismissListener(dialog->updatePauseItem());
alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
@@ -216,20 +211,18 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{
R.string.notifications_policy_follower, R.string.notifications_policy_follower,
R.string.notifications_policy_no_one R.string.notifications_policy_no_one
).map(this::getString).toArray(String[]::new); ).map(this::getString).toArray(String[]::new);
int[] selectedItem={getPushSubscription().policy.ordinal()};
new M3AlertDialogBuilder(getActivity()) new M3AlertDialogBuilder(getActivity())
.setTitle(R.string.settings_notifications_policy) .setTitle(R.string.settings_notifications_policy)
.setSingleChoiceItems(items, selectedItem[0], (dlg, which)->selectedItem[0]=which) .setSingleChoiceItems(items, getPushSubscription().policy.ordinal(), (dlg, which)->{
.setPositiveButton(R.string.ok, (dlg, which)->{ dlg.dismiss();
PushSubscription.Policy prevValue=getPushSubscription().policy; PushSubscription.Policy prevValue=getPushSubscription().policy;
PushSubscription.Policy newValue=PushSubscription.Policy.values()[selectedItem[0]]; PushSubscription.Policy newValue=PushSubscription.Policy.values()[which];
if(prevValue==newValue) if(prevValue==newValue)
return; return;
getPushSubscription().policy=newValue; getPushSubscription().policy=newValue;
updatePolicyItem(prevValue); updatePolicyItem(prevValue);
needUpdateNotificationSettings=true; needUpdateNotificationSettings=true;
}) })
.setNegativeButton(R.string.cancel, null)
.show(); .show();
} }

View File

@@ -28,6 +28,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@@ -47,6 +48,7 @@ public class ComposeLanguageAlertViewController{
private int selectedIndex=0; private int selectedIndex=0;
private Locale selectedLocale; private Locale selectedLocale;
private Locale detectedLocale; private Locale detectedLocale;
private Consumer<SelectedOption> selectionListener;
public ComposeLanguageAlertViewController(Context context, String preferred, SelectedOption previouslySelected, String postText){ public ComposeLanguageAlertViewController(Context context, String preferred, SelectedOption previouslySelected, String postText){
this.context=context; this.context=context;
@@ -221,6 +223,12 @@ public class ComposeLanguageAlertViewController{
if(holder!=null && holder.itemView instanceof Checkable checkable) if(holder!=null && holder.itemView instanceof Checkable checkable)
checkable.setChecked(true); checkable.setChecked(true);
selectedIndex=index; selectedIndex=index;
if(selectionListener!=null)
selectionListener.accept(getSelectedOption());
}
public void setSelectionListener(Consumer<SelectedOption> selectionListener){
this.selectionListener=selectionListener;
} }
private class AllLocalesAdapter extends RecyclerView.Adapter<SimpleLanguageViewHolder>{ private class AllLocalesAdapter extends RecyclerView.Adapter<SimpleLanguageViewHolder>{
@@ -264,8 +272,8 @@ public class ComposeLanguageAlertViewController{
@Override @Override
public void onClick(){ public void onClick(){
selectItem(getAbsoluteAdapterPosition());
selectedLocale=item.locale; selectedLocale=item.locale;
selectItem(getAbsoluteAdapterPosition());
} }
} }
@@ -321,8 +329,8 @@ public class ComposeLanguageAlertViewController{
@Override @Override
public void onClick(){ public void onClick(){
selectItem(getAbsoluteAdapterPosition());
selectedLocale=item.locale; selectedLocale=item.locale;
selectItem(getAbsoluteAdapterPosition());
} }
@Override @Override

View File

@@ -190,28 +190,20 @@ public class ComposePollViewController{
if(l==pollDuration) if(l==pollDuration)
selectedOption=i; selectedOption=i;
} }
int[] chosenOption={0};
new M3AlertDialogBuilder(fragment.getActivity()) new M3AlertDialogBuilder(fragment.getActivity())
.setSingleChoiceItems(options, selectedOption, (dialog, which)->chosenOption[0]=which) .setSingleChoiceItems(options, selectedOption, (dialog, which)->{
.setTitle(R.string.poll_length) pollDuration=POLL_LENGTH_OPTIONS[which];
.setPositiveButton(R.string.ok, (dialog, which)->{
pollDuration=POLL_LENGTH_OPTIONS[chosenOption[0]];
pollDurationValue.setText(UiUtils.formatDuration(fragment.getContext(), pollDuration)); pollDurationValue.setText(UiUtils.formatDuration(fragment.getContext(), pollDuration));
dialog.dismiss();
}) })
.setNegativeButton(R.string.cancel, null) .setTitle(R.string.poll_length)
.show(); .show();
} }
private void showPollStyleAlert(){ private void showPollStyleAlert(){
final int[] option={pollIsMultipleChoice ? R.id.multiple_choice : R.id.single_choice};
AlertDialog alert=new M3AlertDialogBuilder(fragment.getActivity()) AlertDialog alert=new M3AlertDialogBuilder(fragment.getActivity())
.setView(R.layout.poll_style) .setView(R.layout.poll_style)
.setTitle(R.string.poll_style_title) .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(); .show();
CheckableLinearLayout multiple=alert.findViewById(R.id.multiple_choice); CheckableLinearLayout multiple=alert.findViewById(R.id.multiple_choice);
CheckableLinearLayout single=alert.findViewById(R.id.single_choice); CheckableLinearLayout single=alert.findViewById(R.id.single_choice);
@@ -219,11 +211,9 @@ public class ComposePollViewController{
multiple.setChecked(pollIsMultipleChoice); multiple.setChecked(pollIsMultipleChoice);
View.OnClickListener listener=v->{ View.OnClickListener listener=v->{
int id=v.getId(); int id=v.getId();
if(id==option[0]) pollIsMultipleChoice=id==R.id.multiple_choice;
return; pollStyleValue.setText(pollIsMultipleChoice ? R.string.compose_poll_multiple_choice : R.string.compose_poll_single_choice);
((Checkable) alert.findViewById(option[0])).setChecked(false); alert.dismiss();
((Checkable) v).setChecked(true);
option[0]=id;
}; };
single.setOnClickListener(listener); single.setOnClickListener(listener);
multiple.setOnClickListener(listener); multiple.setOnClickListener(listener);