Created
August 1, 2023 14:34
-
-
Save hieuwu/ed8bb24a71f5a139c999e010d89fc1e8 to your computer and use it in GitHub Desktop.
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
@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