Skip to content

Commit 12f0b73

Browse files
committed
Move constrcutor to private
1 parent 9d8a6cf commit 12f0b73

File tree

6 files changed

+15
-32
lines changed

6 files changed

+15
-32
lines changed

libnavui-androidauto/src/main/java/com/mapbox/androidauto/MapboxCarApp.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ object MapboxCarApp {
2626
fun carAppLocationService(): CarAppLocation =
2727
MapboxNavigationApp.getObserver(CarAppLocation::class)
2828

29-
/**
30-
* Audio guidance service available to the car and app.
31-
*/
32-
fun carAppAudioGuidanceService(): MapboxAudioGuidance =
33-
MapboxNavigationApp.getObserver(MapboxAudioGuidance::class)
34-
3529
/**
3630
* Keep your car and app in sync with CarAppState.
3731
*/
@@ -44,9 +38,6 @@ object MapboxCarApp {
4438
*/
4539
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
4640
fun setup() {
47-
if (MapboxNavigationApp.getObservers(MapboxAudioGuidance::class).isEmpty()) {
48-
MapboxNavigationApp.registerObserver(MapboxAudioGuidance())
49-
}
5041
if (MapboxNavigationApp.getObservers(CarAppLocation::class).isEmpty()) {
5142
MapboxNavigationApp.registerObserver(CarAppLocationImpl())
5243
}

libnavui-androidauto/src/main/java/com/mapbox/androidauto/navigation/audioguidance/AppAudioGuidanceUi.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.lifecycle.LifecycleOwner
88
import androidx.lifecycle.lifecycleScope
99
import androidx.lifecycle.repeatOnLifecycle
1010
import com.mapbox.androidauto.MapboxCarApp
11+
import com.mapbox.navigation.ui.voice.api.MapboxAudioGuidance
1112
import com.mapbox.navigation.ui.voice.api.MapboxAudioGuidanceState
1213
import com.mapbox.navigation.ui.voice.view.MapboxSoundButton
1314
import kotlinx.coroutines.flow.collect
@@ -17,7 +18,7 @@ fun Fragment.attachAudioGuidance(
1718
mapboxSoundButton: MapboxSoundButton
1819
) {
1920
val lifecycleOwner = viewLifecycleOwner
20-
val flow = MapboxCarApp.carAppAudioGuidanceService().stateFlow()
21+
val flow = MapboxAudioGuidance.getInstance().stateFlow()
2122
lifecycleOwner.lifecycleScope.launch {
2223
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
2324
flow.collect { state ->
@@ -31,7 +32,7 @@ fun Fragment.attachAudioGuidance(
3132
}
3233
}
3334
mapboxSoundButton.setOnClickListener {
34-
MapboxCarApp.carAppAudioGuidanceService().toggle()
35+
MapboxAudioGuidance.getInstance().toggle()
3536
}
3637
}
3738

@@ -42,15 +43,15 @@ fun Lifecycle.muteAudioGuidance() {
4243
addObserver(object : DefaultLifecycleObserver {
4344
lateinit var initialState: MapboxAudioGuidanceState
4445
override fun onResume(owner: LifecycleOwner) {
45-
with(MapboxCarApp.carAppAudioGuidanceService()) {
46+
with(MapboxAudioGuidance.getInstance()) {
4647
initialState = stateFlow().value
4748
mute()
4849
}
4950
}
5051

5152
override fun onPause(owner: LifecycleOwner) {
5253
if (!initialState.isMuted) {
53-
MapboxCarApp.carAppAudioGuidanceService().unMute()
54+
MapboxAudioGuidance.getInstance().unMute()
5455
}
5556
}
5657
})

libnavui-androidauto/src/main/java/com/mapbox/androidauto/navigation/audioguidance/CarAudioGuidanceUi.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.mapbox.androidauto.MapboxCarApp
1212
import com.mapbox.androidauto.R
1313
import com.mapbox.androidauto.car.MainActionStrip
1414
import com.mapbox.androidauto.car.action.MapboxActionProvider
15+
import com.mapbox.navigation.ui.voice.api.MapboxAudioGuidance
1516
import kotlinx.coroutines.flow.collect
1617
import kotlinx.coroutines.flow.distinctUntilChanged
1718
import kotlinx.coroutines.launch
@@ -28,7 +29,7 @@ class CarAudioGuidanceUi : MapboxActionProvider.ScreenActionProvider {
2829
* Attach this to the screen while navigating.
2930
*/
3031
private fun buildSoundButtonAction(screen: Screen): Action {
31-
val audioGuidance = MapboxCarApp.carAppAudioGuidanceService()
32+
val audioGuidance = MapboxAudioGuidance.getInstance()
3233
val state = audioGuidance.stateFlow().value
3334
return if (!state.isMuted) {
3435
buildIconAction(screen, R.drawable.mapbox_car_ic_volume_on) {
@@ -45,7 +46,7 @@ class CarAudioGuidanceUi : MapboxActionProvider.ScreenActionProvider {
4546
screen.lifecycle.apply {
4647
coroutineScope.launch {
4748
repeatOnLifecycle(Lifecycle.State.STARTED) {
48-
MapboxCarApp.carAppAudioGuidanceService().stateFlow()
49+
MapboxAudioGuidance.getInstance().stateFlow()
4950
.distinctUntilChanged { old, new ->
5051
old.isMuted == new.isMuted && old.isPlayable == new.isPlayable
5152
}

libnavui-voice/src/main/java/com/mapbox/navigation/ui/voice/api/MapboxAudioGuidance.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ internal constructor(
4040
dispatcher: CoroutineDispatcher,
4141
) : MapboxNavigationObserver {
4242

43-
constructor() : this(MapboxAudioGuidanceServices(), Dispatchers.Main)
44-
4543
private var dataStoreOwner: NavigationDataStoreOwner? = null
4644
private var configOwner: NavigationConfigOwner? = null
4745
private var mutedStateFlow = MutableStateFlow(false)
@@ -197,9 +195,14 @@ internal constructor(
197195
dataStoreOwner?.write(STORE_AUDIO_GUIDANCE_MUTED, muted)
198196
}
199197

200-
private companion object {
198+
companion object {
201199
private val STORE_AUDIO_GUIDANCE_MUTED =
202200
booleanDataStoreKey("audio_guidance_muted", false)
203201
private const val DEFAULT_DATA_STORE_NAME = "mapbox_navigation_preferences"
202+
203+
fun getInstance(): MapboxAudioGuidance = MapboxNavigationApp
204+
.getObservers(MapboxAudioGuidance::class)
205+
.firstOrNull() ?: MapboxAudioGuidance(MapboxAudioGuidanceServices(), Dispatchers.Main)
206+
.also { MapboxNavigationApp.registerObserver(it) }
204207
}
205208
}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mapbox.navigation.ui.voice.installer
22

33
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
4-
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
54
import com.mapbox.navigation.ui.base.installer.ComponentInstaller
65
import com.mapbox.navigation.ui.base.installer.Installation
76
import com.mapbox.navigation.ui.voice.api.MapboxAudioGuidance
@@ -17,13 +16,5 @@ import com.mapbox.navigation.ui.voice.view.MapboxAudioGuidanceButton
1716
*/
1817
@ExperimentalPreviewMapboxNavigationAPI
1918
fun ComponentInstaller.audioGuidanceButton(button: MapboxAudioGuidanceButton): Installation {
20-
ensureAudioGuidanceRegistered()
2119
return component(AudioGuidanceButtonComponent(button))
2220
}
23-
24-
@ExperimentalPreviewMapboxNavigationAPI
25-
private fun ensureAudioGuidanceRegistered() {
26-
if (MapboxNavigationApp.getObservers(MapboxAudioGuidance::class).isEmpty()) {
27-
MapboxNavigationApp.registerObserver(MapboxAudioGuidance())
28-
}
29-
}

libnavui-voice/src/main/java/com/mapbox/navigation/ui/voice/internal/ui/AudioGuidanceButtonComponent.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.mapbox.navigation.ui.voice.internal.ui
33
import androidx.core.view.isVisible
44
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
55
import com.mapbox.navigation.core.MapboxNavigation
6-
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
76
import com.mapbox.navigation.ui.base.lifecycle.UIComponent
87
import com.mapbox.navigation.ui.utils.internal.Provider
98
import com.mapbox.navigation.ui.utils.internal.extensions.slice
@@ -53,10 +52,7 @@ class AudioGuidanceButtonComponent(
5352

5453
init {
5554
this.contractProvider = contractProvider ?: Provider {
56-
val audioGuidance = MapboxNavigationApp
57-
.getObservers(MapboxAudioGuidance::class).firstOrNull()
58-
?: MapboxAudioGuidance().also { MapboxNavigationApp.registerObserver(it) }
59-
MapboxAudioComponentContract(coroutineScope, audioGuidance)
55+
MapboxAudioComponentContract(coroutineScope, MapboxAudioGuidance.getInstance())
6056
}
6157
}
6258

0 commit comments

Comments
 (0)