Skip to content

Commit

Permalink
adding PreferencePathDisplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 9, 2024
1 parent 8b76710 commit c877d17
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion OsmAnd/build-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions OsmAnd/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<attr name="maxVisibleWidth" format="dimension"/>
</declare-styleable>
<declare-styleable name="OsmAndTheme">
<attr name="backgroundColor" format="reference|color" />
<attr name="mapBackground" format="reference"/>
<attr name="routeParameterTitleColor" format="color"/>
<attr name="chart_marker_background" format="reference" />
Expand Down
12 changes: 9 additions & 3 deletions OsmAnd/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
</style>

<!-- Dialog popup -->
<style name="Animations"/>
<style name="Animations" />

<style name="Animations.NoAnimation">
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@null</item>
</style>
<!-- PopDownMenu -->
<style name="Animations.PopDownMenu"/>
<style name="Animations.PopDownMenu" />

<style name="Animations.PopDownMenu.Left">
<item name="android:windowEnterAnimation">@anim/grow_from_topleft_to_bottomright</item>
Expand All @@ -196,7 +196,7 @@
</style>

<!-- PopUpMenu -->
<style name="Animations.PopUpMenu"/>
<style name="Animations.PopUpMenu" />

<style name="Animations.PopUpMenu.Fade">
<item name="android:windowEnterAnimation">@anim/popup_show</item>
Expand Down Expand Up @@ -427,4 +427,10 @@
<style name="SearchPreferenceResultBackgroundColor">
<item name="backgroundColor">#00FF00</item>
</style>

<style name="PreferencePathTextAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -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<String> applicationModesKeys;

public PreferencePathDisplayer(final Context context, final Set<String> applicationModesKeys) {
this.context = context;
this.applicationModesKeys = applicationModesKeys;
}

@Override
public CharSequence display(final PreferencePath preferencePath) {
final List<SpannableString> titles = getTitles(preferencePath);
highlightApplicationModeAtStartOfLongPreferencePath(preferencePath, titles);
return join(titles, new SpannableString(" > "));
}

private static List<SpannableString> 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<SpannableString> 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<SpannableString> titles, final SpannableString delimiter) {
return appendAll(insert(titles, delimiter));
}

private static <T> List<T> insert(final List<T> 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<? extends CharSequence> charSequences) {
final SpannableStringBuilder builder = new SpannableStringBuilder();
charSequences.forEach(builder::append);
return new SpannableString(builder);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> getApplicationModesKeys() {
return ApplicationMode
.allPossibleValues()
.stream()
.map(ApplicationMode::getStringKey)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit c877d17

Please sign in to comment.