diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectMultipleProfilesBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectMultipleProfilesBottomSheet.java index 3d0cfb873a..a97fa7dfde 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectMultipleProfilesBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectMultipleProfilesBottomSheet.java @@ -10,6 +10,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import net.osmand.CallbackWithObject; import net.osmand.plus.OsmandApplication; @@ -22,7 +23,9 @@ import net.osmand.plus.settings.NavigationFragment; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet { @@ -31,21 +34,28 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet public static final String DISABLED_KEYS = "disabled_keys"; private List profiles = new ArrayList<>(); - private CallbackWithObject> callback; + private Map compoundButtons = new HashMap<>(); private List selectedProfiles; private List disabledProfiles; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + compoundButtons.clear(); Bundle args = getArguments(); - if (args != null) { - selectedProfiles = args.getStringArrayList(SELECTED_KEYS); - disabledProfiles = args.getStringArrayList(DISABLED_KEYS); - refreshProfiles(getMyApplication()); + if (savedInstanceState != null) { + readBundle(savedInstanceState); + } else if (args != null) { + readBundle(args); } } + private void readBundle(Bundle bundle) { + selectedProfiles = bundle.getStringArrayList(SELECTED_KEYS); + disabledProfiles = bundle.getStringArrayList(DISABLED_KEYS); + refreshProfiles(getMyApplication()); + } + private void refreshProfiles(OsmandApplication app) { profiles.clear(); 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 public void createMenuItems(Bundle savedInstanceState) { items.add(new TitleItem(getString(R.string.application_profiles))); @@ -95,12 +118,18 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet ivIcon.setImageDrawable(drawableIcon); UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app, enable ? activeColorId : disableColorId), compoundButton); - compoundButton.setChecked(profile.isSelected()); + compoundButtons.put(profile.getStringKey(), compoundButton); View.OnClickListener l = !enable ? null : new View.OnClickListener() { @Override public void onClick(View v) { + String key = profile.getStringKey(); boolean selected = !profile.isSelected(); + if (selected) { + selectedProfiles.add(key); + } else { + selectedProfiles.remove(key); + } profile.setSelected(selected); compoundButton.setChecked(selected); } @@ -112,6 +141,13 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet .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 protected void onDismissButtonClickAction() { super.onDismissButtonClickAction(); @@ -125,27 +161,23 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet @Override protected void onRightBottomButtonClick() { - if (callback != null) { - List selectedProfileKeys = new ArrayList<>(); - for (ProfileDataObject profile : profiles) { - if (profile.isSelected() && profile.isEnabled()) { - selectedProfileKeys.add(profile.getStringKey()); + Fragment targetFragment = getTargetFragment(); + if (targetFragment instanceof CallbackWithObject) { + List newSelected = new ArrayList<>(); + for (String profile : selectedProfiles) { + if (!disabledProfiles.contains(profile)) { + newSelected.add(profile); } } - callback.processResult(selectedProfileKeys); + ((CallbackWithObject) targetFragment).processResult(newSelected); } dismiss(); } - public void setCallback(CallbackWithObject> callback) { - this.callback = callback; - } - - public static void showInstance(@NonNull MapActivity mapActivity, + public static void showInstance(@NonNull MapActivity mapActivity, Fragment targetFragment, @Nullable List selectedProfiles, @Nullable List disabledProfiles, - boolean usedOnMap, - CallbackWithObject> callback) { + boolean usedOnMap) { SelectMultipleProfilesBottomSheet fragment = new SelectMultipleProfilesBottomSheet(); Bundle args = new Bundle(); args.putStringArrayList(SELECTED_KEYS, selectedProfiles != null ? @@ -153,8 +185,8 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet args.putStringArrayList(DISABLED_KEYS, disabledProfiles != null ? new ArrayList<>(disabledProfiles) : new ArrayList()); fragment.setArguments(args); + fragment.setTargetFragment(targetFragment, 0); fragment.setUsedOnMap(usedOnMap); - fragment.setCallback(callback); fragment.show(mapActivity.getSupportFragmentManager(), SelectMultipleProfilesBottomSheet.TAG); } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java index 079157e383..547d23e3ec 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java @@ -23,16 +23,19 @@ import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; import net.osmand.AndroidUtils; +import net.osmand.CallbackWithObject; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; +import java.util.List; + /** * Created by rosty on 12/27/16. */ -public class CreateEditActionDialog extends DialogFragment { +public class CreateEditActionDialog extends DialogFragment implements CallbackWithObject { public static final String TAG = CreateEditActionDialog.class.getSimpleName(); @@ -254,4 +257,12 @@ public class CreateEditActionDialog extends DialogFragment { private UiUtilities getIconsCache(){ return getApplication().getUIUtilities(); } + + @Override + public boolean processResult(Object result) { + if (action instanceof SwitchableAction) { + ((SwitchableAction) action).onItemsSelected(getContext(), (List) result); + } + return false; + } } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java b/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java index 2342b76823..3d4c3a9269 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java @@ -39,6 +39,7 @@ public abstract class SwitchableAction extends QuickAction { private transient EditText title; + private transient Adapter adapter; private transient ItemTouchHelper touchHelper; protected SwitchableAction(QuickActionType type) { @@ -73,7 +74,7 @@ public abstract class SwitchableAction extends QuickAction { }); RecyclerView list = (RecyclerView) view.findViewById(R.id.list); - Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() { + adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() { @Override public void onStartDrag(RecyclerView.ViewHolder viewHolder) { touchHelper.startDrag(viewHolder); @@ -115,6 +116,10 @@ public abstract class SwitchableAction extends QuickAction { return hasParams; } + protected Adapter getAdapter() { + return adapter; + } + public abstract List loadListFromParams(); public abstract void executeWithParams(MapActivity activity, String params); @@ -327,4 +332,8 @@ public abstract class SwitchableAction extends QuickAction { protected abstract String getListKey(); protected abstract View.OnClickListener getOnAddBtnClickListener(MapActivity activity, final Adapter adapter); + + protected void onItemsSelected(Context ctx, List selectedItems) { + + } } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/actions/SwitchProfileAction.java b/OsmAnd/src/net/osmand/plus/quickaction/actions/SwitchProfileAction.java index ab6b60eaca..a969b85cb9 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/actions/SwitchProfileAction.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/actions/SwitchProfileAction.java @@ -10,13 +10,13 @@ import androidx.appcompat.widget.SwitchCompat; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import net.osmand.CallbackWithObject; 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.profiles.SelectMultipleProfilesBottomSheet; +import net.osmand.plus.quickaction.CreateEditActionDialog; import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.quickaction.QuickActionType; import net.osmand.plus.quickaction.SwitchableAction; @@ -207,24 +207,11 @@ public class SwitchProfileAction extends SwitchableAction { return new View.OnClickListener() { @Override public void onClick(View v) { + CreateEditActionDialog targetFragment = (CreateEditActionDialog) activity + .getSupportFragmentManager().findFragmentByTag(CreateEditActionDialog.TAG); List selectedProfilesKeys = new ArrayList<>(adapter.getItemsList()); - SelectMultipleProfilesBottomSheet.showInstance(activity, selectedProfilesKeys, - selectedProfilesKeys, false, new CallbackWithObject>() { - @Override - public boolean processResult(List 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; - } - }); + SelectMultipleProfilesBottomSheet.showInstance(activity, targetFragment, + selectedProfilesKeys, selectedProfilesKeys, false); } }; } @@ -239,4 +226,17 @@ public class SwitchProfileAction extends SwitchableAction { return super.fillParams(root, activity); } + @Override + protected void onItemsSelected(Context ctx, List selectedItems) { + Adapter adapter = getAdapter(); + if (adapter == null) { + return; + } + for (String key : selectedItems) { + ApplicationMode appMode = getModeForKey(key); + if (appMode != null) { + adapter.addItem(key, ctx); + } + } + } }