Skip to content

Commit f071a47

Browse files
committed
NAVAND-552: meet code review
1 parent 12a2e6e commit f071a47

File tree

6 files changed

+59
-40
lines changed

6 files changed

+59
-40
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class MapboxSpeechApi @JvmOverloads constructor(
6464
consumer: MapboxNavigationConsumer<Expected<SpeechError, SpeechValue>>
6565
) {
6666
mainJobController.scope.launch {
67-
retrieveVoiceFile(voiceInstruction, consumer)
67+
consumer.accept(retrieveVoiceFile(voiceInstruction))
6868
}
6969
}
7070

@@ -138,19 +138,18 @@ class MapboxSpeechApi @JvmOverloads constructor(
138138
val typeAndAnnouncement = VoiceInstructionsParser.parse(instruction).value
139139
if (typeAndAnnouncement != null && !hasTypeAndAnnouncement(typeAndAnnouncement)) {
140140
predownloadJobController.scope.launch {
141-
retrieveVoiceFile(instruction) {
142-
mainJobController.scope.launch(Dispatchers.Main.immediate) {
143-
it.onValue { speechValue ->
144-
cachedFiles[typeAndAnnouncement] = speechValue
145-
}
141+
val voiceFile = retrieveVoiceFile(instruction)
142+
mainJobController.scope.launch {
143+
voiceFile.onValue { speechValue ->
144+
cachedFiles[typeAndAnnouncement] = speechValue
146145
}
147146
}
148147
}
149148
}
150149
}
151150
}
152151

153-
internal fun destroy() {
152+
internal fun cancelPredownload() {
154153
predownloadJobController.job.children.forEach { it.cancel() }
155154
val announcements = cachedFiles.map { it.value.announcement }
156155
announcements.forEach { clean(it) }
@@ -159,28 +158,25 @@ class MapboxSpeechApi @JvmOverloads constructor(
159158
@Throws(IllegalStateException::class)
160159
private suspend fun retrieveVoiceFile(
161160
voiceInstruction: VoiceInstructions,
162-
consumer: MapboxNavigationConsumer<Expected<SpeechError, SpeechValue>>,
163-
) {
161+
): Expected<SpeechError, SpeechValue> {
164162
when (val result = voiceAPI.retrieveVoiceFile(voiceInstruction)) {
165163
is VoiceState.VoiceFile -> {
166164
val announcement = voiceInstruction.announcement()
167165
val ssmlAnnouncement = voiceInstruction.ssmlAnnouncement()
168-
consumer.accept(
169-
ExpectedFactory.createValue(
170-
SpeechValue(
171-
// Can't be null as it's checked in retrieveVoiceFile
172-
SpeechAnnouncement.Builder(announcement!!)
173-
.ssmlAnnouncement(ssmlAnnouncement)
174-
.file(result.instructionFile)
175-
.build()
176-
)
166+
return ExpectedFactory.createValue(
167+
SpeechValue(
168+
// Can't be null as it's checked in retrieveVoiceFile
169+
SpeechAnnouncement.Builder(announcement!!)
170+
.ssmlAnnouncement(ssmlAnnouncement)
171+
.file(result.instructionFile)
172+
.build()
177173
)
178174
)
179175
}
180176
is VoiceState.VoiceError -> {
181177
val fallback = getFallbackAnnouncement(voiceInstruction)
182178
val speechError = SpeechError(result.exception, null, fallback)
183-
consumer.accept(ExpectedFactory.createError(speechError))
179+
return ExpectedFactory.createError(speechError)
184180
}
185181
}
186182
}

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mapbox.navigation.ui.voice.api
22

3+
import com.mapbox.api.directions.v5.models.LegStep
4+
import com.mapbox.api.directions.v5.models.RouteLeg
35
import com.mapbox.api.directions.v5.models.VoiceInstructions
46
import com.mapbox.navigation.utils.internal.logW
57

@@ -25,42 +27,63 @@ internal class TimeBasedNextVoiceInstructionsProvider(
2527
}
2628

2729
val voiceInstructions = mutableListOf<VoiceInstructions>()
30+
fillCurrentStepVoiceInstructions(
31+
currentStep,
32+
progress.stepDistanceRemaining,
33+
voiceInstructions
34+
)
2835
var cumulatedTime = progress.stepDurationRemaining
29-
val currentStepInstructions = currentStep.voiceInstructions()?.filter { instruction ->
30-
val distanceAlongGeometry = instruction.distanceAlongGeometry()
31-
distanceAlongGeometry != null &&
32-
distanceAlongGeometry <= progress.stepDistanceRemaining
33-
}
34-
if (currentStepInstructions != null) {
35-
voiceInstructions.addAll(currentStepInstructions)
36-
}
3736

37+
// fill next steps
3838
var currentStepIndex = progress.stepIndex
3939
var currentLegIndex = progress.legIndex
4040
while (cumulatedTime < observableTimeSeconds) {
41-
if (currentStepIndex + 1 < (legSteps?.size ?: 0)) {
42-
currentStep = legSteps!![currentStepIndex + 1]
43-
currentStepIndex++
44-
} else {
41+
if (isLastStep(currentStepIndex, legSteps)) {
4542
currentStepIndex = 0
46-
if (currentLegIndex + 1 < legs.size) {
43+
if (isLastLeg(currentLegIndex, legs)) {
44+
break
45+
} else {
4746
legSteps = legs[currentLegIndex + 1].steps()
4847
currentLegIndex++
4948
if (legSteps.isNullOrEmpty()) {
5049
continue
5150
} else {
5251
currentStep = legSteps.first()
5352
}
54-
} else {
55-
break
5653
}
54+
} else {
55+
currentStep = legSteps!![currentStepIndex + 1]
56+
currentStepIndex++
5757
}
5858
currentStep.voiceInstructions()?.let { voiceInstructions.addAll(it) }
5959
cumulatedTime += currentStep.duration()
6060
}
6161
return voiceInstructions
6262
}
6363

64+
private fun fillCurrentStepVoiceInstructions(
65+
currentStep: LegStep,
66+
stepDistanceRemaining: Double,
67+
voiceInstructions: MutableList<VoiceInstructions>
68+
) {
69+
val currentStepInstructions = currentStep.voiceInstructions()?.filter { instruction ->
70+
val distanceAlongGeometry = instruction.distanceAlongGeometry()
71+
distanceAlongGeometry != null &&
72+
distanceAlongGeometry <= stepDistanceRemaining
73+
}
74+
if (currentStepInstructions != null) {
75+
voiceInstructions.addAll(currentStepInstructions)
76+
}
77+
}
78+
79+
private fun isLastStep(currentStepIndex: Int, legSteps: List<LegStep>?): Boolean {
80+
return currentStepIndex + 1 >= (legSteps?.size ?: 0)
81+
}
82+
83+
private fun isLastLeg(currentLegIndex: Int, legs: List<RouteLeg>): Boolean {
84+
return currentLegIndex + 1 >= legs.size
85+
}
86+
6487
private companion object {
6588
private const val LOG_CATEGORY = "TimeBasedNextVoiceInstructionsProvider"
6689
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class VoiceInstructionsPrefetcher internal constructor(
6666
lastDownloadTime = 0
6767
mapboxNavigation.unregisterRoutesObserver(routesObserver)
6868
mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
69-
speechApi.destroy()
69+
speechApi.cancelPredownload()
7070
}
7171

7272
private fun onRoutesChanged(result: RoutesUpdatedResult) {

libnavui-voice/src/test/java/com/mapbox/navigation/ui/voice/api/MapboxAudioGuidanceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class MapboxAudioGuidanceTest {
321321
carAppAudioGuidance.onDetached(mapboxNavigation)
322322

323323
verify(exactly = 1) {
324-
speechApi.destroy()
324+
speechApi.cancelPredownload()
325325
}
326326
}
327327

libnavui-voice/src/test/java/com/mapbox/navigation/ui/voice/api/MapboxSpeechApiTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class MapboxSpeechApiTest {
275275
coEvery { voiceAPI.retrieveVoiceFile(instruction) } returns VoiceState.VoiceFile(file)
276276
val sut = MapboxSpeechApi(mockk(relaxed = true), "pk.123", Locale.US.language)
277277
sut.predownload(listOf(instruction))
278-
sut.destroy()
278+
sut.cancelPredownload()
279279

280280
sut.generatePredownloaded(instruction, consumer)
281281

@@ -505,7 +505,7 @@ class MapboxSpeechApiTest {
505505
fun `destroy with no instructions`() {
506506
val sut = MapboxSpeechApi(mockk(relaxed = true), "pk.123", Locale.US.language)
507507

508-
sut.destroy()
508+
sut.cancelPredownload()
509509

510510
coVerify(exactly = 0) { voiceAPI.clean(any()) }
511511
}
@@ -524,7 +524,7 @@ class MapboxSpeechApiTest {
524524
sut.predownload(listOf(instruction1, instruction2))
525525
clearMocks(voiceAPI, answers = false)
526526

527-
sut.destroy()
527+
sut.cancelPredownload()
528528

529529
coVerify(exactly = 1) {
530530
voiceAPI.clean(announcement1)

libnavui-voice/src/test/java/com/mapbox/navigation/ui/voice/api/VoiceInstructionsPrefetcherTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class VoiceInstructionsPrefetcherTest {
8585
verify(exactly = 1) {
8686
mapboxNavigation.registerRouteProgressObserver(routeProgressObserverSlot.first())
8787
mapboxNavigation.registerRoutesObserver(routesObserverSlot.first())
88-
speechAPI.destroy()
88+
speechAPI.cancelPredownload()
8989
}
9090
}
9191

0 commit comments

Comments
 (0)