Created
March 14, 2021 18:06
-
-
Save GeorgCantor/8240fbdb70e2920a00dd71300ccfb099 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
class PickCompleteViewModel( | |
private val repository: ApiRepository, | |
private val preferences: SharedPreferences | |
) : ViewModel() { | |
val response = MutableLiveData<ResponseMessage>() | |
val error = MutableLiveData<String>() | |
val products = MutableLiveData<MutableSet<Product>>() | |
val currentProduct = MutableLiveData<PickProduct>() | |
val sentSuccess = MutableLiveData<Boolean>() | |
val progressIsVisible = MutableLiveData<Boolean>() | |
private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | |
error.postValue(throwable.message) | |
} | |
fun pick(request: PickRequest) { | |
progressIsVisible.value = true | |
viewModelScope.launch(exceptionHandler) { | |
repository.pick(request).apply { | |
response.postValue(this?.response) | |
error.postValue(this?.error?.error_msg) | |
progressIsVisible.postValue(false) | |
if (this?.success == true) { | |
sentSuccess.postValue(true) | |
preferences.putAny(DEFERRED_REQUEST, "") | |
} | |
} | |
} | |
} | |
fun getProduct(productId: String) { | |
viewModelScope.launch(exceptionHandler) { | |
if (products.value == null || products.value?.all { it.id != productId.toInt() } == true) { | |
repository.products(productId).apply { | |
val prods = mutableSetOf<Product>() | |
products.value?.let { prods.addAll(it) } | |
this?.response?.info?.firstOrNull()?.let { | |
prods.add(Product(it.id!!, it.title!!)) | |
} | |
products.postValue(prods) | |
} | |
} | |
} | |
} | |
fun addProduct(product: PickProduct) { | |
viewModelScope.launch(exceptionHandler) { | |
if (currentProduct.value != product) currentProduct.postValue(product) | |
} | |
} | |
fun setRect(rectName: Rect, rectButton: Rect, name: String) { | |
viewModelScope.launch { | |
val prods = mutableSetOf<Product>() | |
products.value?.let { prods.addAll(it) } | |
prods.forEach { | |
if (it.name == name) { | |
it.rectButton = rectButton | |
it.rectName = rectName | |
it.isVisible = true | |
} | |
} | |
products.postValue(prods) | |
} | |
} | |
fun setClickStatus(product: Product) { | |
viewModelScope.launch { | |
val prods = mutableSetOf<Product>() | |
products.value?.let { prods.addAll(it) } | |
prods.forEach { | |
if (it.id == product.id) it.clickStatus = product.clickStatus | |
} | |
products.postValue(prods) | |
} | |
} | |
fun clearVisibility() { | |
viewModelScope.launch { | |
val prods = mutableSetOf<Product>() | |
products.value?.let { prods.addAll(it) } | |
prods.forEach { | |
it.isVisible = false | |
} | |
products.postValue(prods) | |
} | |
} | |
fun saveForDeferredSending(request: PickRequest) { | |
preferences.putAny(DEFERRED_REQUEST, Gson().toJson(request)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment