From 658db434ff3b84e945656161daa90cb1543a8e08 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Tue, 26 Sep 2023 16:03:45 -0600 Subject: [PATCH 1/3] consolidate generation of apiParams for ToolSyncTasks --- gradle/libs.versions.toml | 2 +- .../cru/godtools/sync/task/ToolSyncTasks.kt | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4af2f4261e..45a675f425 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,7 +18,7 @@ firebase-crashlytics = "18.4.3" firebase-perf = "20.4.1" godtoolsShared = "0.9.1" google-auto-value = "1.10.4" -gtoSupport = "4.1.1" +gtoSupport = "4.2.0-SNAPSHOT" kotlin = "1.9.10" kotlinCoroutines = "1.7.3" kotlinKover = "0.7.3" diff --git a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt index 6a8d600904..2c8092c00b 100644 --- a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt +++ b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt @@ -28,12 +28,6 @@ private const val SYNC_TIME_TOOLS = "last_synced.tools" private const val SYNC_TIME_TOOL = "last_synced.tool." private const val STALE_DURATION_TOOLS = TimeConstants.DAY_IN_MS -private val API_GET_INCLUDES = arrayOf( - Tool.JSON_ATTACHMENTS, - "${Tool.JSON_METATOOL}.${Tool.JSON_DEFAULT_VARIANT}", - "${Tool.JSON_LATEST_TRANSLATIONS}.${Translation.JSON_LANGUAGE}", -) - @Singleton internal class ToolSyncTasks @Inject internal constructor( private val toolsApi: ToolsApi, @@ -42,6 +36,17 @@ internal class ToolSyncTasks @Inject internal constructor( private val toolsRepository: ToolsRepository, private val lastSyncTimeRepository: LastSyncTimeRepository, ) : BaseSyncTasks() { + private companion object { + private val INCLUDES_GET_TOOL = Includes( + Tool.JSON_ATTACHMENTS, + "${Tool.JSON_METATOOL}.${Tool.JSON_DEFAULT_VARIANT}", + "${Tool.JSON_LATEST_TRANSLATIONS}.${Translation.JSON_LANGUAGE}", + ) + + private fun buildApiParams() = JsonApiParams() + .includes(INCLUDES_GET_TOOL) + } + private val toolsMutex = Mutex() private val toolMutex = MutexMap() private val sharesMutex = Mutex() @@ -54,14 +59,13 @@ internal class ToolSyncTasks @Inject internal constructor( } // fetch tools from the API, short-circuit if this response is invalid - val json = toolsApi.list(JsonApiParams().include(*API_GET_INCLUDES)) - .takeIf { it.code() == HTTP_OK }?.body() ?: return@withLock false + val json = toolsApi.list(buildApiParams()).takeIf { it.code() == HTTP_OK }?.body() ?: return@withLock false // store fetched tools syncRepository.storeTools( json.data, existingTools = toolsRepository.getResourcesBlocking().mapNotNull { it.code }.toMutableSet(), - includes = Includes(*API_GET_INCLUDES) + includes = INCLUDES_GET_TOOL ) lastSyncTimeRepository.updateLastSyncTime(SYNC_TIME_TOOLS) true @@ -76,14 +80,13 @@ internal class ToolSyncTasks @Inject internal constructor( } // fetch tools from the API, short-circuit if this response is invalid - val json = toolsApi.getTool(toolCode, JsonApiParams().include(*API_GET_INCLUDES)) - .takeIf { it.code() == HTTP_OK }?.body() ?: return false + val json = toolsApi.getTool(toolCode, buildApiParams()).takeIf { it.code() == HTTP_OK }?.body() ?: return false // store fetched tools syncRepository.storeTools( json.data, existingTools = mutableSetOf(toolCode), - includes = Includes(*API_GET_INCLUDES) + includes = INCLUDES_GET_TOOL ) lastSyncTimeRepository.updateLastSyncTime(SYNC_TIME_TOOL, toolCode) From 330f432d5b5c9584ea057441dbd8832bd77f0fd6 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Tue, 26 Sep 2023 16:22:28 -0600 Subject: [PATCH 2/3] Only retrieve language fields we care about --- .../src/main/kotlin/org/cru/godtools/model/Language.kt | 8 +++++--- .../kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/library/model/src/main/kotlin/org/cru/godtools/model/Language.kt b/library/model/src/main/kotlin/org/cru/godtools/model/Language.kt index f504e41fa0..49f749ccdd 100644 --- a/library/model/src/main/kotlin/org/cru/godtools/model/Language.kt +++ b/library/model/src/main/kotlin/org/cru/godtools/model/Language.kt @@ -12,14 +12,16 @@ import org.ccci.gto.android.common.jsonapi.annotation.JsonApiIgnore import org.ccci.gto.android.common.jsonapi.annotation.JsonApiType import org.cru.godtools.base.util.getDisplayName -private const val JSON_API_TYPE_LANGUAGE = "language" - private const val JSON_CODE = "code" private const val JSON_NAME = "name" -@JsonApiType(JSON_API_TYPE_LANGUAGE) +@JsonApiType(Language.JSONAPI_TYPE) class Language : Base() { companion object { + const val JSONAPI_TYPE = "language" + + val JSONAPI_FIELDS = arrayOf(JSON_CODE, JSON_NAME) + val INVALID_CODE = Locale("x", "inv") fun COMPARATOR_DISPLAY_NAME(context: Context? = null, displayLocale: Locale? = null): Comparator = diff --git a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt index 2c8092c00b..b310dcf61a 100644 --- a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt +++ b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt @@ -20,6 +20,7 @@ import org.cru.godtools.api.ViewsApi import org.cru.godtools.api.model.ToolViews import org.cru.godtools.db.repository.LastSyncTimeRepository import org.cru.godtools.db.repository.ToolsRepository +import org.cru.godtools.model.Language import org.cru.godtools.model.Tool import org.cru.godtools.model.Translation import org.cru.godtools.sync.repository.SyncRepository @@ -45,6 +46,7 @@ internal class ToolSyncTasks @Inject internal constructor( private fun buildApiParams() = JsonApiParams() .includes(INCLUDES_GET_TOOL) + .fields(Language.JSONAPI_TYPE, *Language.JSONAPI_FIELDS) } private val toolsMutex = Mutex() From 63e6f160820870ca4668843fffa05bf4104c4d8c Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Wed, 20 Sep 2023 16:45:02 -0600 Subject: [PATCH 3/3] specify the desired fields for tools from the API --- .../kotlin/org/cru/godtools/model/Tool.kt | 28 +++++++++++++++++-- .../cru/godtools/sync/task/ToolSyncTasks.kt | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt b/library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt index df9e7e62db..d6ad6d0dbb 100644 --- a/library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt +++ b/library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt @@ -8,8 +8,6 @@ import org.ccci.gto.android.common.jsonapi.annotation.JsonApiAttribute import org.ccci.gto.android.common.jsonapi.annotation.JsonApiIgnore import org.ccci.gto.android.common.jsonapi.annotation.JsonApiType -private const val JSON_API_TYPE = "resource" - private const val JSON_TYPE = "resource-type" private const val JSON_TYPE_TRACT = "tract" private const val JSON_TYPE_ARTICLE = "article" @@ -31,9 +29,11 @@ private const val JSON_DEFAULT_ORDER = "attr-default-order" private const val JSON_INITIAL_FAVORITES_PRIORITY = "attr-initial-favorites-priority" private const val JSON_SCREEN_SHARE_DISABLED = "attr-screen-share-disabled" -@JsonApiType(JSON_API_TYPE) +@JsonApiType(Tool.JSONAPI_TYPE) class Tool : Base(), ChangeTrackingModel { companion object { + const val JSONAPI_TYPE = "resource" + const val JSON_ATTACHMENTS = "attachments" const val JSON_LATEST_TRANSLATIONS = "latest-translations" const val JSON_METATOOL = "metatool" @@ -41,6 +41,28 @@ class Tool : Base(), ChangeTrackingModel { const val ATTR_IS_FAVORITE = "isFavorite" + val JSONAPI_FIELDS = arrayOf( + JSON_TYPE, + JSON_ABBREVIATION, + JSON_NAME, + JSON_DESCRIPTION, + JSON_CATEGORY, + JSON_TOTAL_VIEWS, + JSON_BANNER, + JSON_DETAILS_BANNER, + JSON_DETAILS_BANNER_ANIMATION, + JSON_DETAILS_BANNER_YOUTUBE, + JSON_INITIAL_FAVORITES_PRIORITY, + JSON_SCREEN_SHARE_DISABLED, + JSON_DEFAULT_ORDER, + JSON_METATOOL, + JSON_DEFAULT_VARIANT, + JSON_ATTACHMENTS, + JSON_LATEST_TRANSLATIONS, + JSON_HIDDEN, + JSON_SPOTLIGHT + ) + val COMPARATOR_DEFAULT_ORDER = compareBy { it.defaultOrder } val COMPARATOR_FAVORITE_ORDER = compareBy { it.order }.then(COMPARATOR_DEFAULT_ORDER) } diff --git a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt index b310dcf61a..ab0befa576 100644 --- a/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt +++ b/library/sync/src/main/kotlin/org/cru/godtools/sync/task/ToolSyncTasks.kt @@ -46,6 +46,7 @@ internal class ToolSyncTasks @Inject internal constructor( private fun buildApiParams() = JsonApiParams() .includes(INCLUDES_GET_TOOL) + .fields(Tool.JSONAPI_TYPE, *Tool.JSONAPI_FIELDS) .fields(Language.JSONAPI_TYPE, *Language.JSONAPI_FIELDS) }