diff --git a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt index cdec855eb..c1eebc108 100644 --- a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt @@ -26,14 +26,12 @@ class TrendingAdapter( override fun getItemCount(): Int { return if (showAllAtOne) streamItems.size + else if (index >= streamItems.size) streamItems.size - 1 else index } fun updateItems() { index += 10 - if (index > streamItems.size) { - index = streamItems.size - } notifyDataSetChanged() } diff --git a/app/src/main/java/com/github/libretube/obj/NewPipeSubscription.kt b/app/src/main/java/com/github/libretube/obj/NewPipeSubscription.kt index 986592238..1db358271 100644 --- a/app/src/main/java/com/github/libretube/obj/NewPipeSubscription.kt +++ b/app/src/main/java/com/github/libretube/obj/NewPipeSubscription.kt @@ -1,7 +1,7 @@ package com.github.libretube.obj data class NewPipeSubscription( - val name: String, - val service_id: Int, - val url: String + val name: String? = null, + val service_id: Int? = null, + val url: String? = null ) diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index 90040fdf8..437f8b152 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -1,10 +1,8 @@ package com.github.libretube.preferences -import android.content.ContentResolver import android.content.Intent import android.net.Uri import android.os.Bundle -import android.util.Log import android.widget.Toast import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts @@ -24,17 +22,19 @@ import com.github.libretube.dialogs.LogoutDialog import com.github.libretube.util.ImportHelper import com.github.libretube.util.PermissionHelper import com.github.libretube.util.RetrofitInstance -import org.json.JSONObject -import org.json.JSONTokener -import retrofit2.HttpException -import java.io.IOException -import java.io.InputStream -import java.util.zip.ZipEntry -import java.util.zip.ZipInputStream class InstanceSettings : PreferenceFragmentCompat() { val TAG = "InstanceSettings" + private lateinit var getContent: ActivityResultLauncher + override fun onCreate(savedInstanceState: Bundle?) { + getContent = + registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> + ImportHelper(requireActivity() as AppCompatActivity).importSubscriptions(uri) + } + + super.onCreate(savedInstanceState) + } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.instance_settings, rootKey) @@ -128,7 +128,13 @@ class InstanceSettings : PreferenceFragmentCompat() { val importFromYt = findPreference(PreferenceKeys.IMPORT_SUBS) importFromYt?.setOnPreferenceClickListener { - importSubscriptions() + // check StorageAccess + val accessGranted = + PermissionHelper.isStoragePermissionGranted(activity as AppCompatActivity) + // import subscriptions + if (accessGranted) getContent.launch("*/*") + // request permissions if not granted + else PermissionHelper.requestReadWrite(activity as AppCompatActivity) true } } @@ -183,17 +189,4 @@ class InstanceSettings : PreferenceFragmentCompat() { if (!isAdded) return // Fragment not attached to an Activity activity?.runOnUiThread(action) } - - private fun importSubscriptions() { - val token = PreferenceHelper.getToken() - if (token != "") { - // check StorageAccess - val accessGranted = - PermissionHelper.isStoragePermissionGranted(activity as AppCompatActivity) - if (accessGranted) ImportHelper(requireActivity() as AppCompatActivity).importSubscriptions() - else PermissionHelper.requestReadWrite(activity as AppCompatActivity) - } else { - Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() - } - } } 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 74fc85688..2acb8c051 100644 --- a/app/src/main/java/com/github/libretube/util/ImportHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ImportHelper.kt @@ -3,7 +3,6 @@ package com.github.libretube.util import android.net.Uri import android.util.Log import android.widget.Toast -import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.R @@ -22,60 +21,56 @@ class ImportHelper( ) { private val TAG = "ImportHelper" - fun importSubscriptions() { - getContent.launch("*/*") - } + fun importSubscriptions(uri: Uri?) { + if (uri != null) { + try { + val type = activity.contentResolver.getType(uri) - val getContent = - activity.registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> - if (uri != null) { - try { - val type = activity.contentResolver.getType(uri) + 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 - 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 } + entry = zis.nextEntry } - - inputStream?.bufferedReader()?.readLines()?.forEach { - if (it.isNotBlank()) { - val channelId = it.substringBefore(",") - if (channelId.length == 24) { - channels.add(channelId) - } - } - } - 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() } + + inputStream?.bufferedReader()?.readLines()?.forEach { + if (it.isNotBlank()) { + val channelId = it.substringBefore(",") + if (channelId.length == 24) { + channels.add(channelId) + } + } + } + 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() } } + } private fun readTextFromUri(uri: Uri): String { val stringBuilder = StringBuilder() diff --git a/app/src/main/java/com/github/libretube/util/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/util/SubscriptionHelper.kt index fe64e8157..c917d0900 100644 --- a/app/src/main/java/com/github/libretube/util/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/util/SubscriptionHelper.kt @@ -65,21 +65,24 @@ object SubscriptionHelper { } } - suspend fun importSubscriptions(channels: List) { + suspend fun importSubscriptions(newChannels: List) { if (PreferenceHelper.getToken() != "") { - val response = try { + try { val token = PreferenceHelper.getToken() RetrofitInstance.authApi.importSubscriptions( false, token, - channels + newChannels ) } catch (e: Exception) { e.printStackTrace() } } else { - val newChannels = PreferenceHelper.getLocalSubscriptions().toMutableList() + channels - PreferenceHelper.setLocalSubscriptions(newChannels) + val channels = PreferenceHelper.getLocalSubscriptions().toMutableList() + newChannels.forEach { + if (!channels.contains(it)) channels += it + } + PreferenceHelper.setLocalSubscriptions(channels) } }