A small batch completion screen shows the number of prepared documents and lets you mark the entire batch as completed with one button. After a recent change, problems appeared in this flow: sometimes the operation fails halfway through, and in other cases the final counters on the screen do not match the real data in the database.
finalizeBatch operation must move all documents in the batch to the FINAL state.draftCount — the number of documents with status DRAFT;finalCount — the number of documents with status FINAL.BatchViewModel.finalizeBatch and the related DAO methods.locked = true;BatchUiState come from after saving.Example source data for the first scenario:
(1, DRAFT, locked=false)
(2, DRAFT, locked=true)
(3, DRAFT, locked=false)
draftCount and finalCount.kotlinx.coroutines 1.8.12.8.32.6.1StateFlowviewModelScope uses the main dispatcher, Room DAO methods are suspendUse line comments to explain the problem, its impact, the fix and how to test it.