Coder Social home page Coder Social logo

handlebar's Introduction

Handlebar

Typesafe SavedStateHandle in Android ViewModels.

Description

Android ViewModels typically interact with SavedStateHandle through a key-value basis. SavedStateHandle allows for ViewModel state to survive process death, but it comes at the cost of having state that is essentially stringly typed.

class LoginViewModel(
    api: API,
    private val stateHandle: SavedStateHandle
) : ViewModel() {

    private val _status = stateHandle.getLiveData<ProgressStatus>("status")
    val status: LiveData<ProgressStatus> = _status
    
    private val _username = stateHandle.getLiveData<String>("username")
    val username: LiveData<String> = _username
    
    private val _password = stateHandle.getLiveData<String>("password")
    val password: LiveData<String> = _password
    
    suspend fun login() {
        _status.value = ProgressStatus.IN_PROGRESS
        
        viewModelScope.launch {
            api.login(username.value, password.value)
            _status.value = ProgressStatus.COMPLETE
        }
    }
}

Handlebar allows Android developers to capitalize on SavedStateHandle's benefits without sacrificing the safety and readability that type safety provides.

Usage

Create an interface that describes your state properties, marked with the @TypedState annotation. Then create a property on your ViewModel to represent your state, using the extension function SavedStateHandle.asType.

class LoginViewModel(
    api: API,
    stateHandle: SavedStateHandle
) : ViewModel() {

    @TypedState interface LoginState {
        var username: LiveData<String>
        var email: LiveData<String>
        var status: LiveData<ProgressStatus>
    }
    private val state = stateHandle.asType<LoginStateContract>()
    
    // Expose status to observers
    val status: LiveData<ProgressStatus> = state.status
    
    val username: LiveData<String> = state.username
    val password: LiveData<String> = state.password
    
    suspend fun login() {
        state.updateStatus(ProgressStatus.IN_PROGRESS)
        
        viewModelScope.launch {
            api.login(username.value, password.value)        
            state.updateStatus(ProgressStatus.COMPLETE)
        }
    }
}

That's it. Handlebar will generate a new interface [TypedState interface name]Contract using the properties from your @TypedState interface, as well as a concrete implementation connecting this interface to the SavedStateHandle on your ViewModel, so all changes you make on your state property will be backed by the SavedStateHandle.

handlebar's People

Contributors

mattrob33 avatar

Stargazers

 avatar  avatar

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.