Use coroutine to show onboarding again after deleting final track.

This commit is contained in:
voussoir 2022-04-03 11:11:31 -07:00
parent d6a14956e1
commit 222422b884
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 31 additions and 13 deletions

View file

@ -31,10 +31,11 @@ import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
import org.y20k.trackbook.core.Tracklist
import org.y20k.trackbook.core.TracklistElement
import org.y20k.trackbook.helpers.FileHelper
import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.TrackHelper
import org.y20k.trackbook.helpers.UiHelper
@ -109,13 +110,20 @@ class TracklistFragment : Fragment(), TracklistAdapter.TracklistAdapterListener,
/* Overrides onYesNoDialog from YesNoDialogListener */
override fun onYesNoDialog(type: Int, dialogResult: Boolean, payload: Int, payloadString: String) {
CoroutineScope(Dispatchers.IO).launch {
when (type) {
Keys.DIALOG_DELETE_TRACK -> {
when (dialogResult) {
// user tapped remove track
true -> {
toggleOnboardingLayout()
tracklistAdapter.removeTrackAtPosition(activity as Context, payload)
val deferred: Deferred<Unit> = async { tracklistAdapter.removeTrackAtPositionSuspended(activity as Context, payload) }
// wait for result and store in tracklist
withContext(Main) {
deferred.await()
toggleOnboardingLayout()
}
}
// user tapped cancel
false -> {
@ -125,6 +133,7 @@ class TracklistFragment : Fragment(), TracklistAdapter.TracklistAdapterListener,
}
}
}
}
// toggle onboarding layout

View file

@ -31,6 +31,8 @@ import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
@ -164,6 +166,13 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
}
}
/* Suspend function: Wrapper for removeTrackAtPosition */
suspend fun removeTrackAtPositionSuspended(context: Context, position: Int) {
return suspendCoroutine { cont ->
cont.resume(removeTrackAtPosition(context, position))
}
}
/* Removes track and track files for given track id - used by TracklistFragment */
fun removeTrackById(context: Context, trackId: Long) {
CoroutineScope(IO).launch {