fixes a crash when trying to delete a freshly recorded track

This commit is contained in:
y20k 2021-06-28 18:47:00 +02:00
parent 36b891152c
commit c81105186f
No known key found for this signature in database
GPG key ID: 824D4259F41FAFF6
3 changed files with 26 additions and 8 deletions

View file

@ -152,8 +152,7 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
// user tapped remove track // user tapped remove track
true -> { true -> {
// switch to TracklistFragment and remove track there // switch to TracklistFragment and remove track there
val trackId: Long = arguments?.getLong(Keys.ARG_TRACK_ID, -1L) ?: -1L val bundle: Bundle = bundleOf(Keys.ARG_TRACK_ID to track.getTrackId())
val bundle: Bundle = bundleOf(Keys.ARG_TRACK_ID to trackId)
findNavController().navigate(R.id.tracklist_fragment, bundle) findNavController().navigate(R.id.tracklist_fragment, bundle)
} }
} }

View file

@ -31,6 +31,9 @@ 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.Dispatchers.Main
import kotlinx.coroutines.launch
import org.y20k.trackbook.core.TracklistElement import org.y20k.trackbook.core.TracklistElement
import org.y20k.trackbook.helpers.LogHelper import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.TrackHelper import org.y20k.trackbook.helpers.TrackHelper
@ -116,7 +119,7 @@ class TracklistFragment : Fragment(), TracklistAdapter.TracklistAdapterListener,
// user tapped remove track // user tapped remove track
true -> { true -> {
toggleOnboardingLayout(tracklistAdapter.itemCount -1) toggleOnboardingLayout(tracklistAdapter.itemCount -1)
tracklistAdapter.removeTrack(activity as Context, payload) tracklistAdapter.removeTrackAtPosition(activity as Context, payload)
} }
// user tapped cancel // user tapped cancel
false -> { false -> {
@ -161,9 +164,10 @@ class TracklistFragment : Fragment(), TracklistAdapter.TracklistAdapterListener,
val deleteTrackId: Long = arguments?.getLong(Keys.ARG_TRACK_ID, -1L) ?: -1L val deleteTrackId: Long = arguments?.getLong(Keys.ARG_TRACK_ID, -1L) ?: -1L
arguments?.putLong(Keys.ARG_TRACK_ID, -1L) arguments?.putLong(Keys.ARG_TRACK_ID, -1L)
if (deleteTrackId != -1L) { if (deleteTrackId != -1L) {
val position: Int = tracklistAdapter.findPosition(deleteTrackId) CoroutineScope(Main). launch {
tracklistAdapter.removeTrack(this@TracklistFragment.activity as Context, position) tracklistAdapter.removeTrackById(this@TracklistFragment.activity as Context, deleteTrackId)
toggleOnboardingLayout(tracklistAdapter.itemCount -1) toggleOnboardingLayout(tracklistAdapter.itemCount - 1)
}
} }
} }

View file

@ -114,7 +114,7 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
/* Removes track and track files for given position - used by TracklistFragment */ /* Removes track and track files for given position - used by TracklistFragment */
fun removeTrack(context: Context, position: Int) { fun removeTrackAtPosition(context: Context, position: Int) {
CoroutineScope(IO).launch { CoroutineScope(IO).launch {
val deferred: Deferred<Tracklist> = async { FileHelper.deleteTrackSuspended(context, position, tracklist) } val deferred: Deferred<Tracklist> = async { FileHelper.deleteTrackSuspended(context, position, tracklist) }
// wait for result and store in tracklist // wait for result and store in tracklist
@ -125,8 +125,23 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
} }
/* Removes track and track files for given track id - used by TracklistFragment */
fun removeTrackById(context: Context, trackId: Long) {
CoroutineScope(IO).launch {
// reload tracklist //todo check if necessary
// tracklist = FileHelper.readTracklist(context)
val position: Int = findPosition(trackId)
val deferred: Deferred<Tracklist> = async { FileHelper.deleteTrackSuspended(context, position, tracklist) }
// wait for result and store in tracklist
withContext(Main) {
tracklist = deferred.await()
notifyItemRemoved(position) }
}
}
/* Finds current position of track element in adapter list */ /* Finds current position of track element in adapter list */
fun findPosition(trackId: Long): Int { private fun findPosition(trackId: Long): Int {
tracklist.tracklistElements.forEachIndexed {index, tracklistElement -> tracklist.tracklistElements.forEachIndexed {index, tracklistElement ->
if (tracklistElement.getTrackId() == trackId) return index if (tracklistElement.getTrackId() == trackId) return index
} }