forked from osmandapp/OsmAnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b76710
commit c877d17
Showing
6 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
OsmAnd/src/net/osmand/plus/settings/fragments/search/PreferencePathDisplayerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters