Skip to content

Commit 6b3988c

Browse files
committed
Add transitionToError for result failures, support ErrorUiModel, Log errors from audio
1 parent 225a3f0 commit 6b3988c

1 file changed

Lines changed: 64 additions & 11 deletions

File tree

WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModel.kt

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class VoiceToContentViewModel @Inject constructor(
4040
private val selectedSiteRepository: SelectedSiteRepository,
4141
private val recordingUseCase: RecordingUseCase,
4242
private val contextProvider: ContextProvider,
43-
private val prepareVoiceToContentUseCase: PrepareVoiceToContentUseCase
43+
private val prepareVoiceToContentUseCase: PrepareVoiceToContentUseCase,
44+
private val logger: VoiceToContentLogger
4445
) : ScopedViewModel(mainDispatcher) {
4546
private val _requestPermission = MutableLiveData<Unit>()
4647
val requestPermission = _requestPermission as LiveData<Unit>
@@ -84,8 +85,8 @@ class VoiceToContentViewModel @Inject constructor(
8485
transitionToReadyToRecordOrIneligibleForFeature(result.model)
8586
}
8687

87-
is PrepareVoiceToContentResult.Error -> {
88-
transitionToError()
88+
is PrepareVoiceToContentResult.Failure -> {
89+
result.transitionToError()
8990
}
9091
}
9192
}
@@ -122,11 +123,12 @@ class VoiceToContentViewModel @Inject constructor(
122123
file?.let {
123124
executeVoiceToContent(it)
124125
} ?: run {
125-
transitionToError()
126+
logger.logError("$VOICE_TO_CONTENT - unable to access audio file")
127+
transitionToError(GenericFailureMsg)
126128
}
127129
}
128130
is Error -> {
129-
transitionToError()
131+
audioRecorderResult.transitionToError()
130132
}
131133
}
132134
}
@@ -149,13 +151,16 @@ class VoiceToContentViewModel @Inject constructor(
149151
// Workflow
150152
private fun executeVoiceToContent(file: File) {
151153
val site = selectedSiteRepository.getSelectedSite() ?: run {
152-
transitionToError()
154+
transitionToError(GenericFailureMsg)
153155
return
154156
}
155157

156158
viewModelScope.launch {
157-
val result = voiceToContentUseCase.execute(site, file)
158-
Log.i(javaClass.simpleName, "***=> result is ${result.content}")
159+
when (val result = voiceToContentUseCase.execute(site, file)) {
160+
is VoiceToContentResult.Failure -> result.transitionToError()
161+
is VoiceToContentResult.Success ->
162+
Log.i(javaClass.simpleName, "***=> result is ${result.content}")
163+
}
159164
_dismiss.postValue(Unit)
160165
}
161166
}
@@ -191,7 +196,30 @@ class VoiceToContentViewModel @Inject constructor(
191196
_dismiss.postValue(Unit)
192197
}
193198

199+
private fun onRetryTap() {
200+
transitionToInitializing()
201+
start()
202+
}
203+
194204
// transitions
205+
private fun transitionToInitializing() {
206+
_state.value = VoiceToContentUiState(
207+
uiStateType = INITIALIZING,
208+
header = HeaderUIModel(
209+
label = R.string.voice_to_content_base_header_label,
210+
onClose = ::onClose),
211+
secondaryHeader = SecondaryHeaderUIModel(
212+
label = R.string.voice_to_content_secondary_header_label,
213+
isLabelVisible = true,
214+
isProgressIndicatorVisible = true,
215+
isTimeElapsedVisible = false),
216+
recordingPanel = RecordingPanelUIModel(
217+
actionLabel = R.string.voice_to_content_begin_recording_label,
218+
isEnabled = false),
219+
errorPanel = null
220+
)
221+
}
222+
195223
private fun transitionToReadyToRecordOrIneligibleForFeature(model: JetpackAIAssistantFeature) {
196224
val isEligibleForFeature = voiceToContentFeatureUtils.isEligibleForVoiceToContent(model)
197225
val requestsAvailable = voiceToContentFeatureUtils.getRequestLimit(model)
@@ -239,15 +267,40 @@ class VoiceToContentViewModel @Inject constructor(
239267
)
240268
}
241269

242-
// todo: annmarie - transition to error hasn't been fully fleshed out
243-
private fun transitionToError() {
270+
private fun VoiceToContentResult.Failure.transitionToError() {
271+
when (this) {
272+
VoiceToContentResult.Failure.NetworkUnavailable -> transitionToError(NetworkUnavailableMsg, true)
273+
VoiceToContentResult.Failure.RemoteRequestFailure -> transitionToError(GenericFailureMsg)
274+
}
275+
}
276+
277+
private fun PrepareVoiceToContentResult.Failure.transitionToError() {
278+
when (this) {
279+
PrepareVoiceToContentResult.Failure.NetworkUnavailable -> transitionToError(NetworkUnavailableMsg, true)
280+
PrepareVoiceToContentResult.Failure.RemoteRequestFailure -> transitionToError(GenericFailureMsg)
281+
}
282+
}
283+
284+
private fun Error.transitionToError() {
285+
logger.logError("$VOICE_TO_CONTENT - ${this.errorMessage}")
286+
transitionToError(GenericFailureMsg)
287+
}
288+
289+
private fun transitionToError(errorMessage: Int, allowRetry: Boolean = false) {
244290
val currentState = _state.value
245291
_state.value = currentState.copy(
246292
uiStateType = ERROR,
247293
header = currentState.header.copy( label = R.string.voice_to_content_error_label),
248294
secondaryHeader = null,
249-
recordingPanel = null
295+
recordingPanel = null,
296+
errorPanel = ErrorUiModel(errorMessage = errorMessage, allowRetry = allowRetry, onRetryTap = ::onRetryTap)
250297
)
251298
}
299+
300+
companion object {
301+
private val NetworkUnavailableMsg = R.string.error_network_connection
302+
private val GenericFailureMsg = R.string.voice_to_content_generic_error
303+
private const val VOICE_TO_CONTENT = "Voice to content"
304+
}
252305
}
253306

0 commit comments

Comments
 (0)