@@ -15,6 +15,8 @@ import androidx.compose.ui.text.style.TextOverflow
1515import androidx.compose.ui.unit.dp
1616import com.opentasker.core.model.Profile
1717import com.opentasker.core.storage.AppDatabase
18+ import com.opentasker.core.storage.toEntity
19+ import kotlinx.coroutines.launch
1820
1921/* *
2022 * Screen for batch operations on profiles: multi-select, enable/disable/delete all.
@@ -25,10 +27,12 @@ fun BatchOperationsScreen(
2527 db : AppDatabase ,
2628 onBack : () -> Unit
2729) {
28- val allProfiles by db.profileDao().getAllLive().collectAsState(emptyList())
30+ val allProfileEntities by db.profileDao().getAllAsFlow().collectAsState(emptyList())
31+ val allProfiles = allProfileEntities.map { it.toDomain() }
2932
3033 var searchQuery by remember { mutableStateOf(" " ) }
31- var selectedIds by remember { mutableStateOf(setOf<String >()) }
34+ var selectedIds by remember { mutableStateOf(setOf<Long >()) }
35+ val scope = rememberCoroutineScope()
3236
3337 val filtered = allProfiles.filter { profile ->
3438 searchQuery.isEmpty() || profile.name.contains(searchQuery, ignoreCase = true )
@@ -83,11 +87,13 @@ fun BatchOperationsScreen(
8387 ) {
8488 Button (onClick = {
8589 // Enable all selected
86- selectedIds.forEach { id ->
87- val profile = allProfiles.find { it.id == id } ? : return @forEach
88- db.profileDao().insertOrUpdate(profile.copy(enabled = true ))
90+ scope.launch {
91+ selectedIds.forEach { id ->
92+ val profile = allProfiles.find { it.id == id } ? : return @forEach
93+ db.profileDao().insertOrUpdate(profile.copy(enabled = true ).toEntity())
94+ }
95+ selectedIds = emptySet()
8996 }
90- selectedIds = emptySet()
9197 }, modifier = Modifier .weight(1f )) {
9298 Icon (Icons .Default .Done , contentDescription = null )
9399 Spacer (modifier = Modifier .width(4 .dp))
@@ -98,11 +104,13 @@ fun BatchOperationsScreen(
98104
99105 Button (onClick = {
100106 // Disable all selected
101- selectedIds.forEach { id ->
102- val profile = allProfiles.find { it.id == id } ? : return @forEach
103- db.profileDao().insertOrUpdate(profile.copy(enabled = false ))
107+ scope.launch {
108+ selectedIds.forEach { id ->
109+ val profile = allProfiles.find { it.id == id } ? : return @forEach
110+ db.profileDao().insertOrUpdate(profile.copy(enabled = false ).toEntity())
111+ }
112+ selectedIds = emptySet()
104113 }
105- selectedIds = emptySet()
106114 }, modifier = Modifier .weight(1f )) {
107115 Icon (Icons .Default .Block , contentDescription = null )
108116 Spacer (modifier = Modifier .width(4 .dp))
@@ -156,11 +164,13 @@ fun BatchOperationsScreen(
156164 confirmButton = {
157165 Button (
158166 onClick = {
159- selectedIds.forEach { id ->
160- db.profileDao().delete(allProfiles.find { it.id == id } ? : return @forEach)
167+ scope.launch {
168+ selectedIds.forEach { id ->
169+ db.profileDao().delete(allProfiles.find { it.id == id }?.toEntity() ? : return @forEach)
170+ }
171+ selectedIds = emptySet()
172+ showDeleteDialog = false
161173 }
162- selectedIds = emptySet()
163- showDeleteDialog = false
164174 },
165175 colors = ButtonDefaults .buttonColors(
166176 containerColor = MaterialTheme .colorScheme.error
0 commit comments