1
1
package com.yogeshpaliyal.deepr.ui.screens.home
2
2
3
+ import android.database.sqlite.SQLiteConstraintException
4
+ import android.widget.Toast
3
5
import androidx.compose.foundation.clickable
4
6
import androidx.compose.foundation.layout.Column
7
+ import androidx.compose.foundation.layout.Row
8
+ import androidx.compose.foundation.lazy.LazyColumn
9
+ import androidx.compose.foundation.lazy.items
10
+ import androidx.compose.material3.AlertDialog
11
+ import androidx.compose.material3.Button
12
+ import androidx.compose.material3.ButtonDefaults
5
13
import androidx.compose.material3.ExperimentalMaterial3Api
6
14
import androidx.compose.material3.HorizontalDivider
15
+ import androidx.compose.material3.Icon
16
+ import androidx.compose.material3.IconButton
7
17
import androidx.compose.material3.ListItem
8
18
import androidx.compose.material3.ListItemDefaults
9
19
import androidx.compose.material3.ModalBottomSheet
10
20
import androidx.compose.material3.Text
21
+ import androidx.compose.material3.TextField
11
22
import androidx.compose.material3.TopAppBar
12
23
import androidx.compose.material3.TopAppBarDefaults
13
24
import androidx.compose.material3.rememberModalBottomSheetState
14
25
import androidx.compose.runtime.Composable
26
+ import androidx.compose.runtime.getValue
27
+ import androidx.compose.runtime.mutableStateOf
28
+ import androidx.compose.runtime.remember
29
+ import androidx.compose.runtime.setValue
15
30
import androidx.compose.ui.Modifier
16
31
import androidx.compose.ui.graphics.Color
32
+ import androidx.compose.ui.platform.LocalContext
17
33
import com.yogeshpaliyal.deepr.Tags
34
+ import compose.icons.TablerIcons
35
+ import compose.icons.tablericons.Edit
36
+ import compose.icons.tablericons.Trash
18
37
19
38
@OptIn(ExperimentalMaterial3Api ::class )
20
39
@Composable
@@ -23,9 +42,112 @@ fun TagSelectionBottomSheet(
23
42
selectedTag : Tags ? ,
24
43
dismissBottomSheet : () -> Unit ,
25
44
setTagFilter : (Tags ? ) -> Unit ,
45
+ editTag : (Tags ) -> Result <Boolean >,
46
+ deleteTag : (Tags ) -> Result <Boolean >,
26
47
modifier : Modifier = Modifier ,
27
48
) {
28
49
val modalBottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true )
50
+ var isTagEditEnable by remember { mutableStateOf<Tags ?>(null ) }
51
+ var isTagDeleteEnable by remember { mutableStateOf<Tags ?>(null ) }
52
+ var tagEditError by remember { mutableStateOf<String ?>(null ) }
53
+ val context = LocalContext .current
54
+
55
+ isTagEditEnable?.let { tag ->
56
+ AlertDialog (
57
+ onDismissRequest = {
58
+ isTagEditEnable = null
59
+ tagEditError = null
60
+ },
61
+ title = {
62
+ Text (text = " Edit Tag" )
63
+ },
64
+ text = {
65
+ Column {
66
+ TextField (
67
+ value = tag.name,
68
+ onValueChange = {
69
+ isTagEditEnable = tag.copy(name = it)
70
+ },
71
+ isError = tagEditError != null ,
72
+ supportingText = {
73
+ tagEditError?.let {
74
+ Text (text = it)
75
+ }
76
+ },
77
+ )
78
+ }
79
+ },
80
+ confirmButton = {
81
+ Button (onClick = {
82
+ val result = editTag(tag)
83
+ if (result.isFailure) {
84
+ val exception = result.exceptionOrNull()
85
+ when (exception) {
86
+ is SQLiteConstraintException -> {
87
+ tagEditError = " Tag with this name already exists"
88
+ }
89
+
90
+ else -> {
91
+ tagEditError = " Failed to edit tag"
92
+ }
93
+ }
94
+ } else {
95
+ isTagEditEnable = null
96
+ tagEditError = null
97
+ Toast
98
+ .makeText(context, " Tag edited successfully" , Toast .LENGTH_SHORT )
99
+ .show()
100
+ }
101
+ }) {
102
+ Text (" Edit" )
103
+ }
104
+ },
105
+ dismissButton = {
106
+ Button (onClick = {
107
+ isTagEditEnable = null
108
+ tagEditError = null
109
+ }) {
110
+ Text (" Cancel" )
111
+ }
112
+ },
113
+ )
114
+ }
115
+
116
+ isTagDeleteEnable?.let { tag ->
117
+ AlertDialog (
118
+ onDismissRequest = {
119
+ isTagDeleteEnable = null
120
+ },
121
+ title = {
122
+ Text (text = " Delete Tag" )
123
+ },
124
+ text = {
125
+ Text (text = " Are you sure you want to delete this tag?" )
126
+ },
127
+ confirmButton = {
128
+ Button (onClick = {
129
+ val result = deleteTag(tag)
130
+ if (result.isFailure) {
131
+ Toast .makeText(context, " Failed to delete tag ${result.exceptionOrNull()} " , Toast .LENGTH_SHORT ).show()
132
+ } else {
133
+ isTagDeleteEnable = null
134
+ Toast
135
+ .makeText(context, " Tag deleted successfully" , Toast .LENGTH_SHORT )
136
+ .show()
137
+ }
138
+ }, colors = ButtonDefaults .buttonColors(containerColor = Color .Red )) {
139
+ Text (" Delete" )
140
+ }
141
+ },
142
+ dismissButton = {
143
+ Button (onClick = {
144
+ isTagDeleteEnable = null
145
+ }) {
146
+ Text (" Cancel" )
147
+ }
148
+ },
149
+ )
150
+ }
29
151
30
152
ModalBottomSheet (sheetState = modalBottomSheetState, onDismissRequest = dismissBottomSheet) {
31
153
Column (modifier) {
@@ -36,39 +158,64 @@ fun TagSelectionBottomSheet(
36
158
colors = TopAppBarDefaults .topAppBarColors(containerColor = Color .Transparent ),
37
159
)
38
160
HorizontalDivider ()
39
- ListItem (
40
- modifier =
41
- Modifier .clickable {
42
- setTagFilter(null )
43
- dismissBottomSheet()
44
- },
45
- headlineContent = { Text (" All" ) },
46
- colors =
47
- if (selectedTag == null ) {
48
- ListItemDefaults .colors(
49
- headlineColor = androidx.compose.material3.MaterialTheme .colorScheme.primary,
50
- )
51
- } else {
52
- ListItemDefaults .colors(containerColor = Color .Transparent )
53
- },
54
- )
55
- tags.forEach { tag ->
56
- ListItem (
57
- modifier =
58
- Modifier .clickable {
59
- setTagFilter(tag)
60
- dismissBottomSheet()
61
- },
62
- headlineContent = { Text (tag.name) },
63
- colors =
64
- if (selectedTag?.id == tag.id) {
65
- ListItemDefaults .colors(
66
- headlineColor = androidx.compose.material3.MaterialTheme .colorScheme.primary,
67
- )
68
- } else {
69
- ListItemDefaults .colors(containerColor = Color .Transparent )
161
+ LazyColumn {
162
+ item {
163
+ ListItem (
164
+ modifier =
165
+ Modifier .clickable {
166
+ setTagFilter(null )
167
+ dismissBottomSheet()
168
+ },
169
+ headlineContent = { Text (" All" ) },
170
+ colors =
171
+ if (selectedTag == null ) {
172
+ ListItemDefaults .colors(
173
+ headlineColor = androidx.compose.material3.MaterialTheme .colorScheme.primary,
174
+ )
175
+ } else {
176
+ ListItemDefaults .colors(containerColor = Color .Transparent )
177
+ },
178
+ )
179
+ }
180
+ items(tags) { tag ->
181
+ ListItem (
182
+ modifier =
183
+ Modifier .clickable {
184
+ setTagFilter(tag)
185
+ dismissBottomSheet()
186
+ },
187
+ headlineContent = { Text (tag.name) },
188
+ trailingContent = {
189
+ Row {
190
+ IconButton (onClick = {
191
+ isTagEditEnable = tag
192
+ }) {
193
+ Icon (
194
+ imageVector = TablerIcons .Edit ,
195
+ contentDescription = " Edit Tag" ,
196
+ )
197
+ }
198
+
199
+ IconButton (onClick = {
200
+ isTagDeleteEnable = tag
201
+ }) {
202
+ Icon (
203
+ imageVector = TablerIcons .Trash ,
204
+ contentDescription = " Clear Tag" ,
205
+ )
206
+ }
207
+ }
70
208
},
71
- )
209
+ colors =
210
+ if (selectedTag?.id == tag.id) {
211
+ ListItemDefaults .colors(
212
+ headlineColor = androidx.compose.material3.MaterialTheme .colorScheme.primary,
213
+ )
214
+ } else {
215
+ ListItemDefaults .colors(containerColor = Color .Transparent )
216
+ },
217
+ )
218
+ }
72
219
}
73
220
}
74
221
}
0 commit comments