Skip to content

Commit 6e831c3

Browse files
authored
Merge pull request #23 from DatepollSystems/WR-329-auto-select
WR-329: Auto select organization and event when there is only one
2 parents 61d54d0 + bcbc550 commit 6e831c3

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/core/AbstractViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.datepollsystems.waiterrobot.mediator.core
22

33
import dev.icerock.moko.mvvm.viewmodel.ViewModel
44
import kotlinx.coroutines.CoroutineExceptionHandler
5+
import kotlinx.coroutines.Job
56
import kotlinx.coroutines.SupervisorJob
67
import kotlinx.coroutines.flow.MutableStateFlow
78
import kotlinx.coroutines.flow.StateFlow
@@ -46,8 +47,8 @@ abstract class AbstractViewModel<T : State<T>>(
4647
_stateFlow.update(reducer)
4748
}
4849

49-
fun <T> inVmScope(block: suspend () -> T) {
50-
viewModelScope.launch(SupervisorJob() + exceptionHandler) {
50+
fun <T> inVmScope(block: suspend () -> T): Job {
51+
return viewModelScope.launch(SupervisorJob() + exceptionHandler) {
5152
block()
5253
}
5354
}
@@ -60,7 +61,7 @@ abstract class AbstractViewModel<T : State<T>>(
6061
withScreenState(ScreenState.Error(errorTitle, errorMsg, dismiss))
6162
}
6263

63-
private fun dismissError() = inVmScope {
64+
private fun dismissError() {
6465
reduce {
6566
withScreenState(ScreenState.Idle)
6667
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.datepollsystems.waiterrobot.mediator.ui.common
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.material.Text
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.Alignment
8+
import androidx.compose.ui.Modifier
9+
10+
@Composable
11+
fun CenteredText(text: String) {
12+
Box(
13+
modifier = Modifier.fillMaxSize(),
14+
contentAlignment = Alignment.Center
15+
) {
16+
Text(text)
17+
}
18+
}

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/ui/configurePrinters/ConfigurePrintersScreen.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.unit.dp
1515
import org.datepollsystems.waiterrobot.mediator.App
1616
import org.datepollsystems.waiterrobot.mediator.data.api.dto.GetPrinterDto
1717
import org.datepollsystems.waiterrobot.mediator.printer.LocalPrinterInfo
18+
import org.datepollsystems.waiterrobot.mediator.ui.common.CenteredText
1819
import org.datepollsystems.waiterrobot.mediator.ui.common.DropDownInput
1920
import org.datepollsystems.waiterrobot.mediator.ui.common.LoadableScreen
2021
import org.datepollsystems.waiterrobot.mediator.ui.common.SelectedEnvironmentInfo
@@ -163,6 +164,10 @@ fun ConfigurePrintersScreen(vm: ConfigurePrintersViewModel) {
163164
}
164165
}
165166
}
167+
} else if (state.selectedOrganisation == null) {
168+
CenteredText("Please select an Organization.")
169+
} else if (state.selectedEvent == null) {
170+
CenteredText("Please select an Event.")
166171
}
167172
}
168173
}

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/ui/configurePrinters/ConfigurePrintersViewModel.kt

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class ConfigurePrintersViewModel(
2929
init {
3030
val initConfig = MediatorConfiguration.createFromStore()
3131
inVmScope {
32-
val organisations = loadUserOrganisations()
32+
loadUserOrganisations(
33+
selectId = initConfig?.selectedOrganisationId,
34+
selectEventId = initConfig?.selectedEventId
35+
)
3336
if (initConfig != null) {
34-
val selectedOrg = organisations.find { it.id == initConfig.selectedOrganisationId }
35-
36-
val events = if (selectedOrg != null) loadOrganisationEvents(selectedOrg.id) else null
37-
val selectedEvent = events?.find { it.id == initConfig.selectedEventId }
38-
37+
val selectedEvent = state.value.selectedEvent
3938
val bePrinters = if (selectedEvent != null) loadEventPrinters(selectedEvent.id) else null
4039
val backendMap = bePrinters?.associateBy(GetPrinterDto::id) ?: emptyMap()
4140
val localIds = PrinterDiscoverService.localPrinters.map(LocalPrinterInfo::localId)
@@ -56,15 +55,10 @@ class ConfigurePrintersViewModel(
5655
emptyList()
5756
}
5857

59-
val pairedBackendPrinterIds =
60-
initConfig.printerPairings.map(MediatorConfiguration.PrinterPairing::backendPrinterId).toSet()
58+
val pairedBackendPrinterIds = pairings.map { it.bePrinter.id }.toSet()
6159
reduce {
6260
copy(
6361
screenState = ScreenState.Idle,
64-
availableOrganisations = organisations,
65-
availableEvents = events,
66-
selectedOrganisation = selectedOrg,
67-
selectedEvent = selectedEvent,
6862
pairings = pairings,
6963
localPrinters = PrinterDiscoverService.localPrinters.toList(),
7064
unPairedBackendPrinters = bePrinters?.filter { it.id !in pairedBackendPrinterIds }
@@ -74,27 +68,31 @@ class ConfigurePrintersViewModel(
7468
reduce {
7569
copy(
7670
screenState = ScreenState.Idle,
77-
availableOrganisations = organisations,
7871
localPrinters = PrinterDiscoverService.localPrinters.toList(),
7972
)
8073
}
8174
}
8275
}
8376
}
8477

85-
private suspend fun loadUserOrganisations(): List<GetOrganisationDto> {
78+
private suspend fun loadUserOrganisations(selectId: ID? = null, selectEventId: ID? = null) {
8679
val organisations = organisationApi.getUserOrganisations()
8780
reduce { copy(availableOrganisations = organisations) }
88-
return organisations
81+
val organisationToSelect = organisations.singleOrNull()
82+
?: if (selectId != null) organisations.find { it.id == selectId } else null
83+
if (organisationToSelect != null) changeOrganisation(organisationToSelect, selectEventId).join()
8984
}
9085

9186
private suspend fun loadOrganisationEvents(
92-
organisationId: ID = state.value.selectedOrganisation!!.id
93-
): List<GetEventDto> {
87+
organisationId: ID = state.value.selectedOrganisation!!.id,
88+
selectId: ID? = null
89+
) {
9490
// TODO handle organization not set
9591
val events = eventApi.getOrganisationEvents(organisationId)
9692
reduce { copy(screenState = ScreenState.Idle, availableEvents = events) }
97-
return events
93+
val eventToSelect = events.singleOrNull()
94+
?: if (selectId != null) events.find { it.id == selectId } else null
95+
if (eventToSelect != null) changeEvent(eventToSelect).join()
9896
}
9997

10098
private suspend fun loadEventPrinters(eventId: ID = state.value.selectedEvent!!.id): List<GetPrinterDto> {
@@ -133,7 +131,7 @@ class ConfigurePrintersViewModel(
133131
}
134132
}
135133

136-
fun changeOrganisation(organisation: GetOrganisationDto) = inVmScope {
134+
fun changeOrganisation(organisation: GetOrganisationDto, selectEventId: ID? = null) = inVmScope {
137135
reduce {
138136
copy(
139137
selectedOrganisation = organisation,
@@ -144,7 +142,7 @@ class ConfigurePrintersViewModel(
144142
pairings = emptyList()
145143
)
146144
}
147-
loadOrganisationEvents()
145+
loadOrganisationEvents(selectId = selectEventId)
148146
}
149147

150148
fun changeEvent(event: GetEventDto) = inVmScope {

0 commit comments

Comments
 (0)