|
| 1 | +package com.pureswift.swiftandroid |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.ViewGroup |
| 5 | +import android.widget.FrameLayout |
| 6 | +import androidx.compose.foundation.layout.padding |
| 7 | +import androidx.compose.foundation.lazy.LazyColumn |
| 8 | +import androidx.compose.material3.Text |
| 9 | +import androidx.compose.runtime.getValue |
| 10 | +import androidx.compose.runtime.key |
| 11 | +import androidx.compose.runtime.mutableIntStateOf |
| 12 | +import androidx.compose.runtime.setValue |
| 13 | +import androidx.compose.ui.Modifier |
| 14 | +import androidx.compose.ui.platform.ComposeView |
| 15 | +import androidx.compose.ui.unit.dp |
| 16 | +import com.pureswift.swiftandroid.ui.theme.SwiftAndroidTheme |
| 17 | + |
| 18 | +// SwiftUI `List` backed by a Jetpack Compose `LazyColumn`, sourcing its rows from a Swift-implemented `ListViewAdapter`. |
| 19 | +// `ComposeView` is final, so it's hosted as a child of this `FrameLayout` rather than subclassed. |
| 20 | +class ComposeListView(context: Context, private val adapter: ListViewAdapter) : FrameLayout(context) { |
| 21 | + |
| 22 | + private var version by mutableIntStateOf(0) |
| 23 | + |
| 24 | + init { |
| 25 | + val composeView = ComposeView(context) |
| 26 | + composeView.setContent { |
| 27 | + SwiftAndroidTheme { |
| 28 | + key(version) { |
| 29 | + LazyColumn { |
| 30 | + items(adapter.getCount()) { index -> |
| 31 | + Text( |
| 32 | + text = adapter.getItem(index) as String, |
| 33 | + modifier = Modifier.padding(16.dp) |
| 34 | + ) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + addView(composeView, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) |
| 41 | + } |
| 42 | + |
| 43 | + fun getAdapter(): ListViewAdapter = adapter |
| 44 | + |
| 45 | + fun refresh() { |
| 46 | + version++ |
| 47 | + } |
| 48 | +} |
0 commit comments