Merge pull request #1589 from Bnyro/master

add to playlist in the background
This commit is contained in:
Bnyro 2022-10-18 17:42:59 +02:00 committed by GitHub
commit 115ed98765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 30 deletions

View File

@ -1,7 +1,10 @@
package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Toast
@ -11,6 +14,7 @@ import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogAddtoplaylistBinding
import com.github.libretube.extensions.TAG
@ -18,6 +22,9 @@ import com.github.libretube.models.PlaylistViewModel
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
@ -78,9 +85,8 @@ class AddToPlaylistDialog : DialogFragment() {
binding.addToPlaylist.setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition
viewModel.lastSelectedPlaylistId = response[index].id!!
addToPlaylist(
response[index].id!!
)
addToPlaylist(response[index].id!!)
dialog?.dismiss()
}
}
}
@ -88,32 +94,38 @@ class AddToPlaylistDialog : DialogFragment() {
}
private fun addToPlaylist(playlistId: String) {
fun run() {
lifecycleScope.launchWhenCreated {
val appContext = context?.applicationContext ?: return
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.addToPlaylist(
token,
com.github.libretube.api.obj.PlaylistId(playlistId, videoId)
PlaylistId(playlistId, videoId)
)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
toastFromMainThread(appContext, R.string.unknown_error)
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
toastFromMainThread(appContext, R.string.server_error)
return@launch
}
if (response.message == "ok") {
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
dialog?.dismiss()
} else {
Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show()
toastFromMainThread(
appContext,
if (response.message == "ok") R.string.added_to_playlist else R.string.fail
)
}
}
private fun toastFromMainThread(context: Context, stringId: Int) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
stringId,
Toast.LENGTH_SHORT
).show()
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {

View File

@ -6,6 +6,7 @@ import android.util.Log
import android.widget.Toast
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.constants.ShareObjectType
import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG
@ -116,7 +117,7 @@ class PlaylistOptionsBottomSheet(
val response = try {
RetrofitInstance.authApi.importPlaylist(
token,
com.github.libretube.api.obj.PlaylistId(playlistId)
PlaylistId(playlistId)
)
} catch (e: IOException) {
println(e)
@ -133,7 +134,7 @@ class PlaylistOptionsBottomSheet(
try {
RetrofitInstance.authApi.renamePlaylist(
PreferenceHelper.getToken(),
com.github.libretube.api.obj.PlaylistId(
PlaylistId(
playlistId = id,
newName = newName
)
@ -149,7 +150,7 @@ class PlaylistOptionsBottomSheet(
try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
com.github.libretube.api.obj.PlaylistId(id)
PlaylistId(id)
)
} catch (e: Exception) {
return@launch

View File

@ -347,6 +347,7 @@
<string name="recentlyUpdatedReversed">Recently updated (reversed)</string>
<string name="show_more">Show more</string>
<string name="time_code">Time code</string>
<string name="added_to_playlist">Added to playlist</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>