Fixes
This commit is contained in:
parent
6539a35a20
commit
5a5463d4a9
3 changed files with 62 additions and 62 deletions
|
@ -36,6 +36,7 @@ import net.osmand.plus.quickaction.QuickAction;
|
|||
import net.osmand.plus.quickaction.QuickActionRegistry;
|
||||
import net.osmand.plus.quickaction.SwitchableAction;
|
||||
import net.osmand.plus.quickaction.actions.MapStyleAction;
|
||||
import net.osmand.plus.quickaction.actions.SwitchProfileAction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -45,17 +46,12 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
|
||||
private static final String SELECTED_ITEM_KEY = "selected_item";
|
||||
|
||||
public static final String DIALOG_TYPE_KEY = "dialog_type";
|
||||
public static final String PROFILE_DIALOG_TYPE = "profile_dialog_type";
|
||||
public static final String MAP_DIALOG_TYPE = "map_dialog_type";
|
||||
|
||||
private LinearLayout itemsContainer;
|
||||
private View.OnClickListener onClickListener;
|
||||
private ColorStateList rbColorList;
|
||||
|
||||
private String selectedItem;
|
||||
private QuickAction action;
|
||||
private String dialogType;
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
|
@ -68,7 +64,6 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
return;
|
||||
}
|
||||
long id = args.getLong(SwitchableAction.KEY_ID);
|
||||
dialogType = args.getString(DIALOG_TYPE_KEY, MAP_DIALOG_TYPE);
|
||||
OsmandApplication app = mapActivity.getMyApplication();
|
||||
|
||||
QuickActionRegistry quickActionRegistry = app.getQuickActionRegistry();
|
||||
|
@ -167,22 +162,38 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
List<String> stylesList = mapStyleAction.getFilteredStyles();
|
||||
for (String entry : stylesList) {
|
||||
boolean selected = entry.equals(selectedItem);
|
||||
createItemRow(selected, counter, mapStyleAction.getTranslatedItemName(context, entry), entry);
|
||||
createItemRow(selected, counter, null,
|
||||
mapStyleAction.getTranslatedItemName(context, entry), entry);
|
||||
counter++;
|
||||
}
|
||||
} else if (action instanceof SwitchProfileAction) {
|
||||
SwitchProfileAction switchProfileAction = (SwitchProfileAction) action;
|
||||
List<String> profilesKeys = (List<String>) switchProfileAction.loadListFromParams();
|
||||
for (String key : profilesKeys) {
|
||||
ApplicationMode appMode = ApplicationMode.valueOfStringKey(key, null);
|
||||
if (appMode != null) {
|
||||
boolean selected = key.equals(selectedItem);
|
||||
int iconId = appMode.getIconRes();
|
||||
int colorId = appMode.getIconColorInfo().getColor(nightMode);
|
||||
Drawable icon = getIcon(iconId, colorId);
|
||||
String translatedName = appMode.toHumanString();
|
||||
createItemRow(selected, counter, icon, translatedName, key);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
} else if (action instanceof SwitchableAction) {
|
||||
SwitchableAction switchableAction = (SwitchableAction) action;
|
||||
List<Pair<String, String>> sources = (List<Pair<String, String>>) switchableAction.loadListFromParams();
|
||||
for (Pair<String, String> entry : sources) {
|
||||
String tag = entry.first;
|
||||
boolean selected = tag.equals(selectedItem);
|
||||
createItemRow(selected, counter, entry.second, tag);
|
||||
createItemRow(selected, counter, null, entry.second, tag);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createItemRow(boolean selected, int counter, String text, String tag) {
|
||||
private void createItemRow(boolean selected, int counter, Drawable icon, String text, String tag) {
|
||||
View view = itemsContainer.getChildAt(counter);
|
||||
view.setTag(tag);
|
||||
view.setOnClickListener(getOnClickListener());
|
||||
|
@ -194,20 +205,11 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
RadioButton rb = (RadioButton) view.findViewById(R.id.compound_button);
|
||||
rb.setChecked(selected);
|
||||
CompoundButtonCompat.setButtonTintList(rb, rbColorList);
|
||||
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
|
||||
imageView.setImageDrawable(getItemIcon(tag));
|
||||
}
|
||||
|
||||
private Drawable getItemIcon(String tag) {
|
||||
if (PROFILE_DIALOG_TYPE.equals(dialogType)) {
|
||||
ApplicationMode appMode = ApplicationMode.valueOfStringKey(tag, null);
|
||||
if (appMode != null) {
|
||||
int iconId = appMode.getIconRes();
|
||||
int colorId = appMode.getIconColorInfo().getColor(nightMode);
|
||||
return getIcon(iconId, colorId);
|
||||
}
|
||||
if (icon == null) {
|
||||
icon = getContentIcon(action.getIconRes());
|
||||
}
|
||||
return getContentIcon(action.getIconRes());
|
||||
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
|
||||
imageView.setImageDrawable(icon);
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
|
|
|
@ -31,9 +31,6 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static net.osmand.plus.dialogs.SelectMapViewQuickActionsBottomSheet.DIALOG_TYPE_KEY;
|
||||
import static net.osmand.plus.dialogs.SelectMapViewQuickActionsBottomSheet.MAP_DIALOG_TYPE;
|
||||
|
||||
public abstract class SwitchableAction<T> extends QuickAction {
|
||||
|
||||
public static final String KEY_ID = "id";
|
||||
|
@ -125,14 +122,9 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
public abstract String getTranslatedItemName(Context context, String item);
|
||||
|
||||
protected void showChooseDialog(FragmentManager fm) {
|
||||
showChooseDialog(fm, MAP_DIALOG_TYPE);
|
||||
}
|
||||
|
||||
protected void showChooseDialog(FragmentManager fm, String dialogType) {
|
||||
SelectMapViewQuickActionsBottomSheet fragment = new SelectMapViewQuickActionsBottomSheet();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong(KEY_ID, id);
|
||||
args.putString(DIALOG_TYPE_KEY, dialogType);
|
||||
fragment.setArguments(args);
|
||||
fragment.show(fm, SelectMapViewQuickActionsBottomSheet.TAG);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import android.view.View;
|
|||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.core.util.Pair;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
@ -28,9 +27,7 @@ import java.util.ArrayList;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static net.osmand.plus.dialogs.SelectMapViewQuickActionsBottomSheet.PROFILE_DIALOG_TYPE;
|
||||
|
||||
public class SwitchProfileAction extends SwitchableAction<Pair<String, String>> {
|
||||
public class SwitchProfileAction extends SwitchableAction<String> {
|
||||
|
||||
private final static String KEY_PROFILES = "profiles";
|
||||
|
||||
|
@ -49,33 +46,36 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
}
|
||||
|
||||
@Override
|
||||
protected String getTitle(List<Pair<String, String>> filters) {
|
||||
protected String getTitle(List<String> filters) {
|
||||
List<String> profileNames = new ArrayList<>();
|
||||
for (Pair<String, String> p : filters) {
|
||||
profileNames.add(p.second);
|
||||
for (String key : filters) {
|
||||
ApplicationMode appMode = getModeForKey(key);
|
||||
if (appMode != null) {
|
||||
profileNames.add(appMode.toHumanString());
|
||||
}
|
||||
}
|
||||
return TextUtils.join(", ", profileNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveListToParams(List<Pair<String, String>> list) {
|
||||
protected void saveListToParams(List<String> list) {
|
||||
getParams().put(getListKey(), new Gson().toJson(list));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, String>> loadListFromParams() {
|
||||
public List<String> loadListFromParams() {
|
||||
String json = getParams().get(getListKey());
|
||||
|
||||
if (json == null || json.isEmpty()) return new ArrayList<>();
|
||||
|
||||
Type listType = new TypeToken<ArrayList<Pair<String, String>>>() {
|
||||
Type listType = new TypeToken<ArrayList<String>>() {
|
||||
}.getType();
|
||||
|
||||
List<Pair<String, String>> list = new Gson().fromJson(json, listType);
|
||||
List<String> list = new Gson().fromJson(json, listType);
|
||||
|
||||
Iterator<Pair<String, String>> it = list.iterator();
|
||||
Iterator<String> it = list.iterator();
|
||||
while (it.hasNext()) {
|
||||
ApplicationMode appMode = getModeForKey(it.next().first);
|
||||
ApplicationMode appMode = getModeForKey(it.next());
|
||||
if (appMode == null) {
|
||||
it.remove();
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
@Override
|
||||
public void execute(MapActivity activity) {
|
||||
OsmandSettings settings = activity.getMyApplication().getSettings();
|
||||
List<Pair<String, String>> profiles = loadListFromParams();
|
||||
List<String> profiles = loadListFromParams();
|
||||
|
||||
if (profiles.size() == 0) {
|
||||
Toast.makeText(activity, activity.getString(R.string.profiles_for_action_not_found),
|
||||
|
@ -97,26 +97,26 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
|
||||
boolean showDialog = Boolean.valueOf(getParams().get(KEY_DIALOG));
|
||||
if (showDialog) {
|
||||
showChooseDialog(activity.getSupportFragmentManager(), PROFILE_DIALOG_TYPE);
|
||||
showChooseDialog(activity.getSupportFragmentManager());
|
||||
return;
|
||||
}
|
||||
|
||||
int index = -1;
|
||||
final String currentSource = settings.getApplicationMode().getStringKey();
|
||||
final String currentProfile = settings.getApplicationMode().getStringKey();
|
||||
|
||||
for (int idx = 0; idx < profiles.size(); idx++) {
|
||||
if (currentSource.equals(profiles.get(idx).first)) {
|
||||
if (currentProfile.equals(profiles.get(idx))) {
|
||||
index = idx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String nextSource = profiles.get(0).first;
|
||||
String nextProfile = profiles.get(0);
|
||||
|
||||
if (index >= 0 && index + 1 < profiles.size()) {
|
||||
nextSource = profiles.get(index + 1).first;
|
||||
nextProfile = profiles.get(index + 1);
|
||||
}
|
||||
executeWithParams(activity, nextSource);
|
||||
executeWithParams(activity, nextProfile);
|
||||
|
||||
super.execute(activity);
|
||||
}
|
||||
|
@ -149,8 +149,18 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
}
|
||||
|
||||
@Override
|
||||
protected String getItemName(Context context, Pair<String, String> item) {
|
||||
return item.second;
|
||||
protected String getItemName(Context context, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
return appMode.toHumanString();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSelectedItem(OsmandApplication app) {
|
||||
ApplicationMode appMode = app.getSettings().getApplicationMode();
|
||||
return appMode.getStringKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -174,8 +184,8 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
}
|
||||
|
||||
@Override
|
||||
protected int getItemIconRes(Context context, Pair<String, String> item) {
|
||||
ApplicationMode appMode = getModeForKey(item.first);
|
||||
protected int getItemIconRes(Context context, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
return appMode.getIconRes();
|
||||
}
|
||||
|
@ -183,8 +193,8 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
}
|
||||
|
||||
@Override
|
||||
protected int getItemIconColorRes(OsmandApplication app, Pair<String, String> item) {
|
||||
ApplicationMode appMode = getModeForKey(item.first);
|
||||
protected int getItemIconColorRes(OsmandApplication app, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
boolean nightMode = !app.getSettings().isLightContent();
|
||||
return appMode.getIconColorInfo().getColor(nightMode);
|
||||
|
@ -197,10 +207,7 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
return new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
List<String> selectedProfilesKeys = new ArrayList<>();
|
||||
for (Pair<String, String> item : adapter.getItemsList()) {
|
||||
selectedProfilesKeys.add(item.first);
|
||||
}
|
||||
List<String> selectedProfilesKeys = new ArrayList<>(adapter.getItemsList());
|
||||
SelectMultipleProfileBottomSheet.showInstance(activity, selectedProfilesKeys,
|
||||
selectedProfilesKeys, false, new CallbackWithObject<List<String>>() {
|
||||
@Override
|
||||
|
@ -211,8 +218,7 @@ public class SwitchProfileAction extends SwitchableAction<Pair<String, String>>
|
|||
for (String item : result) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
Pair<String, String> profile = new Pair<>(
|
||||
appMode.getStringKey(), appMode.toHumanString());
|
||||
String profile = appMode.getStringKey();
|
||||
adapter.addItem(profile, activity);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue