Skip to content

Instantly share code, notes, and snippets.

@hieuwu
Created August 1, 2023 14:32
Show Gist options
  • Save hieuwu/8f448ef0ec682501cb061fbb64632ebe to your computer and use it in GitHub Desktop.
Save hieuwu/8f448ef0ec682501cb061fbb64632ebe to your computer and use it in GitHub Desktop.
@HiltViewModel
class ProductDetailsViewModel @Inject constructor(
private val productRepository: ProductRepository,
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val _product = MutableStateFlow<Product?>(null)
val product: Flow<Product?> = _product
private val _name = MutableStateFlow("")
val name: Flow<String> = _name
private val _price = MutableStateFlow(0.0)
val price: Flow<Double> = _price
private val _imageUrl = MutableStateFlow("")
val imageUrl: Flow<String> = _imageUrl
init {
val productId = savedStateHandle.get<String>(ProductDetailsDestination.productId)
productId?.let {
getProduct(productId = it)
}
}
private fun getProduct(productId: String) {
viewModelScope.launch {
val result = productRepository.getProduct(productId).asDomainModel()
_product.emit(result)
_name.emit(result.name)
_price.emit(result.price)
}
}
fun onNameChange(name: String) {
_name.value = name
}
fun onPriceChange(price: Double) {
_price.value = price
}
fun onSaveProduct(image: ByteArray) {
viewModelScope.launch {
productRepository.updateProduct(
id = _product.value?.id,
price = _price.value,
name = _name.value,
imageFile = image,
imageName = "image_${_product.value.id}",
)
}
}
fun onImageChange(url: String) {
_imageUrl.value = url
}
private fun ProductDto.asDomainModel(): Product {
return Product(
id = this.id,
name = this.name,
price = this.price,
image = this.image
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment