Skip to content

Commit

Permalink
Mosaik dApps: Use language set by user #154
Browse files Browse the repository at this point in the history
  • Loading branch information
MrStahlfelge committed Dec 5, 2022
1 parent 26c9f00 commit 46125b8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.ergoplatform.persistance.CacheFileManager
import org.ergoplatform.persistance.PreferencesProvider
import org.ergoplatform.transactions.isErgoPaySigningRequest
import org.ergoplatform.uilogic.StringProvider
import java.util.Locale

class MosaikViewModel : ViewModel() {
val browserEvent = SingleLiveEvent<String?>()
Expand All @@ -44,6 +45,7 @@ class MosaikViewModel : ViewModel() {
"Ergo Wallet App (Android)",
BuildConfig.VERSION_NAME,
{ platformType!! },
{ currentLocale },
MosaikGuidManager(),
) {
override val coroutineScope: CoroutineScope
Expand Down Expand Up @@ -114,6 +116,7 @@ class MosaikViewModel : ViewModel() {
}

var platformType: MosaikContext.Platform? = null
private var currentLocale: Locale? = null

fun initialize(
appUrl: String,
Expand All @@ -124,6 +127,7 @@ class MosaikViewModel : ViewModel() {
cacheFileManager: CacheFileManager?
) {
this.platformType = platformType
this.currentLocale = stringProvider.locale
mosaikRuntime.appDatabase = appDb
mosaikRuntime.guidManager.appDatabase = appDb
mosaikRuntime.cacheFileManager = cacheFileManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.ergoplatform.android.ui
import android.content.Context
import android.content.res.Resources
import org.ergoplatform.uilogic.StringProvider
import java.util.*

class AndroidStringProvider(val context: Context) : StringProvider {
override fun getString(stringId: String): String {
Expand All @@ -17,4 +18,7 @@ class AndroidStringProvider(val context: Context) : StringProvider {
val resources = context.resources
return resources.getString(getAndroidStringId(resources, stringId), *formatArgs)
}

override val locale: Locale
get() = Locale.getDefault()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class AppMosaikRuntime(
val appName: String,
val appVersionName: String,
val platformType: () -> MosaikContext.Platform,
val getLocale: () -> Locale?,
val guidManager: MosaikGuidManager,
) : MosaikRuntime(
OkHttpBackendConnector(
Expand All @@ -36,7 +37,7 @@ abstract class AppMosaikRuntime(
MosaikContext(
MosaikContext.LIBRARY_MOSAIK_VERSION,
guidManager.getGuidForHost(getHostname(url)),
Locale.getDefault().language,
(getLocale() ?: Locale.getDefault()).language,
appName,
appVersionName,
platformType(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.ergoplatform.uilogic

import java.util.Locale

interface StringProvider {
fun getString(stringId: String): String
fun getString(stringId: String, vararg formatArgs: Any): String
val locale: Locale
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ergoplatform

import org.ergoplatform.uilogic.StringProvider
import java.util.*

class TestStringProvider: StringProvider {
override fun getString(stringId: String): String {
Expand All @@ -10,4 +11,7 @@ class TestStringProvider: StringProvider {
override fun getString(stringId: String, vararg formatArgs: Any): String {
return stringId
}

override val locale: Locale
get() = Locale.getDefault()
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class MosaikAppComponent(
"Ergo Wallet App (Desktop)",
appVersionString,
{ MosaikContext.Platform.DESKTOP },
{ Application.texts.locale },
MosaikGuidManager().apply {
appDatabase = Application.database
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package org.ergoplatform.desktop.ui

import com.badlogic.gdx.utils.I18NBundle
import org.ergoplatform.uilogic.StringProvider
import java.util.*

class DesktopStringProvider(val i18NBundle: I18NBundle): StringProvider {
class DesktopStringProvider(private val i18NBundle: I18NBundle): StringProvider {
override fun getString(stringId: String): String = i18NBundle.get(stringId)

override fun getString(stringId: String, vararg formatArgs: Any): String {
return i18NBundle.format(stringId, *formatArgs)
}

override val locale: Locale
get() = i18NBundle.locale
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ class MosaikViewController(
mosaikRuntime.switchFavorite()
}
resetDataButton =
UIBarButtonItem(getIosSystemImage(IMAGE_RESET_DATA, UIImageSymbolScale.Small)!!, UIBarButtonItemStyle.Plain)
UIBarButtonItem(
getIosSystemImage(IMAGE_RESET_DATA, UIImageSymbolScale.Small)!!,
UIBarButtonItemStyle.Plain
)
resetDataButton.setOnClickListener { mosaikRuntime.resetAppData() }
resetDataButton.isEnabled = false
navigationController.topViewController.navigationItem.rightBarButtonItems =
Expand Down Expand Up @@ -149,6 +152,7 @@ class MosaikViewController(
else -> org.ergoplatform.mosaik.model.MosaikContext.Platform.DESKTOP
}
},
{ getAppDelegate().texts.locale },
MosaikGuidManager().apply {
appDatabase = getAppDelegate().database
}
Expand Down Expand Up @@ -256,7 +260,10 @@ class MosaikViewController(
presentViewController(
ChooseWalletViewController { walletConfig ->
viewControllerScope.launch(Dispatchers.IO) {
val wallet = getAppDelegate().database.walletDbProvider.loadWalletWithStateById(walletConfig.id)
val wallet =
getAppDelegate().database.walletDbProvider.loadWalletWithStateById(
walletConfig.id
)
runOnMainThread {
onWalletChosen(wallet!!)
}
Expand All @@ -267,7 +274,10 @@ class MosaikViewController(

override fun startAddressIdxChooser() {
presentViewController(
ChooseAddressListDialogViewController(walletForAddressChooser!!.walletConfig.id, false) {
ChooseAddressListDialogViewController(
walletForAddressChooser!!.walletConfig.id,
false
) {
onAddressChosen(it!!)
}, true
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package org.ergoplatform.ios.ui

import com.badlogic.gdx.utils.I18NBundle
import org.ergoplatform.uilogic.StringProvider
import java.util.*

class IosStringProvider(val i18NBundle: I18NBundle): StringProvider {
override fun getString(stringId: String): String = i18NBundle.get(stringId)

override fun getString(stringId: String, vararg formatArgs: Any): String {
return i18NBundle.format(stringId, *formatArgs)
}

override val locale: Locale
get() = i18NBundle.locale
}

0 comments on commit 46125b8

Please sign in to comment.