-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subscription extension methods and sample code (#8)
* β¨ Add SubscriptionExtensions.kt * π¨βπ Add user subscription to demonstrate composite subscription
- Loading branch information
1 parent
c24dece
commit fe352f0
Showing
9 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...able-architecture/src/main/java/com/toggl/komposable/extensions/SubscriptionExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.toggl.komposable.extensions | ||
|
||
import com.toggl.komposable.architecture.Subscription | ||
import com.toggl.komposable.internal.CompositeSubscription | ||
|
||
/** | ||
* @param subscriptions List of subscriptions which should be merged | ||
* @return A [CompositeSubscription] of a given subscriptions | ||
* @see CompositeSubscription | ||
*/ | ||
fun <State, Action : Any> mergeSubscriptions(vararg subscriptions: Subscription<State, Action>): Subscription<State, Action> = | ||
CompositeSubscription(subscriptions.toList()) | ||
|
||
/** | ||
* @receiver First subscription | ||
* @param subscription Second subscription that will be merged with the receiver | ||
* @return A [CompositeSubscription] of a given subscriptions | ||
* @see CompositeSubscription | ||
*/ | ||
infix fun <State, Action : Any> Subscription<State, Action>.mergeWith(subscription: Subscription<State, Action>): Subscription<State, Action> = | ||
mergeSubscriptions(this, subscription) |
2 changes: 2 additions & 0 deletions
2
todo-sample/src/main/java/com/toggl/komposable/sample/todo/AppAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
todo-sample/src/main/java/com/toggl/komposable/sample/todo/AuthReducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.toggl.komposable.sample.todo | ||
|
||
import com.toggl.komposable.architecture.Effect | ||
import com.toggl.komposable.architecture.Mutable | ||
import com.toggl.komposable.architecture.Reducer | ||
import com.toggl.komposable.extensions.mutateWithoutEffects | ||
import com.toggl.komposable.extensions.noEffect | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AuthReducer @Inject constructor() : Reducer<AppState, AppAction> { | ||
override fun reduce(state: Mutable<AppState>, action: AppAction): List<Effect<AppAction>> = | ||
when (action) { | ||
is AppAction.IdentityUpdated -> state.mutateWithoutEffects { copy(identity = action.newIdentity) } | ||
else -> noEffect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
todo-sample/src/main/java/com/toggl/komposable/sample/todo/data/AccountManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.toggl.komposable.sample.todo.data | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AccountManager @Inject constructor() { | ||
fun getLoggedInUser(): Flow<Identity> = flow { | ||
emit(Identity.Unknown) | ||
delay(3000) | ||
emit(Identity.User("John Balance", "[email protected]")) | ||
} | ||
} | ||
|
||
sealed class Identity { | ||
object Unknown : Identity() | ||
data class User(val username: String, val email: String) : Identity() | ||
} |
15 changes: 15 additions & 0 deletions
15
todo-sample/src/main/java/com/toggl/komposable/sample/todo/data/UserSubscription.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.toggl.komposable.sample.todo.data | ||
|
||
import com.toggl.komposable.architecture.Subscription | ||
import com.toggl.komposable.sample.todo.AppAction | ||
import com.toggl.komposable.sample.todo.AppState | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class UserSubscription @Inject constructor(private val accountManager: AccountManager) : Subscription<AppState, AppAction> { | ||
override fun subscribe(state: Flow<AppState>): Flow<AppAction> = | ||
accountManager.getLoggedInUser().map { AppAction.IdentityUpdated(it) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters