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.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main 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.core.TracklistElement
import org.y20k.trackbook.helpers.FileHelper
import org.y20k.trackbook.helpers.LogHelper import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.TrackHelper import org.y20k.trackbook.helpers.TrackHelper
import org.y20k.trackbook.helpers.UiHelper import org.y20k.trackbook.helpers.UiHelper
@ -109,17 +110,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) {
when (type) { CoroutineScope(Dispatchers.IO).launch {
Keys.DIALOG_DELETE_TRACK -> { when (type) {
when (dialogResult) { Keys.DIALOG_DELETE_TRACK -> {
// user tapped remove track when (dialogResult) {
true -> { // user tapped remove track
toggleOnboardingLayout() true -> {
tracklistAdapter.removeTrackAtPosition(activity as Context, payload) toggleOnboardingLayout()
} val deferred: Deferred<Unit> = async { tracklistAdapter.removeTrackAtPositionSuspended(activity as Context, payload) }
// user tapped cancel // wait for result and store in tracklist
false -> { withContext(Main) {
tracklistAdapter.notifyItemChanged(payload) deferred.await()
toggleOnboardingLayout()
}
}
// user tapped cancel
false -> {
tracklistAdapter.notifyItemChanged(payload)
}
} }
} }
} }

View file

@ -31,6 +31,8 @@ import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import java.util.* import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main 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 */ /* Removes track and track files for given track id - used by TracklistFragment */
fun removeTrackById(context: Context, trackId: Long) { fun removeTrackById(context: Context, trackId: Long) {
CoroutineScope(IO).launch { CoroutineScope(IO).launch {