Code Monkey home page Code Monkey logo

kviewmodel--mpp's Introduction

KViewModel

KViewModel it's a lightweight library for MVVM or MVI pattern. Works with Jetpack Compose, XML, UIKit.

Works with Kotlin Multiplatform and Compose Multiplatform!

Implementation

Kotlin DSL

implementation("com.adeo:kviewmodel:0.7.1") // Core functions
implementation("com.adeo:kviewmodel-compose:0.7.1") // Compose extensions
implementation("com.adeo:kviewmodel-odyssey:0.7.1") // Odyssey extensions
implementation "com.adeo:kviewmodel:0.7.1" // Core functions
implementation "com.adeo:kviewmodel-compose:0.7.1" // Compose extensions
implementation "com.adeo:kviewmodel-odyssey:0.7.1" // Odyssey extensions

How to use

Common Code

class TestViewModel : BaseSharedViewModel<TestViewState, TestAction, TestEvent>(initialState = TestViewState())

Events - for user interaction

sealed class TestEvent {
    object IncrementClick : TestEvent()
    object DecrementClick : TestEvent()
    object DetailClick : TestEvent()
}

Actions - single action from view model like show snackbar or navigation

sealed class TestAction {
    data class OpenDetail(val param: Int) : TestAction()
}

ViewState - your current screen state (fields, loaders, etc)

data class TestViewState(
    val titleText: String = "",
    val counter: Int = 0
)

Compose Multiplatform

ViewModel(factory = { TestViewModel() }) { viewModel ->
    val viewState = viewModel.viewStates().observeAsState()
    val viewAction = viewModel.viewActions().observeAsState()

    Text(text = viewState.titleText)
    
    viewAction.value?.let { action -> 
        when (action) {
            is TestAction.FirstCase -> TODO()
            is TestAction.SecondCase -> TODO()
        }
    }
}

Odyssey integration

Allows you to save the ViewModel

setupNavigation(
    startScreen = "YourStartScreen",
    providers = yourCustomProviders
) {
    LocalRootController.current.setupWithViewModels()
    generateGraph()
}
StoredViewModel(factory = { TestViewModel() }) { viewModel ->
    // usual code like above
}

iOS

// ViewState
testViewModel.viewStates().watch { [weak self] viewState in
    guard let self = self else { return }
    
    self.titleView.text = viewState.someText 
}

// Action
testViewModel.viewActions().watch { [weak self] viewAction in
    guard let self = self, let viewAction = viewAction else { return }
    
    switch viewAction {
        case let args as TestAction.OpenDetail:
            self.presentDetail(param: args.param)
                    
        default: break 
    }
}

Problems

Feel free to make issues, we will try to fix it as fast as we can! For proposals, you can also use issue section

kviewmodel--mpp's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.