Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ constructor(
private val repoScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

init {
// Track the saved locationId and register orders + sale_items
// subscriptions whenever it's set. This covers both the user picking
// a location for the first time and app re-launch where the location
// is already in DataStore — without this, observers would see only
// the local store and miss orders other peers had pushed for the
// saved location.
// Track the saved locationId and (re-)configure routing + subscriptions
// whenever it's set. Covers both the user picking a location and app
// re-launch with a value restored from DataStore.
appSettings.locationIdFlow()
.filter { it.isNotEmpty() }
.onEach { locationId -> setActiveLocation(locationId) }
.onEach { setActiveLocation(it) }
.launchIn(repoScope)
}

Expand All @@ -70,6 +67,7 @@ constructor(
}

fun setActiveLocation(locationId: String) {
dittoManager.setRoutingConfig(locationId)
registerSub(
key = "orders",
query = """
Expand Down Expand Up @@ -171,17 +169,6 @@ constructor(
ditto.store.execute(query, baseArgs).use { }
}

suspend fun insertCustomLocation(location: Location) {
ditto.store.execute(
"""
INSERT INTO ${Location.COLLECTION_NAME}
DOCUMENTS (deserialize_json(:json))
ON ID CONFLICT DO UPDATE_LOCAL_DIFF
""".trimIndent(),
mapOf("json" to location.dittoJsonString())
).use { }
}

// ----- Lifecycle -----

suspend fun seedAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ class AppConfigurationStateUseCase @Inject constructor(private val appSettings:

enum class AppConfigurationState {
VALID,
LOCATION_NEEDED,
DEMO_OR_CUSTOM_LOCATION_NEEDED
LOCATION_NEEDED
}

suspend operator fun invoke(): AppConfigurationState {
val isUsingDemoLocations = appSettings.isUsingDemoLocations()
val locationId = appSettings.locationId()
return if (isUsingDemoLocations != null) {
if (locationId.isEmpty()) {
AppConfigurationState.LOCATION_NEEDED
} else {
AppConfigurationState.VALID
}
return if (appSettings.locationId().isEmpty()) {
AppConfigurationState.LOCATION_NEEDED
} else {
AppConfigurationState.DEMO_OR_CUSTOM_LOCATION_NEEDED
AppConfigurationState.VALID
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
package live.ditto.pos.core.domain.usecase

import kotlinx.coroutines.flow.first
import live.ditto.pos.core.data.demo.LocationSeed
import live.ditto.pos.core.data.locations.Location
import live.ditto.pos.core.domain.repository.DittoRepository
import live.ditto.pos.core.domain.usecase.AppConfigurationStateUseCase.AppConfigurationState
import live.ditto.pos.settings.AppSettings
import javax.inject.Inject

class GetCurrentLocationUseCase @Inject constructor(
private val appSettings: AppSettings,
private val dittoRepository: DittoRepository,
private val appConfigurationStateUseCase: AppConfigurationStateUseCase,
private val isUsingDemoLocationsUseCase: IsUsingDemoLocationsUseCase
private val appConfigurationStateUseCase: AppConfigurationStateUseCase
) {

suspend operator fun invoke(): Location? {
val appConfigurationState = appConfigurationStateUseCase()

return if (appConfigurationState == AppConfigurationState.VALID) {
val locationId = appSettings.locationId()
if (isUsingDemoLocationsUseCase()) {
LocationSeed.demoLocations.find { it.id == locationId }
} else {
dittoRepository.observeAllLocations().first().find { it.id == locationId }
}
} else {
null
}
if (appConfigurationStateUseCase() != AppConfigurationState.VALID) return null
val locationId = appSettings.locationId()
return LocationSeed.demoLocations.find { it.id == locationId }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class SetCurrentLocationUseCase @Inject constructor(
) {

// Persist the chosen locationId; DittoRepository observes the same flow
// and registers the orders + sale_items subscriptions for the new value.
// and (re-)configures routing + the orders/sale_items subscriptions for
// the new value.
suspend operator fun invoke(locationId: String) {
appSettings.setLocationId(locationId = locationId)
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import live.ditto.pos.core.presentation.navigation.BottomNavItem

@Composable
fun PosKdsNavigationBar(
showDemoLocationsNavItem: Boolean,
onItemClick: (bottomNavItem: BottomNavItem) -> Unit
) {
val bottomNavItems = buildList {
add(BottomNavItem.PointOfSale)
add(BottomNavItem.KitchenDisplay)
if (showDemoLocationsNavItem) {
add(BottomNavItem.DemoLocationSelection)
}
}
val bottomNavItems = listOf(
BottomNavItem.PointOfSale,
BottomNavItem.KitchenDisplay,
BottomNavItem.DemoLocationSelection
)

NavigationBar {
bottomNavItems.forEach { item ->
Expand All @@ -47,7 +44,6 @@ fun PosKdsNavigationBar(
@Composable
private fun PosKdsNavigationBarPreview() {
PosKdsNavigationBar(
showDemoLocationsNavItem = true,
onItemClick = { }
)
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package live.ditto.pos.core.presentation.composables.screens

import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import live.ditto.pos.R
Expand All @@ -38,91 +27,16 @@ fun AdvancedSettingsScreen(
modifier = modifier.fillMaxWidth(),
title = stringResource(R.string.advanced_settings_title)
) {
SettingsSection(sectionName = R.string.settings_location_section_header) {
TextSettingsItem(text = "Current location ID: ${settingsState.currentLocation?.id}")
HorizontalDivider(modifier = Modifier.fillMaxWidth())
SwitchSettingsItem(
name = R.string.settings_use_demo_locations_name,
description = R.string.settings_use_demo_locations_description,
checkedState = settingsState.isUsingDemoLocations,
onToggleChanged = {
viewModel.shouldUseDemoLocations(it)
onSettingsUpdated()
}
)
}
}
}

@Composable
private fun TextSettingsItem(
text: String
) {
Text(
modifier = Modifier.fillMaxWidth(),
text = text,
textAlign = TextAlign.Start,
style = MaterialTheme.typography.bodySmall
)
}

@Composable
private fun SettingsSection(
modifier: Modifier = Modifier,
@StringRes sectionName: Int,
content: @Composable ColumnScope.() -> Unit
) {
InnerCardWithTitle(
modifier = modifier.fillMaxWidth(),
title = stringResource(sectionName)
) {
Column(
InnerCardWithTitle(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
title = stringResource(R.string.settings_location_section_header)
) {
content()
}
}
}

@Composable
private fun SwitchSettingsItem(
@StringRes name: Int,
@StringRes description: Int? = null,
checkedState: Boolean,
onToggleChanged: (state: Boolean) -> Unit
) {
Surface(
modifier = Modifier
.fillMaxWidth()
) {
Column {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Column {
Text(
text = stringResource(id = name),
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Start
)
description?.let {
Text(
text = stringResource(id = it),
style = MaterialTheme.typography.bodySmall,
textAlign = TextAlign.Start
)
}
}
Spacer(modifier = Modifier.weight(1f))
Switch(checked = checkedState, onCheckedChange = {
onToggleChanged(it)
})
}
}
Text(
modifier = Modifier.fillMaxWidth(),
text = "Current location ID: ${settingsState.currentLocation?.id ?: "none"}",
textAlign = TextAlign.Start,
style = MaterialTheme.typography.bodySmall
)
}
}
}
Loading