I am trying to show DialogBox when my response is received using repository -> ViewModel -> Livedata -> UI. Basically I am using data flow from repository to viewModel to Ui using livedata. But I am using Sealed class also in my ViewModel for managing Uistate.
Below is the code :
ViewModel
class MainViewModel(context: Context, private val repository: ProductRepository) : BaseViewModel<UiState>() {
private val networkLiveData: NetworkLiveData = NetworkLiveData(context)
fun observeNetworkConnectivity(owner: LifecycleOwner, observer: (Boolean) -> Unit) {
networkLiveData.observe(owner) { isConnected ->
if (isConnected == true) {
// Perform network-related operations
observer(isConnected)
} else {
// Handle no network connection
if (isConnected != null) {
observer(isConnected)
}
}
}
}
fun callApi() {
uiState.value = UiState.Loading
viewModelScope.launch {
repository.getAPIResponse()
uiState.value = UiState.Success(repository.productLD)
}
}
fun sendPostAPI(addProductModel: AddProductModel) {
uiState.value = UiState.Loading
viewModelScope.launch {
val response = repository.sendDataAPI(addProductModel)
Log.d("ViewModel","viewmodel sendPostAPI is $response")
uiState.value = UiState.Success(response)
}
}
}
BaseViewModel
open class BaseViewModel<T> : ViewModel() {
fun uiState(): LiveData<T> = uiState
protected val uiState: MutableLiveData<T> = MutableLiveData()
}
Fragment
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
fragment_addProductBinding = FragmentAddProductBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(
this, viewModelFactory
).get(MainViewModel::class.java)
viewModel.observeNetworkConnectivity(viewLifecycleOwner) { isConnected ->
internetState = isConnected
}
show() // triggered here
}
fun show(){
if (internetState) {
viewModel.sendPostAPI(addProductModel)
viewModel.uiState().observe(viewLifecycleOwner) { // refer here line #23
Log.d("AddProduct","updated data is $it")
if (it != null) {
onSuccess(it)
}
}
} else {
requireActivity().toast("Internet Connection Lost !!")
}
}
private fun onSuccess(data: Any) = with(fragment_addProductBinding) {
Log.d("AddProduct","updated data is Success ")
fragment_addProductBinding.progressBarAddProduct.setGone()
fragment_addProductBinding.backButton.isEnabled = true
fragment_addProductBinding.submitButton.isEnabled = true
backButton.setDefaultColor()
submitButton.setDefaultColor()
Log.d("AddProduct","updated data is modelclass $data ")
data as ProductAddedSuccessfullyModel
showDialogBoxOnSuccess(data) // Dialog Box Here
}
private fun showDialogBoxOnSuccess(data: ProductAddedSuccessfullyModel) {
val alertDialogBuilder = AlertDialog.Builder(requireActivity())
alertDialogBuilder.setTitle("Success !!")
alertDialogBuilder.setMessage(data.message)
alertDialogBuilder.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
val alertDialog = alertDialogBuilder.create()
alertDialog.show()
}
Problem is :
My Dialog box for the first time appears for once but for 2 times it appear twice and 3 time 4-5 times .
I checked that it is due to the viewmodel.uiState().observe line #23. I don't understand this behaviour .
Please help.
Thanks in advance!