demo: initialize remote node list with default values

This commit is contained in:
Oscar Mira 2024-03-04 17:15:18 +01:00
parent db85c2234f
commit 743e9997c5
4 changed files with 49 additions and 4 deletions

View File

@ -2,7 +2,11 @@ package im.molly.monero.demo
import android.app.Application
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import im.molly.monero.demo.data.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
* Naive container of global instances.
@ -11,13 +15,20 @@ import im.molly.monero.demo.data.*
*/
object AppModule {
private lateinit var application: Application
private lateinit var populateInitialData: suspend (AppDatabase) -> Unit
private val applicationScope = kotlinx.coroutines.MainScope()
private val database: AppDatabase by lazy {
Room.databaseBuilder(
application, AppDatabase::class.java, "monero-demo.db"
).build()
).addCallback(object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
applicationScope.launch(Dispatchers.IO) {
populateInitialData(database)
}
}
}).build()
}
private val walletDataSource: WalletDataSource by lazy {
@ -40,7 +51,8 @@ object AppModule {
WalletRepository(moneroSdkClient, walletDataSource, settingsRepository, applicationScope)
}
fun provide(application: Application) {
fun provide(application: Application, populateInitialData: suspend (AppDatabase) -> Unit) {
this.application = application
this.populateInitialData = populateInitialData
}
}

View File

@ -0,0 +1,28 @@
package im.molly.monero.demo
import android.net.Uri
import im.molly.monero.MoneroNetwork
import im.molly.monero.demo.data.AppDatabase
import im.molly.monero.demo.data.entity.asEntity
import im.molly.monero.demo.data.model.RemoteNode
val DefaultNodeList = listOf(
MoneroNetwork.Mainnet to listOf(
"http://node.monerodevs.org:18089",
"http://node.sethforprivacy.com:18089",
"http://xmr-node.cakewallet.com:18081",
),
MoneroNetwork.Testnet to listOf(
"http://node2.monerodevs.org:28089",
),
)
suspend fun addDefaultRemoteNodes(appDatabase: AppDatabase) {
val dao = appDatabase.remoteNodeDao()
val nodes = DefaultNodeList.flatMap { (network, urls) ->
urls.map { url ->
RemoteNode(network = network, uri = Uri.parse(url)).asEntity()
}
}.toTypedArray()
dao.upsert(*nodes)
}

View File

@ -12,7 +12,6 @@ import im.molly.monero.isIsolatedProcess
val Context.preferencesDataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
StrictMode.setVmPolicy(
@ -23,7 +22,12 @@ class MainApplication : Application() {
if (isIsolatedProcess()) {
return
}
AppModule.provide(this)
AppModule.provide(
application = this,
populateInitialData = { db ->
addDefaultRemoteNodes(db)
},
)
SyncService.start(this)
}
}

View File

@ -75,6 +75,7 @@ private fun RemoteNodeItem(
headlineContent = {
Text(
text = remoteNode.uri.toString(),
style = MaterialTheme.typography.labelLarge,
modifier = (if (enabled) Modifier else Modifier.alpha(0.3f)),
)
},