show details about what will happen and get confirmation

This commit is contained in:
B. Petersen
2020-03-08 01:47:06 +01:00
parent eb9a759314
commit bcacc09449
4 changed files with 75 additions and 14 deletions
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/i_understand"
android:text="@string/pref_autodel_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
/>
</FrameLayout>
+4
View File
@@ -376,6 +376,10 @@
<string name="pref_autodel_headline">Ephemeral messages</string>
<string name="pref_autodel_device">Delete from device</string>
<string name="pref_autodel_server">Delete from server</string>
<!-- %1$d will be replaced by the number of messages, you can assume plural/lots here. %1$s will be replaced by a timespan in italics or so. -->
<string name="pref_autodel_device_ask">Do you really want to delete %1$d messages now and all new messages %2$s in the future?\n\n\"Saved messages\" are skipped from deletion.</string>
<string name="pref_autodel_server_ask">Do you really want to delete %1$d messages now and all new messages %2$s in the future?\n\n• If you have enabled \"Show classic emails\" or enable it in the future, the affected emails will be deleted from the server as well.\n\n\• "Saved messages\" are skipped from deletion.</string>
<string name="pref_autodel_confirm">I understand, delete all these messages</string>
<string name="pref_blocked_contacts">Blocked contacts</string>
<string name="pref_profile_photo_remove_ask">Remove profile photo?</string>
<string name="pref_help_url">https://delta.chat/help</string>
@@ -12,7 +12,10 @@ import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import com.b44t.messenger.DcContext;
@@ -62,18 +65,10 @@ public class ChatsPreferenceFragment extends ListSummaryPreferenceFragment {
backup.setOnPreferenceClickListener(new BackupListener());
autoDelDevice = findPreference("autodel_device");
autoDelDevice.setOnPreferenceChangeListener((preference, newValue) -> {
updateListSummary(preference, newValue);
dcContext.setConfigInt("delete_device_after", Util.objectToInt(newValue));
return true;
});
autoDelDevice.setOnPreferenceChangeListener(new AutodelChangeListener("delete_device_after"));
autoDelServer = findPreference("autodel_server");
autoDelServer.setOnPreferenceChangeListener((preference, newValue) -> {
updateListSummary(preference, newValue);
dcContext.setConfigInt("delete_server_after", Util.objectToInt(newValue));
return true;
});
autoDelServer.setOnPreferenceChangeListener(new AutodelChangeListener("delete_server_after"));
}
@Override
@@ -143,6 +138,47 @@ public class ChatsPreferenceFragment extends ListSummaryPreferenceFragment {
}
}
private class AutodelChangeListener implements Preference.OnPreferenceChangeListener {
private String coreKey;
AutodelChangeListener(String coreKey) {
this.coreKey = coreKey;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int timeout = Util.objectToInt(newValue);
if (timeout>0) {
Context context = preference.getContext();
int delNowMsgCount = 666; // XXX TODO: get correct count from core.
View gl = View.inflate(getActivity(), R.layout.autodel_confirm, null);
CheckBox confirmCheckbox = gl.findViewById(R.id.i_understand);
String timeoutDescr = getSelectedSummary(preference, newValue);
String msg = context.getString(coreKey.equals("delete_server_after")?
R.string.pref_autodel_server_ask : R.string.pref_autodel_device_ask,
delNowMsgCount, "<i>"+timeoutDescr+"</i>");
new AlertDialog.Builder(context)
.setTitle(preference.getTitle())
.setMessage(Html.fromHtml(msg.replace("\n", "<br>")))
.setView(gl)
.setPositiveButton(android.R.string.ok, (dialog, whichButton) -> {
if (confirmCheckbox.isChecked()) {
updateListSummary(preference, newValue);
dcContext.setConfigInt(coreKey, timeout);
} else {
onPreferenceChange(preference, newValue);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else {
updateListSummary(preference, newValue);
dcContext.setConfigInt(coreKey, timeout);
}
return true;
}
}
/***********************************************************************************************
* Backup
**********************************************************************************************/
@@ -44,13 +44,17 @@ public abstract class ListSummaryPreferenceFragment extends CorrectedPreferenceF
}
}
protected void updateListSummary(Preference preference, Object value) {
protected String getSelectedSummary(Preference preference, Object value) {
ListPreference listPref = (ListPreference) preference;
int entryIndex = Arrays.asList(listPref.getEntryValues()).indexOf(value);
return entryIndex >= 0 && entryIndex < listPref.getEntries().length
? listPref.getEntries()[entryIndex].toString()
: getString(R.string.unknown);
}
listPref.setSummary(entryIndex >= 0 && entryIndex < listPref.getEntries().length
? listPref.getEntries()[entryIndex]
: getString(R.string.unknown));
protected void updateListSummary(Preference preference, Object value) {
ListPreference listPref = (ListPreference) preference;
listPref.setSummary(getSelectedSummary(preference, value));
}
protected void initializeListSummary(ListPreference pref) {