-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from joeloewi7178/development
Development
- Loading branch information
Showing
8 changed files
with
83 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/kotlin/com/joeloewi/croissant/util/TextToSpeechFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.joeloewi.croissant.util | ||
|
||
import android.content.Context | ||
import android.speech.tts.TextToSpeech | ||
import com.joeloewi.croissant.state.foldAsLce | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
class TextToSpeechFactory @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
) { | ||
val flow = callbackFlow { | ||
var textToSpeech: TextToSpeech? = null | ||
|
||
textToSpeech = TextToSpeech(context) { status -> | ||
trySend(runCatching { | ||
if (status != TextToSpeech.SUCCESS) { | ||
throw IllegalStateException() | ||
} | ||
textToSpeech!!.apply { | ||
val currentLocale = Locale.getDefault() | ||
val targetLocale = if (availableLanguages.contains(currentLocale)) { | ||
currentLocale | ||
} else { | ||
Locale.ENGLISH | ||
} | ||
language = targetLocale | ||
} | ||
}.foldAsLce()) | ||
} | ||
|
||
awaitClose { | ||
with(textToSpeech) { | ||
stop() | ||
shutdown() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters