Skip to content

Commit

Permalink
Merge pull request #3119 from CruGlobal/jsonapiUpdates
Browse files Browse the repository at this point in the history
Update jsonapi params for ToolSyncTasks
  • Loading branch information
frett authored Oct 2, 2023
2 parents 0972866 + 63e6f16 commit c768fb3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Language> =
Expand Down
28 changes: 25 additions & 3 deletions library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,16 +29,40 @@ 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"
const val JSON_DEFAULT_VARIANT = "default-variant"

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<Tool> { it.defaultOrder }
val COMPARATOR_FAVORITE_ORDER = compareBy<Tool> { it.order }.then(COMPARATOR_DEFAULT_ORDER)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,12 +29,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,
Expand All @@ -42,6 +37,19 @@ 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)
.fields(Tool.JSONAPI_TYPE, *Tool.JSONAPI_FIELDS)
.fields(Language.JSONAPI_TYPE, *Language.JSONAPI_FIELDS)
}

private val toolsMutex = Mutex()
private val toolMutex = MutexMap()
private val sharesMutex = Mutex()
Expand All @@ -54,14 +62,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
Expand All @@ -76,14 +83,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)

Expand Down

0 comments on commit c768fb3

Please sign in to comment.