Skip to content

Commit

Permalink
Merge branch 'feature/schema-deferred' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Dzieniak committed Feb 26, 2020
2 parents b1b4651 + fb96190 commit c937a9e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 66 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
## 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.

### Kotlin AndroidX Account 1.1.1 *(2019-11-12)*

* Make `removeAccount()` method dependant on scheme mode.

### Kotlin AndroidX Account 1.1.0 *(2019-11-12)*

* Add `mode` property to `AccountScheme`.

### Kotlin AndroidX Account 1.0.1 *(2019-11-12)*

* Correct constant package names in scheme;
* Refactor schema code to remove duplications.

### Kotlin AndroidX Account 1.0.0 *(2019-11-12)*

* Stable library release.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Add dependencies to the *Kotlin-based* project:

```groovy
dependencies {
implementation "co.windly:ktx-account:1.0.0"
kapt "co.windly:ktx-account-compiler:1.0.0"
implementation "co.windly:ktx-account:1.1.3"
kapt "co.windly:ktx-account-compiler:1.1.3"
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region Maven Central
VERSION_NAME=1.0.1-SNAPSHOT
VERSION_NAME=1.1.4-SNAPSHOT
GROUP=co.windly
POM_DESCRIPTION=Maintenance
POM_URL=https://github.com/tommus/ktx-account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ abstract class ${schemeClassName}(context: Context) :

<#if classEnableReactive>
open fun clearRx(<#if schemaMode == "multiple">name: String</#if>): Completable =
Completable
.fromAction { clear(<#if schemaMode == "multiple">name</#if>) }
.subscribeOn(Schedulers.io())
Completable.defer {
Completable
.fromAction { clear(<#if schemaMode == "multiple">name</#if>) }
}
.subscribeOn(Schedulers.io())

</#if>
//endregion
Expand Down Expand Up @@ -78,22 +80,24 @@ abstract class ${schemeClassName}(context: Context) :
</#list>
}

override fun removeAccount(name: String): Completable =
override fun removeAccount(<#if schemaMode == "multiple">name: String</#if>): Completable =
super
.removeAccount(name)
.removeAccount(<#if schemaMode == "multiple">name</#if>)
.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
}
</#list>
}
</#list>
}
}

//endregion
<#list descriptorList as descriptor>
Expand Down Expand Up @@ -189,9 +193,11 @@ abstract class ${schemeClassName}(context: Context) :
*/
</#if><#t>
open fun saveRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String, </#if>${descriptor.fieldName}: ${descriptor.type.simpleName}): Completable =
Completable
.fromAction { save${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name, </#if>${descriptor.fieldName}) }
.subscribeOn(Schedulers.io())
Completable.defer {
Completable
.fromAction { save${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name, </#if>${descriptor.fieldName}) }
}
.subscribeOn(Schedulers.io())

</#if>
<#if descriptor.enableReactive>
Expand All @@ -201,32 +207,34 @@ abstract class ${schemeClassName}(context: Context) :
*/
</#if><#t>
open fun getRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String</#if>): Single<${descriptor.type.simpleName}> =
Single
.fromPublisher<${descriptor.type.simpleName}> {

// Retrieve account.
val account = retrieveNullableAccount(<#if schemaMode == "multiple">name</#if>)

// 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</#if>)

// 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())

Expand All @@ -238,8 +246,11 @@ abstract class ${schemeClassName}(context: Context) :
*/
</#if><#t>
open fun removeRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String</#if>): Completable =
Completable
.fromAction { remove${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name</#if>) }
Completable.defer {
Completable
.fromAction { remove${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name</#if>) }
}
.subscribeOn(Schedulers.io())

</#if>
<#if descriptor.enableReactive>
Expand All @@ -249,11 +260,14 @@ abstract class ${schemeClassName}(context: Context) :
*/
</#if><#t>
open fun observeRx${descriptor.fieldName?cap_first}(<#if schemaMode == "multiple">name: String</#if>): Flowable<${descriptor.type.simpleName}> =
Flowable.defer<${descriptor.type.simpleName}> {
retrieve${descriptor.fieldName?cap_first}Subject(<#if schemaMode == "multiple">name</#if>)
.toFlowable(LATEST)
<#if descriptor.distinctUntilChanged>
.distinctUntilChanged()
</#if>
}
.subscribeOn(Schedulers.io())

</#if>
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,4 @@ abstract class BaseAccountScheme(private val context: Context) {
manager.addAccountExplicitly(account, password, null)
}
.subscribeOn(Schedulers.io())

/**
* Removes account associated with given email.
*/
open fun removeAccount(name: String): Completable =
Completable
.fromAction {

// Retrieve account.
val account = manager
.getAccountsByType(provideAuthenticator())
.first { it.name == name }

// Remove account.
@Suppress("DEPRECATION")
manager.removeAccount(account, null, null)
}
.subscribeOn(Schedulers.io())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package co.windly.ktxaccount.runtime.scheme

import android.accounts.Account
import android.content.Context
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers

/**
* This scheme assumes MULTIPLE accounts for given authenticator can exist.
Expand Down Expand Up @@ -41,5 +43,23 @@ abstract class MultipleAccountScheme(context: Context) : BaseAccountScheme(conte
.firstOrNull { it.name == name }
}

/**
* Removes account associated with given email.
*/
open fun removeAccount(name: String): Completable =
Completable
.fromAction {

// Retrieve account.
val account = manager
.getAccountsByType(provideAuthenticator())
.first { it.name == name }

// Remove account.
@Suppress("DEPRECATION")
manager.removeAccount(account, null, null)
}
.subscribeOn(Schedulers.io())

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package co.windly.ktxaccount.runtime.scheme
import android.accounts.Account
import android.content.Context
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers

/**
* This scheme assumes ONE and ONLY account for given authenticator will ever exist.
Expand All @@ -28,6 +29,12 @@ abstract class SingleAccountScheme(context: Context) : BaseAccountScheme(context
clearExistingAccounts()
.andThen(super.saveAccount(name, password))

/**
* Removes account associated with given email.
*/
open fun removeAccount(): Completable =
clearExistingAccounts()

/**
* Retrieves an account associated with provided authenticator and identified by
* given name.
Expand Down Expand Up @@ -65,12 +72,15 @@ abstract class SingleAccountScheme(context: Context) : BaseAccountScheme(context
.fromAction {

// Clears all accounts associated with this authenticator.
manager.accounts.forEach {
manager
.getAccountsByType(provideAuthenticator())
.forEach {

@Suppress("DEPRECATION")
manager.removeAccount(it, null, null)
}
}
.subscribeOn(Schedulers.io())

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MainActivity : Activity() {

// Remove account.
manager
.removeAccount(name)
.removeAccount()
.subscribe(
::handleRemoveAccountSuccess,
::handleRemoveAccountError
Expand Down

0 comments on commit c937a9e

Please sign in to comment.