store total distance, duration, etc.
This commit is contained in:
parent
cd621ad368
commit
18a1164578
4 changed files with 46 additions and 5 deletions
|
@ -33,7 +33,11 @@ import java.util.*
|
|||
@Parcelize
|
||||
data class Tracklist (@Expose val tracklistFormatVersion: Int = Keys.CURRENT_TRACKLIST_FORMAT_VERSION,
|
||||
@Expose val tracklistElements: MutableList<TracklistElement> = mutableListOf<TracklistElement>(),
|
||||
@Expose var modificationDate: Date = Date()): Parcelable {
|
||||
@Expose var modificationDate: Date = Date(),
|
||||
@Expose var totalDistanceAll: Float = 0f,
|
||||
@Expose var totalDurationAll: Long = 0L,
|
||||
@Expose var totalRecordingPausedAll: Long = 0L,
|
||||
@Expose var totalStepCountAll: Float = 0f): Parcelable {
|
||||
|
||||
/* Return trackelement for given track id */
|
||||
fun getTrackElement(trackId: Long): TracklistElement? {
|
||||
|
|
|
@ -181,6 +181,10 @@ object FileHelper {
|
|||
return suspendCoroutine { cont ->
|
||||
val tracklist: Tracklist = readTracklist(context)
|
||||
tracklist.tracklistElements.add(track.toTracklistElement(context))
|
||||
tracklist.totalDistanceAll += track.length
|
||||
tracklist.totalDurationAll += track.duration
|
||||
tracklist.totalRecordingPausedAll += track.recordingPaused
|
||||
tracklist.totalStepCountAll += track.stepCount
|
||||
cont.resume(saveTracklist(context, tracklist, modificationDate))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,14 @@ package org.y20k.trackbook.helpers
|
|||
import android.content.Context
|
||||
import android.location.Location
|
||||
import android.widget.Toast
|
||||
import androidx.core.net.toUri
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.launch
|
||||
import org.y20k.trackbook.Keys
|
||||
import org.y20k.trackbook.R
|
||||
import org.y20k.trackbook.core.Track
|
||||
import org.y20k.trackbook.core.Tracklist
|
||||
import org.y20k.trackbook.core.TracklistElement
|
||||
import org.y20k.trackbook.core.WayPoint
|
||||
import java.text.SimpleDateFormat
|
||||
|
@ -248,4 +253,27 @@ object TrackHelper {
|
|||
return track
|
||||
}
|
||||
|
||||
|
||||
/* Calculates total distance, duration and pause */
|
||||
fun calculateAndSaveTrackTotals(context: Context, tracklist: Tracklist) {
|
||||
CoroutineScope(IO).launch {
|
||||
var totalDistanceAll: Float = 0f
|
||||
var totalDurationAll: Long = 0L
|
||||
var totalRecordingPausedAll: Long = 0L
|
||||
var totalStepCountAll: Float = 0f
|
||||
tracklist.tracklistElements.forEach { tracklistElement ->
|
||||
val track: Track = FileHelper.readTrack(context, tracklistElement.trackUriString.toUri())
|
||||
totalDistanceAll += track.length
|
||||
totalDurationAll += track.duration
|
||||
totalRecordingPausedAll += track.recordingPaused
|
||||
totalStepCountAll += track.stepCount
|
||||
}
|
||||
tracklist.totalDistanceAll = totalDistanceAll
|
||||
tracklist.totalDurationAll = totalDurationAll
|
||||
tracklist.totalRecordingPausedAll = totalRecordingPausedAll
|
||||
tracklist.totalStepCountAll = totalStepCountAll
|
||||
FileHelper.saveTracklistSuspended(context, tracklist, GregorianCalendar.getInstance().time)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import android.view.ViewGroup
|
|||
import android.widget.ImageButton
|
||||
import android.widget.TextView
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
@ -66,6 +67,10 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
|||
// load tracklist
|
||||
tracklist = FileHelper.readTracklist(context)
|
||||
tracklist.tracklistElements.sortByDescending { tracklistElement -> tracklistElement.date }
|
||||
// calculate total duration and distance, if necessary
|
||||
if (tracklist.tracklistElements.isNotEmpty() && tracklist.totalDurationAll == 0L) {
|
||||
TrackHelper.calculateAndSaveTrackTotals(context, tracklist)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -88,8 +93,8 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
|||
trackElementViewHolder.trackNameView.text = tracklist.tracklistElements[position].name
|
||||
trackElementViewHolder.trackDataView.text = createTrackDataString(position)
|
||||
when (tracklist.tracklistElements[position].starred) {
|
||||
true -> trackElementViewHolder.starButton.setImageDrawable(context.getDrawable(R.drawable.ic_star_filled_24dp))
|
||||
false -> trackElementViewHolder.starButton.setImageDrawable(context.getDrawable(R.drawable.ic_star_outline_24dp))
|
||||
true -> trackElementViewHolder.starButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_filled_24dp))
|
||||
false -> trackElementViewHolder.starButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_outline_24dp))
|
||||
}
|
||||
trackElementViewHolder.trackElement.setOnClickListener {
|
||||
tracklistListener.onTrackElementTapped(tracklist.tracklistElements[position])
|
||||
|
@ -134,11 +139,11 @@ class TracklistAdapter(private val fragment: Fragment) : RecyclerView.Adapter<Re
|
|||
val starButton: ImageButton = view as ImageButton
|
||||
when (tracklist.tracklistElements[position].starred) {
|
||||
true -> {
|
||||
starButton.setImageDrawable(context.getDrawable(R.drawable.ic_star_outline_24dp))
|
||||
starButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_outline_24dp))
|
||||
tracklist.tracklistElements[position].starred = false
|
||||
}
|
||||
false -> {
|
||||
starButton.setImageDrawable(context.getDrawable(R.drawable.ic_star_filled_24dp))
|
||||
starButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_star_filled_24dp))
|
||||
tracklist.tracklistElements[position].starred = true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue