Skip to content

Instantly share code, notes, and snippets.

@Maruchin1
Maruchin1 / UserInfo.kt
Last active February 3, 2025 18:37
Droids On Roids Compose Guidelines
@Composable
fun UserInfo(username: String, email: String, modifier: Modifier = Modifier) {
Column(
modifier = modifier
.fillMaxWidth()
.padding(12.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(text = username, style = MaterialTheme.typography.titleLarge)
Text(text = email, style = MaterialTheme.typography.bodyMedium)
actual interface Parcelable
actual interface Parcelable : android.os.Parcelable
actual typealias Parcelize = kotlinx.parcelize.Parcelize
expect interface Parcelable
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
expect annotation class Parcelize()
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class Product(
val id: String,
val name: String,
val description: String,
val price: Double
) : Parcelable
struct ProductListView: View {
let viewModel: ProductListViewModel
@State var products: [Product] = []
var body: View {
VStack {
...
}
.stateBinding($products, viewModel.products)
}
private struct StateBinding<State>: ViewModifier {
@Binding var state: State
let stateFlow: SkieSwiftStateFlow<State>
func body(content: Content) -> some View {
content.task {
for await state in stateFlow {
self.state = state
}
class ProductListViewModel : ViewModel() {
val products: StateFlow<List<Product>>
}
class UIHostingControllerWrapper<V: View, VM: ViewModel> : UIHostingController<V>, ViewModelStoreOwner {
let viewModelStore = ViewModelStore()
init(rootView: V, viewModel: VM) {
super.init(rootView: rootView)
let key = String(describing: VM.self)
viewModelStore.put(key: key, viewModel: viewModel)
}
object SharedModule {
fun init(networkConnection: NetworkConnection) {
check(koinApplication == null) { "Library already started" }
val nativeAppModule = module {
single { networkConnection }
}
koinApplication = koinApplication {
modules(nativeAppModule, loginModule, homeModule, networkingModule)
}