refactor subscriptions import and fix crash

This commit is contained in:
Bnyro 2022-08-06 11:49:11 +02:00
parent 0e9443fff6
commit 61f1bf6fee
5 changed files with 71 additions and 82 deletions

View File

@ -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()
}

View File

@ -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
)

View File

@ -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<String>
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<Preference>(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()
}
}
}

View File

@ -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,12 +21,7 @@ class ImportHelper(
) {
private val TAG = "ImportHelper"
fun importSubscriptions() {
getContent.launch("*/*")
}
val getContent =
activity.registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
fun importSubscriptions(uri: Uri?) {
if (uri != null) {
try {
val type = activity.contentResolver.getType(uri)
@ -39,11 +33,12 @@ class ImportHelper(
val json = readTextFromUri(uri)
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
channels = subscriptions.subscriptions?.map {
it.url.replace("https://www.youtube.com/channel/", "")
it.url?.replace("https://www.youtube.com/channel/", "")!!
} as ArrayList<String>
} else if (type == "application/zip") {
val zis = ZipInputStream(inputStream)
var entry: ZipEntry? = zis.nextEntry
while (entry != null) {
if (entry.name.endsWith(".csv")) {
inputStream = zis

View File

@ -65,21 +65,24 @@ object SubscriptionHelper {
}
}
suspend fun importSubscriptions(channels: List<String>) {
suspend fun importSubscriptions(newChannels: List<String>) {
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)
}
}