diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b4b46a..90f71fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Change Log +### Kotlin AndroidX Account 1.1.3 *(2020-02-07)* + + * Migrate schema to support deferred streams. + ### Kotlin AndroidX Account 1.1.2 *(2019-12-04)* * Fix single account scheme to remove only authenticator accounts. diff --git a/README.md b/README.md index d9ae11e..eb48e84 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Add dependencies to the *Kotlin-based* project: ```groovy dependencies { - implementation "co.windly:ktx-account:1.1.2" - kapt "co.windly:ktx-account-compiler:1.1.2" + implementation "co.windly:ktx-account:1.1.3" + kapt "co.windly:ktx-account-compiler:1.1.3" } ``` diff --git a/gradle.properties b/gradle.properties index deea0fb..d74941d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ #region Maven Central -VERSION_NAME=1.1.3-SNAPSHOT +VERSION_NAME=1.1.4-SNAPSHOT GROUP=co.windly POM_DESCRIPTION=Maintenance POM_URL=https://github.com/tommus/ktx-account diff --git a/ktx-account-compiler/src/main/resources/co/windly/ktxaccount/compiler/scheme.ftl b/ktx-account-compiler/src/main/resources/co/windly/ktxaccount/compiler/scheme.ftl index d0ea5f3..4ace846 100644 --- a/ktx-account-compiler/src/main/resources/co/windly/ktxaccount/compiler/scheme.ftl +++ b/ktx-account-compiler/src/main/resources/co/windly/ktxaccount/compiler/scheme.ftl @@ -44,9 +44,11 @@ abstract class ${schemeClassName}(context: Context) : <#if classEnableReactive> open fun clearRx(<#if schemaMode == "multiple">name: String): Completable = - Completable - .fromAction { clear(<#if schemaMode == "multiple">name) } - .subscribeOn(Schedulers.io()) + Completable.defer { + Completable + .fromAction { clear(<#if schemaMode == "multiple">name) } + } + .subscribeOn(Schedulers.io()) //endregion @@ -84,16 +86,18 @@ abstract class ${schemeClassName}(context: Context) : .andThen(clearSubjects()) private fun clearSubjects(): Completable = - Completable - .fromAction { - <#list descriptorList as descriptor> - - // Tear down "${descriptor.propertyName}" property subject. - if (${descriptor.fieldName}Subject != null) { - ${descriptor.fieldName}Subject = null + Completable.defer { + Completable + .fromAction { + <#list descriptorList as descriptor> + + // Tear down "${descriptor.propertyName}" property subject. + if (${descriptor.fieldName}Subject != null) { + ${descriptor.fieldName}Subject = null + } + } - - } + } //endregion <#list descriptorList as descriptor> @@ -189,9 +193,11 @@ abstract class ${schemeClassName}(context: Context) : */ <#t> open fun saveRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String, ${descriptor.fieldName}: ${descriptor.type.simpleName}): Completable = - Completable - .fromAction { save${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name, ${descriptor.fieldName}) } - .subscribeOn(Schedulers.io()) + Completable.defer { + Completable + .fromAction { save${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name, ${descriptor.fieldName}) } + } + .subscribeOn(Schedulers.io()) <#if descriptor.enableReactive> @@ -201,32 +207,34 @@ abstract class ${schemeClassName}(context: Context) : */ <#t> open fun getRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String): Single<${descriptor.type.simpleName}> = - Single - .fromPublisher<${descriptor.type.simpleName}> { - - // Retrieve account. - val account = retrieveNullableAccount(<#if schemaMode == "multiple">name) - - // Emit an error for non-existent account. - if (account == null) { - it.onError(NoSuchElementException("Account does not exist.")) - it.onComplete() - return@fromPublisher - } - - // Retrieve property. - val property = manager.getUserData(account, KEY_${descriptor.fieldNameUpperCase}) - - // Complete with default value if property does not exist. - if (property.isNullOrBlank()) { - it.onNext(DEFAULT_${descriptor.fieldNameUpperCase}) + Single.defer<${descriptor.type.simpleName}> { + Single + .fromPublisher<${descriptor.type.simpleName}> { + + // Retrieve account. + val account = retrieveNullableAccount(<#if schemaMode == "multiple">name) + + // Emit an error for non-existent account. + if (account == null) { + it.onError(NoSuchElementException("Account does not exist.")) + it.onComplete() + return@fromPublisher + } + + // Retrieve property. + val property = manager.getUserData(account, KEY_${descriptor.fieldNameUpperCase}) + + // Complete with default value if property does not exist. + if (property.isNullOrBlank()) { + it.onNext(DEFAULT_${descriptor.fieldNameUpperCase}) + it.onComplete() + return@fromPublisher + } + + // Emit property. + it.onNext(property.to${descriptor.type.simpleName}()) it.onComplete() - return@fromPublisher } - - // Emit property. - it.onNext(property.to${descriptor.type.simpleName}()) - it.onComplete() } .subscribeOn(Schedulers.io()) @@ -238,8 +246,11 @@ abstract class ${schemeClassName}(context: Context) : */ <#t> open fun removeRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String): Completable = - Completable - .fromAction { remove${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name) } + Completable.defer { + Completable + .fromAction { remove${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name) } + } + .subscribeOn(Schedulers.io()) <#if descriptor.enableReactive> @@ -249,11 +260,14 @@ abstract class ${schemeClassName}(context: Context) : */ <#t> open fun observeRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String): Flowable<${descriptor.type.simpleName}> = + Flowable.defer<${descriptor.type.simpleName}> { retrieve${descriptor.fieldName?cap_first}Subject(<#if schemaMode == "multiple">name) .toFlowable(LATEST) <#if descriptor.distinctUntilChanged> .distinctUntilChanged() + } + .subscribeOn(Schedulers.io()) //endregion