From a975ba212fb8b4b0929f9025736e876a3d0441f0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 6 Aug 2022 11:55:17 +0200 Subject: [PATCH] fix subscriptions --- .../adapters/SubscriptionChannelAdapter.kt | 4 +- .../com/github/libretube/util/ImportHelper.kt | 82 ++++++++++--------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index 9d4af078b..ef2ff27a1 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -15,8 +15,6 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList() { val TAG = "SubChannelAdapter" - private var subscribed = true - override fun getItemCount(): Int { return subscriptions.size } @@ -30,6 +28,8 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList() - if (type == "application/json") { - val mapper = ObjectMapper() - val json = readTextFromUri(uri) - val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) - channels = subscriptions.subscriptions?.map { - it.url?.replace("https://www.youtube.com/channel/", "")!! - } as ArrayList - } else if (type == "application/zip") { - val zis = ZipInputStream(inputStream) - var entry: ZipEntry? = zis.nextEntry + var inputStream: InputStream? = activity.contentResolver.openInputStream(uri) + var channels = ArrayList() + if (type == "application/json") { + val mapper = ObjectMapper() + val json = readTextFromUri(uri) + val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) + channels = subscriptions.subscriptions?.map { + it.url?.replace("https://www.youtube.com/channel/", "")!! + } as ArrayList + } else if (type == "application/zip") { + val zis = ZipInputStream(inputStream) + var entry: ZipEntry? = zis.nextEntry - while (entry != null) { - if (entry.name.endsWith(".csv")) { - inputStream = zis - break - } - entry = zis.nextEntry + while (entry != null) { + if (entry.name.endsWith(".csv")) { + inputStream = zis + break } - } - - inputStream?.bufferedReader()?.readLines()?.forEach { - if (it.isNotBlank()) { - val channelId = it.substringBefore(",") - if (channelId.length == 24) { - channels.add(channelId) + entry = zis.nextEntry + inputStream?.bufferedReader()?.readLines()?.forEach { + if (it.isNotBlank()) { + val channelId = it.substringBefore(",") + if (channelId.length == 24) { + channels.add(channelId) + } } } + inputStream?.close() } - inputStream?.close() - - CoroutineScope(Dispatchers.IO).launch { - SubscriptionHelper.importSubscriptions(channels) - } - } catch (e: Exception) { - Log.e(TAG, e.toString()) - Toast.makeText( - activity, - R.string.error, - Toast.LENGTH_SHORT - ).show() + } else { + throw IllegalArgumentException("unsupported type") } + + CoroutineScope(Dispatchers.IO).launch { + SubscriptionHelper.importSubscriptions(channels) + } + + Toast.makeText(activity, R.string.importsuccess, Toast.LENGTH_SHORT).show() + } catch (e: Exception) { + Log.e(TAG, e.toString()) + Toast.makeText( + activity, + R.string.error, + Toast.LENGTH_SHORT + ).show() } }