forked from upstream-mirrors/OpenTracks
Added profiles for custom layouts.
Users can add severals custom layouts and select one of them in recording fragment. The last selected layout is the one that will be use in the next recording. Fixes #938.
This commit is contained in:
@@ -1,34 +1,73 @@
|
||||
package de.dennisguse.opentracks.settings;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.util.CsvLayoutUtils;
|
||||
|
||||
class PreferencesOpenHelper {
|
||||
|
||||
private static final int PREFERENCES_VERSION = 1;
|
||||
private final int version;
|
||||
|
||||
private PreferencesOpenHelper() {
|
||||
private PreferencesOpenHelper(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
static PreferencesOpenHelper newInstance() {
|
||||
return new PreferencesOpenHelper();
|
||||
static PreferencesOpenHelper newInstance(int version) {
|
||||
return new PreferencesOpenHelper(version);
|
||||
}
|
||||
|
||||
void checkForUpgrade() {
|
||||
void check() {
|
||||
int lastVersion = PreferencesUtils.getInt(R.string.prefs_last_version_key, 0);
|
||||
if (PREFERENCES_VERSION > lastVersion) {
|
||||
if (version > lastVersion) {
|
||||
onUpgrade();
|
||||
} else if (version < lastVersion) {
|
||||
onDowngrade();
|
||||
}
|
||||
}
|
||||
|
||||
private void onUpgrade() {
|
||||
PreferencesUtils.setInt(R.string.prefs_last_version_key, PREFERENCES_VERSION);
|
||||
switch (PREFERENCES_VERSION) {
|
||||
case 1:
|
||||
upgradeFrom0to1();
|
||||
PreferencesUtils.setInt(R.string.prefs_last_version_key, version);
|
||||
for (int i = 1; i <= version; i++) {
|
||||
switch (i) {
|
||||
case 1:
|
||||
upgradeFrom0to1();
|
||||
break;
|
||||
case 2:
|
||||
upgradeFrom1to2();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Not implemented: upgrade to " + version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void upgradeFrom0to1() {
|
||||
PreferencesUtils.setString(R.string.stats_custom_layout_fields_key, PreferencesUtils.buildDefaultLayout());
|
||||
String preferenceValue = PreferencesUtils.getString(R.string.stats_custom_layouts_key, "");
|
||||
if (preferenceValue.isEmpty()) {
|
||||
PreferencesUtils.setString(R.string.stats_custom_layouts_key, PreferencesUtils.buildDefaultLayout());
|
||||
}
|
||||
}
|
||||
|
||||
private void upgradeFrom1to2() {
|
||||
String csvVersion1CustomLayout = PreferencesUtils.getString(R.string.stats_custom_layouts_key, PreferencesUtils.buildDefaultLayout());
|
||||
ArrayList<String> parts = new ArrayList<>();
|
||||
Collections.addAll(parts, csvVersion1CustomLayout.split(CsvLayoutUtils.ITEM_SEPARATOR));
|
||||
|
||||
if (parts.size() < 2) {
|
||||
PreferencesUtils.setString(R.string.stats_custom_layouts_key, PreferencesUtils.buildDefaultLayout());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!parts.get(1).matches("\\d+")) {
|
||||
parts.add(1, String.valueOf(PreferencesUtils.getLayoutColumnsByDefault()));
|
||||
}
|
||||
PreferencesUtils.setString(R.string.stats_custom_layouts_key, parts.stream().collect(Collectors.joining(CsvLayoutUtils.ITEM_SEPARATOR)));
|
||||
}
|
||||
|
||||
private void onDowngrade() {
|
||||
PreferencesUtils.setString(R.string.stats_custom_layouts_key, PreferencesUtils.buildDefaultLayout());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.content.res.TypedArray;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
@@ -34,17 +35,16 @@ import java.time.Duration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.DataField;
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
import de.dennisguse.opentracks.content.data.Layout;
|
||||
import de.dennisguse.opentracks.content.data.Speed;
|
||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||
import de.dennisguse.opentracks.util.CsvConstants;
|
||||
import de.dennisguse.opentracks.util.CsvLayoutUtils;
|
||||
import de.dennisguse.opentracks.util.TrackIconUtils;
|
||||
import de.dennisguse.opentracks.util.UnitConversions;
|
||||
|
||||
@@ -57,6 +57,8 @@ public class PreferencesUtils {
|
||||
|
||||
private final static String TAG = PreferencesUtils.class.getSimpleName();
|
||||
|
||||
private static final int PREFERENCES_VERSION = 2;
|
||||
|
||||
private PreferencesUtils() {
|
||||
}
|
||||
|
||||
@@ -71,7 +73,7 @@ public class PreferencesUtils {
|
||||
PreferencesUtils.resources = resources;
|
||||
PreferencesUtils.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
PreferencesOpenHelper.newInstance().checkForUpgrade();
|
||||
PreferencesOpenHelper.newInstance(PREFERENCES_VERSION).check();
|
||||
}
|
||||
|
||||
public static void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener changeListener) {
|
||||
@@ -546,12 +548,8 @@ public class PreferencesUtils {
|
||||
return getDefaultExportDirectoryUri(context) != null;
|
||||
}
|
||||
|
||||
public static int getLayoutColumns() {
|
||||
return getInt(R.string.stats_custom_layout_columns_key, resources.getInteger(R.integer.stats_custom_layout_columns_default));
|
||||
}
|
||||
|
||||
public static void setLayoutColumns(int columns) {
|
||||
setInt(R.string.stats_custom_layout_columns_key, columns);
|
||||
public static int getLayoutColumnsByDefault() {
|
||||
return resources.getInteger(R.integer.stats_custom_layout_columns_default);
|
||||
}
|
||||
|
||||
private static List<TypedArray> getMultiTypedArray(String key) {
|
||||
@@ -576,46 +574,84 @@ public class PreferencesUtils {
|
||||
}
|
||||
|
||||
@SuppressLint("ResourceType")
|
||||
static String buildDefaultLayout() {
|
||||
private static String buildDefaultFields() {
|
||||
List<TypedArray> fieldsArrays = getMultiTypedArray("stats_custom_layout_fields_default_value");
|
||||
return resources.getString(R.string.default_activity_default) + CsvConstants.LINE_SEPARATOR
|
||||
+ fieldsArrays.stream().map(i -> i.getString(0) + CsvConstants.ITEM_SEPARATOR + i.getString(1)).collect(Collectors.joining(CsvConstants.LINE_SEPARATOR))
|
||||
+ CsvConstants.LINE_SEPARATOR;
|
||||
return fieldsArrays.stream().map(i -> i.getString(0) + CsvLayoutUtils.PROPERTY_SEPARATOR + i.getString(1)).collect(Collectors.joining(CsvLayoutUtils.ITEM_SEPARATOR))
|
||||
+ CsvLayoutUtils.ITEM_SEPARATOR;
|
||||
}
|
||||
|
||||
static String buildDefaultLayout() {
|
||||
return resources.getString(R.string.stats_custom_layout_default_layout) + CsvLayoutUtils.ITEM_SEPARATOR + getLayoutColumnsByDefault() + CsvLayoutUtils.ITEM_SEPARATOR + buildDefaultFields();
|
||||
}
|
||||
|
||||
public static String getDefaultLayoutName() {
|
||||
return resources.getString(R.string.stats_custom_layout_default_layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return custom layout selected or the first one if any has been selected or the one selected is not exists anymore.
|
||||
*/
|
||||
public static Layout getCustomLayout() {
|
||||
String csvCustomLayout = getString(R.string.stats_custom_layout_fields_key, buildDefaultLayout());
|
||||
List<String> csvParts = Arrays.asList(csvCustomLayout.split(CsvConstants.LINE_SEPARATOR));
|
||||
Layout layout = new Layout(csvParts.get(0));
|
||||
for (int i = 1; i < csvParts.size(); i++) {
|
||||
String[] fieldParts = csvParts.get(i).split(CsvConstants.ITEM_SEPARATOR);
|
||||
layout.addField(fieldParts[0], DataField.getTitleByKey(resources, fieldParts[0]), fieldParts[1].equals(DataField.YES_VALUE), fieldParts[2].equals(DataField.YES_VALUE), fieldParts[0].equals(resources.getString(R.string.stats_custom_layout_coordinates_key)));
|
||||
String csvCustomLayouts = getString(R.string.stats_custom_layouts_key, buildDefaultLayout());
|
||||
String[] csvLines = csvCustomLayouts.split(CsvLayoutUtils.LINE_SEPARATOR);
|
||||
String layoutSelected = getString(R.string.stats_custom_layout_selected_layout_key, null);
|
||||
if (layoutSelected == null) {
|
||||
return Layout.fromCsv(csvLines[0], resources);
|
||||
}
|
||||
|
||||
return layout;
|
||||
for (String line : csvLines) {
|
||||
Layout layout = Layout.fromCsv(line, resources);
|
||||
if (layout.sameName(layoutSelected)) {
|
||||
return layout;
|
||||
}
|
||||
}
|
||||
|
||||
return Layout.fromCsv(csvLines[0], resources);
|
||||
}
|
||||
|
||||
public static void setCustomLayout(Layout layout) {
|
||||
List<DataField> fields = layout.getFields();
|
||||
if (fields.isEmpty()) {
|
||||
return;
|
||||
public static void updateCustomLayouts(@NonNull List<Layout> layouts) {
|
||||
setString(R.string.stats_custom_layouts_key, layouts.stream().map(Layout::toCsv).collect(Collectors.joining(CsvLayoutUtils.LINE_SEPARATOR)));
|
||||
}
|
||||
|
||||
public static void updateCustomLayout(@NonNull Layout layout) {
|
||||
List<Layout> preferenceLayouts = PreferencesUtils.getAllCustomLayouts();
|
||||
Optional<Layout> layoutToBeUpdated = preferenceLayouts.stream().filter(l -> l.sameName(layout)).findFirst();
|
||||
if (layoutToBeUpdated.isPresent()) {
|
||||
layoutToBeUpdated.get().replaceAllFields(layout.getFields());
|
||||
layoutToBeUpdated.get().setColumnsPerRow(layout.getColumnsPerRow());
|
||||
PreferencesUtils.updateCustomLayouts(preferenceLayouts);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addCustomLayout(@NonNull String layoutName) {
|
||||
String newLayoutCsv = layoutName + CsvLayoutUtils.ITEM_SEPARATOR + getLayoutColumnsByDefault() + CsvLayoutUtils.ITEM_SEPARATOR + buildDefaultFields();
|
||||
String customLayoutCsv = getString(R.string.stats_custom_layouts_key, buildDefaultLayout()) + CsvLayoutUtils.LINE_SEPARATOR + newLayoutCsv;
|
||||
setString(R.string.stats_custom_layouts_key, customLayoutCsv);
|
||||
}
|
||||
|
||||
public static void setDefaultLayout(String layoutName) {
|
||||
setString(R.string.stats_custom_layout_selected_layout_key, layoutName);
|
||||
}
|
||||
|
||||
public static List<Layout> getAllCustomLayouts() {
|
||||
List<Layout> layouts = new ArrayList<>();
|
||||
String csvCustomLayout = getString(R.string.stats_custom_layouts_key, buildDefaultLayout());
|
||||
String[] csvLines = csvCustomLayout.split(CsvLayoutUtils.LINE_SEPARATOR);
|
||||
for (String line : csvLines) {
|
||||
layouts.add(Layout.fromCsv(line, resources));
|
||||
}
|
||||
|
||||
String csv = layout.getProfile() + CsvConstants.LINE_SEPARATOR
|
||||
+ fields.stream().map(DataField::toCsv).collect(Collectors.joining(CsvConstants.LINE_SEPARATOR))
|
||||
+ CsvConstants.LINE_SEPARATOR;
|
||||
setString(R.string.stats_custom_layout_fields_key, csv);
|
||||
return layouts;
|
||||
}
|
||||
|
||||
public static List<String> getAllCustomLayoutNames() {
|
||||
return getAllCustomLayouts().stream().map(Layout::getName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static void resetCustomLayoutPreferences() {
|
||||
if (sharedPreferences.contains(resources.getString(R.string.stats_custom_layout_fields_key))) {
|
||||
if (sharedPreferences.contains(resources.getString(R.string.stats_custom_layouts_key))) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.remove(resources.getString(R.string.stats_custom_layout_fields_key));
|
||||
editor.commit();
|
||||
}
|
||||
if (sharedPreferences.contains(resources.getString(R.string.stats_custom_layout_columns_key))) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.remove(resources.getString(R.string.stats_custom_layout_columns_key));
|
||||
editor.remove(resources.getString(R.string.stats_custom_layouts_key));
|
||||
editor.commit();
|
||||
}
|
||||
}
|
||||
|
||||
+20
-16
@@ -18,18 +18,20 @@ import java.util.stream.IntStream;
|
||||
|
||||
import de.dennisguse.opentracks.AbstractActivity;
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.adapters.SettingsCustomLayoutAdapter;
|
||||
import de.dennisguse.opentracks.adapters.SettingsCustomLayoutEditAdapter;
|
||||
import de.dennisguse.opentracks.content.data.Layout;
|
||||
import de.dennisguse.opentracks.content.data.DataField;
|
||||
import de.dennisguse.opentracks.databinding.ActivitySettingsCustomLayoutBinding;
|
||||
import de.dennisguse.opentracks.util.StatisticsUtils;
|
||||
|
||||
public class SettingsCustomLayoutActivity extends AbstractActivity implements SettingsCustomLayoutAdapter.SettingsCustomLayoutItemClickListener {
|
||||
public class SettingsCustomLayoutEditActivity extends AbstractActivity implements SettingsCustomLayoutEditAdapter.SettingsCustomLayoutItemClickListener {
|
||||
|
||||
public static final String EXTRA_LAYOUT = "extraLayout";
|
||||
private ActivitySettingsCustomLayoutBinding viewBinding;
|
||||
private GridLayoutManager gridLayoutManager;
|
||||
private SettingsCustomLayoutAdapter adapterFieldsVisible;
|
||||
private SettingsCustomLayoutAdapter adapterFieldsHidden;
|
||||
private SettingsCustomLayoutEditAdapter adapterFieldsVisible;
|
||||
private SettingsCustomLayoutEditAdapter adapterFieldsHidden;
|
||||
private String profile;
|
||||
private Layout layoutFieldsVisible;
|
||||
private Layout layoutFieldsHidden;
|
||||
private int numColumns;
|
||||
@@ -38,12 +40,13 @@ public class SettingsCustomLayoutActivity extends AbstractActivity implements Se
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
|
||||
// Recycler view with visible stats.
|
||||
layoutFieldsVisible = StatisticsUtils.filterVisible(PreferencesUtils.getCustomLayout(), true);
|
||||
adapterFieldsVisible = new SettingsCustomLayoutAdapter(this, this, layoutFieldsVisible);
|
||||
Layout layout = getIntent().getParcelableExtra(EXTRA_LAYOUT);
|
||||
profile = layout.getName();
|
||||
layoutFieldsVisible = StatisticsUtils.filterVisible(layout, true);
|
||||
adapterFieldsVisible = new SettingsCustomLayoutEditAdapter(this, this, layoutFieldsVisible);
|
||||
|
||||
numColumns = PreferencesUtils.getLayoutColumns();
|
||||
numColumns = layout.getColumnsPerRow();
|
||||
RecyclerView recyclerViewVisible = viewBinding.recyclerViewVisible;
|
||||
gridLayoutManager = new GridLayoutManager(this, numColumns);
|
||||
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
||||
@@ -86,7 +89,6 @@ public class SettingsCustomLayoutActivity extends AbstractActivity implements Se
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
numColumns = position + 1;
|
||||
gridLayoutManager.setSpanCount(numColumns);
|
||||
PreferencesUtils.setLayoutColumns(position + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,24 +96,26 @@ public class SettingsCustomLayoutActivity extends AbstractActivity implements Se
|
||||
|
||||
}
|
||||
});
|
||||
viewBinding.spinnerOptions.setSelection(PreferencesUtils.getLayoutColumns() - 1);
|
||||
viewBinding.spinnerOptions.setSelection(numColumns - 1);
|
||||
|
||||
// Recycler view with not visible stats.
|
||||
layoutFieldsHidden = StatisticsUtils.filterVisible(PreferencesUtils.getCustomLayout(), false);
|
||||
adapterFieldsHidden = new SettingsCustomLayoutAdapter(this, this, layoutFieldsHidden);
|
||||
layoutFieldsHidden = StatisticsUtils.filterVisible(layout, false);
|
||||
adapterFieldsHidden = new SettingsCustomLayoutEditAdapter(this, this, layoutFieldsHidden);
|
||||
RecyclerView recyclerViewNotVisible = viewBinding.recyclerViewNotVisible;
|
||||
recyclerViewNotVisible.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerViewNotVisible.setAdapter(adapterFieldsHidden);
|
||||
|
||||
setTitle(profile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (!layoutFieldsVisible.getFields().isEmpty() || !layoutFieldsHidden.getFields().isEmpty()) {
|
||||
Layout newLayout = new Layout(layoutFieldsVisible.getProfile());
|
||||
newLayout.addFields(layoutFieldsVisible.getFields());
|
||||
newLayout.addFields(layoutFieldsHidden.getFields());
|
||||
PreferencesUtils.setCustomLayout(newLayout);
|
||||
Layout layout = new Layout(profile, numColumns);
|
||||
layout.addFields(layoutFieldsVisible.getFields());
|
||||
layout.addFields(layoutFieldsHidden.getFields());
|
||||
PreferencesUtils.updateCustomLayout(layout);
|
||||
}
|
||||
}
|
||||
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package de.dennisguse.opentracks.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.ItemTouchHelper;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
|
||||
import de.dennisguse.opentracks.AbstractActivity;
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.adapters.SettingsCustomLayoutListAdapter;
|
||||
import de.dennisguse.opentracks.content.data.Layout;
|
||||
import de.dennisguse.opentracks.databinding.ActivitySettingsCustomLayoutListBinding;
|
||||
import de.dennisguse.opentracks.util.RecyclerViewSwipeDeleteCallback;
|
||||
|
||||
public class SettingsCustomLayoutListActivity extends AbstractActivity implements SettingsCustomLayoutListAdapter.SettingsCustomLayoutProfileClickListener {
|
||||
|
||||
private ActivitySettingsCustomLayoutListBinding viewBinding;
|
||||
private SettingsCustomLayoutListAdapter adapter;
|
||||
private RecyclerView recyclerView;
|
||||
private View addProfileLayout;
|
||||
private TextInputEditText addProfileEditText;
|
||||
private TextInputLayout addProfileInputLayout;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setTitle(getString(R.string.custom_layout_list_title));
|
||||
|
||||
adapter = new SettingsCustomLayoutListAdapter(this, this);
|
||||
recyclerView = viewBinding.recyclerView;
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
Button cancelButton = findViewById(R.id.custom_layout_list_cancel_button);
|
||||
Button okButton = findViewById(R.id.custom_layout_list_ok_button);
|
||||
|
||||
cancelButton.setOnClickListener(view -> clearAndHideEditLayout());
|
||||
|
||||
okButton.setEnabled(false);
|
||||
okButton.setOnClickListener(view -> {
|
||||
PreferencesUtils.addCustomLayout(addProfileEditText.getText().toString());
|
||||
clearAndHideEditLayout();
|
||||
adapter.reloadLayouts();
|
||||
});
|
||||
|
||||
addProfileLayout = findViewById(R.id.custom_layout_list_add_linear_layout);
|
||||
addProfileInputLayout = findViewById(R.id.custom_layout_list_input_layout);
|
||||
addProfileEditText = findViewById(R.id.custom_layout_list_edit_name);
|
||||
addProfileEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
if (s == null || s.toString().isEmpty()) {
|
||||
okButton.setEnabled(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (adapter.getLayouts().stream().anyMatch(layout -> layout.sameName(s.toString()))) {
|
||||
okButton.setEnabled(false);
|
||||
addProfileInputLayout.setError(getString(R.string.custom_layout_list_edit_already_exists));
|
||||
} else {
|
||||
okButton.setEnabled(true);
|
||||
addProfileInputLayout.setError("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
}
|
||||
});
|
||||
|
||||
RecyclerViewSwipeDeleteCallback recyclerViewSwipeDeleteCallback = new RecyclerViewSwipeDeleteCallback(this) {
|
||||
@Override
|
||||
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
|
||||
// When there's only one profile it cannot be deleted (so, "disable" movements: drag flags and swipe flags).
|
||||
return adapter.getItemCount() > 1 ? makeMovementFlags(0, ItemTouchHelper.LEFT) : makeMovementFlags(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
|
||||
final int position = viewHolder.getAdapterPosition();
|
||||
final Layout item = adapter.getLayouts().get(position);
|
||||
|
||||
adapter.removeLayout(position);
|
||||
|
||||
Snackbar snackbar = Snackbar.make(recyclerView, getString(R.string.custom_layout_list_layout_removed), Snackbar.LENGTH_LONG);
|
||||
snackbar.setAction(getString(R.string.generic_undo).toUpperCase(), view -> {
|
||||
adapter.restoreItem(item, position);
|
||||
recyclerView.scrollToPosition(position);
|
||||
});
|
||||
|
||||
snackbar.show();
|
||||
}
|
||||
};
|
||||
|
||||
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(recyclerViewSwipeDeleteCallback);
|
||||
itemTouchhelper.attachToRecyclerView(recyclerView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
adapter.reloadLayouts();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected View getRootView() {
|
||||
PreferencesUtils.getCustomLayout();
|
||||
viewBinding = ActivitySettingsCustomLayoutListBinding.inflate(getLayoutInflater());
|
||||
return viewBinding.getRoot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.custom_layout_edit, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if (item.getItemId() == R.id.custom_layout_edit_add_profile) {
|
||||
addProfileLayout.setVisibility(View.VISIBLE);
|
||||
addProfileEditText.requestFocus();
|
||||
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.showSoftInput(addProfileEditText, InputMethodManager.SHOW_IMPLICIT);
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSettingsCustomLayoutProfileClicked(@NonNull Layout layout) {
|
||||
Intent intent = new Intent(this, SettingsCustomLayoutEditActivity.class);
|
||||
intent.putExtra(SettingsCustomLayoutEditActivity.EXTRA_LAYOUT, layout);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private void clearAndHideEditLayout() {
|
||||
addProfileEditText.setText("");
|
||||
addProfileInputLayout.setError("");
|
||||
addProfileLayout.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class UserInterfaceSettingsFragment extends PreferenceFragmentCompat {
|
||||
|
||||
Preference customLayoutPreference = findPreference(getString(R.string.stats_custom_layout_key));
|
||||
customLayoutPreference.setOnPreferenceClickListener((preference) -> {
|
||||
Intent intent = new Intent(getContext(), SettingsCustomLayoutActivity.class);
|
||||
Intent intent = new Intent(getContext(), SettingsCustomLayoutListActivity.class);
|
||||
startActivity(intent);
|
||||
return true;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user