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.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<ProfileDataObject> profiles = new ArrayList<>();
private CallbackWithObject<List<String>> callback;
private Map<String, CompoundButton> compoundButtons = new HashMap<>();
private List<String> selectedProfiles;
private List<String> 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<String> selectedProfileKeys = new ArrayList<>();
for (ProfileDataObject profile : profiles) {
if (profile.isSelected() && profile.isEnabled()) {
selectedProfileKeys.add(profile.getStringKey());
Fragment targetFragment = getTargetFragment();
if (targetFragment instanceof CallbackWithObject) {
List<String> 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<List<String>> callback) {
this.callback = callback;
}
public static void showInstance(@NonNull MapActivity mapActivity,
public static void showInstance(@NonNull MapActivity mapActivity, Fragment targetFragment,
@Nullable List<String> selectedProfiles,
@Nullable List<String> disabledProfiles,
boolean usedOnMap,
CallbackWithObject<List<String>> 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<String>());
fragment.setArguments(args);
fragment.setTargetFragment(targetFragment, 0);
fragment.setUsedOnMap(usedOnMap);
fragment.setCallback(callback);
fragment.show(mapActivity.getSupportFragmentManager(), SelectMultipleProfilesBottomSheet.TAG);
}

View file

@ -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<Object> {
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;
}
}

View file

@ -39,6 +39,7 @@ public abstract class SwitchableAction<T> 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<T> 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<T> extends QuickAction {
return hasParams;
}
protected Adapter getAdapter() {
return adapter;
}
public abstract List<T> loadListFromParams();
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 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.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<String> {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
CreateEditActionDialog targetFragment = (CreateEditActionDialog) activity
.getSupportFragmentManager().findFragmentByTag(CreateEditActionDialog.TAG);
List<String> selectedProfilesKeys = new ArrayList<>(adapter.getItemsList());
SelectMultipleProfilesBottomSheet.showInstance(activity, selectedProfilesKeys,
selectedProfilesKeys, false, new CallbackWithObject<List<String>>() {
@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;
}
});
SelectMultipleProfilesBottomSheet.showInstance(activity, targetFragment,
selectedProfilesKeys, selectedProfilesKeys, false);
}
};
}
@ -239,4 +226,17 @@ public class SwitchProfileAction extends SwitchableAction<String> {
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);
}
}
}
}