Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class BookingsStore @Inject internal constructor(
return@withDefaultContext WooResult(ordersResult.error)
}

if (page == 1 && filters.isEmpty() && query.isNullOrEmpty()) {
// Clear existing bookings when fetching the first page
bookingsDao.deleteAllForSite(site.localId())
}

val entities = response.result.map {
with(bookingDtoMapper) {
it.toEntity(
Expand All @@ -59,7 +54,12 @@ class BookingsStore @Inject internal constructor(
)
}
}
bookingsDao.insertOrReplace(entities)
if (page == 1 && filters.isEmpty() && query.isNullOrEmpty()) {
// Clear existing bookings and insert new ones when fetching the first page
bookingsDao.replaceAllForSite(site.localId(), entities)
} else {
bookingsDao.insertOrReplace(entities)
}
val totalPages = headersParser.getTotalPages(response.headers)
// Determine if we can load more from the total pages header if available, otherwise
// infer it from the number of items returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import kotlinx.coroutines.flow.Flow
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsFilterOption
Expand Down Expand Up @@ -61,6 +62,12 @@ interface BookingsDao {
@Query("DELETE FROM Bookings WHERE localSiteId = :localSiteId")
suspend fun deleteAllForSite(localSiteId: LocalId)

@Transaction
suspend fun replaceAllForSite(siteId: LocalId, entities: List<BookingEntity>) {
deleteAllForSite(siteId)
insertOrReplace(entities)
}

fun observeBookings(
localSiteId: LocalId,
limit: Int? = null,
Expand Down
Loading