Created
June 16, 2020 22:08
-
-
Save ahmed3elshaer/9d1676d4ea9ad612ebd3669d10bec12f to your computer and use it in GitHub Desktop.
ViewStates Example
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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
FeedCommentsViewModel().feedViewState.observe(viewLifecycleOwner, Observer { viewState -> | |
render(viewState) | |
}) | |
} | |
private fun render(viewState: FeedCommentsViewState) { | |
viewState.apply { | |
renderLoading(feedDetailsState.isLoading) | |
} | |
} | |
private fun renderLoading(isLoading:Boolean) { | |
if(isLoading){ | |
//show some view | |
}else{ | |
//hide some view | |
} | |
} |
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
sealed class States{ | |
data class State1(val somePara :Int):States() | |
data class State2(val somePara :Int):States() | |
data class State3(val somePara :Int):States() | |
} | |
data class FeedCommentsViewState( | |
val feedDetailsState: FeedViewState = FeedViewState(), | |
val commentsViewState: CommentViewState = CommentViewState() | |
) | |
data class FeedViewState( | |
override val isLoading: Boolean = false, | |
override val payload: FeedDetails = FeedDetails(), | |
override val error: Throwable? = null | |
) : BaseViewState<FeedDetails>() | |
data class CommentViewState(val isEmpty: Boolean = false, val commentIsUploading: String? = null) : | |
BaseViewState<CommentDetail>() | |
abstract class BaseViewState<T>( | |
open val isLoading: Boolean = false, | |
open val payload: T? = null, | |
open val error: Throwable? = null | |
) |
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
class FeedCommentsViewModel() { | |
val feedViewState: MutableLiveData<FeedCommentsViewState> = MutableLiveData() | |
init { | |
feedViewState.value = FeedCommentsViewState() | |
//start request feed | |
feedViewState.postValue( | |
currentState().copy( | |
feedDetailsState = currentState().feedDetailsState.copy( | |
isLoading = true | |
) | |
) | |
) | |
feedViewState.postValue( | |
currentState().copy( | |
feedDetailsState = currentState().feedDetailsState.copy( | |
payload = FeedDetails(), | |
isLoading = false | |
) | |
) | |
) | |
feedViewState.postValue( | |
currentState().copy( | |
commentsViewState = currentState().commentsViewState.copy( | |
commentIsUploading = "commentId", | |
isEmpty = false | |
) | |
) | |
) | |
} | |
fun currentState() = feedViewState.value!! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment