Simplified the construction of a synchronizer.

There is no harm in letting the initializer hold a reference to the application context. So let it do that in order to simplify the construction of synchronizers.
This commit is contained in:
Kevin Gorham 2020-03-27 16:26:41 -04:00
parent 82381c5381
commit d36e4f276b
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
3 changed files with 11 additions and 13 deletions

View File

@ -78,7 +78,7 @@ class DataDbScannerUtil {
@Test
fun scanExistingDb() {
initializer.open(birthday)
synchronizer = Synchronizer(context, initializer)
synchronizer = Synchronizer(initializer)
println("sync!")
synchronizer.start()

View File

@ -22,7 +22,8 @@ import kotlin.reflect.KProperty
* used before on this device.
*
* @param appContext the application context, used to extract the storage paths for the databases
* and param files. A reference to the context is not held beyond initialization.
* and param files. A reference to the context is held beyond initialization in order to simplify
* synchronizer construction so that an initializer is all that is ever required.
* @param host the host that the synchronizer should use.
* @param port the port that the synchronizer should use when connecting to the host.
* @param alias the alias to use for this synchronizer. Think of it as a unique name that allows
@ -36,6 +37,8 @@ class Initializer(
val port: Int = ZcashSdk.DEFAULT_LIGHTWALLETD_PORT,
private val alias: String = ZcashSdk.DEFAULT_DB_NAME_PREFIX
) {
val context = appContext.applicationContext
init {
validateAlias(alias)
}
@ -44,17 +47,17 @@ class Initializer(
* The path this initializer will use when checking for and downloading sapling params. This
* value is derived from the appContext when this class is constructed.
*/
private val pathParams: String = "${appContext.cacheDir.absolutePath}/params"
private val pathParams: String = "${context.cacheDir.absolutePath}/params"
/**
* The path used for storing cached compact blocks for processing.
*/
private val pathCacheDb: String = cacheDbPath(appContext, alias)
private val pathCacheDb: String = cacheDbPath(context, alias)
/**
* The path used for storing the data derived from the cached compact blocks.
*/
private val pathDataDb: String = dataDbPath(appContext, alias)
private val pathDataDb: String = dataDbPath(context, alias)
/**
* A wrapped version of [cash.z.wallet.sdk.jni.RustBackendWelding] that will be passed to the

View File

@ -403,7 +403,7 @@ fun Synchronizer(
initializer.import(seed, birthdayStore.getBirthday(), true, true)
}
}
return Synchronizer(appContext, initializer)
return Synchronizer(initializer)
}
/**
@ -413,22 +413,17 @@ fun Synchronizer(
* the wallet. From there, the initializer is passed to this function in order to start syncing from
* where the wallet left off.
*
* @param appContext the application context. This is mostly used for finding databases and params
* files within the apps secure storage area.
* @param initializer the helper that is leveraged for creating all the components that the
* Synchronizer requires. It is mainly responsible for initializing the databases associated with
* this synchronizer.
*/
@Suppress("FunctionName")
fun Synchronizer(
appContext: Context,
initializer: Initializer
): Synchronizer {
fun Synchronizer(initializer: Initializer): Synchronizer {
check(initializer.isInitialized) {
"Error: RustBackend must be loaded before creating the Synchronizer. Verify that either" +
" the 'open', 'new' or 'import' function has been called on the Initializer."
}
return Synchronizer(appContext, initializer.rustBackend, initializer.host, initializer.port)
return Synchronizer(initializer.context, initializer.rustBackend, initializer.host, initializer.port)
}
/**