11package org.wordpress.android.ui.voicetocontent
22
3+ import android.util.Log
34import androidx.lifecycle.LiveData
45import androidx.lifecycle.MutableLiveData
56import androidx.lifecycle.viewModelScope
67import dagger.hilt.android.lifecycle.HiltViewModel
78import kotlinx.coroutines.CoroutineDispatcher
89import kotlinx.coroutines.Dispatchers
910import kotlinx.coroutines.launch
11+ import org.wordpress.android.fluxc.model.SiteModel
1012import org.wordpress.android.fluxc.model.jetpackai.JetpackAIAssistantFeature
1113import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAIAssistantFeatureResponse
1214import org.wordpress.android.fluxc.store.jetpackai.JetpackAIStore
1315import org.wordpress.android.modules.UI_THREAD
1416import org.wordpress.android.ui.mysite.SelectedSiteRepository
1517import org.wordpress.android.viewmodel.ScopedViewModel
18+ import java.io.File
1619import javax.inject.Inject
1720import javax.inject.Named
1821
@@ -22,7 +25,8 @@ class VoiceToContentViewModel @Inject constructor(
2225 private val voiceToContentFeatureUtils : VoiceToContentFeatureUtils ,
2326 private val voiceToContentUseCase : VoiceToContentUseCase ,
2427 private val selectedSiteRepository : SelectedSiteRepository ,
25- private val jetpackAIStore : JetpackAIStore
28+ private val jetpackAIStore : JetpackAIStore ,
29+ private val recordingUseCase : RecordingUseCase
2630) : ScopedViewModel(mainDispatcher) {
2731 private val _uiState = MutableLiveData <VoiceToContentResult >()
2832 val uiState = _uiState as LiveData <VoiceToContentResult >
@@ -32,7 +36,48 @@ class VoiceToContentViewModel @Inject constructor(
3236
3337 private fun isVoiceToContentEnabled () = voiceToContentFeatureUtils.isVoiceToContentEnabled()
3438
35- fun execute () {
39+ init {
40+ observeRecordingUpdates()
41+ }
42+
43+ private fun observeRecordingUpdates () {
44+ viewModelScope.launch {
45+ recordingUseCase.recordingUpdates().collect { update ->
46+ if (update.fileSizeLimitExceeded) {
47+ stopRecording()
48+ } else {
49+ // todo: Handle other updates if needed when UI is ready, e.g., elapsed time and file size
50+ Log .d(" AudioRecorder" , " Recording update: $update " )
51+ }
52+ }
53+ }
54+ }
55+
56+ fun startRecording () {
57+ recordingUseCase.startRecording { recordingPath ->
58+ val file = getRecordingFile(recordingPath)
59+ file?.let {
60+ executeVoiceToContent(it)
61+ } ? : run {
62+ _uiState .postValue(VoiceToContentResult (isError = true ))
63+ }
64+ }
65+ }
66+
67+ @Suppress(" ReturnCount" )
68+ private fun getRecordingFile (recordingPath : String ): File ? {
69+ if (recordingPath.isEmpty()) return null
70+ val recordingFile = File (recordingPath)
71+ // Return null if the file does not exist, is not a file, or is empty
72+ if (! recordingFile.exists() || ! recordingFile.isFile || recordingFile.length() == 0L ) return null
73+ return recordingFile
74+ }
75+
76+ fun stopRecording () {
77+ recordingUseCase.stopRecording()
78+ }
79+
80+ fun executeVoiceToContent (file : File ) {
3681 val site = selectedSiteRepository.getSelectedSite() ? : run {
3782 _uiState .postValue(VoiceToContentResult (isError = true ))
3883 return
@@ -44,7 +89,7 @@ class VoiceToContentViewModel @Inject constructor(
4489 when (result) {
4590 is JetpackAIAssistantFeatureResponse .Success -> {
4691 _aiAssistantFeatureState .postValue(result.model)
47- startVoiceToContentFlow()
92+ startVoiceToContentFlow(site, file )
4893 }
4994 is JetpackAIAssistantFeatureResponse .Error -> {
5095 _uiState .postValue(VoiceToContentResult (isError = true ))
@@ -54,17 +99,13 @@ class VoiceToContentViewModel @Inject constructor(
5499 }
55100 }
56101
57- private fun startVoiceToContentFlow () {
58- val site = selectedSiteRepository.getSelectedSite() ? : run {
59- _uiState .postValue(VoiceToContentResult (isError = true ))
60- return
61- }
62-
102+ private fun startVoiceToContentFlow (site : SiteModel , file : File ) {
63103 if (isVoiceToContentEnabled()) {
64104 viewModelScope.launch {
65- val result = voiceToContentUseCase.execute(site)
105+ val result = voiceToContentUseCase.execute(site, file )
66106 _uiState .postValue(result)
67107 }
68108 }
69109 }
70110}
111+
0 commit comments