Comments (4)
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.
Is there any concern why using
data class RecipeViewState
instead of usingsealed 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:
- No need resetting
RecipeViewState
using.copy
for every `RecipeViewState)- No else branch in
override fun render
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.
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.
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)
- add buildSrc module
- add domain layer and entity objects
- add repository interface for domain
- add spotless for linting
- add data layer with model and mappers
- add support for gradle kotlin DSL
- migrate existing gradle files from groovy to kotlin
- add app dependencies in kotlin file
- Retrofit Coroutine Exception Handling HOT 16
- Problem in build project HOT 1
- Using Either<Left, Right> type for usecase return type
- Issue with Gradle Build
- add response and mappers
- add api service interface and factory
- add dependencies for remote layer
- create remote module
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from baking-app-kotlin.