mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
add to playlist in the background
This commit is contained in:
parent
dc261478c6
commit
029dd03228
@ -1,7 +1,10 @@
|
|||||||
package com.github.libretube.ui.dialogs
|
package com.github.libretube.ui.dialogs
|
||||||
|
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
|
import android.content.Context
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
@ -11,6 +14,7 @@ import androidx.fragment.app.activityViewModels
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.PlaylistId
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.databinding.DialogAddtoplaylistBinding
|
import com.github.libretube.databinding.DialogAddtoplaylistBinding
|
||||||
import com.github.libretube.extensions.TAG
|
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.PreferenceHelper
|
||||||
import com.github.libretube.util.ThemeHelper
|
import com.github.libretube.util.ThemeHelper
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@ -78,9 +85,8 @@ class AddToPlaylistDialog : DialogFragment() {
|
|||||||
binding.addToPlaylist.setOnClickListener {
|
binding.addToPlaylist.setOnClickListener {
|
||||||
val index = binding.playlistsSpinner.selectedItemPosition
|
val index = binding.playlistsSpinner.selectedItemPosition
|
||||||
viewModel.lastSelectedPlaylistId = response[index].id!!
|
viewModel.lastSelectedPlaylistId = response[index].id!!
|
||||||
addToPlaylist(
|
addToPlaylist(response[index].id!!)
|
||||||
response[index].id!!
|
dialog?.dismiss()
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,32 +94,38 @@ class AddToPlaylistDialog : DialogFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun addToPlaylist(playlistId: String) {
|
private fun addToPlaylist(playlistId: String) {
|
||||||
fun run() {
|
val appContext = context?.applicationContext ?: return
|
||||||
lifecycleScope.launchWhenCreated {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val response = try {
|
val response = try {
|
||||||
RetrofitInstance.authApi.addToPlaylist(
|
RetrofitInstance.authApi.addToPlaylist(
|
||||||
token,
|
token,
|
||||||
com.github.libretube.api.obj.PlaylistId(playlistId, videoId)
|
PlaylistId(playlistId, videoId)
|
||||||
)
|
)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
println(e)
|
println(e)
|
||||||
Log.e(TAG(), "IOException, you might not have internet connection")
|
Log.e(TAG(), "IOException, you might not have internet connection")
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
toastFromMainThread(appContext, R.string.unknown_error)
|
||||||
return@launchWhenCreated
|
return@launch
|
||||||
} catch (e: HttpException) {
|
} catch (e: HttpException) {
|
||||||
Log.e(TAG(), "HttpException, unexpected response")
|
Log.e(TAG(), "HttpException, unexpected response")
|
||||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
toastFromMainThread(appContext, R.string.server_error)
|
||||||
return@launchWhenCreated
|
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) {
|
private fun Fragment?.runOnUiThread(action: () -> Unit) {
|
||||||
|
@ -6,6 +6,7 @@ import android.util.Log
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.PlaylistId
|
||||||
import com.github.libretube.constants.ShareObjectType
|
import com.github.libretube.constants.ShareObjectType
|
||||||
import com.github.libretube.databinding.DialogTextPreferenceBinding
|
import com.github.libretube.databinding.DialogTextPreferenceBinding
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
@ -116,7 +117,7 @@ class PlaylistOptionsBottomSheet(
|
|||||||
val response = try {
|
val response = try {
|
||||||
RetrofitInstance.authApi.importPlaylist(
|
RetrofitInstance.authApi.importPlaylist(
|
||||||
token,
|
token,
|
||||||
com.github.libretube.api.obj.PlaylistId(playlistId)
|
PlaylistId(playlistId)
|
||||||
)
|
)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
println(e)
|
println(e)
|
||||||
@ -133,7 +134,7 @@ class PlaylistOptionsBottomSheet(
|
|||||||
try {
|
try {
|
||||||
RetrofitInstance.authApi.renamePlaylist(
|
RetrofitInstance.authApi.renamePlaylist(
|
||||||
PreferenceHelper.getToken(),
|
PreferenceHelper.getToken(),
|
||||||
com.github.libretube.api.obj.PlaylistId(
|
PlaylistId(
|
||||||
playlistId = id,
|
playlistId = id,
|
||||||
newName = newName
|
newName = newName
|
||||||
)
|
)
|
||||||
@ -149,7 +150,7 @@ class PlaylistOptionsBottomSheet(
|
|||||||
try {
|
try {
|
||||||
RetrofitInstance.authApi.deletePlaylist(
|
RetrofitInstance.authApi.deletePlaylist(
|
||||||
PreferenceHelper.getToken(),
|
PreferenceHelper.getToken(),
|
||||||
com.github.libretube.api.obj.PlaylistId(id)
|
PlaylistId(id)
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
return@launch
|
return@launch
|
||||||
|
@ -347,6 +347,7 @@
|
|||||||
<string name="recentlyUpdatedReversed">Recently updated (reversed)</string>
|
<string name="recentlyUpdatedReversed">Recently updated (reversed)</string>
|
||||||
<string name="show_more">Show more</string>
|
<string name="show_more">Show more</string>
|
||||||
<string name="time_code">Time code</string>
|
<string name="time_code">Time code</string>
|
||||||
|
<string name="added_to_playlist">Added to playlist</string>
|
||||||
|
|
||||||
<!-- Notification channel strings -->
|
<!-- Notification channel strings -->
|
||||||
<string name="download_channel_name">Download Service</string>
|
<string name="download_channel_name">Download Service</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user