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,31 +105,30 @@ 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 -> {
|
||||
override fun onYesNoDialog(type: Int, dialogResult: Boolean, payload: Int, payloadString: String)
|
||||
{
|
||||
when (type)
|
||||
{
|
||||
Keys.DIALOG_DELETE_TRACK ->
|
||||
{
|
||||
when (dialogResult) {
|
||||
// user tapped remove track
|
||||
true -> {
|
||||
true ->
|
||||
{
|
||||
tracklistAdapter.delete_track_at_position(activity as Context, payload)
|
||||
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 -> {
|
||||
false ->
|
||||
{
|
||||
// The user slid the track over to the side and turned it red, we have to
|
||||
// bring it back.
|
||||
tracklistAdapter.notifyItemChanged(payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// toggle onboarding layout
|
||||
|
|
|
@ -133,9 +133,7 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
|||
toggleStarred(it, positionInTracklist)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -148,36 +146,30 @@ 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()
|
||||
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 {
|
||||
fun delete_track_by_id(context: Context, trackId: Long)
|
||||
{
|
||||
val index: Int = tracklist.tracks.indexOfFirst {it.id == trackId}
|
||||
if (index == -1) {
|
||||
return@launch
|
||||
return
|
||||
}
|
||||
delete_track_at_position(context, index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns if the adapter is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
|
|
Loading…
Reference in a new issue