Skip to content

Commit

Permalink
Pharmacy medicines list now updates after just adding a new medicine.…
Browse files Browse the repository at this point in the history
… Uses a MutableStateFlow to do a flatMap on the Pager.
  • Loading branch information
Nyckoka committed May 9, 2024
1 parent d65503c commit 5a4eb03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,24 +50,29 @@ class PharmacyViewModel(

private val modificationEvents = MutableStateFlow<List<ModificationEvent>>(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<Long?>(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<MedicineStockModel>,
Expand Down Expand Up @@ -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) }
}


Expand Down

0 comments on commit 5a4eb03

Please sign in to comment.