Skip to content

Instantly share code, notes, and snippets.

@hieuwu
Created August 1, 2023 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hieuwu/ed8bb24a71f5a139c999e010d89fc1e8 to your computer and use it in GitHub Desktop.
Save hieuwu/ed8bb24a71f5a139c999e010d89fc1e8 to your computer and use it in GitHub Desktop.
@HiltViewModel
class AddProductViewModel @Inject constructor(
private val productRepository: ProductRepository,
) : ViewModel() {
private val _isLoading = MutableStateFlow(false)
val isLoading: Flow<Boolean> = _isLoading
private val _showSuccessMessage = MutableStateFlow(false)
val showSuccessMessage: Flow<Boolean> = _showSuccessMessage
fun onCreateProduct(name: String, price: Double) {
if (name.isEmpty() || price <= 0) return
viewModelScope.launch {
_isLoading.value = true
val product = Product(
id = UUID.randomUUID().toString(),
name = name,
price = price,
)
productRepository.createProduct(product = product)
_isLoading.value = false
_showSuccessMessage.emit(true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment