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:
parent
82381c5381
commit
d36e4f276b
|
@ -78,7 +78,7 @@ class DataDbScannerUtil {
|
||||||
@Test
|
@Test
|
||||||
fun scanExistingDb() {
|
fun scanExistingDb() {
|
||||||
initializer.open(birthday)
|
initializer.open(birthday)
|
||||||
synchronizer = Synchronizer(context, initializer)
|
synchronizer = Synchronizer(initializer)
|
||||||
|
|
||||||
println("sync!")
|
println("sync!")
|
||||||
synchronizer.start()
|
synchronizer.start()
|
||||||
|
|
|
@ -22,7 +22,8 @@ import kotlin.reflect.KProperty
|
||||||
* used before on this device.
|
* used before on this device.
|
||||||
*
|
*
|
||||||
* @param appContext the application context, used to extract the storage paths for the databases
|
* @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 host the host that the synchronizer should use.
|
||||||
* @param port the port that the synchronizer should use when connecting to the host.
|
* @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
|
* @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,
|
val port: Int = ZcashSdk.DEFAULT_LIGHTWALLETD_PORT,
|
||||||
private val alias: String = ZcashSdk.DEFAULT_DB_NAME_PREFIX
|
private val alias: String = ZcashSdk.DEFAULT_DB_NAME_PREFIX
|
||||||
) {
|
) {
|
||||||
|
val context = appContext.applicationContext
|
||||||
|
|
||||||
init {
|
init {
|
||||||
validateAlias(alias)
|
validateAlias(alias)
|
||||||
}
|
}
|
||||||
|
@ -44,17 +47,17 @@ class Initializer(
|
||||||
* The path this initializer will use when checking for and downloading sapling params. This
|
* 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.
|
* 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.
|
* 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.
|
* 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
|
* A wrapped version of [cash.z.wallet.sdk.jni.RustBackendWelding] that will be passed to the
|
||||||
|
|
|
@ -403,7 +403,7 @@ fun Synchronizer(
|
||||||
initializer.import(seed, birthdayStore.getBirthday(), true, true)
|
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
|
* the wallet. From there, the initializer is passed to this function in order to start syncing from
|
||||||
* where the wallet left off.
|
* 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
|
* @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
|
* Synchronizer requires. It is mainly responsible for initializing the databases associated with
|
||||||
* this synchronizer.
|
* this synchronizer.
|
||||||
*/
|
*/
|
||||||
@Suppress("FunctionName")
|
@Suppress("FunctionName")
|
||||||
fun Synchronizer(
|
fun Synchronizer(initializer: Initializer): Synchronizer {
|
||||||
appContext: Context,
|
|
||||||
initializer: Initializer
|
|
||||||
): Synchronizer {
|
|
||||||
check(initializer.isInitialized) {
|
check(initializer.isInitialized) {
|
||||||
"Error: RustBackend must be loaded before creating the Synchronizer. Verify that either" +
|
"Error: RustBackend must be loaded before creating the Synchronizer. Verify that either" +
|
||||||
" the 'open', 'new' or 'import' function has been called on the Initializer."
|
" 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue