mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-07-12 23:16:44 +02:00
Saves Preferred language between each language pair
This commit is contained in:
@ -25,6 +25,7 @@ class LocalPreferences(context: Context) {
|
|||||||
remove("hidden_users")
|
remove("hidden_users")
|
||||||
remove("relays")
|
remove("relays")
|
||||||
remove("dontTranslateFrom")
|
remove("dontTranslateFrom")
|
||||||
|
remove("languagePreferences")
|
||||||
remove("translateTo")
|
remove("translateTo")
|
||||||
remove("zapAmounts")
|
remove("zapAmounts")
|
||||||
remove("latestContactList")
|
remove("latestContactList")
|
||||||
@ -39,6 +40,7 @@ class LocalPreferences(context: Context) {
|
|||||||
account.hiddenUsers.let { putStringSet("hidden_users", it) }
|
account.hiddenUsers.let { putStringSet("hidden_users", it) }
|
||||||
account.localRelays.let { putString("relays", gson.toJson(it)) }
|
account.localRelays.let { putString("relays", gson.toJson(it)) }
|
||||||
account.dontTranslateFrom.let { putStringSet("dontTranslateFrom", it) }
|
account.dontTranslateFrom.let { putStringSet("dontTranslateFrom", it) }
|
||||||
|
account.languagePreferences.let { putString("languagePreferences", gson.toJson(it)) }
|
||||||
account.translateTo.let { putString("translateTo", it) }
|
account.translateTo.let { putString("translateTo", it) }
|
||||||
account.zapAmountChoices.let { putString("zapAmounts", gson.toJson(it)) }
|
account.zapAmountChoices.let { putString("zapAmounts", gson.toJson(it)) }
|
||||||
account.backupContactList.let { putString("latestContactList", Event.gson.toJson(it)) }
|
account.backupContactList.let { putString("latestContactList", Event.gson.toJson(it)) }
|
||||||
@ -73,6 +75,15 @@ class LocalPreferences(context: Context) {
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val languagePreferences = try {
|
||||||
|
getString("languagePreferences", null)?.let {
|
||||||
|
gson.fromJson(it, object : TypeToken<Map<String, String>>() {}.type) as Map<String, String>
|
||||||
|
} ?: mapOf<String,String>()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
e.printStackTrace()
|
||||||
|
mapOf<String,String>()
|
||||||
|
}
|
||||||
|
|
||||||
if (pubKey != null) {
|
if (pubKey != null) {
|
||||||
return Account(
|
return Account(
|
||||||
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
|
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
|
||||||
@ -80,6 +91,7 @@ class LocalPreferences(context: Context) {
|
|||||||
hiddenUsers,
|
hiddenUsers,
|
||||||
localRelays,
|
localRelays,
|
||||||
dontTranslateFrom,
|
dontTranslateFrom,
|
||||||
|
languagePreferences,
|
||||||
translateTo,
|
translateTo,
|
||||||
zapAmountChoices,
|
zapAmountChoices,
|
||||||
latestContactList
|
latestContactList
|
||||||
|
@ -56,6 +56,7 @@ class Account(
|
|||||||
var hiddenUsers: Set<String> = setOf(),
|
var hiddenUsers: Set<String> = setOf(),
|
||||||
var localRelays: Set<RelaySetupInfo> = Constants.defaultRelays.toSet(),
|
var localRelays: Set<RelaySetupInfo> = Constants.defaultRelays.toSet(),
|
||||||
var dontTranslateFrom: Set<String> = getLanguagesSpokenByUser(),
|
var dontTranslateFrom: Set<String> = getLanguagesSpokenByUser(),
|
||||||
|
var languagePreferences: Map<String, String> = mapOf(),
|
||||||
var translateTo: String = Locale.getDefault().language,
|
var translateTo: String = Locale.getDefault().language,
|
||||||
var zapAmountChoices: List<Long> = listOf(500L, 1000L, 5000L),
|
var zapAmountChoices: List<Long> = listOf(500L, 1000L, 5000L),
|
||||||
var backupContactList: ContactListEvent? = null
|
var backupContactList: ContactListEvent? = null
|
||||||
@ -417,6 +418,15 @@ class Account(
|
|||||||
saveable.invalidateData()
|
saveable.invalidateData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun prefer(source: String, target: String, preference: String) {
|
||||||
|
languagePreferences = languagePreferences + Pair("$source,$target", preference)
|
||||||
|
saveable.invalidateData()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun preferenceBetween(source: String, target: String): String? {
|
||||||
|
return languagePreferences.get("$source,$target")
|
||||||
|
}
|
||||||
|
|
||||||
private fun updateContactListTo(newContactList: ContactListEvent?) {
|
private fun updateContactListTo(newContactList: ContactListEvent?) {
|
||||||
if (newContactList?.follows.isNullOrEmpty()) return
|
if (newContactList?.follows.isNullOrEmpty()) return
|
||||||
|
|
||||||
@ -491,6 +501,8 @@ class Account(
|
|||||||
saveable.invalidateData()
|
saveable.invalidateData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
backupContactList?.let {
|
backupContactList?.let {
|
||||||
println("Loading saved contacts ${it.toJson()}")
|
println("Loading saved contacts ${it.toJson()}")
|
||||||
|
@ -3,15 +3,20 @@ package com.vitorpamplona.amethyst.ui.components
|
|||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.text.ClickableText
|
import androidx.compose.foundation.text.ClickableText
|
||||||
import androidx.compose.material.Divider
|
import androidx.compose.material.Divider
|
||||||
import androidx.compose.material.DropdownMenu
|
import androidx.compose.material.DropdownMenu
|
||||||
import androidx.compose.material.DropdownMenuItem
|
import androidx.compose.material.DropdownMenuItem
|
||||||
|
import androidx.compose.material.Icon
|
||||||
import androidx.compose.material.LocalTextStyle
|
import androidx.compose.material.LocalTextStyle
|
||||||
import androidx.compose.material.MaterialTheme
|
import androidx.compose.material.MaterialTheme
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.Text
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.Check
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
@ -55,14 +60,21 @@ fun TranslateableRichTextViewer(
|
|||||||
val account = accountState?.account ?: return
|
val account = accountState?.account ?: return
|
||||||
|
|
||||||
LaunchedEffect(accountState) {
|
LaunchedEffect(accountState) {
|
||||||
LanguageTranslatorService.autoTranslate(content, account.dontTranslateFrom, account.translateTo)
|
LanguageTranslatorService.autoTranslate(
|
||||||
.addOnCompleteListener { task ->
|
content,
|
||||||
if (task.isSuccessful) {
|
account.dontTranslateFrom,
|
||||||
translatedTextState.value = task.result
|
account.translateTo
|
||||||
} else {
|
).addOnCompleteListener { task ->
|
||||||
translatedTextState.value = ResultOrError(content, null, null, null)
|
if (task.isSuccessful) {
|
||||||
|
if (task.result.sourceLang != null && task.result.targetLang != null) {
|
||||||
|
val preference = account.preferenceBetween(task.result.sourceLang!!, task.result.targetLang!!)
|
||||||
|
showOriginal = preference == task.result.sourceLang
|
||||||
}
|
}
|
||||||
|
translatedTextState.value = task.result
|
||||||
|
} else {
|
||||||
|
translatedTextState.value = ResultOrError(content, null, null, null)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val toBeViewed = if (showOriginal) content else translatedTextState.value.result ?: content
|
val toBeViewed = if (showOriginal) content else translatedTextState.value.result ?: content
|
||||||
@ -82,7 +94,9 @@ fun TranslateableRichTextViewer(
|
|||||||
|
|
||||||
if (source != null && target != null) {
|
if (source != null && target != null) {
|
||||||
if (source != target) {
|
if (source != target) {
|
||||||
Row(modifier = Modifier.fillMaxWidth().padding(top = 5.dp)) {
|
Row(modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(top = 5.dp)) {
|
||||||
val clickableTextStyle =
|
val clickableTextStyle =
|
||||||
SpanStyle(color = MaterialTheme.colors.primary.copy(alpha = 0.52f))
|
SpanStyle(color = MaterialTheme.colors.primary.copy(alpha = 0.52f))
|
||||||
|
|
||||||
@ -131,9 +145,56 @@ fun TranslateableRichTextViewer(
|
|||||||
accountViewModel.dontTranslateFrom(source, context)
|
accountViewModel.dontTranslateFrom(source, context)
|
||||||
langSettingsPopupExpanded = false
|
langSettingsPopupExpanded = false
|
||||||
}) {
|
}) {
|
||||||
|
if (source in account.dontTranslateFrom)
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Check,
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Spacer(modifier = Modifier.size(24.dp))
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.size(10.dp))
|
||||||
|
|
||||||
Text("Never translate from ${Locale(source).displayName}")
|
Text("Never translate from ${Locale(source).displayName}")
|
||||||
}
|
}
|
||||||
Divider()
|
Divider()
|
||||||
|
DropdownMenuItem(onClick = {
|
||||||
|
accountViewModel.prefer(source, target, source)
|
||||||
|
langSettingsPopupExpanded = false
|
||||||
|
}) {
|
||||||
|
if (account.preferenceBetween(source, target) == source)
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Check,
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Spacer(modifier = Modifier.size(24.dp))
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.size(10.dp))
|
||||||
|
|
||||||
|
Text("Show in ${Locale(source).displayName} first")
|
||||||
|
}
|
||||||
|
DropdownMenuItem(onClick = {
|
||||||
|
accountViewModel.prefer(source, target, target)
|
||||||
|
langSettingsPopupExpanded = false
|
||||||
|
}) {
|
||||||
|
if (account.preferenceBetween(source, target) == target)
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Check,
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Spacer(modifier = Modifier.size(24.dp))
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.size(10.dp))
|
||||||
|
|
||||||
|
Text("Show in ${Locale(target).displayName} first")
|
||||||
|
}
|
||||||
|
Divider()
|
||||||
|
|
||||||
val languageList =
|
val languageList =
|
||||||
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())
|
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())
|
||||||
for (i in 0 until languageList.size()) {
|
for (i in 0 until languageList.size()) {
|
||||||
@ -142,6 +203,17 @@ fun TranslateableRichTextViewer(
|
|||||||
accountViewModel.translateTo(lang, context)
|
accountViewModel.translateTo(lang, context)
|
||||||
langSettingsPopupExpanded = false
|
langSettingsPopupExpanded = false
|
||||||
}) {
|
}) {
|
||||||
|
if (lang.language in account.translateTo)
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Check,
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Spacer(modifier = Modifier.size(24.dp))
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.size(10.dp))
|
||||||
|
|
||||||
Text("Always translate to ${lang.displayName}")
|
Text("Always translate to ${lang.displayName}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,4 +82,8 @@ class AccountViewModel(private val account: Account): ViewModel() {
|
|||||||
fun dontTranslateFrom(lang: String, ctx: Context) {
|
fun dontTranslateFrom(lang: String, ctx: Context) {
|
||||||
account.addDontTranslateFrom(lang)
|
account.addDontTranslateFrom(lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun prefer(source: String, target: String, preference: String) {
|
||||||
|
account.prefer(source, target, preference)
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user