Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import com.yogeshpaliyal.deepr.DeeprQueries
import com.yogeshpaliyal.deepr.ListDeeprWithTagsAsc
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -36,7 +37,7 @@ class AutoBackupWorker(
return@withContext
}

val dataToExport = deeprQueries.listDeeprAsc().executeAsList()
val dataToExport = deeprQueries.listDeeprWithTagsAsc().executeAsList()
if (dataToExport.isEmpty()) {
return@withContext
}
Expand All @@ -59,12 +60,11 @@ class AutoBackupWorker(
private fun saveToSelectedLocation(
location: String,
fileName: String = "deepr_backup.csv",
data: List<com.yogeshpaliyal.deepr.Deepr>,
data: List<ListDeeprWithTagsAsc>,
): Boolean =
try {
// For content:// URIs from document picker, create a new document in that folder
val locationUri = location.toUri()
val documentFile = DocumentFile.fromTreeUri(context, locationUri)
val directory = DocumentFile.fromTreeUri(context, locationUri)
var docFile = directory?.findFile(fileName)
if (docFile == null) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/yogeshpaliyal/deepr/backup/CsvWriter.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.yogeshpaliyal.deepr.backup

import com.opencsv.CSVWriter
import com.yogeshpaliyal.deepr.Deepr
import com.yogeshpaliyal.deepr.ListDeeprWithTagsAsc
import com.yogeshpaliyal.deepr.util.Constants
import java.io.OutputStream

class CsvWriter {
fun writeToCsv(
outputStream: OutputStream,
data: List<Deepr>,
data: List<ListDeeprWithTagsAsc>,
) {
outputStream.bufferedWriter().use { writer ->
// Write Header
Expand All @@ -21,6 +21,7 @@ class CsvWriter {
Constants.Header.OPENED_COUNT,
Constants.Header.NAME,
Constants.Header.NOTES,
Constants.Header.TAGS,
),
)
// Write Data
Expand All @@ -32,6 +33,7 @@ class CsvWriter {
item.openedCount.toString(),
item.name,
item.notes,
item.tagsNames ?: "",
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ExportRepositoryImpl(
if (count == 0L) {
return RequestResult.Error(context.getString(R.string.no_data_to_export))
}
val dataToExportInCsvFormat = deeprQueries.listDeeprAsc().executeAsList()
val dataToExportInCsvFormat = deeprQueries.listDeeprWithTagsAsc().executeAsList()
if (dataToExportInCsvFormat.isEmpty()) {
return RequestResult.Error(context.getString(R.string.no_data_available_export))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ImportRepositoryImpl(
val openedCount = row[2].toLongOrNull() ?: 0L
val name = row.getOrNull(3)?.toString() ?: ""
val notes = row.getOrNull(4)?.toString() ?: ""
val tagsString = row.getOrNull(5)?.toString() ?: ""
val existing = deeprQueries.getDeeprByLink(link).executeAsOneOrNull()
if (link.isNotBlank() && existing == null) {
updatedCount++
Expand All @@ -56,6 +57,21 @@ class ImportRepositoryImpl(
name = name,
notes = notes,
)
val linkId = deeprQueries.lastInsertRowId().executeAsOne()

// Import tags if present
if (tagsString.isNotBlank()) {
val tagNames = tagsString.split(",").map { it.trim() }.filter { it.isNotEmpty() }
tagNames.forEach { tagName ->
// Insert tag if it doesn't exist
deeprQueries.insertTag(tagName)
// Get tag ID and link it
val tag = deeprQueries.getTagByName(tagName).executeAsOneOrNull()
if (tag != null) {
deeprQueries.addTagToLink(linkId, tag.id)
}
}
}
}
} else {
skippedCount++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ object Constants {
const val NAME = "Name"
const val CREATED_AT = "CreatedAt"
const val NOTES = "Notes"
const val TAGS = "Tags"
}
}