swipe to remove from playlist

This commit is contained in:
Bnyro 2022-08-02 11:57:30 +02:00
parent e0bd3cc30a
commit 5b798266da
2 changed files with 48 additions and 30 deletions

View File

@ -68,44 +68,31 @@ class PlaylistAdapter(
if (isOwner) {
deletePlaylist.visibility = View.VISIBLE
deletePlaylist.setOnClickListener {
val token = PreferenceHelper.getToken()
removeFromPlaylist(token, position)
removeFromPlaylist(position)
}
}
watchProgress.setWatchProgressLength(videoId, streamItem.duration!!)
}
}
private fun removeFromPlaylist(token: String, position: Int) {
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
RetrofitInstance.authApi.removeFromPlaylist(
token,
PlaylistId(playlistId = playlistId, index = position)
)
} 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
} finally {
}
try {
if (response.message == "ok") {
Log.d(TAG, "deleted!")
videoFeed.removeAt(position)
// FIXME: This needs to run on UI thread?
activity.runOnUiThread { notifyDataSetChanged() }
}
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
fun removeFromPlaylist(position: Int) {
videoFeed.removeAt(position)
activity.runOnUiThread { notifyDataSetChanged() }
CoroutineScope(Dispatchers.IO).launch {
try {
RetrofitInstance.authApi.removeFromPlaylist(
PreferenceHelper.getToken(),
PlaylistId(playlistId = playlistId, index = position)
)
} 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
}
}
run()
}
}

View File

@ -7,7 +7,9 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.adapters.PlaylistAdapter
import com.github.libretube.databinding.FragmentPlaylistBinding
@ -112,6 +114,35 @@ class PlaylistFragment : Fragment() {
// scroll view is not at bottom
}
}
/**
* listener for swiping to the left or right
*/
if (isOwner) {
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.RIGHT
) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(
viewHolder: RecyclerView.ViewHolder,
direction: Int
) {
val position = viewHolder.absoluteAdapterPosition
playlistAdapter!!.removeFromPlaylist(position)
}
}
val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
itemTouchHelper.attachToRecyclerView(binding.playlistRecView)
}
}
}
}