Skip to content

Commit

Permalink
Handles possible nullity on db getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Jun 11, 2023
1 parent 809b342 commit 5251fca
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ abstract class EventDao {
@Query("SELECT id FROM event_table WHERE scenario_id=:scenarioId")
abstract suspend fun getEventsIds(scenarioId: Long): List<Long>

/**
* Get the list of events for a scenario ordered by priority.
*
* @param scenarioId the identifier of the scenario to get the events from.
* @return the flow on the list of events.
*/
@Transaction
@Query("SELECT * FROM event_table WHERE scenario_id=:scenarioId AND id=:eventId")
abstract fun getEvent(scenarioId: Long, eventId: Long): Flow<CompleteEventEntity>

/**
* Add an event to the database.
* @param event the event to be added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ interface ScenarioDao {
@Query("SELECT * FROM scenario_table ORDER BY name ASC")
fun getScenariosWithEvents(): Flow<List<ScenarioWithEvents>>

/**
* Get a scenario with its events.
*
* @return the scenario.
*/
@Transaction
@Query("SELECT * FROM scenario_table WHERE id=:scenarioId")
fun getScenarioWithEvents(scenarioId: Long): Flow<ScenarioWithEvents>

/**
* Get a scenario and its end conditions.
*
Expand All @@ -69,7 +60,7 @@ interface ScenarioDao {
*/
@Transaction
@Query("SELECT * FROM scenario_table WHERE id=:scenarioId")
suspend fun getScenario(scenarioId: Long): ScenarioEntity
suspend fun getScenario(scenarioId: Long): ScenarioEntity?

/**
* Get a complete scenario
Expand All @@ -78,7 +69,7 @@ interface ScenarioDao {
*/
@Transaction
@Query("SELECT * FROM scenario_table WHERE id=:scenarioId")
suspend fun getCompleteScenario(scenarioId: Long): CompleteScenario
suspend fun getCompleteScenario(scenarioId: Long): CompleteScenario?

/**
* Add a new scenario to the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ interface Repository {
* @param scenarioId the identifier of the scenario.
* @return the scenario.
*/
suspend fun getScenario(scenarioId: Long): Scenario
suspend fun getScenario(scenarioId: Long): Scenario?

/**
* Get the list of events for a given scenario.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ internal class RepositoryImpl internal constructor(
entityPrimaryKeySupplier = { endConditionEntity -> endConditionEntity.id },
)

override suspend fun getScenario(scenarioId: Long): Scenario =
scenarioDao.getScenario(scenarioId).toScenario()
override suspend fun getScenario(scenarioId: Long): Scenario? =
scenarioDao.getScenario(scenarioId)?.toScenario()

override suspend fun getEvents(scenarioId: Long): List<Event> =
eventDao.getCompleteEvents(scenarioId).map { it.toEvent() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DetectionRepository private constructor(context: Context) {

suspend fun startDetection(context: Context, progressListener: ProgressListener) {
val id = scenarioId.value ?: return
val scenario = scenarioRepository.getScenario(id)
val scenario = scenarioRepository.getScenario(id) ?: return
val events = scenarioRepository.getCompleteEventList(id)
val endCondition = scenarioRepository.getEndConditions(id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal class BackupRepository private constructor(context: Context) {
launch {
backupEngine.createBackup(
zipFileUri,
scenarios.map {
scenarios.mapNotNull {
database.scenarioDao().getCompleteScenario(it)
},
screenSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ class MainMenu(context: Context, private val scenarioId: Long) : OverlayMenuCont
}
}
R.id.btn_click_list -> {
viewModel.startScenarioEdition()
showScenarioConfigDialog()
viewModel.startScenarioEdition {
showScenarioConfigDialog()
}
}
R.id.btn_stop -> destroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ class MainMenuModel(application: Application) : AndroidViewModel(application) {
}
}

fun startScenarioEdition() {
fun startScenarioEdition(onEditionStarted: () -> Unit) {
scenarioDbId.value?.let { scenarioDatabaseId ->
viewModelScope.launch(Dispatchers.IO) {
editionRepository.startEdition(scenarioDatabaseId)
if (editionRepository.startEdition(scenarioDatabaseId)) {
withContext(Dispatchers.Main) { onEditionStarted() }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class EventsEditor(private val onDeleteEvent: (Event) -> Unit): ListEditor<Event
}

private fun onEditedEventConditionsUpdated(conditions: List<Condition>) {
_editedItem.value?.let { event ->
editedItem.value?.let { event ->
updateEditedItem(event.copy(conditions = conditions))
}
}

private fun onEditedEventActionsUpdated(actions: List<Action>) {
_editedItem.value?.let { event ->
editedItem.value?.let { event ->
updateEditedItem(event.copy(actions = actions))
}
}
Expand Down

0 comments on commit 5251fca

Please sign in to comment.