Skip to content

Instantly share code, notes, and snippets.

@asissuthar
Created November 6, 2022 15:39
Show Gist options
  • Save asissuthar/234e73d6835fd88a203ddaf23900e43d to your computer and use it in GitHub Desktop.
Save asissuthar/234e73d6835fd88a203ddaf23900e43d to your computer and use it in GitHub Desktop.
package com.asissuthar.validation.formfields
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
abstract class FormField<T> {
protected val stateInternal = MutableStateFlow<T?>(null)
// state is StateFlow. It will be helpful for collecting any change in current value.
val state = stateInternal.asStateFlow()
protected val isValidInternal = MutableStateFlow(true)
// isValid is StateFlow. It will be helpful for collecting any change in validation process.
val isValid = isValidInternal.asStateFlow()
// We will call validate method, when we want to perform validation.
abstract suspend fun validate(focusIfError: Boolean = true): Boolean
open fun clearError() {}
open fun clearFocus() {}
open fun disable() {}
open fun enable() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment