Skip to content

Commit

Permalink
Added delete functionality and basic filters to trip history
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Aug 20, 2024
1 parent 6ef2f61 commit 6ec2de9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ package com.ixam97.carStatsViewer.carApp
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
import androidx.car.app.model.Header
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.SectionedItemList
import androidx.car.app.model.Template
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.carApp.utils.autoTripIcon
import com.ixam97.carStatsViewer.carApp.utils.carIconFromRes
import com.ixam97.carStatsViewer.carApp.utils.manualTripIcon
import com.ixam97.carStatsViewer.carApp.utils.monthTripIcon
import com.ixam97.carStatsViewer.carApp.utils.sinceChargeTripIcon
import com.ixam97.carStatsViewer.database.tripData.DrivingSession
import com.ixam97.carStatsViewer.map.Mapbox
import com.ixam97.carStatsViewer.utils.StringFormatters
Expand Down Expand Up @@ -87,18 +80,20 @@ class TripDetailsScreen(carContext: CarContext, private val pTrip: DrivingSessio
invalidate()
}
}.build())
addEndHeaderAction(Action.Builder().apply {
setIcon(carContext.carIconFromRes(R.drawable.ic_delete))
setOnClickListener {
screenManager.pushForResult(ConfirmDeleteTripScreen(carContext)) { result ->
if (result == true) {
// Delete selected Trip
setResult("DeleteTrip")
screenManager.pop()
if ((pTrip.end_epoch_time?:0) > 0) {
addEndHeaderAction(Action.Builder().apply {
setIcon(carContext.carIconFromRes(R.drawable.ic_delete))
setOnClickListener {
screenManager.pushForResult(ConfirmDeleteTripScreen(carContext)) { result ->
if (result == true) {
// Delete selected Trip
setResult("DeleteTrip")
screenManager.pop()
}
}
}
}
}.build())
}.build())
}
setStartHeaderAction(Action.BACK)
}.build())
trip?.let { trip ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.ixam97.carStatsViewer.carApp

import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.Header
import androidx.car.app.model.Pane
import androidx.car.app.model.PaneTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.Template
import androidx.car.app.model.Toggle
import androidx.lifecycle.DefaultLifecycleObserver
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R

class TripHistoryFilterScreen(carContext: CarContext): Screen(carContext), DefaultLifecycleObserver {

private var showManualTrips: Boolean = CarStatsViewer.appPreferences.tripFilterManual
private var showSinceChargeTrips: Boolean = CarStatsViewer.appPreferences.tripFilterCharge
private var showAutomaticTrips: Boolean = CarStatsViewer.appPreferences.tripFilterAuto
private var showMonthlyTrips: Boolean = CarStatsViewer.appPreferences.tripFilterMonth

init {
lifecycle.addObserver(this)
}

override fun onGetTemplate(): Template {
return tripHistoryFilterTemplate()
}

private fun tripHistoryFilterTemplate() = PaneTemplate.Builder(Pane.Builder().apply {
addRow(Row.Builder().apply {
setToggle(Toggle.Builder {
showManualTrips = it
}.setChecked(showManualTrips).build())
setTitle(carContext.getString(R.string.history_dialog_filters_manual))
}.build())
addRow(Row.Builder().apply {
setToggle(Toggle.Builder {
showSinceChargeTrips = it
}.setChecked(showSinceChargeTrips).build())
setTitle(carContext.getString(R.string.history_dialog_filters_charge))
}.build())
addRow(Row.Builder().apply {
setToggle(Toggle.Builder {
showAutomaticTrips = it
}.setChecked(showAutomaticTrips).build())
setTitle(carContext.getString(R.string.history_dialog_filters_auto))
}.build())
addRow(Row.Builder().apply {
setToggle(Toggle.Builder {
showMonthlyTrips = it
}.setChecked(showMonthlyTrips).build())
setTitle(carContext.getString(R.string.history_dialog_filters_month))
}.build())
addAction(Action.Builder().apply {
setTitle(carContext.getString(R.string.dialog_apply))
setFlags(Action.FLAG_PRIMARY)
setOnClickListener {
CarStatsViewer.appPreferences.tripFilterManual = showManualTrips
CarStatsViewer.appPreferences.tripFilterCharge = showSinceChargeTrips
CarStatsViewer.appPreferences.tripFilterAuto = showAutomaticTrips
CarStatsViewer.appPreferences.tripFilterMonth = showMonthlyTrips
screenManager.pop()
}
}.build())
}.build()).apply {
setHeader(Header.Builder().apply {
setTitle(carContext.getString(R.string.history_dialog_filters_title))
setStartHeaderAction(Action.BACK)
}.build())
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.ixam97.carStatsViewer.carApp.utils.manualTripIcon
import com.ixam97.carStatsViewer.carApp.utils.monthTripIcon
import com.ixam97.carStatsViewer.carApp.utils.sinceChargeTripIcon
import com.ixam97.carStatsViewer.database.tripData.DrivingSession
import com.ixam97.carStatsViewer.database.tripData.TripType
import com.ixam97.carStatsViewer.utils.StringFormatters
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -33,6 +34,10 @@ import java.util.Date
class TripHistoryScreen(carContext: CarContext):
Screen(carContext), DefaultLifecycleObserver
{
private var showManualTrips: Boolean = CarStatsViewer.appPreferences.tripFilterManual
private var showSinceChargeTrips: Boolean = CarStatsViewer.appPreferences.tripFilterCharge
private var showAutomaticTrips: Boolean = CarStatsViewer.appPreferences.tripFilterAuto
private var showMonthlyTrips: Boolean = CarStatsViewer.appPreferences.tripFilterMonth

private val maxListLength = carContext.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_LIST)
private val screenManager = carContext.getCarService(ScreenManager::class.java)
Expand All @@ -51,19 +56,8 @@ class TripHistoryScreen(carContext: CarContext):
}

override fun onCreate(owner: LifecycleOwner) {
loadTripsFromDataSource()
super.onCreate(owner)
lifecycleScope.launch(Dispatchers.IO) {
activeDrivingSessions.addAll(CarStatsViewer.tripDataSource.getActiveDrivingSessions())
activeDrivingSessions.sortBy { it.session_type }

pastDrivingSessions.addAll(CarStatsViewer.tripDataSource.getPastDrivingSessions())
pastDrivingSessions.sortByDescending { it.driving_session_id }

contentLoaded = true
withContext(Dispatchers.Main) {
invalidate()
}
}
}

private fun TripHistoryListTemplate() = ListTemplate.Builder().apply {
Expand All @@ -77,7 +71,9 @@ class TripHistoryScreen(carContext: CarContext):
addEndHeaderAction(Action.Builder().apply {
setIcon(carContext.carIconFromRes(R.drawable.ic_filter))
setOnClickListener {

screenManager.pushForResult(TripHistoryFilterScreen(carContext)) { result ->
loadTripsFromDataSource()
}
}
setEnabled(false)
}.build())
Expand Down Expand Up @@ -132,7 +128,10 @@ class TripHistoryScreen(carContext: CarContext):
addText(tripDataString(trip))
setOnClickListener {
screenManager.pushForResult(TripDetailsScreen(carContext, trip)) { result ->
if (result == "DeleteTrip") {
if (result == "DeleteTrip" && (trip.end_epoch_time?:0) > 0) {
lifecycleScope.launch(Dispatchers.IO) {
CarStatsViewer.tripDataSource.deleteDrivingSessionById(trip.driving_session_id)
}
pastDrivingSessions.remove(trip)
invalidate()
}
Expand All @@ -143,8 +142,11 @@ class TripHistoryScreen(carContext: CarContext):
setIcon(carContext.carIconFromRes(R.drawable.ic_delete))
setOnClickListener {
screenManager.pushForResult(ConfirmDeleteTripScreen(carContext)) { result ->
if (result == true) {
if (result == true && (trip.end_epoch_time?:0) > 0) {
// delete corresponding trip and reload list.
lifecycleScope.launch(Dispatchers.IO) {
CarStatsViewer.tripDataSource.deleteDrivingSessionById(trip.driving_session_id)
}
pastDrivingSessions.remove(trip)
invalidate()
}
Expand All @@ -165,4 +167,40 @@ class TripHistoryScreen(carContext: CarContext):
return "$distance, $energy, $consumption, $time"
}

private fun loadTripsFromDataSource() {
lifecycleScope.launch(Dispatchers.IO) {
contentLoaded = false
invalidate()
showManualTrips = CarStatsViewer.appPreferences.tripFilterManual
showSinceChargeTrips = CarStatsViewer.appPreferences.tripFilterCharge
showAutomaticTrips = CarStatsViewer.appPreferences.tripFilterAuto
showMonthlyTrips = CarStatsViewer.appPreferences.tripFilterMonth

activeDrivingSessions.clear()
activeDrivingSessions.addAll(CarStatsViewer.tripDataSource.getActiveDrivingSessions())
activeDrivingSessions.sortBy { it.session_type }

pastDrivingSessions.clear()
pastDrivingSessions.addAll(CarStatsViewer.tripDataSource.getPastDrivingSessions())
pastDrivingSessions.sortByDescending { it.driving_session_id }

pastDrivingSessions.apply {
clear()
val trips = CarStatsViewer.tripDataSource.getPastDrivingSessions()
addAll(trips.subList(0, (trips.size - 1).coerceAtMost(49)))
sortByDescending { it.driving_session_id }

if (!showManualTrips) { removeIf { it.session_type == TripType.MANUAL } }
if (!showSinceChargeTrips) { removeIf { it.session_type == TripType.SINCE_CHARGE } }
if (!showAutomaticTrips) { removeIf { it.session_type == TripType.AUTO } }
if (!showMonthlyTrips) { removeIf { it.session_type == TripType.MONTH } }
}

contentLoaded = true
withContext(Dispatchers.Main) {
invalidate()
}
}
}

}

0 comments on commit 6ec2de9

Please sign in to comment.