-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
28 changed files
with
1,136 additions
and
263 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
158 changes: 158 additions & 0 deletions
158
app/src/main/java/my/noveldokusha/tools/TranslationManager.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,158 @@ | ||
package my.noveldokusha.tools | ||
|
||
import androidx.compose.runtime.mutableStateListOf | ||
import com.google.mlkit.common.model.DownloadConditions | ||
import com.google.mlkit.common.model.RemoteModel | ||
import com.google.mlkit.common.model.RemoteModelManager | ||
import com.google.mlkit.nl.translate.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.tasks.await | ||
import java.util.Locale | ||
|
||
data class TranslationModelState( | ||
val language: String, | ||
val model: TranslateRemoteModel?, | ||
val downloading: Boolean, | ||
val downloadingFailed: Boolean | ||
) { | ||
val locale = Locale(language) | ||
} | ||
|
||
data class TranslatorState( | ||
val translator: Translator, | ||
val source: String, | ||
var target: String | ||
) { | ||
val sourceLocale = Locale(source) | ||
val targetLocale = Locale(target) | ||
fun translate(input: String) = translator.translate(input) | ||
} | ||
|
||
class TranslationManager( | ||
private val coroutineScope: CoroutineScope | ||
) { | ||
val models = mutableStateListOf<TranslationModelState>().apply { | ||
val list = TranslateLanguage.getAllLanguages().map { | ||
TranslationModelState( | ||
language = it, | ||
model = null, | ||
downloading = false, | ||
downloadingFailed = false | ||
) | ||
} | ||
addAll(list) | ||
} | ||
|
||
val modelsDownloaded = mutableStateListOf<TranslationModelState>() | ||
|
||
init { | ||
coroutineScope.launch { | ||
val downloaded = loadDownloadedModelsList().associateBy { it.language } | ||
models.replaceAll { | ||
it.copy(model = downloaded[it.language]) | ||
} | ||
updateModelsDownloaded() | ||
} | ||
} | ||
|
||
private fun updateModelsDownloaded() { | ||
modelsDownloaded.clear() | ||
modelsDownloaded.addAll(models.filter { it.model != null }) | ||
} | ||
|
||
private suspend fun loadDownloadedModelsList() = withContext(Dispatchers.IO) { | ||
RemoteModelManager | ||
.getInstance() | ||
.getDownloadedModels(TranslateRemoteModel::class.java) | ||
.await() | ||
} | ||
|
||
// TODO Should not use blocking code | ||
fun hasModelDownloadedSync(language: String) = runBlocking { | ||
val model = TranslateRemoteModel.Builder(language).build() | ||
val isDownloaded = RemoteModelManager | ||
.getInstance() | ||
.isModelDownloaded(model) | ||
.await() | ||
if (isDownloaded) TranslationModelState( | ||
model = model, | ||
downloading = false, | ||
language = language, | ||
downloadingFailed = false | ||
) else null | ||
} | ||
|
||
/** | ||
* Doesn't check if the model has been downloaded. Must be externally guaranteed. | ||
* @param source [TranslateLanguage] | ||
* @param target [TranslateLanguage] | ||
*/ | ||
fun getTranslator( | ||
source: String, | ||
target: String | ||
): TranslatorState { | ||
val option = TranslatorOptions.Builder() | ||
.setSourceLanguage(source) | ||
.setTargetLanguage(target) | ||
.build() | ||
|
||
return TranslatorState( | ||
translator = Translation.getClient(option), | ||
source = source, | ||
target = target, | ||
) | ||
} | ||
|
||
fun downloadModel(language: String) = coroutineScope.launch { | ||
val index = models.indexOfFirst { it.language == language } | ||
if (index == -1 || models[index].model != null) return@launch | ||
|
||
models[index] = models[index].copy( | ||
downloadingFailed = false, | ||
downloading = true, | ||
) | ||
|
||
RemoteModelManager | ||
.getInstance() | ||
.download( | ||
TranslateRemoteModel.Builder(language).build(), | ||
DownloadConditions.Builder().build() | ||
) | ||
.addOnSuccessListener { | ||
models[index] = models[index].copy( | ||
downloadingFailed = false, | ||
downloading = false, | ||
model = TranslateRemoteModel.Builder(language).build() | ||
) | ||
updateModelsDownloaded() | ||
} | ||
.addOnFailureListener { | ||
models[index] = models[index].copy( | ||
downloadingFailed = true, | ||
downloading = false | ||
) | ||
} | ||
} | ||
|
||
fun removeModel(language: String) = coroutineScope.launch { | ||
|
||
// English can't be removed. | ||
if (language == "en") return@launch | ||
|
||
val index = models.indexOfFirst { it.language == language } | ||
if (index == -1) return@launch | ||
val model = models[index].model ?: return@launch | ||
|
||
RemoteModelManager | ||
.getInstance() | ||
.deleteDownloadedModel(model) | ||
.addOnSuccessListener { | ||
models[index] = models[index].copy( | ||
downloadingFailed = false, | ||
downloading = false, | ||
model = null | ||
) | ||
updateModelsDownloaded() | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/my/noveldokusha/ui/composeViews/Section.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,48 @@ | ||
package my.noveldokusha.ui.composeViews | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import my.noveldokusha.ui.theme.ColorAccent | ||
|
||
@Composable | ||
fun Section( | ||
title: String, | ||
modifier: Modifier = Modifier, | ||
content: @Composable () -> Unit | ||
) { | ||
Surface( | ||
color = MaterialTheme.colors.primaryVariant, | ||
modifier = modifier | ||
.clip(RoundedCornerShape(16.dp)) | ||
) { | ||
Column( | ||
modifier = Modifier.padding(vertical = 16.dp) | ||
) { | ||
Text( | ||
text = title, | ||
style = MaterialTheme.typography.subtitle1, | ||
fontWeight = FontWeight.Bold, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(horizontal = 8.dp) | ||
.padding(bottom = 16.dp), | ||
color = ColorAccent, | ||
textAlign = TextAlign.Center, | ||
) | ||
Divider(color = MaterialTheme.colors.secondary) | ||
content() | ||
} | ||
} | ||
} |
Oops, something went wrong.