Merge pull request #1917 from Bnyro/master

add confirmation for playlist deletion
This commit is contained in:
Bnyro 2022-11-19 16:03:41 +01:00 committed by GitHub
commit 25390209ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 87 deletions

View File

@ -11,23 +11,10 @@
"type": "UNIVERSAL", "type": "UNIVERSAL",
"filters": [], "filters": [],
"attributes": [], "attributes": [],
"versionCode": 22, "versionCode": 23,
"versionName": "0.7.1", "versionName": "0.8.0",
"outputFile": "app-universal-release.apk" "outputFile": "app-universal-release.apk"
}, },
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86_64"
}
],
"attributes": [],
"versionCode": 22,
"versionName": "0.7.1",
"outputFile": "app-x86_64-release.apk"
},
{ {
"type": "ONE_OF_MANY", "type": "ONE_OF_MANY",
"filters": [ "filters": [
@ -37,8 +24,8 @@
} }
], ],
"attributes": [], "attributes": [],
"versionCode": 22, "versionCode": 23,
"versionName": "0.7.1", "versionName": "0.8.0",
"outputFile": "app-x86-release.apk" "outputFile": "app-x86-release.apk"
}, },
{ {
@ -50,10 +37,23 @@
} }
], ],
"attributes": [], "attributes": [],
"versionCode": 22, "versionCode": 23,
"versionName": "0.7.1", "versionName": "0.8.0",
"outputFile": "app-arm64-v8a-release.apk" "outputFile": "app-arm64-v8a-release.apk"
}, },
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86_64"
}
],
"attributes": [],
"versionCode": 23,
"versionName": "0.8.0",
"outputFile": "app-x86_64-release.apk"
},
{ {
"type": "ONE_OF_MANY", "type": "ONE_OF_MANY",
"filters": [ "filters": [
@ -63,8 +63,8 @@
} }
], ],
"attributes": [], "attributes": [],
"versionCode": 22, "versionCode": 23,
"versionName": "0.7.1", "versionName": "0.8.0",
"outputFile": "app-armeabi-v7a-release.apk" "outputFile": "app-armeabi-v7a-release.apk"
} }
], ],

View File

@ -1,28 +1,17 @@
package com.github.libretube.ui.adapters package com.github.libretube.ui.adapters
import android.app.Activity
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.Playlists
import com.github.libretube.databinding.PlaylistsRowBinding import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.viewholders.PlaylistsViewHolder import com.github.libretube.ui.viewholders.PlaylistsViewHolder
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.PreferenceHelper
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
class PlaylistsAdapter( class PlaylistsAdapter(
private val playlists: MutableList<Playlists> private val playlists: MutableList<Playlists>
@ -59,15 +48,16 @@ class PlaylistsAdapter(
videoCount.text = playlist.videos.toString() videoCount.text = playlist.videos.toString()
deletePlaylist.setOnClickListener { deletePlaylist.setOnClickListener {
val builder = MaterialAlertDialogBuilder(root.context) DeletePlaylistDialog(playlist.id!!) {
builder.setTitle(R.string.deletePlaylist) playlists.removeAt(position)
builder.setMessage(R.string.areYouSure) (root.context as BaseActivity).runOnUiThread {
builder.setPositiveButton(R.string.yes) { _, _ -> notifyItemRemoved(position)
PreferenceHelper.getToken() notifyItemRangeChanged(position, itemCount)
deletePlaylist(root.context as Activity, playlist.id!!, position)
} }
builder.setNegativeButton(R.string.cancel, null) }.show(
builder.show() (root.context as BaseActivity).supportFragmentManager,
null
)
} }
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigatePlaylist(root.context, playlist.id, true) NavigationHelper.navigatePlaylist(root.context, playlist.id, true)
@ -87,33 +77,4 @@ class PlaylistsAdapter(
} }
} }
} }
private fun deletePlaylist(activity: Activity, id: String, position: Int) {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(id)
)
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launch
}
try {
if (response.message == "ok") {
playlists.removeAt(position)
activity.runOnUiThread {
notifyItemRemoved(position)
notifyItemRangeChanged(position, itemCount)
}
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
}
}
} }

View File

@ -0,0 +1,53 @@
package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.extensions.TAG
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class DeletePlaylistDialog(
private val playlistId: String,
private val onSuccess: () -> Unit = {}
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.deletePlaylist)
.setMessage(R.string.areYouSure)
.setPositiveButton(R.string.yes) { _, _ ->
PreferenceHelper.getToken()
deletePlaylist()
}
.setNegativeButton(R.string.cancel, null)
.show()
}
private fun deletePlaylist() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(playlistId)
)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@launch
}
try {
if (response.message == "ok") {
onSuccess.invoke()
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
}
}
}

View File

@ -11,6 +11,7 @@ import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -81,9 +82,8 @@ class PlaylistOptionsBottomSheet(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
} }
context?.getString(R.string.deletePlaylist) -> { context?.getString(R.string.deletePlaylist) -> {
deletePlaylist( DeletePlaylistDialog(playlistId)
playlistId .show(parentFragmentManager, null)
)
} }
context?.getString(R.string.renamePlaylist) -> { context?.getString(R.string.renamePlaylist) -> {
val binding = DialogTextPreferenceBinding.inflate(layoutInflater) val binding = DialogTextPreferenceBinding.inflate(layoutInflater)
@ -145,17 +145,4 @@ class PlaylistOptionsBottomSheet(
} }
} }
} }
private fun deletePlaylist(id: String) {
CoroutineScope(Dispatchers.IO).launch {
try {
RetrofitInstance.authApi.deletePlaylist(
PreferenceHelper.getToken(),
PlaylistId(id)
)
} catch (e: Exception) {
return@launch
}
}
}
} }