Skip to content

Instantly share code, notes, and snippets.

Avatar

Shashank Gupta Shahgupta4sq

View GitHub Profile
View EditProfileContract.Kt
interface EditProfileContract {
interface view {
fun setProgress(show: Boolean)
fun showEmptyFirstNameError()
fun showEmptyLastNameError()
}
View EditProfileFragment.Kt
viewModel.getState().observe(this, Observer { handleState(it) })
private fun handleState(state: EditProfileState?) {
if (state?.isCityDialogShown == true) {
showCitySelectionDialog()
return
}
if (state?.isGenderDialogShown == true) {
showGenderSelectionDialog()
return
View EditProfileViewModel.kt
private val state = MutableLiveData<EditProfileState>()
fun getState(): LiveData<EditProfileState> {
return state
}
fun setProgressIndicator(isProgressIndicatorShown: Boolean) {
state.value?.isProgressIndicatorShown = isProgressIndicatorShown
}
View EditProfileState.Kt
data class EditProfileState(
var isProgressIndicatorShown: Boolean = false,
var isCityDialogShown: Boolean = false,
var isGenderDialogShown: Boolean = false)
View EditProfileFragment.Kt
viewModel.getStatus().observe(this, Observer { handleStatus(it) })
private fun handleStatus(status: Status?) {
when (status) {
Status.EMPTY_FIRST_NAME -> Toast.makeText(activity, "Please enter your first name!", Toast.LENGTH_SHORT).show()
Status.EMPTY_LAST_NAME -> Toast.makeText(activity, "Please enter your last name", Toast.LENGTH_SHORT).show()
Status.EMPTY_CITY -> Toast.makeText(activity, "Please choose your home city", Toast.LENGTH_SHORT).show()
Status.INVALID_URI -> Toast.makeText(activity, "Unable to load the photo", Toast.LENGTH_SHORT).show()
Status.SUCCESS -> {
startActivity(HomeFragment.newIntent(activity))
View Status.Kt
enum class Status {
SUCCESS,
ERROR,
NO_NETWORK,
EMPTY_FIRST_NAME,
EMPTY_LAST_NAME,
EMPTY_CITY,
INVALID_URI
}
View EditProfileViewModel.kt
private val status = SingleLiveEvent<Status>()
fun getStatus(): LiveData<Status> {
return status
}
fun handleImage(intent: Intent?) {
intent?.data?.let {
avatar.value = it.toString()
} ?: run { status.value = Status.INVALID_URI }