Fixes p.2

This commit is contained in:
Nazar-Kutz 2020-05-13 14:39:29 +03:00
parent f50d28b235
commit 6b5b3236a4
4 changed files with 92 additions and 40 deletions

View file

@ -10,6 +10,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -22,7 +23,9 @@ import net.osmand.plus.settings.NavigationFragment;
import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet { public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet {
@ -31,21 +34,28 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
public static final String DISABLED_KEYS = "disabled_keys"; public static final String DISABLED_KEYS = "disabled_keys";
private List<ProfileDataObject> profiles = new ArrayList<>(); private List<ProfileDataObject> profiles = new ArrayList<>();
private CallbackWithObject<List<String>> callback; private Map<String, CompoundButton> compoundButtons = new HashMap<>();
private List<String> selectedProfiles; private List<String> selectedProfiles;
private List<String> disabledProfiles; private List<String> disabledProfiles;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
compoundButtons.clear();
Bundle args = getArguments(); Bundle args = getArguments();
if (args != null) { if (savedInstanceState != null) {
selectedProfiles = args.getStringArrayList(SELECTED_KEYS); readBundle(savedInstanceState);
disabledProfiles = args.getStringArrayList(DISABLED_KEYS); } else if (args != null) {
refreshProfiles(getMyApplication()); readBundle(args);
} }
} }
private void readBundle(Bundle bundle) {
selectedProfiles = bundle.getStringArrayList(SELECTED_KEYS);
disabledProfiles = bundle.getStringArrayList(DISABLED_KEYS);
refreshProfiles(getMyApplication());
}
private void refreshProfiles(OsmandApplication app) { private void refreshProfiles(OsmandApplication app) {
profiles.clear(); profiles.clear();
profiles.addAll(NavigationFragment.getBaseProfiles(app)); profiles.addAll(NavigationFragment.getBaseProfiles(app));
@ -56,6 +66,19 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
} }
} }
@Override
public void onResume() {
super.onResume();
for (ProfileDataObject profile : profiles) {
String key = profile.getStringKey();
boolean selected = selectedProfiles.contains(key);
CompoundButton cb = compoundButtons.get(key);
if (cb != null) {
cb.setChecked(selected);
}
}
}
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
items.add(new TitleItem(getString(R.string.application_profiles))); items.add(new TitleItem(getString(R.string.application_profiles)));
@ -95,12 +118,18 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
ivIcon.setImageDrawable(drawableIcon); ivIcon.setImageDrawable(drawableIcon);
UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app, UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app,
enable ? activeColorId : disableColorId), compoundButton); enable ? activeColorId : disableColorId), compoundButton);
compoundButton.setChecked(profile.isSelected()); compoundButtons.put(profile.getStringKey(), compoundButton);
View.OnClickListener l = !enable ? null : new View.OnClickListener() { View.OnClickListener l = !enable ? null : new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
String key = profile.getStringKey();
boolean selected = !profile.isSelected(); boolean selected = !profile.isSelected();
if (selected) {
selectedProfiles.add(key);
} else {
selectedProfiles.remove(key);
}
profile.setSelected(selected); profile.setSelected(selected);
compoundButton.setChecked(selected); compoundButton.setChecked(selected);
} }
@ -112,6 +141,13 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
.create()); .create());
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList(SELECTED_KEYS, new ArrayList<>(selectedProfiles));
outState.putStringArrayList(DISABLED_KEYS, new ArrayList<>(disabledProfiles));
}
@Override @Override
protected void onDismissButtonClickAction() { protected void onDismissButtonClickAction() {
super.onDismissButtonClickAction(); super.onDismissButtonClickAction();
@ -125,27 +161,23 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
@Override @Override
protected void onRightBottomButtonClick() { protected void onRightBottomButtonClick() {
if (callback != null) { Fragment targetFragment = getTargetFragment();
List<String> selectedProfileKeys = new ArrayList<>(); if (targetFragment instanceof CallbackWithObject) {
for (ProfileDataObject profile : profiles) { List<String> newSelected = new ArrayList<>();
if (profile.isSelected() && profile.isEnabled()) { for (String profile : selectedProfiles) {
selectedProfileKeys.add(profile.getStringKey()); if (!disabledProfiles.contains(profile)) {
newSelected.add(profile);
} }
} }
callback.processResult(selectedProfileKeys); ((CallbackWithObject) targetFragment).processResult(newSelected);
} }
dismiss(); dismiss();
} }
public void setCallback(CallbackWithObject<List<String>> callback) { public static void showInstance(@NonNull MapActivity mapActivity, Fragment targetFragment,
this.callback = callback;
}
public static void showInstance(@NonNull MapActivity mapActivity,
@Nullable List<String> selectedProfiles, @Nullable List<String> selectedProfiles,
@Nullable List<String> disabledProfiles, @Nullable List<String> disabledProfiles,
boolean usedOnMap, boolean usedOnMap) {
CallbackWithObject<List<String>> callback) {
SelectMultipleProfilesBottomSheet fragment = new SelectMultipleProfilesBottomSheet(); SelectMultipleProfilesBottomSheet fragment = new SelectMultipleProfilesBottomSheet();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putStringArrayList(SELECTED_KEYS, selectedProfiles != null ? args.putStringArrayList(SELECTED_KEYS, selectedProfiles != null ?
@ -153,8 +185,8 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
args.putStringArrayList(DISABLED_KEYS, disabledProfiles != null ? args.putStringArrayList(DISABLED_KEYS, disabledProfiles != null ?
new ArrayList<>(disabledProfiles) : new ArrayList<String>()); new ArrayList<>(disabledProfiles) : new ArrayList<String>());
fragment.setArguments(args); fragment.setArguments(args);
fragment.setTargetFragment(targetFragment, 0);
fragment.setUsedOnMap(usedOnMap); fragment.setUsedOnMap(usedOnMap);
fragment.setCallback(callback);
fragment.show(mapActivity.getSupportFragmentManager(), SelectMultipleProfilesBottomSheet.TAG); fragment.show(mapActivity.getSupportFragmentManager(), SelectMultipleProfilesBottomSheet.TAG);
} }

View file

@ -23,16 +23,19 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import java.util.List;
/** /**
* Created by rosty on 12/27/16. * Created by rosty on 12/27/16.
*/ */
public class CreateEditActionDialog extends DialogFragment { public class CreateEditActionDialog extends DialogFragment implements CallbackWithObject<Object> {
public static final String TAG = CreateEditActionDialog.class.getSimpleName(); public static final String TAG = CreateEditActionDialog.class.getSimpleName();
@ -254,4 +257,12 @@ public class CreateEditActionDialog extends DialogFragment {
private UiUtilities getIconsCache(){ private UiUtilities getIconsCache(){
return getApplication().getUIUtilities(); return getApplication().getUIUtilities();
} }
@Override
public boolean processResult(Object result) {
if (action instanceof SwitchableAction) {
((SwitchableAction) action).onItemsSelected(getContext(), (List) result);
}
return false;
}
} }

View file

@ -39,6 +39,7 @@ public abstract class SwitchableAction<T> extends QuickAction {
private transient EditText title; private transient EditText title;
private transient Adapter adapter;
private transient ItemTouchHelper touchHelper; private transient ItemTouchHelper touchHelper;
protected SwitchableAction(QuickActionType type) { protected SwitchableAction(QuickActionType type) {
@ -73,7 +74,7 @@ public abstract class SwitchableAction<T> extends QuickAction {
}); });
RecyclerView list = (RecyclerView) view.findViewById(R.id.list); RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() { adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() {
@Override @Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) { public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder); touchHelper.startDrag(viewHolder);
@ -115,6 +116,10 @@ public abstract class SwitchableAction<T> extends QuickAction {
return hasParams; return hasParams;
} }
protected Adapter getAdapter() {
return adapter;
}
public abstract List<T> loadListFromParams(); public abstract List<T> loadListFromParams();
public abstract void executeWithParams(MapActivity activity, String params); public abstract void executeWithParams(MapActivity activity, String params);
@ -327,4 +332,8 @@ public abstract class SwitchableAction<T> extends QuickAction {
protected abstract String getListKey(); protected abstract String getListKey();
protected abstract View.OnClickListener getOnAddBtnClickListener(MapActivity activity, final Adapter adapter); protected abstract View.OnClickListener getOnAddBtnClickListener(MapActivity activity, final Adapter adapter);
protected void onItemsSelected(Context ctx, List<T> selectedItems) {
}
} }

View file

@ -10,13 +10,13 @@ import androidx.appcompat.widget.SwitchCompat;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import net.osmand.CallbackWithObject;
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.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.profiles.SelectMultipleProfilesBottomSheet; import net.osmand.plus.profiles.SelectMultipleProfilesBottomSheet;
import net.osmand.plus.quickaction.CreateEditActionDialog;
import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType; import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.plus.quickaction.SwitchableAction; import net.osmand.plus.quickaction.SwitchableAction;
@ -207,24 +207,11 @@ public class SwitchProfileAction extends SwitchableAction<String> {
return new View.OnClickListener() { return new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
CreateEditActionDialog targetFragment = (CreateEditActionDialog) activity
.getSupportFragmentManager().findFragmentByTag(CreateEditActionDialog.TAG);
List<String> selectedProfilesKeys = new ArrayList<>(adapter.getItemsList()); List<String> selectedProfilesKeys = new ArrayList<>(adapter.getItemsList());
SelectMultipleProfilesBottomSheet.showInstance(activity, selectedProfilesKeys, SelectMultipleProfilesBottomSheet.showInstance(activity, targetFragment,
selectedProfilesKeys, false, new CallbackWithObject<List<String>>() { selectedProfilesKeys, selectedProfilesKeys, false);
@Override
public boolean processResult(List<String> result) {
if (result == null || result.size() == 0) {
return false;
}
for (String item : result) {
ApplicationMode appMode = getModeForKey(item);
if (appMode != null) {
String profile = appMode.getStringKey();
adapter.addItem(profile, activity);
}
}
return true;
}
});
} }
}; };
} }
@ -239,4 +226,17 @@ public class SwitchProfileAction extends SwitchableAction<String> {
return super.fillParams(root, activity); return super.fillParams(root, activity);
} }
@Override
protected void onItemsSelected(Context ctx, List<String> selectedItems) {
Adapter adapter = getAdapter();
if (adapter == null) {
return;
}
for (String key : selectedItems) {
ApplicationMode appMode = getModeForKey(key);
if (appMode != null) {
adapter.addItem(key, ctx);
}
}
}
} }