Skip to content

Commit 44a6c12

Browse files
Refactor backup and import processes to use profile for CSV (#367)
* Refactor backup and import processes to use updated data retrieval methods * Treat blank CSV profile names as absent to prevent invalid profile creation (#368) * Initial plan * Handle blank/whitespace profile names in CSV import Co-authored-by: yogeshpaliyal <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yogeshpaliyal <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: yogeshpaliyal <[email protected]>
1 parent 5edbc00 commit 44a6c12

File tree

9 files changed

+27
-19
lines changed

9 files changed

+27
-19
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/backup/AutoBackupWorker.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.content.Context
44
import androidx.core.net.toUri
55
import androidx.documentfile.provider.DocumentFile
66
import com.yogeshpaliyal.deepr.DeeprQueries
7-
import com.yogeshpaliyal.deepr.ListDeeprWithTagsAsc
7+
import com.yogeshpaliyal.deepr.GetLinksForBackup
88
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
99
import kotlinx.coroutines.Dispatchers
1010
import kotlinx.coroutines.flow.first
@@ -37,7 +37,7 @@ class AutoBackupWorker(
3737
return@withContext
3838
}
3939

40-
val dataToExport = deeprQueries.listDeeprWithTagsAsc().executeAsList()
40+
val dataToExport = deeprQueries.getLinksForBackup().executeAsList()
4141
if (dataToExport.isEmpty()) {
4242
return@withContext
4343
}
@@ -60,7 +60,7 @@ class AutoBackupWorker(
6060
private fun saveToSelectedLocation(
6161
location: String,
6262
fileName: String = "deepr_backup.csv",
63-
data: List<ListDeeprWithTagsAsc>,
63+
data: List<GetLinksForBackup>,
6464
): Boolean =
6565
try {
6666
// For content:// URIs from document picker, create a new document in that folder

app/src/main/java/com/yogeshpaliyal/deepr/backup/CsvWriter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.yogeshpaliyal.deepr.backup
22

33
import com.opencsv.CSVWriter
4-
import com.yogeshpaliyal.deepr.ListDeeprWithTagsAsc
4+
import com.yogeshpaliyal.deepr.GetLinksForBackup
55
import com.yogeshpaliyal.deepr.util.Constants
66
import java.io.OutputStream
77

88
class CsvWriter {
99
fun writeToCsv(
1010
outputStream: OutputStream,
11-
data: List<ListDeeprWithTagsAsc>,
11+
data: List<GetLinksForBackup>,
1212
) {
1313
outputStream.bufferedWriter().use { writer ->
1414
// Write Header
@@ -24,6 +24,7 @@ class CsvWriter {
2424
Constants.Header.TAGS,
2525
Constants.Header.THUMBNAIL,
2626
Constants.Header.IS_FAVOURITE,
27+
Constants.Header.PROFILE_NAME,
2728
),
2829
)
2930
// Write Data
@@ -35,9 +36,10 @@ class CsvWriter {
3536
item.openedCount.toString(),
3637
item.name,
3738
item.notes,
38-
item.tagsNames ?: "",
39+
item.tags ?: "",
3940
item.thumbnail,
4041
item.isFavourite.toString(),
42+
item.profileName,
4143
),
4244
)
4345
}

app/src/main/java/com/yogeshpaliyal/deepr/backup/ExportRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ExportRepositoryImpl(
3030
if (count == 0L) {
3131
return RequestResult.Error(context.getString(R.string.no_data_to_export))
3232
}
33-
val dataToExportInCsvFormat = deeprQueries.listDeeprWithTagsAsc().executeAsList()
33+
val dataToExportInCsvFormat = deeprQueries.getLinksForBackup().executeAsList()
3434
if (dataToExportInCsvFormat.isEmpty()) {
3535
return RequestResult.Error(context.getString(R.string.no_data_available_export))
3636
}

app/src/main/java/com/yogeshpaliyal/deepr/backup/ImportRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ImportRepositoryImpl(
1515
deeprQueries: DeeprQueries,
1616
appPreferenceDataStore: AppPreferenceDataStore,
1717
) : ImportRepository {
18-
private val csvImporter = CsvBookmarkImporter(context, deeprQueries)
18+
private val csvImporter = CsvBookmarkImporter(context, deeprQueries, appPreferenceDataStore)
1919
private val chromeImporter = ChromeBookmarkImporter(context, deeprQueries, appPreferenceDataStore)
2020
private val mozillaImporter = MozillaBookmarkImporter(context, deeprQueries, appPreferenceDataStore)
2121

app/src/main/java/com/yogeshpaliyal/deepr/backup/importer/ChromeBookmarkImporter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.yogeshpaliyal.deepr.backup.importer
33
import android.content.Context
44
import com.yogeshpaliyal.deepr.DeeprQueries
55
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
6-
import kotlinx.coroutines.flow.singleOrNull
6+
import kotlinx.coroutines.flow.first
77
import org.jsoup.nodes.Document
88
import org.jsoup.nodes.Element
99

@@ -22,7 +22,7 @@ class ChromeBookmarkImporter(
2222

2323
// Chrome bookmarks use <a> tags inside <dt> elements
2424
val links = document.select("dt > a[href]")
25-
val profileId = appPreferenceDataStore.getSelectedProfileId.singleOrNull() ?: 1L
25+
val profileId = appPreferenceDataStore.getSelectedProfileId.first()
2626
links.forEach { link ->
2727
val url = link.attr("href")
2828
val title = link.text()

app/src/main/java/com/yogeshpaliyal/deepr/backup/importer/CsvBookmarkImporter.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import com.opencsv.CSVReaderBuilder
77
import com.opencsv.exceptions.CsvException
88
import com.yogeshpaliyal.deepr.DeeprQueries
99
import com.yogeshpaliyal.deepr.backup.ImportResult
10+
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
1011
import com.yogeshpaliyal.deepr.util.Constants
1112
import com.yogeshpaliyal.deepr.util.RequestResult
13+
import kotlinx.coroutines.flow.first
1214
import java.io.IOException
1315

1416
/**
@@ -17,13 +19,16 @@ import java.io.IOException
1719
class CsvBookmarkImporter(
1820
private val context: Context,
1921
private val deeprQueries: DeeprQueries,
22+
private val appPreferenceDataStore: AppPreferenceDataStore,
2023
) : BookmarkImporter {
2124
override suspend fun import(uri: Uri): RequestResult<ImportResult> {
2225
var updatedCount = 0
2326
var skippedCount = 0
2427

2528
try {
29+
val defaultProfileId = appPreferenceDataStore.getSelectedProfileId.first()
2630
context.contentResolver.openInputStream(uri)?.use { inputStream ->
31+
2732
inputStream.reader().use { reader ->
2833
val customParser =
2934
CSVParserBuilder()
@@ -54,21 +59,21 @@ class CsvBookmarkImporter(
5459
val tagsString = row.getOrNull(5) ?: ""
5560
val thumbnail = row.getOrNull(6) ?: ""
5661
val isFavourite = row.getOrNull(7)?.toLongOrNull() ?: 0
57-
val profileName = row.getOrNull(8)
62+
val profileName = row.getOrNull(8)?.trim()?.takeIf { it.isNotBlank() }
5863
val existing = deeprQueries.getDeeprByLink(link).executeAsOneOrNull()
5964
if (link.isNotBlank() && existing == null) {
6065
updatedCount++
6166
deeprQueries.transaction {
6267
val profileID =
6368
profileName?.let {
64-
val profile = deeprQueries.getProfileByName(profileName).executeAsOneOrNull()
69+
val profile = deeprQueries.getProfileByName(it).executeAsOneOrNull()
6570
if (profile == null) {
66-
deeprQueries.insertProfile(profileName)
71+
deeprQueries.insertProfile(it)
6772
deeprQueries.lastInsertRowId().executeAsOneOrNull()
6873
} else {
6974
profile.id
7075
}
71-
}
76+
} ?: defaultProfileId
7277
deeprQueries.importDeepr(
7378
link = link,
7479
openedCount = openedCount,
@@ -77,7 +82,7 @@ class CsvBookmarkImporter(
7782
thumbnail = thumbnail,
7883
isFavourite = isFavourite,
7984
createdAt = createdAt,
80-
profileId = profileID ?: 1L,
85+
profileId = profileID,
8186
)
8287
val linkId = deeprQueries.lastInsertRowId().executeAsOne()
8388

app/src/main/java/com/yogeshpaliyal/deepr/backup/importer/HtmlBookmarkImporter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.yogeshpaliyal.deepr.DeeprQueries
66
import com.yogeshpaliyal.deepr.backup.ImportResult
77
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
88
import com.yogeshpaliyal.deepr.util.RequestResult
9-
import kotlinx.coroutines.flow.singleOrNull
9+
import kotlinx.coroutines.flow.firstOrNull
1010
import org.jsoup.Jsoup
1111
import org.jsoup.nodes.Document
1212
import org.jsoup.nodes.Element
@@ -34,7 +34,7 @@ abstract class HtmlBookmarkImporter(
3434
val existing = deeprQueries.getDeeprByLink(bookmark.url).executeAsOneOrNull()
3535
if (bookmark.url.isNotBlank() && existing == null) {
3636
try {
37-
val profileId = appPreferenceDataStore.getSelectedProfileId.singleOrNull() ?: 1L
37+
val profileId = appPreferenceDataStore.getSelectedProfileId.firstOrNull() ?: 1L
3838
deeprQueries.transaction {
3939
deeprQueries.insertDeepr(
4040
link = bookmark.url,

app/src/main/java/com/yogeshpaliyal/deepr/backup/importer/MozillaBookmarkImporter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.yogeshpaliyal.deepr.backup.importer
33
import android.content.Context
44
import com.yogeshpaliyal.deepr.DeeprQueries
55
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
6-
import kotlinx.coroutines.flow.singleOrNull
6+
import kotlinx.coroutines.flow.first
77
import org.jsoup.nodes.Document
88
import org.jsoup.nodes.Element
99

@@ -33,7 +33,7 @@ class MozillaBookmarkImporter(
3333
val lastModified = link.attr("last_modified")
3434
val shortcutUrl = link.attr("shortcuturl")
3535
val tags = link.attr("tags")
36-
val profileId = appPreferenceDataStore.getSelectedProfileId.singleOrNull() ?: 1L
36+
val profileId = appPreferenceDataStore.getSelectedProfileId.first()
3737
if (url.isNotBlank() && !url.startsWith("place:")) {
3838
val tagList =
3939
if (tags.isNotBlank()) {

app/src/main/java/com/yogeshpaliyal/deepr/util/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ object Constants {
1010
const val TAGS = "Tags"
1111
const val THUMBNAIL = "Thumbnail"
1212
const val IS_FAVOURITE = "isFavourite"
13+
const val PROFILE_NAME = "profileName"
1314
}
1415
}

0 commit comments

Comments
 (0)