Skip to content

Commit

Permalink
Merge pull request #110 from arkivanov/update-counter-sample
Browse files Browse the repository at this point in the history
Update sample Counter
  • Loading branch information
arkivanov authored Mar 29, 2021
2 parents cefba8d + 441f374 commit 79ac94d
Show file tree
Hide file tree
Showing 28 changed files with 206 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import com.arkivanov.decompose.extensions.compose.jetpack.rememberRootComponent
import com.arkivanov.decompose.instancekeeper.InstanceKeeper
import com.arkivanov.decompose.lifecycle.asDecomposeLifecycle
import com.arkivanov.decompose.statekeeper.StateKeeper
import com.arkivanov.sample.counter.shared.root.CounterRootContainer
import com.arkivanov.sample.counter.shared.root.CounterRootComponent
import com.arkivanov.sample.counter.shared.ui.android.CounterRootView
import com.arkivanov.sample.counter.shared.ui.compose.invoke
import com.arkivanov.sample.counter.shared.ui.compose.CounterRootUi

class MainActivity : AppCompatActivity() {

Expand All @@ -41,7 +41,8 @@ class MainActivity : AppCompatActivity() {
ComposeAppTheme {
Surface(color = MaterialTheme.colors.background) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
rememberRootComponent(::CounterRootContainer).model()
val component = rememberRootComponent(::CounterRootComponent)
CounterRootUi(component)
}
}
}
Expand All @@ -62,7 +63,7 @@ class MainActivity : AppCompatActivity() {
backPressedDispatcher = BackPressedDispatcher(onBackPressedDispatcher)
)

val root = CounterRootContainer(componentContext)
val root = CounterRootComponent(componentContext)

val viewContext =
DefaultViewContext(
Expand All @@ -72,7 +73,7 @@ class MainActivity : AppCompatActivity() {

viewContext.apply {
child(parent) {
CounterRootView(root.model)
CounterRootView(root)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.arkivanov.decompose.lifecycle.LifecycleRegistry
import com.arkivanov.decompose.lifecycle.destroy
import com.arkivanov.decompose.lifecycle.resume
import com.arkivanov.sample.counter.shared.renderableChild
import com.arkivanov.sample.counter.shared.root.CounterRootContainer
import com.arkivanov.sample.counter.shared.root.CounterRootComponent
import com.arkivanov.sample.counter.shared.root.RootR
import com.ccfraser.muirwik.components.mContainer
import com.ccfraser.muirwik.components.mCssBaseline
Expand All @@ -19,7 +19,7 @@ class App : RComponent<RProps, RState>() {

private val lifecycle = LifecycleRegistry()
private val ctx = DefaultComponentContext(lifecycle = lifecycle)
private val root = CounterRootContainer(ctx)
private val root = CounterRootComponent(ctx)

override fun componentDidMount() {
lifecycle.resume()
Expand All @@ -33,7 +33,7 @@ class App : RComponent<RProps, RState>() {
mCssBaseline()

mContainer(maxWidth = Breakpoint.xs) {
renderableChild(RootR::class, root.model)
renderableChild(RootR::class, root)
}
}
}
4 changes: 2 additions & 2 deletions sample/counter/ios-app/ios-app/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Counter

struct ContentView: View {
@State
private var componentHolder = ComponentHolder(factory: CounterRootContainerBuilderKt.CounterRootContainer)
private var componentHolder = ComponentHolder(factory: CounterRootComponent.init)

var body: some View {
CounterRootView(componentHolder.component.model)
CounterRootView(componentHolder.component)
.onAppear { LifecycleRegistryExtKt.resume(self.componentHolder.lifecycle) }
.onDisappear { LifecycleRegistryExtKt.stop(self.componentHolder.lifecycle) }
}
Expand Down
58 changes: 27 additions & 31 deletions sample/counter/ios-app/ios-app/CounterInnerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,36 @@ import SwiftUI
import Counter

struct CounterInnerView: View {
private let events: CounterInnerContainerEvents
private let counter: CounterModel
private let counterInner: CounterInner

@ObservedObject
private var leftChild: ObservableValue<RouterState<AnyObject, CounterInnerContainerChild>>
private var leftRouterState: ObservableValue<RouterState<AnyObject, CounterInnerChild>>

@ObservedObject
private var rightChild: ObservableValue<RouterState<AnyObject, CounterInnerContainerChild>>
private var rightRouterState: ObservableValue<RouterState<AnyObject, CounterInnerChild>>

init(_ model: CounterInnerContainerModel) {
self.events = model
self.counter = model.counter
self.leftChild = ObservableValue(model.leftChild)
self.rightChild = ObservableValue(model.rightChild)
init(_ counterInner: CounterInner) {
self.counterInner = counterInner
self.leftRouterState = ObservableValue(counterInner.leftRouterState)
self.rightRouterState = ObservableValue(counterInner.rightRouterState)
}

var body: some View {
let activeLeftChild = self.leftChild.value.activeChild.instance
let activeRightChild = self.rightChild.value.activeChild.instance

let activeLeftChild = self.leftRouterState.value.activeChild.instance
let activeRightChild = self.rightRouterState.value.activeChild.instance
return VStack(spacing: 8) {
CounterView(self.counter)
CounterView(self.counterInner.counter)

HStack(spacing: 8) {
ChildView(child: activeLeftChild, next: self.events.onNextLeftChild, prev: self.events.onPrevLeftChild)
ChildView(child: activeRightChild, next: self.events.onNextRightChild, prev: self.events.onPrevRightChild)
ChildView(child: activeLeftChild, next: self.counterInner.onNextLeftChild, prev: self.counterInner.onPrevLeftChild)
ChildView(child: activeRightChild, next: self.counterInner.onNextRightChild, prev: self.counterInner.onPrevRightChild)
}
}
}

private func ChildView(
child: CounterInnerContainerChild,
child: CounterInnerChild,
next: @escaping () -> Void,
prev: @escaping () -> Void
) -> some View {
Expand All @@ -58,26 +56,24 @@ struct CounterInnerView: View {

struct CounterInnerView_Previews: PreviewProvider {
static var previews: some View {
CounterInnerView(Model())
CounterInnerView(CounterInnerPreview())
}

class Model : CounterInnerContainerModel {
let counter: CounterModel = CounterView_Previews.Model()
class CounterInnerPreview : CounterInner {
let counter: Counter = CounterView_Previews.CounterPreview()

let leftChild: Value<RouterState<AnyObject, CounterInnerContainerChild>> =
simpleRouterState(
CounterInnerContainerChild(
counter: CounterView_Previews.Model(),
isBackEnabled: true
)
let leftRouterState: Value<RouterState<AnyObject, CounterInnerChild>> = simpleRouterState(
CounterInnerChild(
counter: CounterView_Previews.CounterPreview(),
isBackEnabled: true
)
)

let rightChild: Value<RouterState<AnyObject, CounterInnerContainerChild>> =
simpleRouterState(
CounterInnerContainerChild(
counter: CounterView_Previews.Model(),
isBackEnabled: false
)
let rightRouterState: Value<RouterState<AnyObject, CounterInnerChild>> = simpleRouterState(
CounterInnerChild(
counter: CounterView_Previews.CounterPreview(),
isBackEnabled: false
)
)

init() {
Expand Down
37 changes: 17 additions & 20 deletions sample/counter/ios-app/ios-app/CounterRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ import SwiftUI
import Counter

struct CounterRootView: View {
private let events: CounterRootContainerEvents
private let counter: CounterModel
private let counterRoot: CounterRoot

@ObservedObject
private var child: ObservableValue<RouterState<AnyObject, CounterRootContainerChild>>
private var routerState: ObservableValue<RouterState<AnyObject, CounterRootChild>>

init(_ model: CounterRootContainerModel) {
self.events = model
self.counter = model.counter
self.child = ObservableValue(model.child)
init(_ counterRoot: CounterRoot) {
self.counterRoot = counterRoot
self.routerState = ObservableValue(counterRoot.routerState)
}


var body: some View {
let activeChild = self.child.value.activeChild.instance
let activeChild = self.routerState.value.activeChild.instance

return VStack(spacing: 8) {
CounterView(self.counter)
CounterView(self.counterRoot.counter)

Button(action: self.events.onNextChild, label: { Text("Next Child") })
Button(action: self.counterRoot.onNextChild, label: { Text("Next Child") })

Button(action: self.events.onPrevChild, label: { Text("Prev Child") })
Button(action: self.counterRoot.onPrevChild, label: { Text("Prev Child") })
.disabled(!activeChild.isBackEnabled)

CounterInnerView(activeChild.inner)
Expand All @@ -41,18 +39,17 @@ struct CounterRootView: View {

struct CounterRootView_Previews: PreviewProvider {
static var previews: some View {
CounterRootView(Model())
CounterRootView(CoutnerRootPreview())
}

class Model : CounterRootContainerModel {
let counter: CounterModel = CounterView_Previews.Model()
class CoutnerRootPreview : CounterRoot {
let counter: Counter = CounterView_Previews.CounterPreview()

let child: Value<RouterState<AnyObject, CounterRootContainerChild>> =
simpleRouterState(
CounterRootContainerChild(
inner: CounterInnerView_Previews.Model(),
isBackEnabled: true
)
let routerState: Value<RouterState<AnyObject, CounterRootChild>> = simpleRouterState(
CounterRootChild(
inner: CounterInnerView_Previews.CounterInnerPreview(),
isBackEnabled: true
)
)

func onNextChild() {
Expand Down
16 changes: 8 additions & 8 deletions sample/counter/ios-app/ios-app/CounterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import Counter

struct CounterView: View {
@ObservedObject
private var data: ObservableValue<CounterData>
private var model: ObservableValue<CounterModel>

init(_ model: CounterModel) {
self.data = ObservableValue(model.data)
init(_ counter: Counter) {
self.model = ObservableValue(counter.model)
}

var body: some View {
Text(data.value.text)
Text(model.value.text)
.padding()
.border(Color.black, width: 2)
}
}

struct CounterView_Previews: PreviewProvider {
static var previews: some View {
CounterView(Model())
CounterView(CounterPreview())
}

class Model : CounterModel {
let data: Value<CounterData> = mutableValue(
CounterData(text: "Counter0: 100")
class CounterPreview : Counter {
let model: Value<CounterModel> = mutableValue(
CounterModel(text: "Counter0: 100")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.child
import com.arkivanov.decompose.extensions.android.layoutInflater
import com.arkivanov.sample.counter.shared.R
import com.arkivanov.sample.counter.shared.inner.CounterInnerContainer.Model
import com.arkivanov.sample.counter.shared.inner.CounterInner

@ExperimentalDecomposeApi
@Suppress("FunctionName") // Factory function
fun ViewContext.CounterInnerView(model: Model): View {
fun ViewContext.CounterInnerView(counterInner: CounterInner): View {
val root = layoutInflater.inflate(R.layout.counter_inner, parent, false)
val leftNextButton: View = root.findViewById(R.id.button_left_next)
val leftPrevButton: View = root.findViewById(R.id.button_left_prev)
Expand All @@ -20,22 +20,22 @@ fun ViewContext.CounterInnerView(model: Model): View {
val rightPrevButton: View = root.findViewById(R.id.button_right_prev)
val rightRouter: RouterView = root.findViewById(R.id.router_right)

leftNextButton.setOnClickListener { model.onNextLeftChild() }
leftPrevButton.setOnClickListener { model.onPrevLeftChild() }
rightNextButton.setOnClickListener { model.onNextRightChild() }
rightPrevButton.setOnClickListener { model.onPrevRightChild() }
leftNextButton.setOnClickListener { counterInner.onNextLeftChild() }
leftPrevButton.setOnClickListener { counterInner.onPrevLeftChild() }
rightNextButton.setOnClickListener { counterInner.onNextRightChild() }
rightPrevButton.setOnClickListener { counterInner.onPrevRightChild() }

child(root.findViewById(R.id.container_counter)) {
CounterView(model.counter)
CounterView(counterInner.counter)
}

leftRouter.children(model.leftChild, lifecycle) { parent, child, _ ->
leftRouter.children(counterInner.leftRouterState, lifecycle) { parent, child, _ ->
parent.removeAllViews()
parent.addView(CounterView(child.counter))
leftPrevButton.isEnabled = child.isBackEnabled
}

rightRouter.children(model.rightChild, lifecycle) { parent, child, _ ->
rightRouter.children(counterInner.rightRouterState, lifecycle) { parent, child, _ ->
parent.removeAllViews()
parent.addView(CounterView(child.counter))
rightPrevButton.isEnabled = child.isBackEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.child
import com.arkivanov.decompose.extensions.android.layoutInflater
import com.arkivanov.sample.counter.shared.R
import com.arkivanov.sample.counter.shared.root.CounterRootContainer.Model
import com.arkivanov.sample.counter.shared.root.CounterRoot

@ExperimentalDecomposeApi
@Suppress("FunctionName") // Factory function
fun ViewContext.CounterRootView(model: Model): View {
fun ViewContext.CounterRootView(counterRoot: CounterRoot): View {
val root = layoutInflater.inflate(R.layout.counter_root, parent, false)
val nextButton: View = root.findViewById(R.id.button_next)
val router: RouterView = root.findViewById(R.id.router)

nextButton.setOnClickListener { model.onNextChild() }
nextButton.setOnClickListener { counterRoot.onNextChild() }

child(root.findViewById(R.id.container_counter)) {
CounterView(model.counter)
CounterView(counterRoot.counter)
}

router.children(model.child, lifecycle) { parent, child, _ ->
router.children(counterRoot.routerState, lifecycle) { parent, child, _ ->
parent.removeAllViews()
parent.addView(CounterInnerView(child.inner))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.layoutInflater
import com.arkivanov.decompose.value.observe
import com.arkivanov.sample.counter.shared.R
import com.arkivanov.sample.counter.shared.counter.Counter.Model
import com.arkivanov.sample.counter.shared.counter.Counter

@ExperimentalDecomposeApi
@Suppress("FunctionName") // Factory function
fun ViewContext.CounterView(model: Model): View {
fun ViewContext.CounterView(counter: Counter): View {
val root = layoutInflater.inflate(R.layout.counter, parent, false)
val counterText: TextView = root.findViewById(R.id.text_count)

model.data.observe(lifecycle) { data ->
counter.model.observe(lifecycle) { data ->
counterText.text = data.text
}

Expand Down
Loading

0 comments on commit 79ac94d

Please sign in to comment.