Use IO extensions in ImportHelper.

This commit is contained in:
Isira Seneviratne 2022-09-17 06:26:23 +05:30
parent 8629adba29
commit fa0a993418

View File

@ -15,49 +15,39 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.InputStreamReader
class ImportHelper(
private val activity: Activity
) {
class ImportHelper(private val activity: Activity) {
/** /**
* Import subscriptions by a file uri * Import subscriptions by a file uri
*/ */
fun importSubscriptions(uri: Uri?) { fun importSubscriptions(uri: Uri?) {
if (uri == null) return if (uri == null) return
try { try {
var channels = ArrayList<String>() val channels = when (activity.contentResolver.getType(uri)) {
val fileType = activity.contentResolver.getType(uri) "application/json" -> {
if (fileType == "application/json") {
// NewPipe subscriptions format // NewPipe subscriptions format
val mapper = ObjectMapper() val mapper = ObjectMapper()
val json = readRawTextFromUri(uri) val json = activity.contentResolver.openInputStream(uri)?.use {
it.bufferedReader().use { reader -> reader.readText() }
}.orEmpty()
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
channels = subscriptions.subscriptions?.map { subscriptions.subscriptions.orEmpty().map {
it.url?.replace("https://www.youtube.com/channel/", "")!! it.url!!.replace("https://www.youtube.com/channel/", "")
} as ArrayList<String> }
} else if ( }
fileType == "text/csv" || "text/csv", "text/comma-separated-values" -> {
fileType == "text/comma-separated-values"
) {
// import subscriptions from Google/YouTube Takeout // import subscriptions from Google/YouTube Takeout
val inputStream = activity.contentResolver.openInputStream(uri) activity.contentResolver.openInputStream(uri)?.use {
BufferedReader(InputStreamReader(inputStream)).use { reader -> it.bufferedReader().useLines { lines ->
var line: String? = reader.readLine() lines.map { line -> line.substringBefore(",") }
while (line != null) { .filter { channelId -> channelId.length == 24 }
val channelId = line.substringBefore(",") .toList()
if (channelId.length == 24) channels.add(channelId)
line = reader.readLine()
} }
}.orEmpty()
} }
inputStream?.close() else -> throw IllegalArgumentException("Unsupported file type")
} else {
throw IllegalArgumentException("Unsupported file type")
} }
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
@ -75,20 +65,6 @@ class ImportHelper(
} }
} }
private fun readRawTextFromUri(uri: Uri): String {
val stringBuilder = StringBuilder()
activity.contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
var line: String? = reader.readLine()
while (line != null) {
stringBuilder.append(line)
line = reader.readLine()
}
}
}
return stringBuilder.toString()
}
/** /**
* write the text to the document * write the text to the document
*/ */