diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt index a0a8eded0..74e44b6a6 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -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() } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt index 1ea1cf89e..ea673d5b5 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt @@ -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) + } } } }