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) { if (isOwner) {
deletePlaylist.visibility = View.VISIBLE deletePlaylist.visibility = View.VISIBLE
deletePlaylist.setOnClickListener { deletePlaylist.setOnClickListener {
val token = PreferenceHelper.getToken() removeFromPlaylist(position)
removeFromPlaylist(token, position)
} }
} }
watchProgress.setWatchProgressLength(videoId, streamItem.duration!!) watchProgress.setWatchProgressLength(videoId, streamItem.duration!!)
} }
} }
private fun removeFromPlaylist(token: String, position: Int) { fun removeFromPlaylist(position: Int) {
fun run() { videoFeed.removeAt(position)
CoroutineScope(Dispatchers.IO).launch { activity.runOnUiThread { notifyDataSetChanged() }
val response = try { CoroutineScope(Dispatchers.IO).launch {
RetrofitInstance.authApi.removeFromPlaylist( try {
token, RetrofitInstance.authApi.removeFromPlaylist(
PlaylistId(playlistId = playlistId, index = position) PreferenceHelper.getToken(),
) PlaylistId(playlistId = playlistId, index = position)
} catch (e: IOException) { )
println(e) } catch (e: IOException) {
Log.e(TAG, "IOException, you might not have internet connection") println(e)
return@launch Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) { return@launch
Log.e(TAG, "HttpException, unexpected response") } catch (e: HttpException) {
return@launch Log.e(TAG, "HttpException, unexpected response")
} finally { return@launch
}
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())
}
} }
} }
run()
} }
} }

View File

@ -7,7 +7,9 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.PlaylistAdapter import com.github.libretube.adapters.PlaylistAdapter
import com.github.libretube.databinding.FragmentPlaylistBinding import com.github.libretube.databinding.FragmentPlaylistBinding
@ -112,6 +114,35 @@ class PlaylistFragment : Fragment() {
// scroll view is not at bottom // 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)
}
} }
} }
} }