From 6ec2de965724142cb0c56f6114d30c0420f8b280 Mon Sep 17 00:00:00 2001 From: Maximilian Goldschmidt Date: Tue, 20 Aug 2024 20:42:30 +0200 Subject: [PATCH] Added delete functionality and basic filters to trip history --- .../carApp/TripDetailsScreen.kt | 29 +++----- .../carApp/TripHistoryFilterScreen.kt | 73 +++++++++++++++++++ .../carApp/TripHistoryScreen.kt | 68 +++++++++++++---- 3 files changed, 138 insertions(+), 32 deletions(-) create mode 100644 automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryFilterScreen.kt diff --git a/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripDetailsScreen.kt b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripDetailsScreen.kt index 27c01dff..dcd8c8a5 100644 --- a/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripDetailsScreen.kt +++ b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripDetailsScreen.kt @@ -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 @@ -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 -> diff --git a/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryFilterScreen.kt b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryFilterScreen.kt new file mode 100644 index 00000000..f08f00a1 --- /dev/null +++ b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryFilterScreen.kt @@ -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() +} \ No newline at end of file diff --git a/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryScreen.kt b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryScreen.kt index fa48aa3d..f5ce4954 100644 --- a/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryScreen.kt +++ b/automotive/src/carapp/java/com/ixam97/carStatsViewer/carApp/TripHistoryScreen.kt @@ -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 @@ -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) @@ -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 { @@ -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()) @@ -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() } @@ -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() } @@ -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() + } + } + } + } \ No newline at end of file