Remove coroutine when deleting track, just do it synchronously.
My coroutine solution worked for bringing the onboarding back, but it broke the cancel button since it touched a view out of the owning thread. I probably could fix that by adding a coroutine for cancel too, but the delete operation should be so fast that synchronous might not even be a problem.
This commit is contained in:
parent
75a0a3ae40
commit
8a72bb2b2e
2 changed files with 34 additions and 43 deletions
|
@ -105,26 +105,25 @@ class TracklistFragment : Fragment(), TracklistAdapter.TracklistAdapterListener,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Overrides onYesNoDialog from YesNoDialogListener */
|
/* Overrides onYesNoDialog from YesNoDialogListener */
|
||||||
override fun onYesNoDialog(type: Int, dialogResult: Boolean, payload: Int, payloadString: String) {
|
override fun onYesNoDialog(type: Int, dialogResult: Boolean, payload: Int, payloadString: String)
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
{
|
||||||
when (type) {
|
when (type)
|
||||||
Keys.DIALOG_DELETE_TRACK -> {
|
{
|
||||||
when (dialogResult) {
|
Keys.DIALOG_DELETE_TRACK ->
|
||||||
// user tapped remove track
|
{
|
||||||
true -> {
|
when (dialogResult) {
|
||||||
toggleOnboardingLayout()
|
// user tapped remove track
|
||||||
val deferred: Deferred<Unit> = async { tracklistAdapter.delete_track_at_position_suspended(activity as Context, payload) }
|
true ->
|
||||||
// wait for result and store in tracklist
|
{
|
||||||
withContext(Main) {
|
tracklistAdapter.delete_track_at_position(activity as Context, payload)
|
||||||
deferred.await()
|
toggleOnboardingLayout()
|
||||||
toggleOnboardingLayout()
|
}
|
||||||
}
|
// user tapped cancel
|
||||||
|
false ->
|
||||||
}
|
{
|
||||||
// user tapped cancel
|
// The user slid the track over to the side and turned it red, we have to
|
||||||
false -> {
|
// bring it back.
|
||||||
tracklistAdapter.notifyItemChanged(payload)
|
tracklistAdapter.notifyItemChanged(payload)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,9 +133,7 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
||||||
toggleStarred(it, positionInTracklist)
|
toggleStarred(it, positionInTracklist)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -148,35 +146,29 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
||||||
|
|
||||||
fun delete_track_at_position(context: Context, ui_index: Int)
|
fun delete_track_at_position(context: Context, ui_index: Int)
|
||||||
{
|
{
|
||||||
CoroutineScope(IO).launch {
|
val track_index = ui_index - 1 // position 0 is the statistics element
|
||||||
val track_index = ui_index - 1 // position 0 is the statistics element
|
val track = tracklist.tracks[track_index]
|
||||||
val track = tracklist.tracks[track_index]
|
track.delete(context)
|
||||||
val deferred: Deferred<Unit> = async { track.delete_suspended(context) }
|
tracklist.tracks.remove(track)
|
||||||
// wait for result and store in tracklist
|
notifyItemChanged(0)
|
||||||
withContext(Main) {
|
notifyItemRemoved(ui_index)
|
||||||
deferred.await()
|
notifyItemRangeChanged(ui_index, tracklist.tracks.size)
|
||||||
tracklist.tracks.remove(track)
|
|
||||||
notifyItemChanged(0)
|
|
||||||
notifyItemRemoved(ui_index)
|
|
||||||
notifyItemRangeChanged(ui_index, tracklist.tracks.size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun delete_track_at_position_suspended(context: Context, position: Int) {
|
suspend fun delete_track_at_position_suspended(context: Context, position: Int)
|
||||||
|
{
|
||||||
return suspendCoroutine { cont ->
|
return suspendCoroutine { cont ->
|
||||||
cont.resume(delete_track_at_position(context, position))
|
cont.resume(delete_track_at_position(context, position))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun delete_track_by_id(context: Context, trackId: Long) {
|
fun delete_track_by_id(context: Context, trackId: Long)
|
||||||
CoroutineScope(IO).launch {
|
{
|
||||||
val index: Int = tracklist.tracks.indexOfFirst {it.id == trackId}
|
val index: Int = tracklist.tracks.indexOfFirst {it.id == trackId}
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
return@launch
|
return
|
||||||
}
|
|
||||||
delete_track_at_position(context, index + 1)
|
|
||||||
}
|
}
|
||||||
|
delete_track_at_position(context, index + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns if the adapter is empty */
|
/* Returns if the adapter is empty */
|
||||||
|
|
Loading…
Reference in a new issue