Skip to content

Commit

Permalink
Construct ActivityStore directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 4, 2024
1 parent 1013f03 commit c122314
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 54 deletions.
20 changes: 12 additions & 8 deletions docs/Formula-Android.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ class MyApp : Application() {

FormulaAndroid.init(this) {
activity(MyActivity::class) {
store(MyActivityComponent(this)) {
bind(CounterFeatureFactory())
}
ActivityStore(
fragmentStore = FragmentFlowStore.init(MyActivityComponent(this)) {
bind(CounterFeatureFactory())
}
)
}
}
}
Expand Down Expand Up @@ -357,9 +359,11 @@ Now that we have our dependencies configured, let's bind the flow factory to our
val appComponent = AppComponent()
FormulaAndroid.init(this) {
activity(MyActivity::class) {
store(appComponent) {
bind(AuthFlowFactory())
}
ActivityStore(
fragmentStore = FragmentFlowStore.init(appComponent) {
bind(AuthFlowFactory())
}
)
}
}
```
Expand Down Expand Up @@ -393,7 +397,7 @@ class MyApp : Application() {
FormulaAndroid.init(this) {
activity<MyActivity> {

store(
ActivityStore(
streams = {
// You can subscribe to your RxJava streams here.
val timerState = Observable
Expand Down Expand Up @@ -453,7 +457,7 @@ FormulaAndroid.init(this) {
// This component will survive configuration changes.
val activityComponent = appComponent.createMyActivityComponent()

store(
ActivityStore(
configureActivity = {
// in this callback `this` is the instance of MyActivity
// so we can use it to inject dependencies
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class MyApp : Application() {

FormulaAndroid.init(this) {
activity<MyActivity> {
store(
ActivityStore(
streams = {
val formula = CounterFormula()
update(formula.toObservable(), MyActivity::render)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FormulaAppCompatActivity
import org.junit.Before
import org.junit.Rule
Expand All @@ -25,7 +26,7 @@ class ActivityLifecycleEventTest {
FormulaAndroid.init(app) {
activity<TestActivity> {
events = mutableListOf()
store(
ActivityStore(
streams = {
activityLifecycleState().subscribe {
events.add(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FormulaAppCompatActivity
import com.jakewharton.rxrelay3.PublishRelay
import org.junit.Before
Expand Down Expand Up @@ -35,7 +36,7 @@ class ActivityUpdateTest {
initFormula = { app ->
FormulaAndroid.init(app) {
activity<TestActivity> {
store(
ActivityStore(
streams = {
update(updateRelay, TestActivity::applyUpdate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FormulaAppCompatActivity
import io.reactivex.rxjava3.core.Observable
import org.junit.Before
Expand Down Expand Up @@ -38,7 +39,7 @@ class ActivityUpdateTimingTest {
initFormula = { app ->
FormulaAndroid.init(app) {
activity<TestActivity> {
store(
ActivityStore(
streams = {
update(updateRelay, TestActivity::applyUpdate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.Feature
import com.instacart.formula.android.FeatureFactory
import com.instacart.formula.android.FragmentFlowStore
Expand All @@ -26,9 +27,9 @@ class FragmentAndroidEventTest {
initFormula = { app ->
FormulaAndroid.init(app) {
activity<TestFragmentActivity> {
store(
ActivityStore(
configureActivity = {
initialContract = TestLifecycleKey()
it.initialContract = TestLifecycleKey()
},
fragmentStore = FragmentFlowStore.init {
val featureFactory = object : FeatureFactory<Unit, TestLifecycleKey> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FragmentFlowState
import com.instacart.formula.android.FragmentKey
import com.instacart.formula.android.BackCallback
Expand Down Expand Up @@ -40,10 +41,10 @@ class FragmentFlowRenderViewTest {
initFormula = { app ->
FormulaAndroid.init(app) {
activity<TestFragmentActivity> {
store(
configureActivity = {
initialContract = TestKey()
onPreCreated(this)
ActivityStore(
configureActivity = { activity ->
activity.initialContract = TestKey()
onPreCreated(activity)
},
onRenderFragmentState = { a, state ->
lastState = state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FormulaFragment
import com.instacart.formula.android.ActivityStoreContext
import com.instacart.formula.android.FeatureFactory
Expand Down Expand Up @@ -33,9 +34,9 @@ class FragmentLifecycleStateTest {
started = mutableListOf()
resumed = mutableListOf()

store(
ActivityStore(
configureActivity = {
initialContract = TestKey()
it.initialContract = TestKey()
},
fragmentStore = FragmentFlowStore.init {
bind(featureFactory<TestKey>(this@activity))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.Feature
import com.instacart.formula.android.FeatureFactory
import com.instacart.formula.android.FragmentFlowStore
Expand All @@ -31,11 +32,11 @@ class FragmentLifecycleTest {
@get:Rule val formulaRule = TestFormulaRule(initFormula = { app ->
FormulaAndroid.init(app) {
activity<TestFragmentActivity> {
store(
configureActivity = {
ActivityStore(
configureActivity = { activity ->
lifecycleCallback = TestFragmentLifecycleCallback()
contract = TestLifecycleKey()
initialContract = contract
activity.initialContract = contract
},
fragmentStore = FragmentFlowStore.init {
val featureFactory = object : FeatureFactory<Unit, TestLifecycleKey> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.reactivex.rxjava3.disposables.Disposable
* @param onFragmentLifecycleEvent This is callback for when a fragment is added or removed.
*/
class ActivityStore<Activity : FragmentActivity>(
val fragmentStore: FragmentFlowStore,
val fragmentStore: FragmentFlowStore = FragmentFlowStore.EMPTY,
val streams: (StreamConfigurator<Activity>.() -> Disposable)? = null,
val configureActivity: ((Activity) -> Unit)? = null,
val onRenderFragmentState: ((Activity, FragmentFlowState) -> Unit)? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,4 @@ abstract class ActivityStoreContext<out Activity : FragmentActivity> {
* it will do nothing.
*/
abstract fun send(effect: Activity.() -> Unit)

/**
* Creates an [ActivityStore].
*
* @param configureActivity This is called when activity is created before view inflation. You can use this to
* configure / inject the activity.
* @param onRenderFragmentState This is called after [FragmentFlowState] is applied to UI.
* @param onFragmentLifecycleEvent This is called after each [FragmentLifecycleEvent].
* @param streams This provides ability to configure arbitrary RxJava streams that survive
* configuration changes. Check [StreamConfigurator] for utility methods.
* @param fragmentStore [FragmentFlowStore] used to provide state management for individual screens.
*/
fun <ActivityT : FragmentActivity> store(
configureActivity: (ActivityT.() -> Unit)? = null,
onRenderFragmentState: ((ActivityT, FragmentFlowState) -> Unit)? = null,
onFragmentLifecycleEvent: ((FragmentLifecycleEvent) -> Unit)? = null,
streams: (StreamConfigurator<ActivityT>.() -> Disposable)? = null,
fragmentStore: FragmentFlowStore = FragmentFlowStore.EMPTY,
): ActivityStore<ActivityT> {
return ActivityStore(
fragmentStore = fragmentStore,
configureActivity = configureActivity,
onFragmentLifecycleEvent = onFragmentLifecycleEvent,
onRenderFragmentState = onRenderFragmentState,
streams = streams
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ActivityStoreFactoryTest {
environment = FragmentEnvironment(),
activities = {
activity(FakeActivity::class) {
store()
ActivityStore()
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.instacart.formula.compose.stopwatch
import android.app.Application
import android.util.Log
import com.instacart.formula.FormulaAndroid
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FragmentEnvironment
import com.instacart.formula.android.FragmentFlowStore

Expand All @@ -20,7 +21,7 @@ class StopwatchApp : Application() {
),
activities = {
activity<StopwatchActivity> {
store(
ActivityStore(
fragmentStore = FragmentFlowStore.init(Unit) {
bind(StopwatchFeatureFactory())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.util.Log
import com.examples.todoapp.tasks.TaskListFeatureFactory
import com.instacart.formula.FormulaAndroid
import com.instacart.formula.android.ActivityStore
import com.instacart.formula.android.FragmentEnvironment
import com.instacart.formula.android.FragmentFlowStore

Expand All @@ -23,7 +24,7 @@ class TodoApp : Application() {
activity<TodoActivity> {
val component = TodoAppComponent(this)

store(
ActivityStore(
fragmentStore = FragmentFlowStore.init(component) {
bind(TaskListFeatureFactory())
}
Expand Down

0 comments on commit c122314

Please sign in to comment.