Skip to content

Commit 2c1ec14

Browse files
committed
feat: added support for open count
1 parent 6b1027c commit 2c1ec14

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/MainActivity.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fun Content(viewModel: AccountViewModel) {
186186
.padding(8.dp),
187187
accounts = accounts,
188188
onItemClick = {
189+
viewModel.incrementOpenedCount(it.id)
189190
openDeeplink(context, it.link)
190191
},
191192
onRemoveClick = {
@@ -229,7 +230,7 @@ fun Content(viewModel: AccountViewModel) {
229230
) {
230231
OutlinedButton(onClick = {
231232
if (isValidDeeplink(inputText.value)) {
232-
viewModel.insertAccount(inputText.value)
233+
viewModel.insertAccount(inputText.value, false)
233234
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT)
234235
.show()
235236
inputText.value = ""
@@ -248,7 +249,7 @@ fun Content(viewModel: AccountViewModel) {
248249
if (isValidDeeplink(inputText.value)) {
249250
val success = openDeeplink(context, inputText.value)
250251
if (success) {
251-
viewModel.insertAccount(inputText.value)
252+
viewModel.insertAccount(inputText.value, true)
252253
Toast.makeText(
253254
context,
254255
"Saved",
@@ -326,11 +327,19 @@ fun DeeprItem(
326327
style = MaterialTheme.typography.bodyLarge
327328
)
328329
Spacer(modifier = Modifier.height(4.dp))
329-
Text(
330-
text = formatDateTime(account.createdAt),
331-
style = MaterialTheme.typography.bodySmall,
332-
color = MaterialTheme.colorScheme.onSurfaceVariant
333-
)
330+
Row(verticalAlignment = Alignment.CenterVertically) {
331+
Text(
332+
text = formatDateTime(account.createdAt),
333+
style = MaterialTheme.typography.bodySmall,
334+
color = MaterialTheme.colorScheme.onSurfaceVariant
335+
)
336+
Spacer(modifier = Modifier.weight(1f))
337+
Text(
338+
text = "Opened: ${account.openedCount}",
339+
style = MaterialTheme.typography.bodySmall,
340+
color = MaterialTheme.colorScheme.onSurfaceVariant
341+
)
342+
}
334343
}
335344
Box {
336345
IconButton(onClick = { expanded = true }) {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class AccountViewModel(private val deeprQueries: DeeprQueries) : ViewModel() {
4848
sortOrder.value = order
4949
}
5050

51-
fun insertAccount(link: String) {
51+
fun insertAccount(link: String, executed: Boolean) {
5252
viewModelScope.launch {
53-
deeprQueries.insertDeepr(link = link)
53+
deeprQueries.insertDeepr(link = link, if (executed) 1 else 0)
5454
}
5555
}
5656

@@ -59,4 +59,10 @@ class AccountViewModel(private val deeprQueries: DeeprQueries) : ViewModel() {
5959
deeprQueries.deleteDeeprById(id)
6060
}
6161
}
62+
63+
fun incrementOpenedCount(id: Long) {
64+
viewModelScope.launch {
65+
deeprQueries.incrementOpenedCount(id)
66+
}
67+
}
6268
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
CREATE TABLE Deepr (
22
id INTEGER PRIMARY KEY NOT NULL,
33
link TEXT NOT NULL,
4-
createdAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
4+
createdAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
openedCount INTEGER NOT NULL DEFAULT 0
56
);
67

78
insertDeepr:
8-
INSERT INTO Deepr (link) VALUES (?);
9+
INSERT INTO Deepr (link, openedCount) VALUES (?, ?);
910

1011
listDeeprDesc:
1112
SELECT * FROM Deepr ORDER BY createdAt DESC;
@@ -24,3 +25,6 @@ SELECT * FROM Deepr WHERE link LIKE '%' || ? || '%' ORDER BY createdAt DESC;
2425

2526
searchDeeprAsc:
2627
SELECT * FROM Deepr WHERE link LIKE '%' || ? || '%' ORDER BY createdAt ASC;
28+
29+
incrementOpenedCount:
30+
UPDATE Deepr SET openedCount = openedCount + 1 WHERE id = ?;

0 commit comments

Comments
 (0)