mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Improved user feedback on subscription import
This commit is contained in:
parent
eb194f110d
commit
cbbbf65428
@ -9,6 +9,7 @@ import com.github.libretube.R
|
|||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
|
import com.github.libretube.extensions.toastFromMainThread
|
||||||
import com.github.libretube.obj.NewPipeSubscription
|
import com.github.libretube.obj.NewPipeSubscription
|
||||||
import com.github.libretube.obj.NewPipeSubscriptions
|
import com.github.libretube.obj.NewPipeSubscriptions
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@ -17,56 +18,64 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
|
|
||||||
class ImportHelper(private val activity: Activity) {
|
class ImportHelper(
|
||||||
|
private val activity: Activity
|
||||||
|
) {
|
||||||
/**
|
/**
|
||||||
* Import subscriptions by a file uri
|
* Import subscriptions by a file uri
|
||||||
*/
|
*/
|
||||||
fun importSubscriptions(uri: Uri?) {
|
fun importSubscriptions(uri: Uri?) {
|
||||||
if (uri == null) return
|
if (uri == null) return
|
||||||
try {
|
try {
|
||||||
val channels = when (activity.contentResolver.getType(uri)) {
|
val applicationContext = activity.applicationContext
|
||||||
"application/json" -> {
|
val channels = getChannelsFromUri(uri)
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
SubscriptionHelper.importSubscriptions(channels)
|
SubscriptionHelper.importSubscriptions(channels)
|
||||||
|
}.invokeOnCompletion {
|
||||||
|
applicationContext.toastFromMainThread(R.string.importsuccess)
|
||||||
}
|
}
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
Toast.makeText(activity, R.string.importsuccess, Toast.LENGTH_SHORT).show()
|
Log.e(TAG(), e.toString())
|
||||||
|
Toast.makeText(activity, R.string.unsupported_file_format, Toast.LENGTH_SHORT).show()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
Toast.makeText(
|
Toast.makeText(activity, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||||
activity,
|
|
||||||
R.string.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<String> {
|
||||||
|
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?) {
|
fun exportSubscriptions(uri: Uri?) {
|
||||||
if (uri == null) return
|
if (uri == null) return
|
||||||
|
@ -372,6 +372,7 @@
|
|||||||
<string name="alternative_player_layout_summary">Show the related videos as a row above the comments instead of below.</string>
|
<string name="alternative_player_layout_summary">Show the related videos as a row above the comments instead of below.</string>
|
||||||
<string name="audio_track">Audio track</string>
|
<string name="audio_track">Audio track</string>
|
||||||
<string name="default_audio_track">Default</string>
|
<string name="default_audio_track">Default</string>
|
||||||
|
<string name="unsupported_file_format">Unsupported file format!</string>
|
||||||
|
|
||||||
<!-- Notification channel strings -->
|
<!-- Notification channel strings -->
|
||||||
<string name="download_channel_name">Download Service</string>
|
<string name="download_channel_name">Download Service</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user