From cbbbf65428dcf9ab9ec8ab022b44e1400a479614 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 16 Nov 2022 16:54:18 +0100 Subject: [PATCH] Improved user feedback on subscription import --- .../com/github/libretube/util/ImportHelper.kt | 79 +++++++++++-------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/libretube/util/ImportHelper.kt b/app/src/main/java/com/github/libretube/util/ImportHelper.kt index b02c05e65..dab77b37d 100644 --- a/app/src/main/java/com/github/libretube/util/ImportHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ImportHelper.kt @@ -9,6 +9,7 @@ import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper import com.github.libretube.extensions.TAG +import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.obj.NewPipeSubscription import com.github.libretube.obj.NewPipeSubscriptions import kotlinx.coroutines.CoroutineScope @@ -17,56 +18,64 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import java.io.FileOutputStream -class ImportHelper(private val activity: Activity) { +class ImportHelper( + private val activity: Activity +) { /** * Import subscriptions by a file uri */ fun importSubscriptions(uri: Uri?) { if (uri == null) return try { - val channels = when (activity.contentResolver.getType(uri)) { - "application/json" -> { - // NewPipe subscriptions format - val mapper = ObjectMapper() - val json = activity.contentResolver.openInputStream(uri)?.use { - it.bufferedReader().use { reader -> reader.readText() } - }.orEmpty() - - val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) - subscriptions.subscriptions.orEmpty().map { - it.url!!.replace("https://www.youtube.com/channel/", "") - } - } - "text/csv", "text/comma-separated-values" -> { - // import subscriptions from Google/YouTube Takeout - activity.contentResolver.openInputStream(uri)?.use { - it.bufferedReader().useLines { lines -> - lines.map { line -> line.substringBefore(",") } - .filter { channelId -> channelId.length == 24 } - .toList() - } - }.orEmpty() - } - else -> throw IllegalArgumentException("Unsupported file type") - } - + val applicationContext = activity.applicationContext + val channels = getChannelsFromUri(uri) CoroutineScope(Dispatchers.IO).launch { SubscriptionHelper.importSubscriptions(channels) + }.invokeOnCompletion { + applicationContext.toastFromMainThread(R.string.importsuccess) } - - Toast.makeText(activity, R.string.importsuccess, Toast.LENGTH_SHORT).show() + } catch (e: IllegalArgumentException) { + Log.e(TAG(), e.toString()) + Toast.makeText(activity, R.string.unsupported_file_format, Toast.LENGTH_SHORT).show() } catch (e: Exception) { Log.e(TAG(), e.toString()) - Toast.makeText( - activity, - R.string.error, - Toast.LENGTH_SHORT - ).show() + Toast.makeText(activity, R.string.server_error, Toast.LENGTH_SHORT).show() } } /** - * write the text to the document + * Get a list of channel IDs from a file [Uri] + */ + private fun getChannelsFromUri(uri: Uri): List { + return when (activity.contentResolver.getType(uri)) { + "application/json" -> { + // NewPipe subscriptions format + val mapper = ObjectMapper() + val json = activity.contentResolver.openInputStream(uri)?.use { + it.bufferedReader().use { reader -> reader.readText() } + }.orEmpty() + + val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) + subscriptions.subscriptions.orEmpty().map { + it.url!!.replace("https://www.youtube.com/channel/", "") + } + } + "text/csv", "text/comma-separated-values" -> { + // import subscriptions from Google/YouTube Takeout + activity.contentResolver.openInputStream(uri)?.use { + it.bufferedReader().useLines { lines -> + lines.map { line -> line.substringBefore(",") } + .filter { channelId -> channelId.length == 24 } + .toList() + } + }.orEmpty() + } + else -> throw IllegalArgumentException("Unsupported file type") + } + } + + /** + * Write the text to the document */ fun exportSubscriptions(uri: Uri?) { if (uri == null) return diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 04d59f718..81231ac6b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -372,6 +372,7 @@ Show the related videos as a row above the comments instead of below. Audio track Default + Unsupported file format! Download Service