A simple BMI (Body Mass Index) Calculator built using Kotlin Multiplatform (KMP) with:
- Jetpack Compose for Android
- SwiftUI for iOS
- Shared MVVM architecture using StateFlow
This project demonstrates how to build a real-world cross‑platform app with clean architecture and proper state management.
-
Input weight (kg) and height (cm)
-
BMI calculation using standard formula
-
Validation for empty and invalid inputs
-
BMI category detection:
- Underweight
- Normal
- Overweight
- Obese
-
Color‑coded BMI result
-
Shared business logic between Android & iOS
This app follows MVVM with Unidirectional Data Flow.
UI (Compose / SwiftUI)
↓ Intent
Shared ViewModel (KMP)
↓
StateFlow<BmiUiState>
↓
UI observes & renders
- Immutable state
- Predictable UI behavior
- Easy testing
- Platform‑independent business logic
BMI/
├── androidApp/ → Android app (Jetpack Compose)
├── iosApp/ → iOS app (SwiftUI)
└── shared/ → Shared KMP module
- BmiViewModel – handles logic & state
- BmiUiState – immutable UI state
- StateFlow – reactive state updates
data class BmiUiState(
val weight: String = "",
val height: String = "",
val bmiResult: BmiResult? = null,
val error: String? = null
)- Observes
StateFlowusingcollectAsState() - Fully declarative UI
- Color‑coded BMI output
val state by viewModel.uiState.collectAsState()ObservableObjectfor ViewModel wrapper@Publishedfor UI updates@MainActorfor thread safety- Manual
Bindingto avoid mutating Kotlin state directly
@Published var state: BmiUiState?State updates are dispatched via ViewModel functions, not direct mutation.
- Weight must be > 0
- Height must be > 0
- Non‑numeric input is rejected
- Error messages shown in UI
| Category | Color |
|---|---|
| Underweight | Blue |
| Normal | Green |
| Overweight | Orange |
| Obese | Red |
BMI = weight (kg) / (height (m) * height (m))
- Kotlin Coroutines
- StateFlow
- Jetpack Compose
- SwiftUI
- Open project in Android Studio
- Select
androidApp - Run on emulator or device
- Open
iosApp.xcodeprojin Xcode - Select simulator
- Run
This project helps you understand:
- Kotlin Multiplatform basics
- Sharing ViewModels across platforms
- StateFlow ↔ SwiftUI integration
- Immutable state handling
- Clean MVVM architecture
- Unit conversion (kg/lbs, cm/ft)
- Save BMI history
- Charts & analytics
- Dependency Injection (Koin)
- Unit tests
MIT License
Manabendu
Built as a learning‑oriented KMP sample project 🚀