-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
190 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,82 @@ | ||
This is a Kotlin Multiplatform project targeting Android, iOS, Web, Desktop. | ||
# MVVMate | ||
|
||
* `/composeApp` is for code that will be shared across your Compose Multiplatform applications. | ||
It contains several subfolders: | ||
- `commonMain` is for code that’s common for all targets. | ||
- Other folders are for Kotlin code that will be compiled for only the platform indicated in the folder name. | ||
For example, if you want to use Apple’s CoreCrypto for the iOS part of your Kotlin app, | ||
`iosMain` would be the right folder for such calls. | ||
MVVMate is a minimal state management library for Compose Multiplatform, based on the MVVM architecture. | ||
|
||
* `/iosApp` contains iOS applications. Even if you’re sharing your UI with Compose Multiplatform, | ||
you need this entry point for your iOS app. This is also where you should add SwiftUI code for your project. | ||
[![Maven Central](https://img.shields.io/maven-central/v/com.helloanwar.mvvmate.core)](https://search.maven.org/artifact/com.helloanwar/mvvmate.core) | ||
[![Documentation](https://img.shields.io/badge/docs-API-blue)](https://anwarpro.github.io/mvvmate) | ||
|
||
## Overview | ||
|
||
Learn more about [Kotlin Multiplatform](https://www.jetbrains.com/help/kotlin-multiplatform-dev/get-started.html), | ||
[Compose Multiplatform](https://github.com/JetBrains/compose-multiplatform/#compose-multiplatform), | ||
[Kotlin/Wasm](https://kotl.in/wasm/)… | ||
This library provides base classes and interfaces for managing UI state, handling user actions, and emitting side effects in a Compose Multiplatform project. | ||
|
||
We would appreciate your feedback on Compose/Web and Kotlin/Wasm in the public Slack channel [#compose-web](https://slack-chats.kotlinlang.org/c/compose-web). | ||
If you face any issues, please report them on [GitHub](https://github.com/JetBrains/compose-multiplatform/issues). | ||
### Key Components | ||
|
||
You can open the web application by running the `:composeApp:wasmJsBrowserDevelopmentRun` Gradle task. | ||
- **BaseViewModel**: A base ViewModel class that manages state and handles user actions. | ||
- **BaseViewModelWithEffect**: Extends `BaseViewModel` to also manage UI side effects. | ||
- **UiState**: A marker interface for defining UI states. | ||
- **UiAction**: A marker interface for user actions. | ||
- **UiEffect**: A marker interface for UI side effects. | ||
|
||
## Installation | ||
|
||
To use MVVMate in your project, add the following to your `build.gradle.kts`: | ||
|
||
### For Kotlin Multiplatform Projects: | ||
|
||
```kotlin | ||
kotlin { | ||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
implementation("com.helloanwar.mvvmate.core:1.0.0") | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### For Android Projects: | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("com.helloanwar.mvvmate.core:1.0.0") | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Here's a simple example of how you can use `MVVMate` in your Compose Multiplatform project: | ||
|
||
```kotlin | ||
// Define your UI State | ||
data class MyUiState(val message: String = "") : UiState | ||
|
||
// Define your User Actions | ||
sealed class MyUiAction : UiAction { | ||
object ShowMessage : MyUiAction() | ||
} | ||
|
||
// Define your ViewModel | ||
class MyViewModel : BaseViewModel<MyUiState, MyUiAction>(MyUiState()) { | ||
override suspend fun onAction(action: MyUiAction) { | ||
when (action) { | ||
MyUiAction.ShowMessage -> updateState { copy(message = "Hello, World!") } | ||
} | ||
} | ||
} | ||
|
||
// Use the ViewModel in your Composable function | ||
@Composable | ||
fun MyScreen(viewModel: MyViewModel) { | ||
val state by viewModel.state.collectAsState() | ||
|
||
Text(text = state.message) | ||
Button(onClick = { viewModel.handleAction(MyUiAction.ShowMessage) }) { | ||
Text("Show Message") | ||
} | ||
} | ||
``` | ||
|
||
## API Documentation | ||
|
||
For more detailed information, you can check the [API documentation](https://anwarpro.github.io/mvvmate) generated by Dokka. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
core/src/commonMain/composeResources/drawable/compose-multiplatform.xml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/src/commonMain/kotlin/com/helloanwar/mvvmate/core/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
package com.helloanwar.mvvmate.core | ||
|
||
/** | ||
* Platform interface representing platform-specific implementations. | ||
* | ||
* This interface is expected to be implemented by each target in a Kotlin Multiplatform project (e.g., Android, iOS). | ||
* It is typically used to get platform-specific information like the name of the platform. | ||
*/ | ||
interface Platform { | ||
/** | ||
* The name of the platform (e.g., "Android", "iOS"). | ||
*/ | ||
val name: String | ||
} | ||
|
||
|
||
/** | ||
* Function to get the platform-specific implementation of [Platform]. | ||
* | ||
* This function is expected to be implemented separately in each platform's source set. | ||
* | ||
* @return A [Platform] instance that represents the current platform. | ||
*/ | ||
expect fun getPlatform(): Platform |
6 changes: 6 additions & 0 deletions
6
core/src/commonMain/kotlin/com/helloanwar/mvvmate/core/UiAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
package com.helloanwar.mvvmate.core | ||
|
||
/** | ||
* Marker interface representing an action or intent from the user. | ||
* | ||
* This interface is used in the MVVM pattern to define user actions that are handled by the ViewModel. | ||
* Examples of actions might include button clicks, text input changes, etc. | ||
*/ | ||
interface UiAction |
6 changes: 6 additions & 0 deletions
6
core/src/commonMain/kotlin/com/helloanwar/mvvmate/core/UiEffect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
package com.helloanwar.mvvmate.core | ||
|
||
/** | ||
* Marker interface representing a UI side effect. | ||
* | ||
* Side effects are typically actions that do not directly modify the UI state but are meant to trigger external events, | ||
* such as showing a toast, navigating to another screen, or showing a dialog. | ||
*/ | ||
interface UiEffect |
6 changes: 6 additions & 0 deletions
6
core/src/commonMain/kotlin/com/helloanwar/mvvmate/core/UiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
package com.helloanwar.mvvmate.core | ||
|
||
/** | ||
* Marker interface representing the state of the UI. | ||
* | ||
* This interface is implemented by classes that define the data necessary to render the UI. | ||
* In the MVVM pattern, the state represents the current state of the ViewModel and is observed by the UI. | ||
*/ | ||
interface UiState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters