Add delete button to ConfigureProfileFragment

This commit is contained in:
Dima-1 2019-12-23 11:48:41 +02:00
parent 0e81f6bb38
commit 2497dc1640
2 changed files with 53 additions and 1 deletions

View file

@ -78,6 +78,13 @@
android:title="@string/export_profile"
tools:icon="@drawable/ic_action_app_configuration" />
<Preference
android:key="delete_profile"
android:layout="@layout/preference_button"
android:persistent="false"
android:title="@string/profile_alert_delete_title"
tools:icon="@drawable/ic_action_delete_dark" />
<Preference
android:key="export_profile_descr"
android:layout="@layout/preference_info_descr_with_title"

View file

@ -2,6 +2,7 @@ package net.osmand.plus.settings;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
@ -14,6 +15,7 @@ import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
@ -25,6 +27,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
@ -66,6 +69,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
private static final String CONFIGURE_MAP = "configure_map";
private static final String CONFIGURE_SCREEN = "configure_screen";
private static final String EXPORT_PROFILE = "export_profile";
private static final String DELETE_PROFILE = "delete_profile";
private static final String PROFILE_APPEARANCE = "profile_appearance";
@ColorRes
@ -204,6 +208,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
settingsActions.setIconSpaceReserved(false);
setupExportProfilePref();
setupDeleteProfilePref();
}
private void setupNavigationSettingsPref() {
@ -261,6 +266,12 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light));
}
private void setupDeleteProfilePref() {
Preference deleteProfile = findPreference(DELETE_PROFILE);
deleteProfile.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_delete_dark,
isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light));
}
private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) {
try {
Context ctx = requireContext();
@ -379,11 +390,45 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
}
}
}, new ProfileSettingsItem(app.getSettings(), profile));
} else if (DELETE_PROFILE.equals(prefId)) {
onDeleteProfileClick();
}
return super.onPreferenceClick(preference);
}
void onDeleteProfileClick() {
final ApplicationMode profile = getSelectedAppMode();
if (getActivity() != null) {
if (profile.getParent() != null) {
AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
bld.setTitle(R.string.profile_alert_delete_title);
bld.setMessage(String
.format(getString(R.string.profile_alert_delete_msg),
profile.getCustomProfileName()));
bld.setPositiveButton(R.string.shared_string_delete,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
OsmandApplication app = getMyApplication();
if (app != null) {
ApplicationMode.deleteCustomMode(ApplicationMode.valueOfStringKey(profile.getStringKey(), ApplicationMode.DEFAULT), app);
app.getSettings().APPLICATION_MODE.set(ApplicationMode.DEFAULT);
}
if (getActivity() != null) {
getActivity().onBackPressed();
}
}
});
bld.setNegativeButton(R.string.shared_string_dismiss, null);
bld.show();
} else {
Toast.makeText(getActivity(), R.string.profile_alert_cant_delete_base,
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();