deleting profiles - done.

checks on save - name, type, icon
This commit is contained in:
madwasp79 2019-04-23 17:50:43 +03:00
parent 7b60c15a49
commit 2de8df6407
6 changed files with 111 additions and 58 deletions

View file

@ -190,7 +190,7 @@ public class AppInitializer implements IProgress {
} }
app.getSettings().SHOW_TRAVEL_UPDATE_CARD.set(true); app.getSettings().SHOW_TRAVEL_UPDATE_CARD.set(true);
app.getSettings().SHOW_TRAVEL_NEEDED_MAPS_CARD.set(true); app.getSettings().SHOW_TRAVEL_NEEDED_MAPS_CARD.set(true);
ApplicationMode.initCustomProfiles(app.getSettings()); ApplicationMode.initCustomModes(app.getSettings());
initSettings = true; initSettings = true;
} }

View file

@ -8,6 +8,8 @@ import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.StateChangedListener; import net.osmand.StateChangedListener;
@ -416,7 +418,7 @@ public class ApplicationMode {
} }
public String toHumanString(Context ctx) { public String toHumanString(Context ctx) {
if (Algorithms.isEmpty(userProfileName)) { if (Algorithms.isEmpty(userProfileName) && key != -1) {
return ctx.getString(key); return ctx.getString(key);
} else { } else {
return userProfileName; return userProfileName;
@ -471,7 +473,7 @@ public class ApplicationMode {
@Expose private final int key; @Expose private final int key;
@Expose private final String stringKey; @Expose private final String stringKey;
@Expose private String userProfileName = ""; @Expose private String userProfileName;
@Expose private int mapIconsSetId = 0; @Expose private int mapIconsSetId = 0;
@Expose private ApplicationMode parent; @Expose private ApplicationMode parent;
@Expose private int mapIconId = R.drawable.map_world_globe_dark; @Expose private int mapIconId = R.drawable.map_world_globe_dark;
@ -491,8 +493,7 @@ public class ApplicationMode {
private static StateChangedListener<String> listener; private static StateChangedListener<String> listener;
private static OsmAndAppCustomization.OsmAndAppCustomizationListener customizationListener; private static OsmAndAppCustomization.OsmAndAppCustomizationListener customizationListener;
public static void saveCustomModeToSettings(OsmandSettings settings){
public void saveCustomProfileToSettings(OsmandSettings settings){
List<ApplicationMode> customModes = new ArrayList<>(); List<ApplicationMode> customModes = new ArrayList<>();
for (ApplicationMode mode : values) { for (ApplicationMode mode : values) {
if (mode.parent != null) { if (mode.parent != null) {
@ -504,9 +505,7 @@ public class ApplicationMode {
settings.CUSTOM_APP_PROFILES.set(profiles); settings.CUSTOM_APP_PROFILES.set(profiles);
} }
public static void initCustomModes(OsmandSettings settings){
public static boolean initCustomProfiles(OsmandSettings settings){
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Type t = new TypeToken<ArrayList<ApplicationMode>>() {}.getType(); Type t = new TypeToken<ArrayList<ApplicationMode>>() {}.getType();
List<ApplicationMode> customProfiles = gson.fromJson(settings.CUSTOM_APP_PROFILES.get(), t); List<ApplicationMode> customProfiles = gson.fromJson(settings.CUSTOM_APP_PROFILES.get(), t);
@ -515,17 +514,20 @@ public class ApplicationMode {
for (ApplicationMode m : customProfiles) { for (ApplicationMode m : customProfiles) {
if (!values.contains(m)) { if (!values.contains(m)) {
values.add(m); values.add(m);
if (m.getParent() != null) { }
LOG.debug("parent: " + m.getParent().getStringKey()); }
} else { }
LOG.debug("parent: propal!!!!!111 " );
} }
public static void deleteCustomMode(String userModeTitle, OsmandApplication app) {
Iterator<ApplicationMode> it = values.iterator();
while (it.hasNext()) {
ApplicationMode m = it.next();
if (m.userProfileName == userModeTitle) {
it.remove();
} }
} }
return true; ApplicationMode.saveCustomModeToSettings(app.getSettings());
}
return false;
} }

View file

@ -1,20 +1,20 @@
package net.osmand.plus.profiles; package net.osmand.plus.profiles;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.util.Algorithms;
public class EditProfileActivity extends OsmandActionBarActivity { public class EditProfileActivity extends OsmandActionBarActivity {
public static final int DELETE_ID = 1010;
public static final String FRAGMENT_TAG = "editProfileFragment";
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
getMyApplication().applyTheme(this); getMyApplication().applyTheme(this);
@ -25,34 +25,29 @@ public class EditProfileActivity extends OsmandActionBarActivity {
EditProfileFragment editProfileFragment = new EditProfileFragment(); EditProfileFragment editProfileFragment = new EditProfileFragment();
editProfileFragment.setArguments(getIntent().getExtras()); editProfileFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, getSupportFragmentManager().beginTransaction().add(android.R.id.content,
editProfileFragment, "editProfileFragment").commit(); editProfileFragment, FRAGMENT_TAG).commit();
} }
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuItem m = menu.add(0, DELETE_ID,0, R.string.action_delete).setIcon(R.drawable.ic_action_delete_dark);
MenuItem m = menu.add(0, 0, 0, R.string.action_delete).setIcon(R.drawable.ic_action_delete_dark);
MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS); MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS);
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);
return true; return true;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId(); int itemId = item.getItemId();
switch (itemId) { switch (itemId) {
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;
case DELETE_ID:
((EditProfileFragment)getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG)).onDeleteProfileClick();
return true;
} }
return false; return false;

View file

@ -1,19 +1,19 @@
package net.osmand.plus.profiles; package net.osmand.plus.profiles;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -24,10 +24,13 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandActionBarActivity; 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.profiles.SelectIconBottomSheetDialogFragment.IconIdListener;
import net.osmand.plus.widgets.OsmandTextFieldBoxes; import net.osmand.plus.widgets.OsmandTextFieldBoxes;
import net.osmand.router.GeneralRouter.GeneralRouterProfile; import net.osmand.router.GeneralRouter.GeneralRouterProfile;
import net.sf.junidecode.App;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import studio.carbonylgroup.textfieldboxes.ExtendedEditText; import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
@ -297,18 +301,38 @@ public class EditProfileFragment extends BaseOsmAndFragment {
return updateProfile(); return updateProfile();
} }
List<ApplicationMode> copyAllModes = new ArrayList<>(ApplicationMode.allPossibleValues()); if (profile.getUserProfileTitle().isEmpty()) {
// List<ApplicationMode> copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication())); AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
Iterator<ApplicationMode> it = copyAllModes.iterator(); bld.setTitle("Enter Profile Name");
while (it.hasNext()) { bld.setMessage("Profile name shouldn't be empty!");
ApplicationMode am = it.next(); bld.setNegativeButton(R.string.shared_string_dismiss, null);
if (am.getStringKey().equals(profile.stringKey)) { bld.show();
if (ApplicationMode.values(getMyApplication()).contains(am)) { bld.setOnDismissListener(new OnDismissListener() {
//todo unregister mode from available @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; String customStringKey = profile.stringKey;
if (isNew && profile.getParent() != null) { if (isNew && profile.getParent() != null) {
customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode(); customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode();
@ -332,11 +356,18 @@ public class EditProfileFragment extends BaseOsmAndFragment {
break; break;
} }
ApplicationMode customMode = builder.customReg(); ApplicationMode mode = builder.customReg();
customMode.saveCustomProfileToSettings(getSettings()); ApplicationMode.saveCustomModeToSettings(getSettings());
//todo build profile, save and register:
StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+",");
Set<ApplicationMode> 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; return true;
} }
@ -345,8 +376,27 @@ public class EditProfileFragment extends BaseOsmAndFragment {
return false; 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<RoutingProfile> getRoutingProfiles() { private ArrayList<RoutingProfile> getRoutingProfiles() {
ArrayList<RoutingProfile> routingProfiles = new ArrayList<>(); ArrayList<RoutingProfile> routingProfiles = new ArrayList<>();
@ -449,4 +499,5 @@ public class EditProfileFragment extends BaseOsmAndFragment {
this.routingProfile = routingProfile; this.routingProfile = routingProfile;
} }
} }
} }

View file

@ -63,7 +63,7 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder>
holder.descr.setText(String.format("Type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getParent().getStringKey().replace("_", " ")))); holder.descr.setText(String.format("Type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getParent().getStringKey().replace("_", " "))));
} else { } else {
holder.title.setText(app.getResources().getString(item.getStringResource())); 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) holder.title.setTextColor(app.getResources().getColor(isNightMode(app)
@ -79,12 +79,18 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder>
listener.changeProfileStatus(item, holder.aSwitch.isChecked()); listener.changeProfileStatus(item, holder.aSwitch.isChecked());
} }
}); });
if (item.getParent() != null) {
holder.profileOptions.setOnClickListener(new OnClickListener() { holder.profileOptions.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
listener.editProfile(item); listener.editProfile(item);
} }
}); });
} else {
holder.profileOptions.setVisibility(View.INVISIBLE);
}
} }
@Override @Override

View file

@ -31,7 +31,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
private ProfileMenuAdapter adapter; private ProfileMenuAdapter adapter;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private LinearLayout addProfileBtn; private LinearLayout addNewProfileBtn;
ProfileListener listener = null; ProfileListener listener = null;
ProfileTypeDialogListener typeListener = null; ProfileTypeDialogListener typeListener = null;
@ -59,7 +59,6 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
listener = new ProfileListener() { listener = new ProfileListener() {
@Override @Override
public void changeProfileStatus(ApplicationMode item, boolean isSelected) { public void changeProfileStatus(ApplicationMode item, boolean isSelected) {
//LOG.debug(getString(item.getStringResource()) + " - " + isSelected);
StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+","); StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+",");
ApplicationMode mode = null; ApplicationMode mode = null;
for (ApplicationMode sam : allAppModes) { for (ApplicationMode sam : allAppModes) {
@ -108,9 +107,9 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
View view = inflater.inflate(R.layout.profiles_list_fragment, container, false); View view = inflater.inflate(R.layout.profiles_list_fragment, container, false);
recyclerView = view.findViewById(R.id.profiles_list); 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 @Override