Skip to content

Commit

Permalink
Merge pull request #608 from arkivanov/imports-docs-extensions
Browse files Browse the repository at this point in the history
Added imports to code snippets in docs/extensions
  • Loading branch information
arkivanov authored Jan 17, 2024
2 parents ab7a214 + 7018cac commit 5017c95
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 66 deletions.
55 changes: 37 additions & 18 deletions docs/extensions/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,29 @@ You can find an example of using this extension module in the [Counter](https://
Initializing the root in `Activity`:

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.arkivanov.decompose.defaultComponentContext
import com.arkivanov.decompose.extensions.android.DefaultViewContext
import com.arkivanov.essenty.lifecycle.essentyLifecycle

setContentView(R.layout.main_activity)
class MainActivity : AppCompatActivity() {

val root = CounterRootComponent(defaultComponentContext())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val viewContext =
DefaultViewContext(
parent = findViewById(R.id.content),
lifecycle = essentyLifecycle()
)
setContentView(R.layout.main_activity)

viewContext.apply {
child(parent) {
CounterRootView(root)
val root = DefaultCounterComponent(defaultComponentContext())

val viewContext =
DefaultViewContext(
parent = findViewById(R.id.content),
lifecycle = essentyLifecycle()
)

viewContext.apply {
parent.addView(CounterView(root))
}
}
}
Expand All @@ -77,15 +84,21 @@ override fun onCreate(savedInstanceState: Bundle?) {
A simple child view:

```kotlin
fun ViewContext.CounterView(counter: Counter): View {
import android.view.View
import android.widget.TextView
import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.layoutInflater
import com.arkivanov.decompose.value.observe

fun ViewContext.CounterView(component: CounterComponent): View {
// Inflate the layout without adding it to the parent
val layout = layoutInflater.inflate(R.layout.counter, parent, false)

// Find required views
val counterText: TextView = layout.findViewById(R.id.text_count)

// Observe Counter models and update the view
counter.model.observe(lifecycle) { data ->
component.model.observe(lifecycle) { data ->
counterText.text = data.text
}

Expand All @@ -96,21 +109,27 @@ fun ViewContext.CounterView(counter: Counter): View {
`StackRouterView` example:

```kotlin
fun ViewContext.CounterRootView(counterRoot: CounterRoot): View {
import android.view.View
import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.child
import com.arkivanov.decompose.extensions.android.layoutInflater
import com.arkivanov.decompose.extensions.android.stack.StackRouterView

fun ViewContext.RootView(component: RootComponent): View {
val layout = layoutInflater.inflate(R.layout.counter_root, parent, false)
val nextButton: View = layout.findViewById(R.id.button_next)
val routerView: StackRouterView = layout.findViewById(R.id.router)

nextButton.setOnClickListener { counterRoot.onNextChild() }

// Create a child `ViewContext` for the `CounterView`
// Create a child `ViewContext` for the permanent `CounterView`
child(layout.findViewById(R.id.container_counter)) {
// Reuse the `CounterView`
CounterView(counterRoot.counter)
CounterView(component.counter)
}

// Subscribe the `StackRouterView` to the `ChildStack` changes
routerView.children(counterRoot.childStack, lifecycle) { parent, newStack, _ ->
routerView.children(component.childStack, lifecycle) { parent, newStack, _ ->
// Remove all existing views
parent.removeAllViews()

Expand Down
Loading

0 comments on commit 5017c95

Please sign in to comment.