Use BufferedReader#lines().

This commit is contained in:
Isira Seneviratne 2023-03-06 09:57:41 +05:30
parent 0885cb5b9a
commit 53913c9e1f

View File

@ -15,10 +15,10 @@ import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.obj.ImportPlaylistFile import com.github.libretube.obj.ImportPlaylistFile
import com.github.libretube.obj.NewPipeSubscription import com.github.libretube.obj.NewPipeSubscription
import com.github.libretube.obj.NewPipeSubscriptions import com.github.libretube.obj.NewPipeSubscriptions
import kotlin.streams.toList
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream import kotlinx.serialization.json.encodeToStream
import okio.use
object ImportHelper { object ImportHelper {
/** /**
@ -58,8 +58,8 @@ object ImportHelper {
"text/csv", "text/comma-separated-values" -> { "text/csv", "text/comma-separated-values" -> {
// import subscriptions from Google/YouTube Takeout // import subscriptions from Google/YouTube Takeout
activity.contentResolver.openInputStream(uri)?.use { activity.contentResolver.openInputStream(uri)?.use {
it.bufferedReader().useLines { lines -> it.bufferedReader().use { reader ->
lines.map { line -> line.substringBefore(",") } reader.lines().map { line -> line.substringBefore(",") }
.filter { channelId -> channelId.length == 24 } .filter { channelId -> channelId.length == 24 }
.toList() .toList()
} }
@ -103,7 +103,7 @@ object ImportHelper {
"text/csv", "text/comma-separated-values" -> { "text/csv", "text/comma-separated-values" -> {
val playlist = ImportPlaylist() val playlist = ImportPlaylist()
activity.contentResolver.openInputStream(uri)?.use { activity.contentResolver.openInputStream(uri)?.use {
val lines = it.bufferedReader().readLines() val lines = it.bufferedReader().use { reader -> reader.lines().toList() }
playlist.name = lines[1].split(",").reversed()[2] playlist.name = lines[1].split(",").reversed()[2]
val splitIndex = lines.indexOfFirst { line -> line.isBlank() } val splitIndex = lines.indexOfFirst { line -> line.isBlank() }
lines.subList(splitIndex + 2, lines.size).forEach { line -> lines.subList(splitIndex + 2, lines.size).forEach { line ->