Created
December 2, 2020 19:16
-
-
Save beigirad/5489fe50a5e8226c9d2d93e17050fb9d to your computer and use it in GitHub Desktop.
Single State Pattern
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
data class StateModel( | |
val detail: ViewResource<Entity1>, | |
val something: Entity2?, | |
val error: Event<String>? | |
) { | |
companion object { | |
val initial = StateModel( | |
detail = ViewResource.NotAvailable, | |
something = null, | |
error = 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
import androidx.annotation.Nullable; | |
public class Event<T> { | |
private T content; | |
private boolean hasBeenHandled = false; | |
public Event(T content) { | |
this.content = content; | |
} | |
@Nullable | |
public T getContentIfNotHandled() { | |
if (hasBeenHandled) { | |
return null; | |
} else { | |
hasBeenHandled = true; | |
return content; | |
} | |
} | |
public boolean hasBeenHandled() { | |
return this.hasBeenHandled; | |
} | |
@Nullable | |
public T peekContent() { | |
return content; | |
} | |
public void ifNotHandled(EventHandler<T> eventHandler) { | |
if (!hasBeenHandled) { | |
hasBeenHandled = true; | |
eventHandler.handle(content); | |
} | |
} | |
public interface EventHandler<T> { | |
void handle(T content); | |
} | |
} |
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
private val _state = MutableStateFlow(StateModel.initial) | |
val state: StateFlow<StateModel> = _state.asStateFlow() | |
fun getDetail() { | |
_state.value = _state.value.copy(detail = ViewResource.Loading()) | |
viewModelScope.launch { | |
when (val result = repository.getDetail(productId)) { | |
is Either.Success -> | |
_state.value = _state.value.copy(detail = ViewResource.Ok(result.value)) | |
is Either.Failure -> | |
_state.value = _state.value.copy(detail = ViewResource.Error(result.message, result.code)) | |
} | |
} | |
} |
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
viewModel.state.onEach { state -> | |
binding.tvName = state.detail.name | |
binding.progress.isVisiable = state.detail is ViewResource.Loading | |
state.error?.ifNotHandled(::toast) | |
}.launchIn(viewLifecycleOwner.lifecycleScope) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment