Giter Site home page Giter Site logo

Comments (4)

aliumujib avatar aliumujib commented on May 16, 2024 1

Nice conversation you've got going here, there are situations where you'd want to use copy, for example when you want to reuse data from a previous state.
A concrete example of this is if you had a paged list of recipes and you wanted to append a new batch of recipes to the existing data in the list. So you'd essentially have to append the new data to the existing list, then make a new copy of the viewstate while replacing the old list with your new updated list like this:

var currentRecipeList = state.recipeList currentRecipeList.toMutableList().append(newRecipeList()) newState = state.copy(recipeList = newRecipeList)

That's a reason why I frequently use data classes instead of sealed classes to model State. Haven't yet found a cleaner approach. I'd gladly take suggestions however.

Sorry guys, I don't know how to make a new line appear within code snippets.

from baking-app-kotlin.

Ezike avatar Ezike commented on May 16, 2024

Is there any concern why using data class RecipeViewState instead of using sealed class RecipeViewState?

Equivalent to your existing code using sealed class

sealed class RecipeViewState : ViewState {
 object Loading : RecipeViewState()
 object Refreshing : RecipeViewState()
 data class LoadedRecipes(val recipes: List<RecipeModel>) : RecipeViewState()
 data class LikedRecipe(val recipe: RecipeModel) : RecipeViewState() // adding a new state for liked item
 data class Error(val error: String?, val errorEvent: ViewEvent<String>?, val isEmpty: Boolean) // Unavailable, No data or empty etc
}


// in Fragment 
// instead of:
    override fun render(state: RecipeViewState) {
        when {
            state.isDataUnavailable -> binding.renderEmptyState(state)
            state.isDataAvailableError -> binding.renderDataAvailableErrorState(state)
            state.isNoDataError -> binding.renderNoDataErrorState(state)
            state.isLoading -> binding.renderLoadingState()
            state.isRefreshing -> binding.renderRefreshState()
            else -> binding.renderSuccessState(state)
        }
    }

// modifed to:

    override fun render(state: RecipeViewState) {
        when (state) {
            is Error -> {  
                if (state.error != null) binding.renderNoDataErrorState(state)
                if (state.errorEvent != null) binding.renderDataAvailableErrorState(state)
                if (state.isEmpty) binding.renderEmptyState(state) 
            }
            Loading -> binding.renderLoadingState()
            Refreshing -> binding.renderRefreshState()
            is LikedRecipe -> binding.renderLikedState(state)
            is LoadedRecipes -> binding.renderSuccessState(state)
            // no else branch
        }
    }

Pros:

  1. No need resetting RecipeViewState using .copy for every `RecipeViewState)
  2. No else branch in override fun render

@Ezike

I'm not even sure why I used a data class anymore, but I remember I needed the list of recipes in a lot of the view states, so I thought I'd just use one class instead of many classes that take the recipe list as a parameter..

Also using copy isn't resetting the viewstate, it's making a new viewstate just like yours does.
They're all good approaches.

from baking-app-kotlin.

mochadwi avatar mochadwi commented on May 16, 2024

Also using copy isn't resetting the viewstate, it's making a new viewstate just like yours does.

Thanks for clarifying! *I've corrected my description

I'm not even sure why I used a data class anymore, but I remember I needed the list of recipes in a lot of the view states, so I thought I'd just use one class instead of many classes that take the recipe list as a parameter..

I see, it's a matter of preference, then~

from baking-app-kotlin.

Ezike avatar Ezike commented on May 16, 2024

Nice conversation you've got going here, there are situations where you'd want to use copy, for example when you want to reuse data from a previous state.
A concrete example of this is if you had a paged list of recipes and you wanted to append a new batch of recipes to the existing data in the list. So you'd essentially have to append the new data to the existing list, then make a new copy of the viewstate while replacing the old list with your new updated list like this:

var currentRecipeList = state.recipeList currentRecipeList.toMutableList().append(newRecipeList()) newState = state.copy(recipeList = newRecipeList)

That's a reason why I frequently use data classes instead of sealed classes to model State. Haven't yet found a cleaner approach. I'd gladly take suggestions however.

Sorry guys, I don't know how to make a new line appear within code snippets.

Definitely, that makes sense. With sealed classes, you'd need to do a type check to achieve a similar behavior, since not all sub classes of the sealed class have the list as a property.
For instance, you'd need to check if the previous state was a success state before gaining access to the old list..
Data classes are better suited for such cases.

from baking-app-kotlin.

Related Issues (17)

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.