Skip to content

Commit 9db54ec

Browse files
Merge pull request #238 from smswithoutborders/mul-del
Delete multiple messages
2 parents 9b9a63f + fbb4652 commit 9db54ec

19 files changed

Lines changed: 340 additions & 122 deletions

File tree

app/src/main/java/com/example/sw0b_001/Models/Messages/EncryptedContentDAO.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ interface EncryptedContentDAO {
2828
@Delete
2929
fun delete(message: EncryptedContent)
3030

31+
@Delete
32+
fun deleteMultiple(messages: List<EncryptedContent>)
33+
3134
@Query("SELECT * FROM EncryptedContent WHERE id=:encryptedContentId")
3235
fun get(encryptedContentId: Long): EncryptedContent
3336

app/src/main/java/com/example/sw0b_001/Models/Messages/MessagesViewModel.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@ class MessagesViewModel : ViewModel() {
7070
}
7171
}
7272
}
73+
74+
fun deleteMultiple(context: Context, messages: List<EncryptedContent>, onCompleteCallback: () -> Unit) {
75+
viewModelScope.launch(Dispatchers.Default) {
76+
Datastore.getDatastore(context).encryptedContentDAO().deleteMultiple(messages)
77+
launch(Dispatchers.Main) {
78+
onCompleteCallback()
79+
}
80+
}
81+
}
7382
}

app/src/main/java/com/example/sw0b_001/Models/Platforms/PlatformsViewModel.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class PlatformsViewModel : ViewModel() {
3535
var message by mutableStateOf<EncryptedContent?>(null)
3636
var bottomTabsItem by mutableStateOf<BottomTabsItems>(BottomTabsItems.BottomBarRecentTab)
3737

38+
// Selection mode properties
39+
var isSelectionMode by mutableStateOf(false)
40+
var selectedMessagesCount by mutableIntStateOf(0)
41+
var onSelectAll: (() -> Unit)? = null
42+
var onDeleteSelected: (() -> Unit)? = null
43+
var onCancelSelection: (() -> Unit)? = null
44+
3845

3946
fun reset() {
4047
platform = null
@@ -66,4 +73,4 @@ class PlatformsViewModel : ViewModel() {
6673
fun getAccount(context: Context, accountIdentifier: String): StoredPlatformsEntity? {
6774
return Datastore.getDatastore(context).storedPlatformsDao().fetchAccount(accountIdentifier)
6875
}
69-
}
76+
}

app/src/main/java/com/example/sw0b_001/ui/appbars/RecentAppBar.kt

Lines changed: 136 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import androidx.compose.foundation.text.KeyboardActions
1313
import androidx.compose.foundation.text.KeyboardOptions
1414
import androidx.compose.material.icons.Icons
1515
import androidx.compose.material.icons.filled.AccountCircle
16+
import androidx.compose.material.icons.filled.Cancel
1617
import androidx.compose.material.icons.filled.Close
18+
import androidx.compose.material.icons.filled.Delete
1719
import androidx.compose.material.icons.filled.MoreVert
1820
import androidx.compose.material.icons.filled.Search
21+
import androidx.compose.material.icons.filled.SelectAll
1922
import androidx.compose.material3.CenterAlignedTopAppBar
2023
import androidx.compose.material3.DropdownMenu
2124
import androidx.compose.material3.DropdownMenuItem
@@ -62,6 +65,11 @@ fun RecentAppBar(
6265
isSearchActive: Boolean,
6366
onToggleSearch: () -> Unit,
6467
onSearchDone: () -> Unit,
68+
isSelectionMode: Boolean = false,
69+
selectedCount: Int = 0,
70+
onSelectAll: (() -> Unit)? = null,
71+
onDeleteSelected: (() -> Unit)? = null,
72+
onCancelSelection: (() -> Unit)? = null,
6573
) {
6674
val context = LocalContext.current
6775
val focusManager = LocalFocusManager.current
@@ -81,87 +89,123 @@ fun RecentAppBar(
8189
val phoneNumber = remember { getPhoneNumberFromPrefs(context) }
8290

8391
Column(modifier = Modifier.fillMaxWidth()) {
84-
CenterAlignedTopAppBar(
85-
title = {
86-
if (!isSearchActive) {
87-
Text(stringResource(R.string.app_name))
88-
}
89-
},
90-
actions = {
91-
if (!isSearchActive) {
92-
IconButton(onClick = onToggleSearch) {
92+
if (isSelectionMode) {
93+
// Selection mode app bar
94+
CenterAlignedTopAppBar(
95+
title = {
96+
Text(stringResource(R.string.selected_messages, selectedCount))
97+
},
98+
navigationIcon = {
99+
IconButton(onClick = { onCancelSelection?.invoke() }) {
93100
Icon(
94-
imageVector = Icons.Filled.Search,
95-
contentDescription = stringResource(R.string.search)
101+
imageVector = Icons.Filled.Cancel,
102+
contentDescription = stringResource(R.string.cancel)
103+
)
104+
}
105+
},
106+
actions = {
107+
IconButton(onClick = { onSelectAll?.invoke() }) {
108+
Icon(
109+
imageVector = Icons.Filled.SelectAll,
110+
contentDescription = stringResource(R.string.select_all)
96111
)
97112
}
98-
}
99113

100-
IconButton(onClick = { showMenu = !showMenu }) {
101-
Icon(
102-
imageVector = Icons.Filled.MoreVert,
103-
contentDescription = stringResource(R.string.menu)
104-
)
105-
}
106-
DropdownMenu(
107-
expanded = showMenu,
108-
onDismissRequest = { showMenu = false }
109-
) {
110-
if (!phoneNumber.isNullOrBlank()) {
111-
DropdownMenuItem(
112-
text = {
113-
Row(verticalAlignment = Alignment.CenterVertically) {
114-
Icon(
115-
imageVector = Icons.Default.AccountCircle,
116-
contentDescription = stringResource(R.string.your_account),
117-
modifier = Modifier.size(40.dp),
118-
tint = MaterialTheme.colorScheme.onSurfaceVariant
119-
)
120-
Spacer(modifier = Modifier.width(16.dp))
121-
Column {
122-
Text(
123-
text = stringResource(R.string.your_account),
124-
fontWeight = FontWeight.SemiBold,
125-
style = MaterialTheme.typography.bodyMedium
126-
)
127-
Text(
128-
text = phoneNumber,
129-
style = MaterialTheme.typography.bodySmall,
130-
color = MaterialTheme.colorScheme.onSurfaceVariant
131-
)
132-
}
133-
}
134-
},
135-
onClick = { },
136-
enabled = false
114+
IconButton(onClick = { onDeleteSelected?.invoke() }) {
115+
Icon(
116+
imageVector = Icons.Filled.Delete,
117+
contentDescription = stringResource(R.string.delete)
137118
)
138-
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
139119
}
140-
DropdownMenuItem(
141-
text = { Text(stringResource(R.string.settings)) },
142-
onClick = {
143-
context.startActivity(
144-
Intent(context, SettingsActivity::class.java).apply {
145-
flags =
146-
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
147-
}
120+
},
121+
colors = TopAppBarDefaults.topAppBarColors(),
122+
scrollBehavior = scrollBehavior,
123+
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection)
124+
)
125+
} else {
126+
// Normal mode app bar
127+
CenterAlignedTopAppBar(
128+
title = {
129+
if (!isSearchActive) {
130+
Text(stringResource(R.string.app_name))
131+
}
132+
},
133+
actions = {
134+
if (!isSearchActive) {
135+
IconButton(onClick = onToggleSearch) {
136+
Icon(
137+
imageVector = Icons.Filled.Search,
138+
contentDescription = stringResource(R.string.search)
148139
)
149-
showMenu = false
150140
}
151-
)
152-
DropdownMenuItem(
153-
text = { Text(stringResource(R.string.about)) },
154-
onClick = {
155-
navController.navigate(AboutScreen)
156-
showMenu = false
141+
}
142+
143+
IconButton(onClick = { showMenu = !showMenu }) {
144+
Icon(
145+
imageVector = Icons.Filled.MoreVert,
146+
contentDescription = stringResource(R.string.menu)
147+
)
148+
}
149+
DropdownMenu(
150+
expanded = showMenu,
151+
onDismissRequest = { showMenu = false }
152+
) {
153+
if (!phoneNumber.isNullOrBlank()) {
154+
DropdownMenuItem(
155+
text = {
156+
Row(verticalAlignment = Alignment.CenterVertically) {
157+
Icon(
158+
imageVector = Icons.Default.AccountCircle,
159+
contentDescription = stringResource(R.string.your_account),
160+
modifier = Modifier.size(40.dp),
161+
tint = MaterialTheme.colorScheme.onSurfaceVariant
162+
)
163+
Spacer(modifier = Modifier.width(16.dp))
164+
Column {
165+
Text(
166+
text = stringResource(R.string.your_account),
167+
fontWeight = FontWeight.SemiBold,
168+
style = MaterialTheme.typography.bodyMedium
169+
)
170+
Text(
171+
text = phoneNumber,
172+
style = MaterialTheme.typography.bodySmall,
173+
color = MaterialTheme.colorScheme.onSurfaceVariant
174+
)
175+
}
176+
}
177+
},
178+
onClick = { },
179+
enabled = false
180+
)
181+
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
157182
}
158-
)
159-
}
160-
},
161-
colors = TopAppBarDefaults.topAppBarColors(),
162-
scrollBehavior = scrollBehavior,
163-
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection)
164-
)
183+
DropdownMenuItem(
184+
text = { Text(stringResource(R.string.settings)) },
185+
onClick = {
186+
context.startActivity(
187+
Intent(context, SettingsActivity::class.java).apply {
188+
flags =
189+
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
190+
}
191+
)
192+
showMenu = false
193+
}
194+
)
195+
DropdownMenuItem(
196+
text = { Text(stringResource(R.string.about)) },
197+
onClick = {
198+
navController.navigate(AboutScreen)
199+
showMenu = false
200+
}
201+
)
202+
}
203+
},
204+
colors = TopAppBarDefaults.topAppBarColors(),
205+
scrollBehavior = scrollBehavior,
206+
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection)
207+
)
208+
}
165209

166210
if (isSearchActive) {
167211
Row(
@@ -221,3 +265,23 @@ fun RecentsAppBarPreview() {
221265
)
222266
}
223267
}
268+
269+
@Preview(showBackground = true)
270+
@Composable
271+
fun RecentsAppBarSelectionModePreview() {
272+
AppTheme(darkTheme = false) {
273+
RecentAppBar(
274+
navController = NavController(context = LocalContext.current),
275+
onSearchQueryChanged = { },
276+
searchQuery = "",
277+
isSearchActive = false,
278+
onToggleSearch = { },
279+
onSearchDone = {},
280+
isSelectionMode = true,
281+
selectedCount = 3,
282+
onSelectAll = { },
283+
onDeleteSelected = { },
284+
onCancelSelection = { }
285+
)
286+
}
287+
}

app/src/main/java/com/example/sw0b_001/ui/views/HomepageView.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ fun HomepageView(
147147
},
148148
onSearchDone = {
149149
isSearchDone = true
150-
}
150+
},
151+
isSelectionMode = platformsViewModel.isSelectionMode,
152+
selectedCount = platformsViewModel.selectedMessagesCount,
153+
onSelectAll = platformsViewModel.onSelectAll,
154+
onDeleteSelected = platformsViewModel.onDeleteSelected,
155+
onCancelSelection = platformsViewModel.onCancelSelection
151156
)
152157
}
153158
}
@@ -415,5 +420,3 @@ fun HomepageViewLoggedInMessages_Preview() {
415420
)
416421
}
417422
}
418-
419-

app/src/main/java/com/example/sw0b_001/ui/views/InboxView.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ fun InboxView(
8282
onClickCallback = {
8383
platformsViewModel.message = message
8484
navController.navigate(BridgeViewScreen)
85-
}
85+
},
86+
logo = TODO(),
87+
isSelected = TODO(),
88+
isSelectionMode = TODO(),
89+
onLongClickCallback = TODO()
8690
)
8791
}
8892
}

0 commit comments

Comments
 (0)