Improved user feedback on subscription import

This commit is contained in:
Bnyro 2022-11-16 16:54:18 +01:00
parent eb194f110d
commit cbbbf65428
2 changed files with 45 additions and 35 deletions

View File

@ -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,14 +18,36 @@ 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
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" -> { "application/json" -> {
// NewPipe subscriptions format // NewPipe subscriptions format
val mapper = ObjectMapper() val mapper = ObjectMapper()
@ -49,24 +72,10 @@ class ImportHelper(private val activity: Activity) {
} }
else -> throw IllegalArgumentException("Unsupported file type") 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?) { fun exportSubscriptions(uri: Uri?) {
if (uri == null) return if (uri == null) return

View File

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