From 5a4eb03b3d3f574421ea31d882a9ebe542c91acc Mon Sep 17 00:00:00 2001 From: Nyck Date: Thu, 9 May 2024 02:50:25 +0100 Subject: [PATCH] Pharmacy medicines list now updates after just adding a new medicine. Uses a MutableStateFlow to do a flatMap on the Pager. --- .../ist/pharmacist/PharmacistApplication.kt | 2 +- .../ui/screens/pharmacy/PharmacyViewModel.kt | 50 +++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/PharmacistApplication.kt b/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/PharmacistApplication.kt index 9869a13..0241c90 100644 --- a/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/PharmacistApplication.kt +++ b/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/PharmacistApplication.kt @@ -84,7 +84,7 @@ class PharmacistApplication : DependenciesContainer, Application() { companion object { const val MEDICINE_NOTIFICATION_CHANNEL = "MedicineNotifications" - const val API_ENDPOINT = "https://pharmacist-e9t4.onrender.com" // "http://10.0.2.2:8080" + const val API_ENDPOINT = "http://10.0.2.2:8080" // "https://pharmacist-e9t4.onrender.com" const val TAG = "PharmacistApp" } } diff --git a/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/ui/screens/pharmacy/PharmacyViewModel.kt b/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/ui/screens/pharmacy/PharmacyViewModel.kt index 976519e..893a664 100644 --- a/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/ui/screens/pharmacy/PharmacyViewModel.kt +++ b/src/frontend/app/src/main/kotlin/pt/ulisboa/ist/pharmacist/ui/screens/pharmacy/PharmacyViewModel.kt @@ -9,8 +9,10 @@ import androidx.paging.PagingConfig import androidx.paging.PagingData import androidx.paging.cachedIn import androidx.paging.map +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.launch import pt.ulisboa.ist.pharmacist.service.http.PharmacistService import pt.ulisboa.ist.pharmacist.service.http.connection.isSuccess @@ -48,24 +50,29 @@ class PharmacyViewModel( private val modificationEvents = MutableStateFlow>(emptyList()) - private val _medicinesState = Pager( - config = PagingConfig( - pageSize = PAGE_SIZE, - prefetchDistance = PREFETCH_DISTANCE, - enablePlaceholders = false - ), - pagingSourceFactory = { - PharmacyMedicinesPagingSource( - pharmaciesService = pharmacistService.pharmaciesService, - pid = pharmacyId - ) - } - ).flow.cachedIn(viewModelScope) - .combine(modificationEvents) { pagingData, modificationEvents -> - modificationEvents.fold(pagingData) { pagingDataAcc, modificationEvent -> - applyModificationEvent(pagingDataAcc, modificationEvent) + private val newMedicineFlow = MutableStateFlow(null) + + @OptIn(ExperimentalCoroutinesApi::class) + private val _medicinesState = newMedicineFlow.flatMapLatest { + Pager( + config = PagingConfig( + pageSize = PAGE_SIZE, + prefetchDistance = PREFETCH_DISTANCE, + enablePlaceholders = false + ), + pagingSourceFactory = { + PharmacyMedicinesPagingSource( + pharmaciesService = pharmacistService.pharmaciesService, + pid = pharmacyId + ) } - } + ).flow.cachedIn(viewModelScope) + .combine(modificationEvents) { pagingData, modificationEvents -> + modificationEvents.fold(pagingData) { pagingDataAcc, modificationEvent -> + applyModificationEvent(pagingDataAcc, modificationEvent) + } + } + } private fun applyModificationEvent( pagingData: PagingData, @@ -216,14 +223,7 @@ class PharmacyViewModel( } fun onMedicineAdded(medicineId: Long, quantity: Long) { - // TODO: This does not work. This paging is sus - pharmacy?.let { - modificationEvents.value += StockModificationEvent( - medicineId, - MedicineStockOperation.ADD, - quantity - ) - } + viewModelScope.launch { newMedicineFlow.emit(medicineId) } }