mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Merge pull request #1868 from Bnyro/master
Improved user feedback on subscription import
This commit is contained in:
commit
068214df85
@ -9,6 +9,7 @@ import com.github.libretube.R
|
||||
import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.SubscriptionHelper
|
||||
import com.github.libretube.extensions.TAG
|
||||
import com.github.libretube.extensions.toastFromMainThread
|
||||
import com.github.libretube.obj.NewPipeSubscription
|
||||
import com.github.libretube.obj.NewPipeSubscriptions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@ -17,14 +18,36 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.FileOutputStream
|
||||
|
||||
class ImportHelper(private val activity: Activity) {
|
||||
class ImportHelper(
|
||||
private val activity: Activity
|
||||
) {
|
||||
/**
|
||||
* Import subscriptions by a file uri
|
||||
*/
|
||||
fun importSubscriptions(uri: Uri?) {
|
||||
if (uri == null) return
|
||||
try {
|
||||
val channels = when (activity.contentResolver.getType(uri)) {
|
||||
val applicationContext = activity.applicationContext
|
||||
val channels = getChannelsFromUri(uri)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
SubscriptionHelper.importSubscriptions(channels)
|
||||
}.invokeOnCompletion {
|
||||
applicationContext.toastFromMainThread(R.string.importsuccess)
|
||||
}
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Log.e(TAG(), e.toString())
|
||||
Toast.makeText(activity, R.string.unsupported_file_format, Toast.LENGTH_SHORT).show()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG(), e.toString())
|
||||
Toast.makeText(activity, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
@ -49,24 +72,10 @@ class ImportHelper(private val activity: Activity) {
|
||||
}
|
||||
else -> throw IllegalArgumentException("Unsupported file 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()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* write the text to the document
|
||||
* Write the text to the document
|
||||
*/
|
||||
fun exportSubscriptions(uri: Uri?) {
|
||||
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="audio_track">Audio track</string>
|
||||
<string name="default_audio_track">Default</string>
|
||||
<string name="unsupported_file_format">Unsupported file format!</string>
|
||||
|
||||
<!-- Notification channel strings -->
|
||||
<string name="download_channel_name">Download Service</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user