From 5251fca53e39c76f40309faa4fd9c5f54d613c1b Mon Sep 17 00:00:00 2001 From: Nain57 Date: Sun, 11 Jun 2023 17:30:08 +0200 Subject: [PATCH] Handles possible nullity on db getters --- .../smartautoclicker/core/database/dao/EventDao.kt | 10 ---------- .../core/database/dao/ScenarioDao.kt | 13 ++----------- .../smartautoclicker/core/domain/Repository.kt | 2 +- .../smartautoclicker/core/domain/RepositoryImpl.kt | 4 ++-- .../core/processing/domain/DetectionRepository.kt | 2 +- .../feature/backup/domain/BackupRepository.kt | 2 +- .../feature/floatingmenu/ui/MainMenu.kt | 5 +++-- .../feature/floatingmenu/ui/MainMenuModel.kt | 6 ++++-- .../feature/scenario/config/data/EventsEditor.kt | 4 ++-- 9 files changed, 16 insertions(+), 32 deletions(-) diff --git a/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/EventDao.kt b/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/EventDao.kt index b059ba10b..f87c2b08b 100644 --- a/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/EventDao.kt +++ b/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/EventDao.kt @@ -90,16 +90,6 @@ abstract class EventDao { @Query("SELECT id FROM event_table WHERE scenario_id=:scenarioId") abstract suspend fun getEventsIds(scenarioId: Long): List - /** - * 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 - /** * Add an event to the database. * @param event the event to be added. diff --git a/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/ScenarioDao.kt b/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/ScenarioDao.kt index 9d71a854e..96b9dc836 100644 --- a/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/ScenarioDao.kt +++ b/core/database/src/main/java/com/buzbuz/smartautoclicker/core/database/dao/ScenarioDao.kt @@ -44,15 +44,6 @@ interface ScenarioDao { @Query("SELECT * FROM scenario_table ORDER BY name ASC") fun getScenariosWithEvents(): Flow> - /** - * Get a scenario with its events. - * - * @return the scenario. - */ - @Transaction - @Query("SELECT * FROM scenario_table WHERE id=:scenarioId") - fun getScenarioWithEvents(scenarioId: Long): Flow - /** * Get a scenario and its end conditions. * @@ -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 @@ -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. diff --git a/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/Repository.kt b/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/Repository.kt index 325f63401..147745bfe 100644 --- a/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/Repository.kt +++ b/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/Repository.kt @@ -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. diff --git a/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/RepositoryImpl.kt b/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/RepositoryImpl.kt index 61b47ade8..a134d1e85 100644 --- a/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/RepositoryImpl.kt +++ b/core/domain/src/main/java/com/buzbuz/smartautoclicker/core/domain/RepositoryImpl.kt @@ -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 = eventDao.getCompleteEvents(scenarioId).map { it.toEvent() } diff --git a/core/processing/src/main/java/com/buzbuz/smartautoclicker/core/processing/domain/DetectionRepository.kt b/core/processing/src/main/java/com/buzbuz/smartautoclicker/core/processing/domain/DetectionRepository.kt index 8132f78a9..333d788ab 100644 --- a/core/processing/src/main/java/com/buzbuz/smartautoclicker/core/processing/domain/DetectionRepository.kt +++ b/core/processing/src/main/java/com/buzbuz/smartautoclicker/core/processing/domain/DetectionRepository.kt @@ -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) diff --git a/feature/backup/src/main/java/com/buzbuz/smartautoclicker/feature/backup/domain/BackupRepository.kt b/feature/backup/src/main/java/com/buzbuz/smartautoclicker/feature/backup/domain/BackupRepository.kt index 3df2bdc8b..60df54666 100644 --- a/feature/backup/src/main/java/com/buzbuz/smartautoclicker/feature/backup/domain/BackupRepository.kt +++ b/feature/backup/src/main/java/com/buzbuz/smartautoclicker/feature/backup/domain/BackupRepository.kt @@ -72,7 +72,7 @@ internal class BackupRepository private constructor(context: Context) { launch { backupEngine.createBackup( zipFileUri, - scenarios.map { + scenarios.mapNotNull { database.scenarioDao().getCompleteScenario(it) }, screenSize, diff --git a/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenu.kt b/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenu.kt index 84be4d332..9596012c5 100644 --- a/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenu.kt +++ b/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenu.kt @@ -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() } diff --git a/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenuModel.kt b/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenuModel.kt index 01d3d9cf7..7b1ba9ff8 100644 --- a/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenuModel.kt +++ b/feature/floating-menu/src/main/java/com/buzbuz/smartautoclicker/feature/floatingmenu/ui/MainMenuModel.kt @@ -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() } + } } } } diff --git a/feature/scenario-config/src/main/java/com/buzbuz/smartautoclicker/feature/scenario/config/data/EventsEditor.kt b/feature/scenario-config/src/main/java/com/buzbuz/smartautoclicker/feature/scenario/config/data/EventsEditor.kt index f332547af..70b574eb9 100644 --- a/feature/scenario-config/src/main/java/com/buzbuz/smartautoclicker/feature/scenario/config/data/EventsEditor.kt +++ b/feature/scenario-config/src/main/java/com/buzbuz/smartautoclicker/feature/scenario/config/data/EventsEditor.kt @@ -69,13 +69,13 @@ class EventsEditor(private val onDeleteEvent: (Event) -> Unit): ListEditor) { - _editedItem.value?.let { event -> + editedItem.value?.let { event -> updateEditedItem(event.copy(conditions = conditions)) } } private fun onEditedEventActionsUpdated(actions: List) { - _editedItem.value?.let { event -> + editedItem.value?.let { event -> updateEditedItem(event.copy(actions = actions)) } }