Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 14, 2024
1 parent 300bb53 commit 0e8ab87
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,92 +1,7 @@
package net.osmand.plus.settings.fragments.search;

import androidx.preference.Preference;
@FunctionalInterface
public interface SearchableInfoProvider {

import net.osmand.plus.settings.preferences.EditTextPreferenceEx;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference;
import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import de.KnollFrank.lib.settingssearch.common.Lists;

class SearchableInfoProvider implements de.KnollFrank.lib.settingssearch.search.provider.SearchableInfoProvider {

@Override
public Optional<String> getSearchableInfo(final Preference preference) {
if (preference instanceof final ListPreferenceEx listPreferenceEx) {
return Optional.of(getSearchableInfo(listPreferenceEx));
}
if (preference instanceof final SwitchPreferenceEx switchPreferenceEx) {
return Optional.of(getSearchableInfo(switchPreferenceEx));
}
if (preference instanceof final MultiSelectBooleanPreference multiSelectBooleanPreference) {
return Optional.of(getSearchableInfo(multiSelectBooleanPreference));
}
if (preference instanceof final EditTextPreferenceEx editTextPreferenceEx) {
return Optional.of(getSearchableInfo(editTextPreferenceEx));
}
if (preference instanceof final SizePreference sizePreference) {
return Optional.of(getSizePreferenceSearchableInfo(sizePreference));
}
return Optional.empty();
}

private static String getSearchableInfo(final ListPreferenceEx preference) {
return String.join(
", ",
concat(
Optional.ofNullable(preference.getDialogTitle()),
Optional.ofNullable(preference.getDescription()),
Optional.ofNullable(preference.getEntries())));
}

private static String getSearchableInfo(final SwitchPreferenceEx preference) {
return String.join(
", ",
Lists.getPresentElements(
Arrays.asList(
Optional.ofNullable(preference.getSummaryOff()),
Optional.ofNullable(preference.getSummaryOn()),
Optional.ofNullable(preference.getDescription()))));
}

private static String getSearchableInfo(final MultiSelectBooleanPreference preference) {
return String.join(
", ",
concat(
Optional.ofNullable(preference.getDialogTitle()),
Optional.ofNullable(preference.getDescription()),
Optional.ofNullable(preference.getEntries())));
}

private static String getSearchableInfo(final EditTextPreferenceEx preference) {
return String.join(
", ",
Lists.getPresentElements(
Arrays.asList(
Optional.ofNullable(preference.getText()),
Optional.ofNullable(preference.getDescription()))));
}

private static String getSizePreferenceSearchableInfo(final SizePreference preference) {
return String.join(
", ",
Lists.getPresentElements(
Arrays.asList(
Optional.ofNullable(preference.getDialogTitle()),
Optional.ofNullable(preference.getSummary()))));
}

private static List<CharSequence> concat(final Optional<CharSequence> dialogTitle,
final Optional<CharSequence> description,
final Optional<CharSequence[]> entries) {
final List<CharSequence> result = Lists.getPresentElements(Arrays.asList(dialogTitle, description));
result.addAll(Lists.asList(entries));
return result;
}
String getSearchableInfo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import androidx.annotation.IdRes;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static SearchPreferenceFragments createSearchPreferenceFragments(
new SearchDatabaseConfigBuilder()
.withFragmentFactory(new FragmentFactory())
.withPreferenceFragmentConnected2PreferenceProvider(new PreferenceFragmentConnected2PreferenceProvider())
.withSearchableInfoProvider(new SearchableInfoProvider())
.withSearchableInfoProvider(SettingsSearchButtonHelper::getSearchableInfo)
.withPreferenceDialogAndSearchableInfoProvider(new PreferenceDialogAndSearchableInfoProvider())
.withPreferenceSearchablePredicate(new PreferenceSearchablePredicate())
.build())
Expand All @@ -78,6 +79,12 @@ public static SearchPreferenceFragments createSearchPreferenceFragments(
.build();
}

private static Optional<String> getSearchableInfo(final Preference preference) {
return preference instanceof final SearchableInfoProvider searchableInfoProvider ?
Optional.of(searchableInfoProvider.getSearchableInfo()) :
Optional.empty();
}

private void onClickShowSearchPreferenceFragment(final ImageView searchPreferenceButton) {
final SearchPreferenceFragments searchPreferenceFragments =
createSearchPreferenceFragments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import androidx.preference.EditTextPreference;

public class EditTextPreferenceEx extends EditTextPreference {
import net.osmand.plus.settings.fragments.search.SearchableInfoProvider;

import java.util.List;
import java.util.Optional;

import de.KnollFrank.lib.settingssearch.common.Lists;

public class EditTextPreferenceEx extends EditTextPreference implements SearchableInfoProvider {

private String description;

Expand Down Expand Up @@ -36,4 +43,14 @@ public void setDescription(String description) {
public void setDescription(int descriptionResId) {
setDescription(getContext().getString(descriptionResId));
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
Lists.getPresentElements(
List.of(
Optional.ofNullable(getText()),
Optional.ofNullable(getDescription()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmAndPreferencesDataStore;
import net.osmand.plus.settings.fragments.search.SearchableInfoProvider;

public class ListPreferenceEx extends DialogPreference {
import java.util.List;
import java.util.Optional;

import de.KnollFrank.lib.settingssearch.common.Lists;

public class ListPreferenceEx extends DialogPreference implements SearchableInfoProvider {

private String[] entries;
private Object[] entryValues;
Expand Down Expand Up @@ -148,4 +154,22 @@ private void persistValue(Object value) {
((OsmAndPreferencesDataStore) dataStore).putValue(getKey(), value);
}
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
concat(
Optional.ofNullable(getDialogTitle()),
Optional.ofNullable(getDescription()),
Optional.ofNullable(getEntries())));
}

static List<CharSequence> concat(final Optional<CharSequence> dialogTitle,
final Optional<CharSequence> description,
final Optional<CharSequence[]> entries) {
final List<CharSequence> result = Lists.getPresentElements(List.of(dialogTitle, description));
result.addAll(Lists.asList(entries));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package net.osmand.plus.settings.preferences;

import static net.osmand.plus.settings.preferences.ListPreferenceEx.concat;

import android.content.Context;
import android.util.AttributeSet;

import androidx.preference.MultiSelectListPreference;
import androidx.preference.PreferenceDataStore;

import net.osmand.plus.settings.backend.OsmAndPreferencesDataStore;
import net.osmand.plus.settings.fragments.search.SearchableInfoProvider;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class MultiSelectBooleanPreference extends MultiSelectListPreference {
public class MultiSelectBooleanPreference extends MultiSelectListPreference implements SearchableInfoProvider {

private String description;

Expand Down Expand Up @@ -109,4 +113,14 @@ public Set<String> getPersistedBooleanPrefsIds(Set<String> defaultReturnValue) {

return enabledPrefs;
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
concat(
Optional.ofNullable(getDialogTitle()),
Optional.ofNullable(getDescription()),
Optional.ofNullable(getEntries())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
import androidx.preference.DialogPreference;

import net.osmand.plus.R;
import net.osmand.plus.settings.fragments.search.SearchableInfoProvider;
import net.osmand.plus.settings.vehiclesize.SizeType;
import net.osmand.plus.settings.vehiclesize.VehicleSizes;
import net.osmand.plus.settings.vehiclesize.containers.Metric;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

public class SizePreference extends DialogPreference {
import de.KnollFrank.lib.settingssearch.common.Lists;

public class SizePreference extends DialogPreference implements SearchableInfoProvider {

private SizeType sizeType;
private Metric metric;
Expand Down Expand Up @@ -78,7 +83,17 @@ private String getString(int stringId) {
return getContext().getString(stringId);
}

public String getValue () {
public String getValue() {
return getPersistedString(defaultValue);
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
Lists.getPresentElements(
List.of(
Optional.ofNullable(getDialogTitle()),
Optional.ofNullable(getSummary()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import androidx.preference.SwitchPreferenceCompat;

public class SwitchPreferenceEx extends SwitchPreferenceCompat {
import net.osmand.plus.settings.fragments.search.SearchableInfoProvider;

import java.util.List;
import java.util.Optional;

import de.KnollFrank.lib.settingssearch.common.Lists;

public class SwitchPreferenceEx extends SwitchPreferenceCompat implements SearchableInfoProvider {

private String description;

Expand Down Expand Up @@ -43,4 +50,15 @@ protected void onClick() {
getPreferenceManager().showDialog(this);
}
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
Lists.getPresentElements(
List.of(
Optional.ofNullable(getSummaryOff()),
Optional.ofNullable(getSummaryOn()),
Optional.ofNullable(getDescription()))));
}
}

0 comments on commit 0e8ab87

Please sign in to comment.