Skip to content

Commit 50405e0

Browse files
feat: refactor tag handling in AccountViewModel and HomeBottomContent (#109)
1 parent ce3897c commit 50405e0

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,12 @@ fun HomeBottomContent(
112112
viewModel.removeTagFromLink(deeprInfo.id, tagId)
113113
}
114114

115-
// Then add selected tags
116-
selectedTags.forEach { tag ->
117-
if (tag.id > 0) {
118-
// Existing tag
119-
viewModel.addTagToLink(deeprInfo.id, tag.id)
120-
} else {
121-
// New tag
122-
viewModel.addTagToLinkByName(deeprInfo.id, tag.name)
123-
}
124-
}
125-
126115
if (deeprInfo.id == 0L) {
127116
// New Account
128-
viewModel.insertAccount(deeprInfo.link, deeprInfo.name, executeAfterSave)
117+
viewModel.insertAccount(deeprInfo.link, deeprInfo.name, executeAfterSave, selectedTags)
129118
} else {
130119
// Edit
131-
viewModel.updateDeeplink(deeprInfo.id, deeprInfo.link, deeprInfo.name)
120+
viewModel.updateDeeplink(deeprInfo.id, deeprInfo.link, deeprInfo.name, selectedTags)
132121
}
133122
onSaveDialogInfoChange(SaveDialogInfo(deeprInfo, executeAfterSave))
134123
}

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ class AccountViewModel(
116116
}
117117

118118
// Add tag to link
119-
fun addTagToLink(
119+
private suspend fun addTagToLink(
120120
linkId: Long,
121121
tagId: Long,
122122
) {
123-
viewModelScope.launch(Dispatchers.IO) {
123+
withContext(Dispatchers.IO) {
124124
deeprQueries.addTagToLink(linkId, tagId)
125125
}
126126
}
127127

128128
// Add tag by name (creates tag if it doesn't exist)
129-
fun addTagToLinkByName(
129+
private suspend fun addTagToLinkByName(
130130
linkId: Long,
131131
tagName: String,
132132
) {
133-
viewModelScope.launch(Dispatchers.IO) {
133+
withContext(Dispatchers.IO) {
134134
// Create the tag if it doesn't exist
135135
deeprQueries.insertTag(tagName)
136136

@@ -200,9 +200,31 @@ class AccountViewModel(
200200
link: String,
201201
name: String,
202202
executed: Boolean,
203+
tagsList: List<Tags>,
203204
) {
204205
viewModelScope.launch(Dispatchers.IO) {
205206
deeprQueries.insertDeepr(link = link, name, if (executed) 1 else 0)
207+
deeprQueries.lastInsertRowId().executeAsOneOrNull()?.let {
208+
modifyTagsForLink(it, tagsList)
209+
}
210+
}
211+
}
212+
213+
suspend fun modifyTagsForLink(
214+
linkId: Long,
215+
tagsList: List<Tags>,
216+
) {
217+
withContext(Dispatchers.IO) {
218+
// Then add selected tags
219+
tagsList.forEach { tag ->
220+
if (tag.id > 0) {
221+
// Existing tag
222+
addTagToLink(linkId, tag.id)
223+
} else {
224+
// New tag
225+
addTagToLinkByName(linkId, tag.name)
226+
}
227+
}
206228
}
207229
}
208230

@@ -222,9 +244,11 @@ class AccountViewModel(
222244
id: Long,
223245
newLink: String,
224246
newName: String,
247+
tagsList: List<Tags>,
225248
) {
226249
viewModelScope.launch(Dispatchers.IO) {
227250
deeprQueries.updateDeeplink(newLink, newName, id)
251+
modifyTagsForLink(id, tagsList)
228252
}
229253
}
230254

0 commit comments

Comments
 (0)