From c877d1776b4973797d2dca4d0a1b21708ab7a671 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Mon, 9 Dec 2024 17:21:09 +0100 Subject: [PATCH] adding PreferencePathDisplayer --- OsmAnd/build-common.gradle | 2 +- OsmAnd/res/values/attrs.xml | 1 + OsmAnd/res/values/styles.xml | 12 ++- .../search/PreferencePathDisplayer.java | 89 +++++++++++++++++++ .../PreferencePathDisplayerFactory.java | 23 +++++ .../search/SettingsSearchButtonHelper.java | 1 + 6 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayer.java create mode 100644 OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayerFactory.java diff --git a/OsmAnd/build-common.gradle b/OsmAnd/build-common.gradle index 958fb7df1c9..04957ac3667 100644 --- a/OsmAnd/build-common.gradle +++ b/OsmAnd/build-common.gradle @@ -375,7 +375,7 @@ dependencies { implementation "androidx.car.app:app-projected:1.4.0" implementation 'com.google.android.gms:play-services-location:21.3.0' - implementation 'com.github.KnollFrank:SettingsSearch:5ebe9dcfe7' + implementation 'com.github.KnollFrank:SettingsSearch:17e42c2fde' // https://mvnrepository.com/artifact/com.google.guava/guava implementation 'com.google.guava:guava:33.3.1-android' diff --git a/OsmAnd/res/values/attrs.xml b/OsmAnd/res/values/attrs.xml index 08f66cdec11..01763cb06c6 100644 --- a/OsmAnd/res/values/attrs.xml +++ b/OsmAnd/res/values/attrs.xml @@ -4,6 +4,7 @@ + diff --git a/OsmAnd/res/values/styles.xml b/OsmAnd/res/values/styles.xml index b675cbf7e2b..76df1de5c16 100644 --- a/OsmAnd/res/values/styles.xml +++ b/OsmAnd/res/values/styles.xml @@ -171,14 +171,14 @@ - - - + + diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayer.java b/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayer.java new file mode 100644 index 00000000000..f27e7a976a5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayer.java @@ -0,0 +1,89 @@ +package net.osmand.plus.settings.fragments.search; + +import android.content.Context; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.SpannableStringBuilder; +import android.text.style.TextAppearanceSpan; + +import net.osmand.plus.R; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import de.KnollFrank.lib.settingssearch.PreferencePath; +import de.KnollFrank.lib.settingssearch.db.preference.pojo.SearchablePreferencePOJO; + +class PreferencePathDisplayer implements de.KnollFrank.lib.settingssearch.results.recyclerview.PreferencePathDisplayer { + + private final Context context; + private final Set applicationModesKeys; + + public PreferencePathDisplayer(final Context context, final Set applicationModesKeys) { + this.context = context; + this.applicationModesKeys = applicationModesKeys; + } + + @Override + public CharSequence display(final PreferencePath preferencePath) { + final List titles = getTitles(preferencePath); + highlightApplicationModeAtStartOfLongPreferencePath(preferencePath, titles); + return join(titles, new SpannableString(" > ")); + } + + private static List getTitles(final PreferencePath preferencePath) { + return preferencePath + .preferences() + .stream() + .map(PreferencePathDisplayer::getTitle) + .collect(Collectors.toList()); + } + + private static SpannableString getTitle(final SearchablePreferencePOJO searchablePreferencePOJO) { + return searchablePreferencePOJO + .getTitle() + .map(SpannableString::new) + .orElseGet(() -> new SpannableString("?")); + } + + private void highlightApplicationModeAtStartOfLongPreferencePath(final PreferencePath preferencePath, final List titles) { + if (preferencePath.preferences().size() >= 2 && isApplicationMode(preferencePath.preferences().get(0))) { + highlight(titles.get(0)); + } + } + + private boolean isApplicationMode(final SearchablePreferencePOJO preference) { + return preference + .getKey() + .filter(applicationModesKeys::contains) + .isPresent(); + } + + private void highlight(final Spannable spannable) { + spannable.setSpan( + new TextAppearanceSpan(context, R.style.PreferencePathTextAppearance), + 0, + spannable.length(), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + private static SpannableString join(final List titles, final SpannableString delimiter) { + return appendAll(insert(titles, delimiter)); + } + + private static List insert(final List ts, final T delimiter) { + return ts + .stream() + .flatMap(t -> Stream.of(t, delimiter)) + .limit(ts.size() * 2L - 1) + .collect(Collectors.toList()); + } + + private static SpannableString appendAll(final List charSequences) { + final SpannableStringBuilder builder = new SpannableStringBuilder(); + charSequences.forEach(builder::append); + return new SpannableString(builder); + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayerFactory.java b/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayerFactory.java new file mode 100644 index 00000000000..eec609d3f2b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayerFactory.java @@ -0,0 +1,23 @@ +package net.osmand.plus.settings.fragments.search; + +import android.content.Context; + +import net.osmand.plus.settings.backend.ApplicationMode; + +import java.util.Set; +import java.util.stream.Collectors; + +class PreferencePathDisplayerFactory { + + public static de.KnollFrank.lib.settingssearch.results.recyclerview.PreferencePathDisplayer createPreferencePathDisplayer(final Context context) { + return new PreferencePathDisplayer(context, getApplicationModesKeys()); + } + + private static Set getApplicationModesKeys() { + return ApplicationMode + .allPossibleValues() + .stream() + .map(ApplicationMode::getStringKey) + .collect(Collectors.toSet()); + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java index fb5ac4f8403..ac8b138000b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java @@ -68,6 +68,7 @@ public static SearchPreferenceFragments createSearchPreferenceFragments( .build()) .withSearchConfig( new SearchConfigBuilder() + .withPreferencePathDisplayer(PreferencePathDisplayerFactory.createPreferencePathDisplayer(fragmentActivity)) .withSearchPreferenceFragmentUI(new SearchPreferenceFragmentUI()) .withSearchResultsFragmentUI(new SearchResultsFragmentUI()) .withPrepareShow(new PrepareShow())