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.
master
voussoir 2022-04-06 21:50:40 -07:00
parent 75a0a3ae40
commit 8a72bb2b2e
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
2 changed files with 34 additions and 43 deletions

View File

@ -105,26 +105,25 @@ 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()
val deferred: Deferred<Unit> = async { tracklistAdapter.delete_track_at_position_suspended(activity as Context, payload) }
// wait for result and store in tracklist
withContext(Main) {
deferred.await()
toggleOnboardingLayout()
}
}
// user tapped cancel
false -> {
tracklistAdapter.notifyItemChanged(payload)
}
override fun onYesNoDialog(type: Int, dialogResult: Boolean, payload: Int, payloadString: String)
{
when (type)
{
Keys.DIALOG_DELETE_TRACK ->
{
when (dialogResult) {
// user tapped remove track
true ->
{
tracklistAdapter.delete_track_at_position(activity as Context, payload)
toggleOnboardingLayout()
}
// user tapped cancel
false ->
{
// The user slid the track over to the side and turned it red, we have to
// bring it back.
tracklistAdapter.notifyItemChanged(payload)
}
}
}

View File

@ -133,9 +133,7 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
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)
{
CoroutineScope(IO).launch {
val track_index = ui_index - 1 // position 0 is the statistics element
val track = tracklist.tracks[track_index]
val deferred: Deferred<Unit> = async { track.delete_suspended(context) }
// wait for result and store in tracklist
withContext(Main) {
deferred.await()
tracklist.tracks.remove(track)
notifyItemChanged(0)
notifyItemRemoved(ui_index)
notifyItemRangeChanged(ui_index, tracklist.tracks.size)
}
}
val track_index = ui_index - 1 // position 0 is the statistics element
val track = tracklist.tracks[track_index]
track.delete(context)
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 ->
cont.resume(delete_track_at_position(context, position))
}
}
fun delete_track_by_id(context: Context, trackId: Long) {
CoroutineScope(IO).launch {
val index: Int = tracklist.tracks.indexOfFirst {it.id == trackId}
if (index == -1) {
return@launch
}
delete_track_at_position(context, index + 1)
fun delete_track_by_id(context: Context, trackId: Long)
{
val index: Int = tracklist.tracks.indexOfFirst {it.id == trackId}
if (index == -1) {
return
}
delete_track_at_position(context, index + 1)
}
/* Returns if the adapter is empty */