Skip to content

Commit d8b0339

Browse files
Merge pull request #14800 from woocommerce/issue/WOOMOB-1559-fux-db-transaction-issue-for-bookings
Fix UI flickering when fetching bookings for the first time
2 parents 4e3256f + 8a2cf76 commit d8b0339

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/bookings/BookingsStore.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ class BookingsStore @Inject internal constructor(
4646
return@withDefaultContext WooResult(ordersResult.error)
4747
}
4848

49-
if (page == 1 && filters.isEmpty() && query.isNullOrEmpty()) {
50-
// Clear existing bookings when fetching the first page
51-
bookingsDao.deleteAllForSite(site.localId())
52-
}
53-
5449
val entities = response.result.map {
5550
with(bookingDtoMapper) {
5651
it.toEntity(
@@ -59,7 +54,12 @@ class BookingsStore @Inject internal constructor(
5954
)
6055
}
6156
}
62-
bookingsDao.insertOrReplace(entities)
57+
if (page == 1 && filters.isEmpty() && query.isNullOrEmpty()) {
58+
// Clear existing bookings and insert new ones when fetching the first page
59+
bookingsDao.replaceAllForSite(site.localId(), entities)
60+
} else {
61+
bookingsDao.insertOrReplace(entities)
62+
}
6363
val totalPages = headersParser.getTotalPages(response.headers)
6464
// Determine if we can load more from the total pages header if available, otherwise
6565
// infer it from the number of items returned

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/dao/BookingsDao.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.room.Dao
44
import androidx.room.Insert
55
import androidx.room.OnConflictStrategy
66
import androidx.room.Query
7+
import androidx.room.Transaction
78
import kotlinx.coroutines.flow.Flow
89
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
910
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsFilterOption
@@ -61,6 +62,12 @@ interface BookingsDao {
6162
@Query("DELETE FROM Bookings WHERE localSiteId = :localSiteId")
6263
suspend fun deleteAllForSite(localSiteId: LocalId)
6364

65+
@Transaction
66+
suspend fun replaceAllForSite(siteId: LocalId, entities: List<BookingEntity>) {
67+
deleteAllForSite(siteId)
68+
insertOrReplace(entities)
69+
}
70+
6471
fun observeBookings(
6572
localSiteId: LocalId,
6673
limit: Int? = null,

0 commit comments

Comments
 (0)