Skip to content

Commit 1ba023e

Browse files
committed
feat: Add functionality to edit deeplinks with a dialog
1 parent f016747 commit 1ba023e

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.yogeshpaliyal.deepr.ui.components
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Spacer
5+
import androidx.compose.foundation.layout.fillMaxWidth
6+
import androidx.compose.foundation.layout.height
7+
import androidx.compose.foundation.layout.padding
8+
import androidx.compose.material3.AlertDialog
9+
import androidx.compose.material3.Button
10+
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.OutlinedButton
12+
import androidx.compose.material3.Text
13+
import androidx.compose.material3.TextField
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableStateOf
17+
import androidx.compose.runtime.remember
18+
import androidx.compose.runtime.setValue
19+
import androidx.compose.ui.Modifier
20+
import androidx.compose.ui.unit.dp
21+
import com.yogeshpaliyal.deepr.Deepr
22+
import com.yogeshpaliyal.deepr.util.isValidDeeplink
23+
24+
@Composable
25+
fun EditDeeplinkDialog(
26+
deepr: Deepr,
27+
onDismiss: () -> Unit,
28+
onSave: (String) -> Unit
29+
) {
30+
var link by remember { mutableStateOf(deepr.link) }
31+
var isError by remember { mutableStateOf(false) }
32+
33+
AlertDialog(
34+
onDismissRequest = onDismiss,
35+
title = { Text("Edit Deeplink") },
36+
text = {
37+
Column {
38+
TextField(
39+
value = link,
40+
onValueChange = {
41+
link = it
42+
isError = false
43+
},
44+
label = { Text("Deeplink") },
45+
isError = isError,
46+
supportingText = {
47+
if (isError) {
48+
Text(
49+
text = "Please enter a valid deeplink",
50+
color = MaterialTheme.colorScheme.error
51+
)
52+
}
53+
},
54+
modifier = Modifier.fillMaxWidth()
55+
)
56+
57+
Spacer(modifier = Modifier.height(8.dp))
58+
59+
Text(
60+
text = "Edit your deeplink URL",
61+
style = MaterialTheme.typography.bodySmall,
62+
modifier = Modifier.padding(horizontal = 4.dp)
63+
)
64+
}
65+
},
66+
confirmButton = {
67+
Button(
68+
onClick = {
69+
if (isValidDeeplink(link)) {
70+
onSave(link)
71+
} else {
72+
isError = true
73+
}
74+
}
75+
) {
76+
Text("Save")
77+
}
78+
},
79+
dismissButton = {
80+
OutlinedButton(onClick = onDismiss) {
81+
Text("Cancel")
82+
}
83+
}
84+
)
85+
}

app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/Home.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import androidx.compose.ui.text.style.TextOverflow
5252
import androidx.compose.ui.unit.dp
5353
import com.yogeshpaliyal.deepr.Deepr
5454
import com.yogeshpaliyal.deepr.ui.components.CreateShortcutDialog
55+
import com.yogeshpaliyal.deepr.ui.components.EditDeeplinkDialog
5556
import com.yogeshpaliyal.deepr.util.createShortcut
5657
import com.yogeshpaliyal.deepr.util.isValidDeeplink
5758
import com.yogeshpaliyal.deepr.util.openDeeplink
@@ -62,6 +63,7 @@ import com.yogeshpaliyal.deepr.viewmodel.AccountViewModel
6263
import com.yogeshpaliyal.deepr.viewmodel.SortOrder
6364
import compose.icons.tablericons.Copy
6465
import compose.icons.tablericons.DotsVertical
66+
import compose.icons.tablericons.Edit
6567
import compose.icons.tablericons.Filter
6668
import compose.icons.tablericons.Link
6769
import compose.icons.tablericons.Plus
@@ -191,6 +193,7 @@ fun Content(viewModel: AccountViewModel) {
191193
var isError by remember { mutableStateOf(false) }
192194
val context = LocalContext.current
193195
var showShortcutDialog by remember { mutableStateOf<Deepr?>(null) }
196+
var showEditDialog by remember { mutableStateOf<Deepr?>(null) }
194197

195198
showShortcutDialog?.let { deepr ->
196199
CreateShortcutDialog(
@@ -203,6 +206,18 @@ fun Content(viewModel: AccountViewModel) {
203206
)
204207
}
205208

209+
showEditDialog?.let { deepr ->
210+
EditDeeplinkDialog(
211+
deepr = deepr,
212+
onDismiss = { showEditDialog = null },
213+
onSave = { newLink ->
214+
viewModel.updateDeeplink(deepr.id, newLink)
215+
Toast.makeText(context, "Deeplink updated", Toast.LENGTH_SHORT).show()
216+
showEditDialog = null
217+
}
218+
)
219+
}
220+
206221
DeeprList(
207222
modifier = Modifier
208223
.weight(1f)
@@ -219,6 +234,9 @@ fun Content(viewModel: AccountViewModel) {
219234
onShortcutClick = {
220235
showShortcutDialog = it
221236
},
237+
onEditClick = {
238+
showEditDialog = it
239+
},
222240
onItemLongClick = {
223241
val clipboard =
224242
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
@@ -307,6 +325,7 @@ fun DeeprList(
307325
onItemClick: (Deepr) -> Unit,
308326
onRemoveClick: (Deepr) -> Unit,
309327
onShortcutClick: (Deepr) -> Unit,
328+
onEditClick: (Deepr) -> Unit,
310329
onItemLongClick: (Deepr) -> Unit
311330
) {
312331
if (accounts.isEmpty()) {
@@ -355,6 +374,7 @@ fun DeeprList(
355374
onItemClick = onItemClick,
356375
onRemoveClick = onRemoveClick,
357376
onShortcutClick = onShortcutClick,
377+
onEditClick = onEditClick,
358378
onItemLongClick = onItemLongClick
359379
)
360380
}
@@ -370,6 +390,7 @@ fun DeeprItem(
370390
onItemClick: (Deepr) -> Unit,
371391
onRemoveClick: (Deepr) -> Unit,
372392
onShortcutClick: (Deepr) -> Unit,
393+
onEditClick: (Deepr) -> Unit,
373394
onItemLongClick: (Deepr) -> Unit
374395
) {
375396
var expanded by remember { mutableStateOf(false) }
@@ -455,6 +476,19 @@ fun DeeprItem(
455476
)
456477
}
457478
)
479+
DropdownMenuItem(
480+
text = { Text("Edit") },
481+
onClick = {
482+
onEditClick(account)
483+
expanded = false
484+
},
485+
leadingIcon = {
486+
Icon(
487+
TablerIcons.Edit,
488+
contentDescription = "Edit"
489+
)
490+
}
491+
)
458492
DropdownMenuItem(
459493
text = { Text("Delete") },
460494
onClick = {

app/src/main/java/com/yogeshpaliyal/deepr/viewmodel/AccountViewModel.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import app.cash.sqldelight.coroutines.asFlow
66
import app.cash.sqldelight.coroutines.mapToList
77
import com.yogeshpaliyal.deepr.Deepr
88
import com.yogeshpaliyal.deepr.DeeprQueries
9+
import kotlinx.coroutines.ExperimentalCoroutinesApi
910
import kotlinx.coroutines.flow.MutableStateFlow
1011
import kotlinx.coroutines.flow.SharingStarted
1112
import kotlinx.coroutines.flow.StateFlow
@@ -23,6 +24,7 @@ class AccountViewModel(private val deeprQueries: DeeprQueries) : ViewModel() {
2324
private val searchQuery = MutableStateFlow("")
2425
private val sortOrder = MutableStateFlow(SortOrder.DESC)
2526

27+
@OptIn(ExperimentalCoroutinesApi::class)
2628
val accounts: StateFlow<List<Deepr>> =
2729
combine(searchQuery, sortOrder) { query, order ->
2830
Pair(query, order)
@@ -69,4 +71,10 @@ class AccountViewModel(private val deeprQueries: DeeprQueries) : ViewModel() {
6971
deeprQueries.incrementOpenedCount(id)
7072
}
7173
}
74+
75+
fun updateDeeplink(id: Long, newLink: String) {
76+
viewModelScope.launch {
77+
deeprQueries.updateDeeplink(newLink, id)
78+
}
79+
}
7280
}

app/src/main/sqldelight/com/yogeshpaliyal/deepr/Deepr.sq

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ SELECT * FROM Deepr ORDER BY createdAt DESC;
1414
listDeeprAsc:
1515
SELECT * FROM Deepr ORDER BY createdAt ASC;
1616

17-
getDeeprById:
18-
SELECT * FROM Deepr WHERE id = ?;
19-
2017
deleteDeeprById:
2118
DELETE FROM Deepr WHERE id = ?;
2219

@@ -40,3 +37,6 @@ SELECT * FROM Deepr WHERE link LIKE '%' || ? || '%' ORDER BY openedCount ASC;
4037

4138
incrementOpenedCount:
4239
UPDATE Deepr SET openedCount = openedCount + 1 WHERE id = ?;
40+
41+
updateDeeplink:
42+
UPDATE Deepr SET link = ? WHERE id = ?;

0 commit comments

Comments
 (0)