From 2de8df64076334b0748fc042d5e0660acbd18273 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Tue, 23 Apr 2019 17:50:43 +0300 Subject: [PATCH] deleting profiles - done. checks on save - name, type, icon --- .../src/net/osmand/plus/AppInitializer.java | 2 +- .../src/net/osmand/plus/ApplicationMode.java | 32 +++---- .../plus/profiles/EditProfileActivity.java | 21 ++--- .../plus/profiles/EditProfileFragment.java | 87 +++++++++++++++---- .../plus/profiles/ProfileMenuAdapter.java | 20 +++-- .../profiles/SettingsProfileFragment.java | 7 +- 6 files changed, 111 insertions(+), 58 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index dbb5f3cc24..57fa4653c4 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -190,7 +190,7 @@ public class AppInitializer implements IProgress { } app.getSettings().SHOW_TRAVEL_UPDATE_CARD.set(true); app.getSettings().SHOW_TRAVEL_NEEDED_MAPS_CARD.set(true); - ApplicationMode.initCustomProfiles(app.getSettings()); + ApplicationMode.initCustomModes(app.getSettings()); initSettings = true; } diff --git a/OsmAnd/src/net/osmand/plus/ApplicationMode.java b/OsmAnd/src/net/osmand/plus/ApplicationMode.java index 02ae475b9f..f92eac8784 100644 --- a/OsmAnd/src/net/osmand/plus/ApplicationMode.java +++ b/OsmAnd/src/net/osmand/plus/ApplicationMode.java @@ -8,6 +8,8 @@ import com.google.gson.annotations.Expose; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; import net.osmand.PlatformUtil; import net.osmand.StateChangedListener; @@ -416,7 +418,7 @@ public class ApplicationMode { } public String toHumanString(Context ctx) { - if (Algorithms.isEmpty(userProfileName)) { + if (Algorithms.isEmpty(userProfileName) && key != -1) { return ctx.getString(key); } else { return userProfileName; @@ -471,7 +473,7 @@ public class ApplicationMode { @Expose private final int key; @Expose private final String stringKey; - @Expose private String userProfileName = ""; + @Expose private String userProfileName; @Expose private int mapIconsSetId = 0; @Expose private ApplicationMode parent; @Expose private int mapIconId = R.drawable.map_world_globe_dark; @@ -491,8 +493,7 @@ public class ApplicationMode { private static StateChangedListener listener; private static OsmAndAppCustomization.OsmAndAppCustomizationListener customizationListener; - - public void saveCustomProfileToSettings(OsmandSettings settings){ + public static void saveCustomModeToSettings(OsmandSettings settings){ List customModes = new ArrayList<>(); for (ApplicationMode mode : values) { if (mode.parent != null) { @@ -504,9 +505,7 @@ public class ApplicationMode { settings.CUSTOM_APP_PROFILES.set(profiles); } - - - public static boolean initCustomProfiles(OsmandSettings settings){ + public static void initCustomModes(OsmandSettings settings){ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); Type t = new TypeToken>() {}.getType(); List customProfiles = gson.fromJson(settings.CUSTOM_APP_PROFILES.get(), t); @@ -515,17 +514,20 @@ public class ApplicationMode { for (ApplicationMode m : customProfiles) { if (!values.contains(m)) { values.add(m); - if (m.getParent() != null) { - LOG.debug("parent: " + m.getParent().getStringKey()); - } else { - LOG.debug("parent: propal!!!!!111 " ); - } - } } - return true; } - return false; + } + + public static void deleteCustomMode(String userModeTitle, OsmandApplication app) { + Iterator it = values.iterator(); + while (it.hasNext()) { + ApplicationMode m = it.next(); + if (m.userProfileName == userModeTitle) { + it.remove(); + } + } + ApplicationMode.saveCustomModeToSettings(app.getSettings()); } diff --git a/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java b/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java index 631d3ea94a..76f8af340d 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java +++ b/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java @@ -1,20 +1,20 @@ package net.osmand.plus.profiles; -import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.view.MenuItemCompat; import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import net.osmand.plus.R; import net.osmand.plus.activities.OsmandActionBarActivity; -import net.osmand.util.Algorithms; public class EditProfileActivity extends OsmandActionBarActivity { + public static final int DELETE_ID = 1010; + public static final String FRAGMENT_TAG = "editProfileFragment"; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { getMyApplication().applyTheme(this); @@ -25,34 +25,29 @@ public class EditProfileActivity extends OsmandActionBarActivity { EditProfileFragment editProfileFragment = new EditProfileFragment(); editProfileFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().add(android.R.id.content, - editProfileFragment, "editProfileFragment").commit(); + editProfileFragment, FRAGMENT_TAG).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { - - MenuItem m = menu.add(0, 0, 0, R.string.action_delete).setIcon(R.drawable.ic_action_delete_dark); + MenuItem m = menu.add(0, DELETE_ID,0, R.string.action_delete).setIcon(R.drawable.ic_action_delete_dark); MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS); super.onCreateOptionsMenu(menu); return true; - } - - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - - - } @Override public boolean onOptionsItemSelected(MenuItem item) { - int itemId = item.getItemId(); switch (itemId) { case android.R.id.home: finish(); return true; + case DELETE_ID: + ((EditProfileFragment)getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG)).onDeleteProfileClick(); + return true; } return false; diff --git a/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java index 694042132e..7517e18bb4 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java @@ -1,19 +1,19 @@ package net.osmand.plus.profiles; +import android.app.Activity; +import android.content.DialogInterface; +import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.graphics.drawable.GradientDrawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; -import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBar; +import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; @@ -24,10 +24,13 @@ import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import net.osmand.PlatformUtil; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.OsmandActionBarActivity; @@ -37,6 +40,7 @@ import net.osmand.plus.profiles.ProfileBottomSheetDialogFragment.ProfileTypeDial import net.osmand.plus.profiles.SelectIconBottomSheetDialogFragment.IconIdListener; import net.osmand.plus.widgets.OsmandTextFieldBoxes; import net.osmand.router.GeneralRouter.GeneralRouterProfile; +import net.sf.junidecode.App; import org.apache.commons.logging.Log; import studio.carbonylgroup.textfieldboxes.ExtendedEditText; @@ -297,18 +301,38 @@ public class EditProfileFragment extends BaseOsmAndFragment { return updateProfile(); } - List copyAllModes = new ArrayList<>(ApplicationMode.allPossibleValues()); -// List copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication())); - Iterator it = copyAllModes.iterator(); - while (it.hasNext()) { - ApplicationMode am = it.next(); - if (am.getStringKey().equals(profile.stringKey)) { - if (ApplicationMode.values(getMyApplication()).contains(am)) { - //todo unregister mode from available + if (profile.getUserProfileTitle().isEmpty()) { + AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); + bld.setTitle("Enter Profile Name"); + bld.setMessage("Profile name shouldn't be empty!"); + bld.setNegativeButton(R.string.shared_string_dismiss, null); + bld.show(); + bld.setOnDismissListener(new OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + //todo focus name textview } - it.remove(); + }); + return false; + } + + for (ApplicationMode m : ApplicationMode.allPossibleValues()) { + if (m.getUserProfileName()!=null && m.getUserProfileName().equals(profile.getUserProfileTitle())) { + AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); + bld.setTitle("Duplicate Name"); + bld.setMessage("There is already profile with such name"); + bld.setNegativeButton(R.string.shared_string_dismiss, null); + bld.show(); + bld.setOnDismissListener(new OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + //todo focus name textview + } + }); + return false; } } + String customStringKey = profile.stringKey; if (isNew && profile.getParent() != null) { customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode(); @@ -332,11 +356,18 @@ public class EditProfileFragment extends BaseOsmAndFragment { break; } - ApplicationMode customMode = builder.customReg(); - customMode.saveCustomProfileToSettings(getSettings()); - - //todo build profile, save and register: + ApplicationMode mode = builder.customReg(); + ApplicationMode.saveCustomModeToSettings(getSettings()); + StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+","); + Set availableAppModes = new LinkedHashSet<>(ApplicationMode.values(getMyApplication())); + availableAppModes.add(mode); + for (ApplicationMode sam : availableAppModes) { + vls.append(sam.getStringKey()).append(","); + } + if (getSettings() != null) { + getSettings().AVAILABLE_APP_MODES.set(vls.toString()); + } return true; } @@ -345,8 +376,27 @@ public class EditProfileFragment extends BaseOsmAndFragment { return false; } + void onDeleteProfileClick() { + if (getActivity()!=null) { + AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); + bld.setTitle("Delete Profile"); + bld.setMessage(String.format("Are you sure you want to delete %s profile", profile.userProfileTitle)); + bld.setPositiveButton(R.string.shared_string_delete, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ApplicationMode.deleteCustomMode(profile.getUserProfileTitle(), getMyApplication()); + if (getActivity() != null) { + getActivity().onBackPressed(); + } + } + }); + bld.setNegativeButton(R.string.shared_string_dismiss, null); + bld.show(); + } + } + /** - * For now there are only default nav profiles todo: add profiles from custom routing xml-s + * For now there are only default nav profiles placeholders todo: add profiles from custom routing xml-s */ private ArrayList getRoutingProfiles() { ArrayList routingProfiles = new ArrayList<>(); @@ -449,4 +499,5 @@ public class EditProfileFragment extends BaseOsmAndFragment { this.routingProfile = routingProfile; } } + } diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java index a4122b63c4..341a148da0 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java @@ -63,7 +63,7 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter holder.descr.setText(String.format("Type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getParent().getStringKey().replace("_", " ")))); } else { holder.title.setText(app.getResources().getString(item.getStringResource())); - holder.descr.setText(String.format("Type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getStringKey().replace("_", " ")))); + holder.descr.setText(String.format("Base Profile, type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getStringKey().replace("_", " ")))); } holder.title.setTextColor(app.getResources().getColor(isNightMode(app) @@ -79,12 +79,18 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter listener.changeProfileStatus(item, holder.aSwitch.isChecked()); } }); - holder.profileOptions.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - listener.editProfile(item); - } - }); + + if (item.getParent() != null) { + holder.profileOptions.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + listener.editProfile(item); + } + }); + } else { + holder.profileOptions.setVisibility(View.INVISIBLE); + } + } @Override diff --git a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java index 96d353911c..e83c8418b2 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java @@ -31,7 +31,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { private ProfileMenuAdapter adapter; private RecyclerView recyclerView; - private LinearLayout addProfileBtn; + private LinearLayout addNewProfileBtn; ProfileListener listener = null; ProfileTypeDialogListener typeListener = null; @@ -59,7 +59,6 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { listener = new ProfileListener() { @Override public void changeProfileStatus(ApplicationMode item, boolean isSelected) { - //LOG.debug(getString(item.getStringResource()) + " - " + isSelected); StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+","); ApplicationMode mode = null; for (ApplicationMode sam : allAppModes) { @@ -108,9 +107,9 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { View view = inflater.inflate(R.layout.profiles_list_fragment, container, false); recyclerView = view.findViewById(R.id.profiles_list); - addProfileBtn = view.findViewById(R.id.add_profile_btn); + addNewProfileBtn = view.findViewById(R.id.add_profile_btn); - addProfileBtn.setOnClickListener(new OnClickListener() { + addNewProfileBtn.setOnClickListener(new OnClickListener() { @Override