Skip to content

Commit

Permalink
Merge pull request #534 from mjaakko/prerelease_fixes
Browse files Browse the repository at this point in the history
Do not save coverage layer settings when leaving the dialog + fix test
  • Loading branch information
mjaakko authored Feb 20, 2025
2 parents e47f8fe + b1df184 commit 47767ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package xyz.malkki.neostumbler.ui.composables.shared

import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.hasScrollAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isNotDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.swipeUp
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
Expand Down Expand Up @@ -54,9 +58,17 @@ class DateRangePickerDialogTest {
.onNode(hasText(dates.first().format(DATE_FORMAT), substring = true))
.performClick()

composeTestRule
.onNode(hasText(dates.last().format(DATE_FORMAT), substring = true))
.performClick()
val endDate =
composeTestRule.onNode(hasText(dates.last().format(DATE_FORMAT), substring = true))

// If the end date is not visible, scroll the calendar to show the next month
if (endDate.isNotDisplayed()) {
composeTestRule.onNode(hasScrollAction()).performTouchInput {
swipeUp(startY = centerY, endY = top)
}
}

endDate.performClick()

composeTestRule.onNodeWithText("select").performClick()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down Expand Up @@ -54,17 +55,17 @@ fun CoverageLayerSettings(settingsStore: DataStore<Preferences> = koinInject(PRE
if (dialogOpen.value) {
CoverageLayerDialog(
currentTileJsonUrl = tileJsonUrl.value,
onDialogClose = { newTileJsonUrl ->
onDialogClose = { newTileJsonUrl, save ->
coroutineScope.launch {
settingsStore.updateData { prefs ->
prefs.toMutablePreferences().apply {
if (save) {
settingsStore.edit { prefs ->
if (newTileJsonUrl.isNullOrEmpty()) {
remove(stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL))
} else {
set(
stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL),
newTileJsonUrl,
prefs.remove(
stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL)
)
} else {
prefs[stringPreferencesKey(PreferenceKeys.COVERAGE_TILE_JSON_URL)] =
newTileJsonUrl
}
}
}
Expand All @@ -82,7 +83,10 @@ fun CoverageLayerSettings(settingsStore: DataStore<Preferences> = koinInject(PRE
}

@Composable
private fun CoverageLayerDialog(currentTileJsonUrl: String?, onDialogClose: (String?) -> Unit) {
private fun CoverageLayerDialog(
currentTileJsonUrl: String?,
onDialogClose: (String?, Boolean) -> Unit,
) {
val tileJsonUrl = rememberSaveable { mutableStateOf(currentTileJsonUrl) }

val showSuggestedServicesDialog = rememberSaveable { mutableStateOf(false) }
Expand All @@ -99,7 +103,7 @@ private fun CoverageLayerDialog(currentTileJsonUrl: String?, onDialogClose: (Str
)
}

BasicAlertDialog(onDismissRequest = { onDialogClose(null) }) {
BasicAlertDialog(onDismissRequest = { onDialogClose(null, false) }) {
Surface(
modifier = Modifier.wrapContentWidth().wrapContentHeight(),
shape = MaterialTheme.shapes.large,
Expand Down Expand Up @@ -130,7 +134,7 @@ private fun CoverageLayerDialog(currentTileJsonUrl: String?, onDialogClose: (Str
Row {
Spacer(modifier = Modifier.weight(1.0f))

TextButton(onClick = { onDialogClose(tileJsonUrl.value) }) {
TextButton(onClick = { onDialogClose(tileJsonUrl.value, true) }) {
Text(text = stringResource(id = R.string.save))
}
}
Expand Down

0 comments on commit 47767ea

Please sign in to comment.