Merge pull request #6674 from Bnyro/master

fix: import and export of FreeTube newline format
This commit is contained in:
Bnyro 2024-10-28 17:45:08 +01:00 committed by GitHub
commit a1cd869e6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,6 +26,7 @@ import com.github.libretube.obj.YouTubeWatchHistoryFileItem
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.TextUtils import com.github.libretube.util.TextUtils
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream import kotlinx.serialization.json.encodeToStream
import java.util.stream.Collectors import java.util.stream.Collectors
@ -164,7 +165,9 @@ object ImportHelper {
val playlistFile = activity.contentResolver.openInputStream(uri)?.use { inputStream -> val playlistFile = activity.contentResolver.openInputStream(uri)?.use { inputStream ->
val text = inputStream.bufferedReader().readText() val text = inputStream.bufferedReader().readText()
runCatching { runCatching {
JsonHelper.json.decodeFromString<List<FreeTubeImportPlaylist>>(text) text.lines().map { line ->
JsonHelper.json.decodeFromString<FreeTubeImportPlaylist>(line)
}
}.getOrNull() ?: runCatching { }.getOrNull() ?: runCatching {
listOf(JsonHelper.json.decodeFromString<FreeTubeImportPlaylist>(text)) listOf(JsonHelper.json.decodeFromString<FreeTubeImportPlaylist>(text))
}.getOrNull() }.getOrNull()
@ -263,8 +266,11 @@ object ImportHelper {
ImportFormat.FREETUBE -> { ImportFormat.FREETUBE -> {
val playlists = PlaylistsHelper.exportFreeTubePlaylists() val playlists = PlaylistsHelper.exportFreeTubePlaylists()
val freeTubeExportDb = playlists.joinToString("\n") { playlist ->
JsonHelper.json.encodeToString(playlist)
}
activity.contentResolver.openOutputStream(uri)?.use { activity.contentResolver.openOutputStream(uri)?.use {
JsonHelper.json.encodeToStream(playlists, it) it.write(freeTubeExportDb.toByteArray())
} }
activity.toastFromMainDispatcher(R.string.exportsuccess) activity.toastFromMainDispatcher(R.string.exportsuccess)
} }