Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e35da349 authored by mitulsheth's avatar mitulsheth
Browse files

Merge branch 'main' of https://gitlab.e.foundation/e/os/maps into...

Merge branch 'main' of https://gitlab.e.foundation/e/os/maps into Fix_removing_places_from_the_favorites-ai-review
parents 78729699 3467ca7f
Loading
Loading
Loading
Loading
Loading
+98 −30
Original line number Diff line number Diff line
@@ -25,6 +25,10 @@ import earth.maps.cardinal.data.LocationRepository
import earth.maps.cardinal.data.OrientationRepository
import earth.maps.cardinal.data.RoutingMode
import earth.maps.cardinal.data.room.RoutingProfileRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import javax.inject.Inject
import javax.inject.Singleton

@@ -36,17 +40,39 @@ class FerrostarWrapperRepository @Inject constructor(
    private val orientationRepository: OrientationRepository,
    private val routingProfileRepository: RoutingProfileRepository
) {
    lateinit var walking: FerrostarWrapper
    lateinit var cycling: FerrostarWrapper
    lateinit var driving: FerrostarWrapper
    lateinit var truck: FerrostarWrapper
    lateinit var motorScooter: FerrostarWrapper
    lateinit var motorcycle: FerrostarWrapper
    private val _isInitialized = MutableStateFlow(false)
    val isInitialized = _isInitialized.asStateFlow()

    private var _walking: FerrostarWrapper? = null
    private var _cycling: FerrostarWrapper? = null
    private var _driving: FerrostarWrapper? = null
    private var _truck: FerrostarWrapper? = null
    private var _motorScooter: FerrostarWrapper? = null
    private var _motorcycle: FerrostarWrapper? = null

    val walking: FerrostarWrapper get() = _walking ?: throw IllegalStateException("Walking wrapper not initialized")
    val cycling: FerrostarWrapper get() = _cycling ?: throw IllegalStateException("Cycling wrapper not initialized")
    val driving: FerrostarWrapper get() = _driving ?: throw IllegalStateException("Driving wrapper not initialized")
    val truck: FerrostarWrapper get() = _truck ?: throw IllegalStateException("Truck wrapper not initialized")
    val motorScooter: FerrostarWrapper get() = _motorScooter ?: throw IllegalStateException("MotorScooter wrapper not initialized")
    val motorcycle: FerrostarWrapper get() = _motorcycle ?: throw IllegalStateException("Motorcycle wrapper not initialized")

    private val pendingOptions = mutableMapOf<RoutingMode, RoutingOptions>()

    val androidTtsObserver = AndroidTtsObserver(context)

    /**
     * Suspends the caller until the repository has been initialized with a Valhalla endpoint.
     *
     * Once [_isInitialized] becomes true, this function resumes. If the repository
     * is already initialized, it returns immediately.
     */
    suspend fun awaitInitialization() {
        _isInitialized.filter { it }.first()
    }

    fun setValhallaEndpoint(endpoint: String) {
        walking = FerrostarWrapper(
        _walking = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -55,7 +81,7 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )
        cycling = FerrostarWrapper(
        _cycling = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -64,7 +90,7 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )
        driving = FerrostarWrapper(
        _driving = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -73,7 +99,7 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )
        truck = FerrostarWrapper(
        _truck = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -82,7 +108,7 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )
        motorScooter = FerrostarWrapper(
        _motorScooter = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -91,7 +117,7 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )
        motorcycle = FerrostarWrapper(
        _motorcycle = FerrostarWrapper(
            context,
            locationRepository,
            orientationRepository,
@@ -100,36 +126,78 @@ class FerrostarWrapperRepository @Inject constructor(
            androidTtsObserver,
            routingProfileRepository
        )

        _isInitialized.value = true

        // Apply pending options
        synchronized(pendingOptions) {
            pendingOptions.forEach { (mode, options) ->
                setOptionsForMode(mode, options)
            }
            pendingOptions.clear()
        }
    }

    /**
     * Updates the routing options for the specified mode by modifying the existing wrapper.
     * Updates the [RoutingOptions] for the specified [mode] by modifying the existing wrapper.
     *
     * If the wrapper for the given [mode] is not yet initialized (i.e., [setValhallaEndpoint]
     * hasn't been called), the options are stored in [pendingOptions] and applied
     * automatically once initialization completes.
     *
     * @param mode The [RoutingMode] to update (e.g., PEDESTRIAN, AUTO).
     * @param routingOptions The new configuration options to apply.
     */
    fun setOptionsForMode(mode: RoutingMode, routingOptions: RoutingOptions) {
        when (mode) {
            RoutingMode.PEDESTRIAN -> walking.setOptions(routingOptions)
            RoutingMode.BICYCLE -> cycling.setOptions(routingOptions)
            RoutingMode.AUTO -> driving.setOptions(routingOptions)
            RoutingMode.TRUCK -> truck.setOptions(routingOptions)
            RoutingMode.MOTOR_SCOOTER -> motorScooter.setOptions(routingOptions)
            RoutingMode.MOTORCYCLE -> motorcycle.setOptions(routingOptions)
            else -> {}
        val wrapper = getWrapperForMode(mode)
        if (wrapper != null) {
            wrapper.setOptions(routingOptions)
        } else {
            synchronized(pendingOptions) {
                pendingOptions[mode] = routingOptions
            }
        }
    }

    /**
     * Resets the routing options for the specified mode to defaults by recreating the wrapper.
     * Resets the [RoutingOptions] for the specified [mode] to their default values.
     *
     * This retrieves the defaults from [routingProfileRepository]. If the [FerrostarWrapper]
     * for this mode is already initialized, the options are applied immediately.
     *
     * If the wrapper is not yet initialized (i.e., [setValhallaEndpoint] hasn't been called),
     * the default options are stored in [pendingOptions] and applied automatically
     * during initialization.
     *
     * @param mode The [RoutingMode] to reset (e.g., PEDESTRIAN, BICYCLE).
     */
    fun resetOptionsToDefaultsForMode(mode: RoutingMode) {
        val defaultOptions = routingProfileRepository.createDefaultOptionsForMode(mode)
        when (mode) {
            RoutingMode.PEDESTRIAN -> walking.setOptions(defaultOptions)
            RoutingMode.BICYCLE -> cycling.setOptions(defaultOptions)
            RoutingMode.AUTO -> driving.setOptions(defaultOptions)
            RoutingMode.TRUCK -> truck.setOptions(defaultOptions)
            RoutingMode.MOTOR_SCOOTER -> motorScooter.setOptions(defaultOptions)
            RoutingMode.MOTORCYCLE -> motorcycle.setOptions(defaultOptions)
            else -> {}
        val wrapper = getWrapperForMode(mode)
        if (wrapper != null) {
            wrapper.setOptions(defaultOptions)
        } else {
            defaultOptions?.let {
                synchronized(pendingOptions) {
                    pendingOptions[mode] = it
                }
            }
        }
    }

    /**
     * Resolves the internal [FerrostarWrapper] instance associated with a specific [RoutingMode].*
     * @param mode The [RoutingMode] for which to retrieve the wrapper.
     * @return The corresponding [FerrostarWrapper] (e.g., [_walking], [_cycling]),
     * or `null` if the mode is unrecognized or the wrapper hasn't been initialized via [setValhallaEndpoint].
     */
    private fun getWrapperForMode(mode: RoutingMode): FerrostarWrapper? = when (mode) {
        RoutingMode.PEDESTRIAN -> _walking
        RoutingMode.BICYCLE -> _cycling
        RoutingMode.AUTO -> _driving
        RoutingMode.TRUCK -> _truck
        RoutingMode.MOTOR_SCOOTER -> _motorScooter
        RoutingMode.MOTORCYCLE -> _motorcycle
        else -> null
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -108,6 +108,9 @@ class DirectionsViewModel @Inject constructor(


    suspend fun initializeRoutingMode() {
        // Wait for FerrostarWrapperRepository to be initialized before setting options
        ferrostarWrapperRepository.awaitInitialization()

        // Set initial routing mode from preferences
        selectedRoutingMode = appPreferenceRepository.lastRoutingMode.value.let { modeString ->
            RoutingMode.entries.find { it.value == modeString } ?: RoutingMode.AUTO
@@ -161,6 +164,9 @@ class DirectionsViewModel @Inject constructor(

    private fun fetchDrivingDirections(origin: Place, destination: Place) {
        viewModelScope.launch {
            // Wait for FerrostarWrapperRepository to be initialized
            ferrostarWrapperRepository.awaitInitialization()

            routeStateRepository.setLoading(true)

            try {
+22 −0
Original line number Diff line number Diff line
/*
 *     Cardinal Maps
 *     Copyright (C) 2025 Cardinal Maps Authors
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package earth.maps.cardinal.ui.directions

import earth.maps.cardinal.MainCoroutineRule
@@ -95,6 +113,8 @@ class DirectionsViewModelTest {
        )
        coEvery { mockRoutingProfileRepository.getProfilesForMode(any()) } returns flowOf(emptyList())

        // Mock FerrostarWrapperRepository
        coEvery { mockFerrostarWrapperRepository.awaitInitialization() } returns Unit
        coEvery { mockFerrostarWrapperRepository.resetOptionsToDefaultsForMode(any()) } returns Unit

        viewModel = DirectionsViewModel(
@@ -253,6 +273,8 @@ class DirectionsViewModelTest {
            address = null
        )

        mockFerrostarWrapperRepository.awaitInitialization()

        // Setup mock to throw an exception
        val mockFerrostarWrapper = mockk<FerrostarWrapper>()
        coEvery { mockFerrostarWrapperRepository.driving } returns mockFerrostarWrapper