optimizing profile menu

This commit is contained in:
madwasp79 2019-04-09 18:03:39 +03:00
parent f224cdf7d8
commit 73be369ac6
6 changed files with 81 additions and 75 deletions

View file

@ -19,7 +19,7 @@ public class ApplicationMode {
private static List<ApplicationMode> values = new ArrayList<>(); private static List<ApplicationMode> values = new ArrayList<>();
private static List<ApplicationMode> cachedFilteredValues = new ArrayList<>(); private static List<ApplicationMode> cachedFilteredValues = new ArrayList<>();
/* /*
* DEFAULT("Browse map"), CAR("Car"), BICYCLE("Bicycle"), PEDESTRIAN("Pedestrian"); NAUTICAL("boat") * DEFAULT("Browse map"), CAR("Car"), BICYCLE("Bicycle"), PEDESTRIAN("Pedestrian"); NAUTICAL("boat"); PUBLIC_TRANSPORT("Public transport")
*/ */
public static final ApplicationMode DEFAULT = create(R.string.app_mode_default, "default").speed(1.5f, 5).arrivalDistance(90).defLocation(). public static final ApplicationMode DEFAULT = create(R.string.app_mode_default, "default").speed(1.5f, 5).arrivalDistance(90).defLocation().
icon(R.drawable.map_world_globe_dark, R.drawable.ic_world_globe_dark).reg(); icon(R.drawable.map_world_globe_dark, R.drawable.ic_world_globe_dark).reg();
@ -57,8 +57,8 @@ public class ApplicationMode {
carLocation().icon(R.drawable.map_action_train, R.drawable.ic_action_train).reg(); carLocation().icon(R.drawable.map_action_train, R.drawable.ic_action_train).reg();
static { static {
ApplicationMode[] exceptDefault = new ApplicationMode[]{CAR, PEDESTRIAN, BICYCLE, BOAT, AIRCRAFT, PUBLIC_TRANSPORT, TRAIN}; ApplicationMode[] exceptDefault = new ApplicationMode[]{CAR, PEDESTRIAN, BICYCLE, BOAT, PUBLIC_TRANSPORT};
ApplicationMode[] exceptPedestrianAndDefault = new ApplicationMode[]{CAR, BICYCLE, BOAT, AIRCRAFT, PUBLIC_TRANSPORT, TRAIN}; ApplicationMode[] exceptPedestrianAndDefault = new ApplicationMode[]{CAR, BICYCLE, BOAT, PUBLIC_TRANSPORT};
ApplicationMode[] exceptAirBoatDefault = new ApplicationMode[]{CAR, BICYCLE, PEDESTRIAN}; ApplicationMode[] exceptAirBoatDefault = new ApplicationMode[]{CAR, BICYCLE, PEDESTRIAN};
ApplicationMode[] pedestrian = new ApplicationMode[]{PEDESTRIAN}; ApplicationMode[] pedestrian = new ApplicationMode[]{PEDESTRIAN};
ApplicationMode[] pedestrianBicycle = new ApplicationMode[]{PEDESTRIAN, BICYCLE}; ApplicationMode[] pedestrianBicycle = new ApplicationMode[]{PEDESTRIAN, BICYCLE};
@ -101,6 +101,7 @@ public class ApplicationMode {
} }
private static class ApplicationModeBuilder { private static class ApplicationModeBuilder {
@ -191,6 +192,10 @@ public class ApplicationMode {
return builder; return builder;
} }
public static ApplicationModeBuilder createCustomMode(int key, String stringKey) {
return create(key, stringKey);
}
private ApplicationMode(int key, String stringKey) { private ApplicationMode(int key, String stringKey) {
this.key = key; this.key = key;
this.stringKey = stringKey; this.stringKey = stringKey;

View file

@ -2,13 +2,21 @@ package net.osmand.plus.profiles;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import net.osmand.plus.ApplicationMode;
public class AppProfile implements Parcelable { public class AppProfile implements Parcelable {
private int iconRes; private int iconRes;
private String title; private String title;
private String navType; private String navType;
private boolean isSelected; private boolean isSelected;
private String prefKey;
private float defSpeed;
private int distForTurn;
private int arrivalDistance;
private int offRouteDistance;
public AppProfile(int iconRes, String title, String descr) { public AppProfile(int iconRes, String title, String descr) {
this.iconRes = iconRes; this.iconRes = iconRes;
@ -17,7 +25,6 @@ public class AppProfile implements Parcelable {
} }
public int getIconRes() { public int getIconRes() {
return iconRes; return iconRes;
} }

View file

@ -13,13 +13,11 @@ import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.TextView; import android.widget.TextView;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment; import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.router.GeneralRouter.GeneralRouterProfile;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
public class NavTypeBottomSheetDialogFragment extends BottomSheetDialogFragment { public class NavTypeBottomSheetDialogFragment extends BottomSheetDialogFragment {

View file

@ -10,35 +10,40 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileViewHolder; import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileViewHolder;
import net.sf.junidecode.App;
public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder> { public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder> {
private List<AppProfile> items; private List<ApplicationMode> items;
private Set<ApplicationMode> selectedItems;
private ProfileListener listener = null; private ProfileListener listener = null;
private final OsmandApplication app; private final OsmandApplication app;
public ProfileMenuAdapter(List<ApplicationMode> items, Set<ApplicationMode> selectedItems, OsmandApplication app, ProfileListener listener) {
public ProfileMenuAdapter(List<AppProfile> items, OsmandApplication app, ProfileListener listener) {
this.items = items; this.items = items;
this.listener = listener; this.listener = listener;
this.app = app; this.app = app;
this.selectedItems = selectedItems;
} }
public List<AppProfile> getItems() { public List<ApplicationMode> getItems() {
return items; return items;
} }
public void addItem(AppProfile profileItem) { public void addItem(ApplicationMode profileItem) {
items.add(profileItem); items.add(profileItem);
notifyDataSetChanged(); notifyDataSetChanged();
} }
public void updateItemsList(List<AppProfile> newList) { public void updateItemsList(List<ApplicationMode> newList) {
items.clear(); items.clear();
items.addAll(newList); items.addAll(newList);
notifyDataSetChanged(); notifyDataSetChanged();
@ -54,12 +59,17 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder>
@Override @Override
public void onBindViewHolder(@NonNull final ProfileViewHolder holder, int position) { public void onBindViewHolder(@NonNull final ProfileViewHolder holder, int position) {
final AppProfile item = items.get(position); final ApplicationMode item = items.get(position);
holder.title.setText(item.getTitle()); if (item.getParent() != null) {
holder.descr.setText(String.format("Type: %s", item.getParent().getStringKey()));
} else {
holder.title.setText(app.getResources().getString(item.getStringResource()));
holder.descr.setText(String.format("Type: %s", item.getStringKey()));
}
holder.title.setTextColor(app.getResources().getColor(isNightMode(app) ? R.color.main_font_dark : R.color.main_font_light)); holder.title.setTextColor(app.getResources().getColor(isNightMode(app) ? R.color.main_font_dark : R.color.main_font_light));
holder.descr.setText(String.format("Type: %s", item.getNavType())); holder.icon.setImageDrawable(app.getUIUtilities().getIcon(item.getSmallIconDark(), isNightMode(app) ? R.color.active_buttons_and_links_dark : R.color.active_buttons_and_links_light));
holder.icon.setImageDrawable(app.getUIUtilities().getIcon(item.getIconRes(), isNightMode(app) ? R.color.active_buttons_and_links_dark : R.color.active_buttons_and_links_light)); holder.aSwitch.setChecked(selectedItems.contains(item));
holder.aSwitch.setChecked(item.isSelected());
holder.aSwitch.setOnClickListener(new OnClickListener() { holder.aSwitch.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -99,8 +109,8 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder>
} }
public interface ProfileListener { public interface ProfileListener {
void changeProfileStatus(AppProfile item, boolean isSelected); void changeProfileStatus(ApplicationMode item, boolean isSelected);
void editProfile(AppProfile item); void editProfile(ApplicationMode item);
} }
} }

View file

@ -19,7 +19,7 @@ public class SelectedProfileActivity extends OsmandActionBarActivity {
Intent intent = getIntent(); Intent intent = getIntent();
if (intent.getExtras() != null) { if (intent.getExtras() != null) {
if (getSupportActionBar() != null) { if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(((AppProfile) intent.getParcelableExtra("profile")).getTitle()); // getSupportActionBar().setTitle(((AppProfile) intent.getParcelableExtra("profile")).getTitle());
getSupportActionBar().setElevation(5.0f); getSupportActionBar().setElevation(5.0f);
} }
} }

View file

@ -1,7 +1,6 @@
package net.osmand.plus.profiles; package net.osmand.plus.profiles;
import android.content.Context; import android.content.Context;
import android.content.Intent;
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;
@ -12,7 +11,6 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -33,54 +31,42 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
ProfileListener listener = null; ProfileListener listener = null;
private List<ApplicationMode> allDefaultModes; private List<ApplicationMode> allAppModes;
private Set<ApplicationMode> selectedDefaultModes; private Set<ApplicationMode> availableAppModes;
private List<AppProfile> profilesList;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Context ctx = getMyApplication().getApplicationContext(); Context ctx = getMyApplication().getApplicationContext();
profilesList = new ArrayList<>(); allAppModes = ApplicationMode.allPossibleValues();
allDefaultModes = ApplicationMode.allPossibleValues(); allAppModes.remove(ApplicationMode.DEFAULT);
allDefaultModes.remove(ApplicationMode.DEFAULT); allAppModes.remove(ApplicationMode.AIRCRAFT);
allDefaultModes.remove(ApplicationMode.AIRCRAFT); allAppModes.remove(ApplicationMode.HIKING);
allDefaultModes.remove(ApplicationMode.HIKING); allAppModes.remove(ApplicationMode.TRAIN);
allDefaultModes.remove(ApplicationMode.TRAIN); availableAppModes = new LinkedHashSet<>(ApplicationMode.values(getMyApplication()));
selectedDefaultModes = new LinkedHashSet<>(ApplicationMode.values(getMyApplication())); availableAppModes.remove(ApplicationMode.DEFAULT);
selectedDefaultModes.remove(ApplicationMode.DEFAULT);
for (ApplicationMode am : allDefaultModes) {
AppProfile profileItem = new AppProfile(
am.getSmallIconDark(),
am.toHumanStringCtx(getMyApplication().getApplicationContext()),
getNavType(am, ctx));
if (selectedDefaultModes.contains(am)) {
profileItem.setSelected(true);
}
profilesList.add(profileItem);
}
} }
private String getNavType(ApplicationMode am, Context ctx) { // private String getNavType(ApplicationMode am, Context ctx) {
if (am.getParent() != null) { // if (am.getParent() != null) {
return getNavType(am.getParent(), ctx); // return getNavType(am.getParent(), ctx);
} else { // } else {
switch(am.getStringKey()) { // switch(am.getStringKey()) {
case "car": // case "car":
return ctx.getResources().getString(R.string.rendering_value_car_name); // return ctx.getResources().getString(R.string.rendering_value_car_name);
case "bicycle": // case "bicycle":
return ctx.getResources().getString(R.string.rendering_value_bicycle_name); // return ctx.getResources().getString(R.string.rendering_value_bicycle_name);
case "pedestrian": // case "pedestrian":
return ctx.getResources().getString(R.string.rendering_value_pedestrian_name); // return ctx.getResources().getString(R.string.rendering_value_pedestrian_name);
case "public_transport": // case "public_transport":
return ctx.getResources().getString(R.string.app_mode_public_transport); // return ctx.getResources().getString(R.string.app_mode_public_transport);
case "boat": // case "boat":
return ctx.getResources().getString(R.string.app_mode_boat); // return ctx.getResources().getString(R.string.app_mode_boat);
} // }
} // }
return ""; // return "";
} // }
@Nullable @Nullable
@Override @Override
@ -97,23 +83,23 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
}); });
listener = new ProfileListener() { listener = new ProfileListener() {
@Override @Override
public void changeProfileStatus(AppProfile item, boolean isSelected) { public void changeProfileStatus(ApplicationMode item, boolean isSelected) {
LOG.debug(item.getTitle() + " - " + 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 : allDefaultModes) { for (ApplicationMode sam : allAppModes) {
if (sam.toHumanString(getContext()).equals(item.getTitle())) { if (sam.getStringKey().equals(item.getStringKey())) {
mode = sam; mode = sam;
} }
} }
if(isSelected && mode != null) { if(isSelected && mode != null) {
selectedDefaultModes.add(mode); availableAppModes.add(mode);
} else if (mode != null) { } else if (mode != null) {
selectedDefaultModes.remove(mode); availableAppModes.remove(mode);
} }
for (ApplicationMode sam : selectedDefaultModes) { for (ApplicationMode sam : availableAppModes) {
vls.append(sam.getStringKey()).append(","); vls.append(sam.getStringKey()).append(",");
} }
getSettings().AVAILABLE_APP_MODES.set(vls.toString()); getSettings().AVAILABLE_APP_MODES.set(vls.toString());
@ -121,13 +107,13 @@ public class SettingsProfileFragment extends BaseOsmAndFragment {
} }
@Override @Override
public void editProfile(AppProfile item) { public void editProfile(ApplicationMode item) {
Intent intent = new Intent(getActivity(), SelectedProfileActivity.class); // Intent intent = new Intent(getActivity(), SelectedProfileActivity.class);
intent.putExtra("profile", item); // intent.putExtra("profile", item);
startActivity(intent); // startActivity(intent);
} }
}; };
adapter = new ProfileMenuAdapter(profilesList, getMyApplication(), listener); adapter = new ProfileMenuAdapter(allAppModes, availableAppModes, getMyApplication(), listener);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);