Skip to content

Commit d5fccee

Browse files
Add tags to markdown (#151)
* refactor: update terminology from 'deeplink' to 'link' for consistency * feat: enhance markdown sync with tags and update query for linked items
1 parent bc2ef9a commit d5fccee

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/sync/SyncRepositoryImpl.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.yogeshpaliyal.deepr.sync
22

33
import android.content.Context
44
import androidx.core.net.toUri
5-
import com.yogeshpaliyal.deepr.Deepr
65
import com.yogeshpaliyal.deepr.DeeprQueries
6+
import com.yogeshpaliyal.deepr.ListDeeprWithTagsAsc
77
import com.yogeshpaliyal.deepr.R
88
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
99
import com.yogeshpaliyal.deepr.util.RequestResult
@@ -41,7 +41,7 @@ class SyncRepositoryImpl(
4141
return@withContext RequestResult.Error(context.getString(R.string.no_data_to_export))
4242
}
4343

44-
val dataToSync = deeprQueries.listDeeprAsc().executeAsList()
44+
val dataToSync = deeprQueries.listDeeprWithTagsAsc().executeAsList()
4545
if (dataToSync.isEmpty()) {
4646
return@withContext RequestResult.Error(context.getString(R.string.no_data_available_export))
4747
}
@@ -96,7 +96,7 @@ class SyncRepositoryImpl(
9696

9797
private fun writeMarkdownData(
9898
file: OutputStream,
99-
data: List<Deepr>,
99+
data: List<ListDeeprWithTagsAsc>,
100100
) {
101101
file.use { outputStream ->
102102
outputStream.bufferedWriter().use { writer ->
@@ -109,14 +109,26 @@ class SyncRepositoryImpl(
109109
writer.write("**Warning:** Please maintain the markdown table format when editing this file.\n\n")
110110

111111
// Write markdown table header
112-
writer.write("| Name | Link | Created At | Opened Count |\n")
113-
writer.write("|------|------|------------|--------------|\n")
112+
writer.write("| Name | Link | Created At | Opened Count | Tags |\n")
113+
writer.write("|------|------|------------|--------------|------|\n")
114114

115115
// Write data rows
116116
data.forEach { item ->
117117
val escapedName = item.name.replace("|", "\\|").replace("\n", " ")
118118
val escapedLink = item.link.replace("|", "\\|").replace("\n", " ")
119-
val row = "| $escapedName | $escapedLink | ${item.createdAt} | ${item.openedCount} |\n"
119+
120+
// Format tags as hashtags
121+
val tags =
122+
if (item.tagsNames.isNullOrEmpty()) {
123+
""
124+
} else {
125+
item.tagsNames
126+
.split(", ")
127+
.filter { it.isNotEmpty() }
128+
.joinToString(" ") { "#${it.replace(" ", "").replace("|", "")}" }
129+
}
130+
131+
val row = "| $escapedName | $escapedLink | ${item.createdAt} | ${item.openedCount} | $tags |\n"
120132
writer.write(row)
121133
}
122134

@@ -134,9 +146,9 @@ class SyncRepositoryImpl(
134146

135147
for (line in lines) {
136148
val trimmedLine = line.trim()
137-
if (trimmedLine.startsWith("| Name | Link | Created At | Opened Count |")) {
149+
if (trimmedLine.startsWith("| Name | Link | Created At | Opened Count | Tags |")) {
138150
foundHeader = true
139-
} else if (foundHeader && trimmedLine.startsWith("|------|------|------------|--------------|")) {
151+
} else if (foundHeader && trimmedLine.startsWith("|------|------|------------|--------------|------|")) {
140152
foundSeparator = true
141153
break
142154
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
2525
import kotlinx.coroutines.flow.SharingStarted
2626
import kotlinx.coroutines.flow.StateFlow
2727
import kotlinx.coroutines.flow.combine
28+
import kotlinx.coroutines.flow.first
2829
import kotlinx.coroutines.flow.flatMapLatest
2930
import kotlinx.coroutines.flow.receiveAsFlow
3031
import kotlinx.coroutines.flow.stateIn
@@ -216,6 +217,7 @@ class AccountViewModel(
216217
deeprQueries.lastInsertRowId().executeAsOneOrNull()?.let {
217218
modifyTagsForLink(it, tagsList)
218219
}
220+
syncToMarkdown()
219221
}
220222
}
221223

@@ -284,6 +286,7 @@ class AccountViewModel(
284286
viewModelScope.launch(Dispatchers.IO) {
285287
deeprQueries.updateDeeplink(newLink, newName, id)
286288
modifyTagsForLink(id, tagsList)
289+
syncToMarkdown()
287290
}
288291
}
289292

@@ -372,6 +375,10 @@ class AccountViewModel(
372375

373376
fun syncToMarkdown() {
374377
viewModelScope.launch(Dispatchers.IO) {
378+
val isEnabled = preferenceDataStore.getSyncEnabled.first()
379+
if (!isEnabled) {
380+
return@launch
381+
}
375382
val result = syncRepository.syncToMarkdown()
376383
when (result) {
377384
is RequestResult.Success -> {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ ORDER BY
6969
listDeeprAsc:
7070
SELECT * FROM Deepr ORDER BY createdAt ASC;
7171

72+
listDeeprWithTagsAsc:
73+
SELECT
74+
Deepr.id AS id,
75+
Deepr.link,
76+
Deepr.name,
77+
Deepr.createdAt,
78+
Deepr.openedCount,
79+
GROUP_CONCAT(Tags.name, ', ') AS tagsNames
80+
FROM
81+
Deepr
82+
LEFT JOIN LinkTags ON Deepr.id = LinkTags.linkId
83+
LEFT JOIN Tags ON LinkTags.tagId = Tags.id
84+
GROUP BY
85+
Deepr.id
86+
ORDER BY
87+
Deepr.createdAt ASC;
88+
7289
deleteDeeprById:
7390
DELETE FROM Deepr WHERE id = ?;
7491

0 commit comments

Comments
 (0)