-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to export minimal library information to a CSV file (mihon…
…app/mihon#1161) (cherry picked from commit fab8b17d99c44a08555b1f584c56d62a47737ca0)
- Loading branch information
1 parent
2c3baa9
commit e006f1c
Showing
4 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/java/eu/kanade/tachiyomi/data/export/LibraryExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eu.kanade.tachiyomi.data.export | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import tachiyomi.domain.manga.model.Manga | ||
|
||
object LibraryExporter { | ||
|
||
data class ExportOptions( | ||
val includeTitle: Boolean, | ||
val includeAuthor: Boolean, | ||
val includeArtist: Boolean, | ||
) | ||
|
||
suspend fun exportToCsv( | ||
context: Context, | ||
uri: Uri, | ||
favorites: List<Manga>, | ||
options: ExportOptions, | ||
onExportComplete: () -> Unit, | ||
) { | ||
withContext(Dispatchers.IO) { | ||
context.contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
val csvData = generateCsvData(favorites, options) | ||
outputStream.write(csvData.toByteArray()) | ||
} | ||
onExportComplete() | ||
} | ||
} | ||
|
||
private val escapeRequired = listOf("\r", "\n", "\"", ",") | ||
|
||
private fun generateCsvData(favorites: List<Manga>, options: ExportOptions): String { | ||
val columnSize = listOf( | ||
options.includeTitle, | ||
options.includeAuthor, | ||
options.includeArtist, | ||
) | ||
.count { it } | ||
|
||
val rows = buildList(favorites.size) { | ||
favorites.forEach { manga -> | ||
buildList(columnSize) { | ||
if (options.includeTitle) add(manga.title) | ||
if (options.includeAuthor) add(manga.author) | ||
if (options.includeArtist) add(manga.artist) | ||
} | ||
.let(::add) | ||
} | ||
} | ||
return rows.joinToString("\r\n") { columns -> | ||
columns.joinToString(",") columns@{ column -> | ||
if (column.isNullOrBlank()) return@columns "" | ||
if (escapeRequired.any { column.contains(it) }) { | ||
column.replace("\"", "\"\"").let { "\"$it\"" } | ||
} else { | ||
column | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters