shared-viewmodels

Share ViewModels across Android and iOS in Kotlin Multiplatform Mobile projects.

1|Updated Apr 24, 2026
One-click install
npx skills add https://github.com/dimitriRemoiville/cc-mobile --skill shared-viewmodels
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: shared-viewmodels
Source: https://github.com/dimitriRemoiville/cc-mobile/tree/main/plugins/cc-mobile-kmm/skills/shared-viewmodels
Command: npx skills add https://github.com/dimitriRemoiville/cc-mobile --skill shared-viewmodels

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Centralizes cross-platform UI state management by providing a canonical pattern for shared ViewModels in KMM projects, reducing boilerplate and divergence between Android and iOS implementations.

Core Features & Use Cases

  • Shared three-part contract: state: StateFlow, events: Flow via a Channel, and onAction(action) as the single entry point.
  • Canonical shape and example: A ViewModel exposes a public state, a channel-based events stream, and a single entry function, and is consumed from Jetpack Compose on Android and SwiftUI on iOS.
  • Non-null initial state, error handling with domain types, and testability guidance for ViewModels.

Quick Start

Install and adopt the shared-viewmodels pattern in your KMM project to coordinate UI state and actions across Android and iOS.

Frequently Asked Questions about shared-viewmodels

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I share ViewModels between Android and iOS in a Kotlin Multiplatform Mobile project?

To share ViewModels in KMM, you encapsulate a three-part contract exposing state via StateFlow, events via a Channel, and a single onAction entry point. This canonical pattern is consumed directly by Jetpack Compose and SwiftUI.

What is the best way to manage UI state with StateFlow in Kotlin Multiplatform Mobile?

Managing UI state with StateFlow in KMM requires a shared ViewModel that enforces non-null initial state and delegates business logic to use cases. This ensures platform-appropriate error handling and reduces divergence between Android and iOS.

Can I use a single entry point for UI actions in both Jetpack Compose and SwiftUI?

Yes, you can use a single onAction entry point in a KMM ViewModel to handle UI actions across both Jetpack Compose and SwiftUI. This centralizes action processing and reduces boilerplate in your platform-specific UI layers.

How do you handle UI events in a shared KMM ViewModel without losing data?

To handle UI events in a shared KMM ViewModel, you expose a Channel-based events stream alongside your StateFlow state. This ensures events are managed appropriately and consumed consistently across Android and iOS implementations.

Does the shared-viewmodels pattern require non-null initial state for SwiftUI and Compose?

Yes, the shared ViewModel pattern enforces non-null initial state to ensure both SwiftUI and Jetpack Compose receive valid data immediately upon subscription. This requirement prevents null-related rendering issues across platforms.

What are the limitations of using a three-part contract for cross-platform UI state management?

The limitation of this three-part contract is that it requires strict adherence to delegating business logic to use cases and managing domain-specific error types. This architectural enforcement may increase initial setup complexity for simple KMM applications.