2019-01-29 10:08:33 -08:00
package cash.z.android.wallet.di.module
2019-02-27 03:44:44 -08:00
import android.content.SharedPreferences
import android.preference.PreferenceManager
2019-01-29 10:08:33 -08:00
import cash.z.android.wallet.BuildConfig
import cash.z.android.wallet.ZcashWalletApplication
2019-02-27 03:44:44 -08:00
import cash.z.android.wallet.sample.*
2019-02-12 16:09:06 -08:00
import cash.z.android.wallet.sample.SampleProperties.COMPACT_BLOCK_PORT
import cash.z.android.wallet.sample.SampleProperties.COMPACT_BLOCK_SERVER
2019-02-27 03:44:44 -08:00
import cash.z.android.wallet.sample.SampleProperties.PREFS_SERVER_NAME
import cash.z.android.wallet.sample.SampleProperties.PREFS_WALLET_DISPLAY_NAME
2019-01-29 10:08:33 -08:00
import cash.z.wallet.sdk.data.*
import cash.z.wallet.sdk.jni.JniConverter
import cash.z.wallet.sdk.secure.Wallet
import dagger.Module
import dagger.Provides
2019-02-27 03:44:44 -08:00
import javax.inject.Named
2019-01-29 10:08:33 -08:00
import javax.inject.Singleton
/ * *
* Module that contributes all the objects necessary for the synchronizer , which is basically everything that has
* application scope .
* /
@Module
internal object SynchronizerModule {
2019-02-27 03:44:44 -08:00
@JvmStatic
@Provides
@Singleton
fun providePrefs ( ) : SharedPreferences {
return PreferenceManager . getDefaultSharedPreferences ( ZcashWalletApplication . instance )
}
2019-05-21 04:29:51 -07:00
@JvmStatic
@Provides
@Singleton
fun provideJniConverter ( ) : JniConverter {
return JniConverter ( ) . also {
if ( BuildConfig . DEBUG ) it . initLogs ( )
}
}
2019-02-27 03:44:44 -08:00
@JvmStatic
@Provides
@Singleton
fun provideWalletConfig ( prefs : SharedPreferences ) : WalletConfig {
val walletName = prefs . getString ( PREFS _WALLET _DISPLAY _NAME , null )
twig ( " FOUND WALLET DISPLAY NAME : $walletName " )
return when ( walletName ) {
AliceWallet . displayName -> AliceWallet
BobWallet . displayName , null -> BobWallet // Default wallet
CarolWallet . displayName -> CarolWallet
DaveWallet . displayName -> DaveWallet
else -> WalletConfig . create ( walletName )
}
}
@JvmStatic
@Provides
@Singleton
@Named ( PREFS _SERVER _NAME )
fun provideServer ( prefs : SharedPreferences ) : String {
val serverName = prefs . getString ( PREFS _SERVER _NAME , null )
// in theory, the actual stored value itself could be null so provide the default this way to be safe
val server = Servers . values ( ) . firstOrNull { it . displayName == serverName } ?. host ?: COMPACT _BLOCK _SERVER //TODO: validate that this is a hostname or IP. For now use default, instead
twig ( " FOUND SERVER DISPLAY NAME : $serverName ( $server ) " )
return server
}
2019-01-29 10:08:33 -08:00
@JvmStatic
@Provides
@Singleton
2019-02-27 03:44:44 -08:00
fun provideDownloader ( @Named ( PREFS _SERVER _NAME ) server : String , twigger : Twig ) : CompactBlockStream {
return CompactBlockStream ( server , COMPACT _BLOCK _PORT , twigger )
2019-01-29 10:08:33 -08:00
}
@JvmStatic
@Provides
@Singleton
2019-05-21 04:29:51 -07:00
fun provideProcessor ( application : ZcashWalletApplication , converter : JniConverter , walletConfig : WalletConfig ) : CompactBlockProcessor {
return CompactBlockProcessor ( application , converter , walletConfig . cacheDbName , walletConfig . dataDbName )
2019-01-29 10:08:33 -08:00
}
@JvmStatic
@Provides
@Singleton
2019-02-27 03:44:44 -08:00
fun provideRepository ( application : ZcashWalletApplication , walletConfig : WalletConfig , converter : JniConverter ) : TransactionRepository {
return PollingTransactionRepository ( application , walletConfig . dataDbName , 10 _000L )
2019-01-29 10:08:33 -08:00
}
@JvmStatic
@Provides
@Singleton
2019-02-27 03:44:44 -08:00
fun provideWallet ( application : ZcashWalletApplication , walletConfig : WalletConfig , converter : JniConverter ) : Wallet {
2019-02-24 11:21:49 -08:00
return Wallet (
context = application ,
converter = converter ,
2019-02-27 03:44:44 -08:00
dataDbPath = application . getDatabasePath ( walletConfig . dataDbName ) . absolutePath ,
2019-02-24 11:21:49 -08:00
paramDestinationDir = " ${application.cacheDir.absolutePath} /params " ,
2019-02-27 03:44:44 -08:00
seedProvider = walletConfig . seedProvider ,
spendingKeyProvider = walletConfig . spendingKeyProvider
2019-02-24 11:21:49 -08:00
)
2019-01-29 10:08:33 -08:00
}
2019-02-02 21:45:51 -08:00
@JvmStatic
@Provides
@Singleton
2019-05-21 04:29:51 -07:00
fun provideManager ( wallet : Wallet , repository : TransactionRepository , downloader : CompactBlockStream ) : ActiveTransactionManager {
return ActiveTransactionManager ( repository , downloader . connection , wallet )
2019-02-07 19:18:32 -08:00
}
2019-01-29 10:08:33 -08:00
@JvmStatic
@Provides
@Singleton
fun provideSynchronizer (
downloader : CompactBlockStream ,
processor : CompactBlockProcessor ,
repository : TransactionRepository ,
2019-02-02 21:45:51 -08:00
manager : ActiveTransactionManager ,
2019-02-21 07:32:21 -08:00
wallet : Wallet
2019-01-29 10:08:33 -08:00
) : Synchronizer {
2019-05-21 04:29:51 -07:00
return SdkSynchronizer ( downloader , processor , repository , manager , wallet , batchSize = 100 , blockPollFrequency = 50 _000L )
2019-01-29 10:08:33 -08:00
}
}