From da91e721addfe4bbb7f8236dfa6bf875eecbcff3 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Fri, 20 Dec 2024 09:05:18 +0100 Subject: [PATCH] starting to make WakeTimeBottomSheet searchable --- OsmAnd/build-common.gradle | 2 - .../bottomsheets/WakeTimeBottomSheet.java | 56 ++++++++++--------- .../fragments/TurnScreenOnFragment.java | 43 +++++++++++--- .../SettingsSearchParameterizedTest.java | 24 ++++---- 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/OsmAnd/build-common.gradle b/OsmAnd/build-common.gradle index 44abb87a9ab..7db0ac391ce 100644 --- a/OsmAnd/build-common.gradle +++ b/OsmAnd/build-common.gradle @@ -397,8 +397,6 @@ dependencies { implementation 'com.github.KnollFrank:SettingsSearch:a2aa865f48' // https://mvnrepository.com/artifact/com.google.guava/guava implementation 'com.google.guava:guava:33.3.1-android' - // https://mvnrepository.com/artifact/com.codepoetics/ambivalence - androidTestImplementation 'com.codepoetics:ambivalence:0.2' //implementation "androidx.tracing:tracing:1.1.0" //debugImplementation 'androidx.test:monitor:1.6.1' diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java index b186bd49377..19036444b23 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/WakeTimeBottomSheet.java @@ -9,11 +9,12 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; +import androidx.preference.Preference; import com.google.android.material.slider.Slider; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.utils.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; @@ -24,9 +25,13 @@ import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.fragments.ApplyQueryType; import net.osmand.plus.settings.fragments.OnPreferenceChanged; +import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; import net.osmand.plus.settings.preferences.ListPreferenceEx; +import net.osmand.plus.utils.UiUtilities; + +import java.util.Optional; -public class WakeTimeBottomSheet extends BasePreferenceBottomSheet { +public class WakeTimeBottomSheet extends BasePreferenceBottomSheet implements SearchablePreferenceDialog { public static final String TAG = WakeTimeBottomSheet.class.getSimpleName(); @@ -94,8 +99,6 @@ public void onClick(View v) { sliderView = UiUtilities.getInflater(ctx, nightMode).inflate(R.layout.bottom_sheet_item_slider_with_two_text, null); AndroidUiHelper.updateVisibility(sliderView, !keepScreenOnEnabled); - Context themedCtx = UiUtilities.getThemedContext(ctx, nightMode); - TextView tvSliderTitle = sliderView.findViewById(android.R.id.title); tvSliderTitle.setText(getString(R.string.wake_time)); @@ -134,8 +137,7 @@ public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { for (BaseBottomSheetItem item : items) { - if (item instanceof BottomSheetItemWithCompoundButton) { - BottomSheetItemWithCompoundButton itemWithCompoundButton = (BottomSheetItemWithCompoundButton) item; + if (item instanceof final BottomSheetItemWithCompoundButton itemWithCompoundButton) { itemWithCompoundButton.getCompoundButton().setSaveEnabled(false); } } @@ -181,24 +183,28 @@ private ListPreferenceEx getListPreference() { return (ListPreferenceEx) getPreference(); } - public static boolean showInstance(@NonNull FragmentManager fragmentManager, String prefId, Fragment target, boolean usedOnMap, - @Nullable ApplicationMode appMode, ApplyQueryType applyQueryType, - boolean profileDependent) { - try { - Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, prefId); - - WakeTimeBottomSheet fragment = new WakeTimeBottomSheet(); - fragment.setArguments(args); - fragment.setUsedOnMap(usedOnMap); - fragment.setAppMode(appMode); - fragment.setApplyQueryType(applyQueryType); - fragment.setTargetFragment(target, 0); - fragment.setProfileDependent(profileDependent); - fragment.show(fragmentManager, TAG); - return true; - } catch (RuntimeException e) { - return false; - } + @NonNull + public static WakeTimeBottomSheet createInstance(final Preference preference, + final Fragment target, + final boolean usedOnMap, + final @Nullable ApplicationMode appMode, + final ApplyQueryType applyQueryType, + final boolean profileDependent) { + final WakeTimeBottomSheet bottomSheet = new WakeTimeBottomSheet(); + bottomSheet.setApplyQueryType(applyQueryType); + bottomSheet.setProfileDependent(profileDependent); + return BasePreferenceBottomSheetInitializer + .initialize(bottomSheet) + .with(Optional.of(preference), appMode, usedOnMap, target); + } + + @Override + public void show(final FragmentManager fragmentManager, final OsmandApplication app) { + show(fragmentManager, TAG); + } + + @Override + public String getSearchableInfo() { + return getString(R.string.turn_screen_on_wake_time_descr, getString(R.string.keep_screen_on)); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java index f87abd4aec9..490131c0782 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/TurnScreenOnFragment.java @@ -1,8 +1,11 @@ package net.osmand.plus.settings.fragments; +import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference; + import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; @@ -11,10 +14,15 @@ import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.bottomsheets.ScreenTimeoutBottomSheet; import net.osmand.plus.settings.bottomsheets.WakeTimeBottomSheet; +import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialog; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider; import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.plus.settings.preferences.SwitchPreferenceEx; -public class TurnScreenOnFragment extends BaseSettingsFragment { +import java.util.Optional; + +public class TurnScreenOnFragment extends BaseSettingsFragment implements ShowableSearchablePreferenceDialogProvider { public static final String TAG = TurnScreenOnFragment.class.getSimpleName(); @@ -45,17 +53,36 @@ protected void onBindPreferenceViewHolder(@NonNull Preference preference, @NonNu } @Override - public void onDisplayPreferenceDialog(Preference preference) { + public Optional> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) { + return settings.TURN_SCREEN_ON_TIME_INT.getId().equals(preference.getKey()) ? + Optional.of( + new ShowableSearchablePreferenceDialog<>( + WakeTimeBottomSheet + .createInstance( + preference, + target, + false, + getSelectedAppMode(), + getApplyQueryType(), + isProfileDependent())) { + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + searchablePreferenceDialog.show(getFragmentManager(), app); + } + }) : + Optional.empty(); + } + + @Override + public void onDisplayPreferenceDialog(final Preference preference) { + if (showDialogForPreference(preference, this)) { + return; + } FragmentManager fragmentManager = getFragmentManager(); - ApplicationMode appMode = getSelectedAppMode(); String prefId = preference.getKey(); if (settings.USE_SYSTEM_SCREEN_TIMEOUT.getId().equals(prefId)) { if (fragmentManager != null) { - ScreenTimeoutBottomSheet.showInstance(fragmentManager, prefId, this, false, appMode, getApplyQueryType(), isProfileDependent()); - } - } else if (settings.TURN_SCREEN_ON_TIME_INT.getId().equals(prefId)) { - if (fragmentManager != null) { - WakeTimeBottomSheet.showInstance(fragmentManager, prefId, this, false, appMode, getApplyQueryType(), isProfileDependent()); + ScreenTimeoutBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode(), getApplyQueryType(), isProfileDependent()); } } else { super.onDisplayPreferenceDialog(preference); diff --git a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java index 29120e8d5e1..6f000db8cf6 100644 --- a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java +++ b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java @@ -9,12 +9,12 @@ import static net.osmand.plus.settings.fragments.search.SettingsSearchTest.searchResultsView; import static net.osmand.plus.settings.fragments.search.SettingsSearchTest.searchView; +import android.content.Context; + import androidx.annotation.StringRes; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.filters.LargeTest; -import com.codepoetics.ambivalence.Either; - import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.test.common.AndroidTest; @@ -35,7 +35,7 @@ public class SettingsSearchParameterizedTest extends AndroidTest { public String description; @Parameterized.Parameter(value = 1) - public Either searchQueryIdOrString; + public Function searchQueryProvider; @Parameterized.Parameters(name = "{0}") public static Iterable data() { @@ -82,6 +82,8 @@ public static Iterable data() { {"VoiceLanguageBottomSheetFragment: language_description", searchQuery(R.string.language_description)}, {"VoiceLanguageBottomSheetFragment: tts_description", searchQuery(R.string.tts_description)}, {"VoiceLanguageBottomSheetFragment: recorded_description", searchQuery(R.string.recorded_description)}, + + {"WakeTimeBottomSheet: description", searchQuery(context -> context.getString(R.string.turn_screen_on_wake_time_descr, context.getString(R.string.keep_screen_on)))} }); } @@ -90,7 +92,7 @@ public static Iterable data() { @Test public void testSearchAndFind() { - testSearchAndFind(getSearchQuery()); + testSearchAndFind(searchQueryProvider.apply(app)); } private void testSearchAndFind(final String searchQuery) { @@ -104,17 +106,15 @@ private void testSearchAndFind(final String searchQuery) { onView(searchResultsView()).check(matches(hasSearchResultWithSubstring(searchQuery))); } - private String getSearchQuery() { - return searchQueryIdOrString.join( - app::getString, - Function.identity()); + private static Function searchQuery(final @StringRes int id) { + return context -> context.getString(id); } - private static Either searchQuery(final @StringRes int id) { - return Either.ofLeft(id); + private static Function searchQuery(final String str) { + return context -> str; } - private static Either searchQuery(final String str) { - return Either.ofRight(str); + private static Function searchQuery(final Function searchQueryProvider) { + return searchQueryProvider; } }