Skip to content

Commit

Permalink
Avoid testing against Result.Success or Result.Failure in Sync Workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomeaux committed Mar 24, 2024
1 parent 1e88a59 commit 3b7d678
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 127 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/boardgamegeek/db/PlayDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ interface PlayDao {
suspend fun updateSyncTimestamp(internalId: Long, timestamp: Long): Int

@Query("UPDATE plays SET sync_hash_code = 0")
suspend fun clearSyncHashCodes()
suspend fun clearSyncHashCodes(): Int

@Query("UPDATE plays SET location = :location WHERE _id = :internalId")
suspend fun updateLocation(internalId: Long, location: String): Int
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/boardgamegeek/io/SafeApiCall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import timber.log.Timber
import java.io.IOException
import java.lang.Exception
import java.net.SocketTimeoutException
Expand All @@ -30,6 +31,7 @@ suspend fun <T> safeApiCall(context: Context, dispatcher: CoroutineDispatcher =
}
else -> throwable.localizedMessage
}
Timber.w(errorMessage)
Result.failure(Exception(errorMessage))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,26 @@ class GameCollectionRepository(

suspend fun loadUnupdatedItems() = withContext(Dispatchers.IO) { collectionDao.loadItemsNotUpdated() }

suspend fun refresh(options: Map<String, String>, updatedTimestamp: Long = System.currentTimeMillis()): Int = withContext(Dispatchers.IO) {
suspend fun refresh(options: Map<String, String>, updatedTimestamp: Long): Result<Int> = withContext(Dispatchers.IO) {
var count = 0
val username = prefs[AccountPreferences.KEY_USERNAME, ""]
if (!username.isNullOrBlank()) {
val response = api.collection(username, options)
response.items?.forEach {
val item = it.mapToCollectionItem()
if (isItemStatusSetToSync(item)) {
val (collectionId, _) = upsert(it, updatedTimestamp)
if (collectionId != INVALID_ID) count++
} else {
Timber.i("Skipped collection item '${item.gameName}' [ID=${item.gameId}, collection ID=${item.collectionId}] - collection status not synced")
val result = safeApiCall(context) { api.collection(username, options) }
if (result.isSuccess) {
result.getOrNull()?.items?.forEach {
val item = it.mapToCollectionItem()
if (isItemStatusSetToSync(item)) {
val (collectionId, _) = upsert(it, updatedTimestamp)
if (collectionId != INVALID_ID) count++
} else {
Timber.i("Skipped collection item '${item.gameName}' [ID=${item.gameId}, collection ID=${item.collectionId}] - collection status not synced")
}
}
} else {
return@withContext Result.failure(result.exception())
}
}
count
Result.success(count)
}

private fun isItemStatusSetToSync(item: CollectionItem): Boolean {
Expand Down Expand Up @@ -554,9 +558,7 @@ class GameCollectionRepository(
suspend fun resetTimestamps(internalId: Long): Int = withContext(Dispatchers.IO) { collectionDao.clearDirtyTimestamps(internalId) }

suspend fun deleteUnupdatedItems(timestamp: Long): Int = withContext(Dispatchers.IO) {
collectionDao.deleteUnupdatedItems(timestamp).also {
Timber.i("Deleted $it collection items not updated since ${timestamp.formatTimestamp(context)}")
}
collectionDao.deleteUnupdatedItems(timestamp)
}

fun enqueueUploadRequest(gameId: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class UserRepository(
result.getOrNull()?.let { user ->
upsertUser(user.mapForUpsert(timestamp)) // update the current user
user.buddies?.buddies?.let { buddies ->
Timber.d("Downloaded ${buddies.size} buddies")
var savedCount = 0
buddies
.map { it.mapForBuddyUpsert(timestamp) }
Expand All @@ -111,7 +112,7 @@ class UserRepository(
userDao.upsert(it)
savedCount++
}
Timber.d("Downloaded ${buddies.size} buddies, saved $savedCount buddies")
Timber.d("Saved $savedCount buddies")

val deletedCount = userDao.deleteBuddiesAsOf(timestamp)
Timber.d("Deleted $deletedCount buddies")
Expand Down
Loading

0 comments on commit 3b7d678

Please sign in to comment.