mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
refactor subscriptions import and fix crash
This commit is contained in:
parent
0e9443fff6
commit
61f1bf6fee
@ -26,14 +26,12 @@ class TrendingAdapter(
|
|||||||
|
|
||||||
override fun getItemCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return if (showAllAtOne) streamItems.size
|
return if (showAllAtOne) streamItems.size
|
||||||
|
else if (index >= streamItems.size) streamItems.size - 1
|
||||||
else index
|
else index
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateItems() {
|
fun updateItems() {
|
||||||
index += 10
|
index += 10
|
||||||
if (index > streamItems.size) {
|
|
||||||
index = streamItems.size
|
|
||||||
}
|
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.github.libretube.obj
|
package com.github.libretube.obj
|
||||||
|
|
||||||
data class NewPipeSubscription(
|
data class NewPipeSubscription(
|
||||||
val name: String,
|
val name: String? = null,
|
||||||
val service_id: Int,
|
val service_id: Int? = null,
|
||||||
val url: String
|
val url: String? = null
|
||||||
)
|
)
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package com.github.libretube.preferences
|
package com.github.libretube.preferences
|
||||||
|
|
||||||
import android.content.ContentResolver
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
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.ImportHelper
|
||||||
import com.github.libretube.util.PermissionHelper
|
import com.github.libretube.util.PermissionHelper
|
||||||
import com.github.libretube.util.RetrofitInstance
|
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() {
|
class InstanceSettings : PreferenceFragmentCompat() {
|
||||||
val TAG = "InstanceSettings"
|
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?) {
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
setPreferencesFromResource(R.xml.instance_settings, rootKey)
|
setPreferencesFromResource(R.xml.instance_settings, rootKey)
|
||||||
@ -128,7 +128,13 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
|
|
||||||
val importFromYt = findPreference<Preference>(PreferenceKeys.IMPORT_SUBS)
|
val importFromYt = findPreference<Preference>(PreferenceKeys.IMPORT_SUBS)
|
||||||
importFromYt?.setOnPreferenceClickListener {
|
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
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,17 +189,4 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
if (!isAdded) return // Fragment not attached to an Activity
|
if (!isAdded) return // Fragment not attached to an Activity
|
||||||
activity?.runOnUiThread(action)
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package com.github.libretube.util
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
@ -22,60 +21,56 @@ class ImportHelper(
|
|||||||
) {
|
) {
|
||||||
private val TAG = "ImportHelper"
|
private val TAG = "ImportHelper"
|
||||||
|
|
||||||
fun importSubscriptions() {
|
fun importSubscriptions(uri: Uri?) {
|
||||||
getContent.launch("*/*")
|
if (uri != null) {
|
||||||
}
|
try {
|
||||||
|
val type = activity.contentResolver.getType(uri)
|
||||||
|
|
||||||
val getContent =
|
var inputStream: InputStream? = activity.contentResolver.openInputStream(uri)
|
||||||
activity.registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
|
var channels = ArrayList<String>()
|
||||||
if (uri != null) {
|
if (type == "application/json") {
|
||||||
try {
|
val mapper = ObjectMapper()
|
||||||
val type = activity.contentResolver.getType(uri)
|
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<String>
|
||||||
|
} else if (type == "application/zip") {
|
||||||
|
val zis = ZipInputStream(inputStream)
|
||||||
|
var entry: ZipEntry? = zis.nextEntry
|
||||||
|
|
||||||
var inputStream: InputStream? = activity.contentResolver.openInputStream(uri)
|
while (entry != null) {
|
||||||
var channels = ArrayList<String>()
|
if (entry.name.endsWith(".csv")) {
|
||||||
if (type == "application/json") {
|
inputStream = zis
|
||||||
val mapper = ObjectMapper()
|
break
|
||||||
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<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
|
|
||||||
break
|
|
||||||
}
|
|
||||||
entry = zis.nextEntry
|
|
||||||
}
|
}
|
||||||
|
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 {
|
private fun readTextFromUri(uri: Uri): String {
|
||||||
val stringBuilder = StringBuilder()
|
val stringBuilder = StringBuilder()
|
||||||
|
@ -65,21 +65,24 @@ object SubscriptionHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun importSubscriptions(channels: List<String>) {
|
suspend fun importSubscriptions(newChannels: List<String>) {
|
||||||
if (PreferenceHelper.getToken() != "") {
|
if (PreferenceHelper.getToken() != "") {
|
||||||
val response = try {
|
try {
|
||||||
val token = PreferenceHelper.getToken()
|
val token = PreferenceHelper.getToken()
|
||||||
RetrofitInstance.authApi.importSubscriptions(
|
RetrofitInstance.authApi.importSubscriptions(
|
||||||
false,
|
false,
|
||||||
token,
|
token,
|
||||||
channels
|
newChannels
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val newChannels = PreferenceHelper.getLocalSubscriptions().toMutableList() + channels
|
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
||||||
PreferenceHelper.setLocalSubscriptions(newChannels)
|
newChannels.forEach {
|
||||||
|
if (!channels.contains(it)) channels += it
|
||||||
|
}
|
||||||
|
PreferenceHelper.setLocalSubscriptions(channels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user