Skip to content

Commit

Permalink
starting to make WakeTimeBottomSheet searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 20, 2024
1 parent f6e689b commit da91e72
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
2 changes: 0 additions & 2 deletions OsmAnd/build-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -45,17 +53,36 @@ protected void onBindPreferenceViewHolder(@NonNull Preference preference, @NonNu
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
public Optional<ShowableSearchablePreferenceDialog<?>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +35,7 @@ public class SettingsSearchParameterizedTest extends AndroidTest {
public String description;

@Parameterized.Parameter(value = 1)
public Either<Integer, String> searchQueryIdOrString;
public Function<Context, String> searchQueryProvider;

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
Expand Down Expand Up @@ -82,6 +82,8 @@ public static Iterable<Object[]> 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)))}
});
}

Expand All @@ -90,7 +92,7 @@ public static Iterable<Object[]> data() {

@Test
public void testSearchAndFind() {
testSearchAndFind(getSearchQuery());
testSearchAndFind(searchQueryProvider.apply(app));
}

private void testSearchAndFind(final String searchQuery) {
Expand All @@ -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<Context, String> searchQuery(final @StringRes int id) {
return context -> context.getString(id);
}

private static Either<Integer, String> searchQuery(final @StringRes int id) {
return Either.ofLeft(id);
private static Function<Context, String> searchQuery(final String str) {
return context -> str;
}

private static Either<Integer, String> searchQuery(final String str) {
return Either.ofRight(str);
private static Function<Context, String> searchQuery(final Function<Context, String> searchQueryProvider) {
return searchQueryProvider;
}
}

0 comments on commit da91e72

Please sign in to comment.