Skip to content

Commit 11f8db8

Browse files
Refactor to use separate DeeprOpenLog table for tracking opens
Co-authored-by: yogeshpaliyal <[email protected]>
1 parent b21b47f commit 11f8db8

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class AccountViewModel(
274274
fun incrementOpenedCount(id: Long) {
275275
viewModelScope.launch(Dispatchers.IO) {
276276
deeprQueries.incrementOpenedCount(id)
277+
deeprQueries.insertDeeprOpenLog(id)
277278
}
278279
}
279280

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ id INTEGER PRIMARY KEY NOT NULL,
33
link TEXT NOT NULL,
44
name TEXT NOT NULL DEFAULT '',
55
createdAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
6-
openedCount INTEGER NOT NULL DEFAULT 0,
7-
lastOpenedAt TEXT
6+
openedCount INTEGER NOT NULL DEFAULT 0
87
);
98

109
CREATE TABLE Tags (
@@ -20,6 +19,13 @@ FOREIGN KEY (linkId) REFERENCES Deepr(id) ON DELETE CASCADE,
2019
FOREIGN KEY (tagId) REFERENCES Tags(id) ON DELETE CASCADE
2120
);
2221

22+
CREATE TABLE DeeprOpenLog (
23+
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
24+
deeplinkId INTEGER NOT NULL,
25+
openedAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
26+
FOREIGN KEY (deeplinkId) REFERENCES Deepr(id) ON DELETE CASCADE
27+
);
28+
2329
lastInsertRowId:
2430
SELECT last_insert_rowid();
2531

@@ -33,7 +39,7 @@ SELECT
3339
Deepr.name,
3440
Deepr.createdAt,
3541
Deepr.openedCount,
36-
Deepr.lastOpenedAt,
42+
(SELECT openedAt FROM DeeprOpenLog WHERE deeplinkId = Deepr.id ORDER BY openedAt DESC LIMIT 1) AS lastOpenedAt,
3743
GROUP_CONCAT(Tags.name, ', ') AS tagsNames,
3844
GROUP_CONCAT(Tags.id, ', ') AS tagsIds
3945
FROM
@@ -78,7 +84,7 @@ SELECT
7884
Deepr.name,
7985
Deepr.createdAt,
8086
Deepr.openedCount,
81-
Deepr.lastOpenedAt,
87+
(SELECT openedAt FROM DeeprOpenLog WHERE deeplinkId = Deepr.id ORDER BY openedAt DESC LIMIT 1) AS lastOpenedAt,
8288
GROUP_CONCAT(Tags.name, ', ') AS tagsNames
8389
FROM
8490
Deepr
@@ -93,7 +99,7 @@ deleteDeeprById:
9399
DELETE FROM Deepr WHERE id = ?;
94100

95101
incrementOpenedCount:
96-
UPDATE Deepr SET openedCount = openedCount + 1, lastOpenedAt = CURRENT_TIMESTAMP WHERE id = ?;
102+
UPDATE Deepr SET openedCount = openedCount + 1 WHERE id = ?;
97103

98104
updateDeeplink:
99105
UPDATE Deepr SET link = ? , name = ? WHERE id = ?;
@@ -144,3 +150,11 @@ SELECT Tags.id,Tags.name FROM Tags INNER JOIN LinkTags ON Tags.id = LinkTags.tag
144150

145151
hasTagLinks:
146152
SELECT COUNT(*) FROM LinkTags WHERE tagId = ?;
153+
154+
-- Deeplink Open Log operations
155+
insertDeeprOpenLog:
156+
INSERT INTO DeeprOpenLog (deeplinkId) VALUES (?);
157+
158+
getLastOpenedTime:
159+
SELECT openedAt FROM DeeprOpenLog WHERE deeplinkId = ? ORDER BY openedAt DESC LIMIT 1;
160+
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
ALTER TABLE Deepr ADD COLUMN lastOpenedAt TEXT;
1+
CREATE TABLE DeeprOpenLog (
2+
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
3+
deeplinkId INTEGER NOT NULL,
4+
openedAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
FOREIGN KEY (deeplinkId) REFERENCES Deepr(id) ON DELETE CASCADE
6+
);

0 commit comments

Comments
 (0)