Skip to content

Commit

Permalink
Fix artwork caching (#40)
Browse files Browse the repository at this point in the history
* Add primary key to artworks table

* Fix artwork caching bug
  • Loading branch information
jocmp committed Jul 1, 2024
1 parent 0037e98 commit ee0b2cf
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import edu.gvsu.art.db.ArtGalleryDatabase
import org.koin.dsl.module

internal val databaseModule = module {
single {
single<ArtGalleryDatabase> {
ArtGalleryDatabase(
driver = AndroidSqliteDriver(
schema = ArtGalleryDatabase.Schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package edu.gvsu.art.gallery.di
import edu.gvsu.art.client.api.ArtGalleryClient
import edu.gvsu.art.client.repository.*
import edu.gvsu.art.gallery.BuildConfig
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module

internal val repositoryModule = module {
single {
ArtGalleryClient.create(
baseURL = BuildConfig.ART_GALLERY_BASE_URL,
cacheDirectory = androidApplication().cacheDir
baseURL = BuildConfig.ART_GALLERY_BASE_URL
)
}
single<ArtistRepository> { DefaultArtistRepository(database = get(), client = get()) }
Expand Down
6 changes: 5 additions & 1 deletion artgalleryclient/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ plugins {

android {
namespace = "edu.gvsu.art.client"
compileSdkVersion = "android-34"
compileSdk = 34

defaultConfig {
minSdk = 26
}
}

sqldelight {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package edu.gvsu.art.client.api

import edu.gvsu.art.client.api.GalleryConverter.moshi
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.create
import retrofit2.http.GET
import retrofit2.http.Query
import java.io.File

interface ArtGalleryClient {
@GET("objectDetail")
Expand Down Expand Up @@ -39,20 +36,10 @@ interface ArtGalleryClient {
suspend fun fetchTourStop(@Query("id") id: String): TourStopDetail

companion object {
fun create(baseURL: String, cacheDirectory: File): ArtGalleryClient {
val httpClient = OkHttpClient.Builder()
.cache(
Cache(
File(cacheDirectory, "http_cache"),
50L * 1024L * 1024L // 50 MiB
)
)
.build()

fun create(baseURL: String): ArtGalleryClient {
return Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(httpClient)
.build()
.create()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

/**
* Cache time occurred less than a day ago
*/
internal fun isFreshCache(cacheDateTime: String?): Boolean {
val today = LocalDateTime.now(ZoneId.of("UTC"))
if (cacheDateTime.isNullOrBlank()) {
return false
}

val now = LocalDateTime.now(ZoneId.of("UTC"))
val cacheTime =
LocalDateTime.parse(cacheDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
LocalDateTime.parse(cacheDateTime, CACHE_TIME_FORMAT)

// cache time occurred less than a day ago
return cacheTime < today.plusDays(1)
return cacheTime.plusDays(1) > now
}

internal val CACHE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
DROP TABLE IF EXISTS Artworks;

CREATE TABLE Artworks (
id TEXT NOT NULL,
CREATE TABLE IF NOT EXISTS Artworks (
id TEXT NOT NULL PRIMARY KEY,
is_public INTEGER,
media_representations TEXT,
name TEXT,
Expand All @@ -21,5 +19,6 @@ CREATE TABLE Artworks (
media_medium_url TEXT,
media_large_url TEXT,
thumbnail_url TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
ar_digital_asset_url TEXT
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
BEGIN TRANSACTION;

CREATE TABLE artworks_copy (
id TEXT NOT NULL PRIMARY KEY,
is_public INTEGER,
media_representations TEXT,
name TEXT,
artist_id TEXT,
artist_name TEXT,
historical_context TEXT,
work_description TEXT,
work_date TEXT,
work_medium TEXT,
location TEXT,
identifier TEXT,
credit_line TEXT,
location_latitude REAL,
location_longitude REAL,
related_objects TEXT,
media_small_url TEXT,
media_medium_url TEXT,
media_large_url TEXT,
thumbnail_url TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
ar_digital_asset_url TEXT
);

INSERT OR IGNORE INTO artworks_copy (
id,
is_public,
media_representations,
name,
artist_id,
artist_name,
historical_context,
work_description,
work_date,
work_medium,
location,
identifier,
credit_line,
location_latitude,
location_longitude,
related_objects,
media_small_url,
media_medium_url,
media_large_url,
thumbnail_url,
created_at,
ar_digital_asset_url
)
SELECT
id,
is_public,
media_representations,
name,
artist_id,
artist_name,
historical_context,
work_description,
work_date,
work_medium,
location,
identifier,
credit_line,
location_latitude,
location_longitude,
related_objects,
media_small_url,
media_medium_url,
media_large_url,
thumbnail_url,
created_at,
ar_digital_asset_url
FROM Artworks;

DROP TABLE Artworks;

ALTER TABLE artworks_copy RENAME TO Artworks;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.gvsu.art.client.repository

import junit.framework.TestCase.assertTrue
import org.junit.Test
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.assertFalse

class CachingTest {
@Test
fun isFreshCache_nullValue() {
assertFalse(isFreshCache(cacheDateTime = null))
}

@Test
fun isFreshCache_oldDate() {
assertFalse(isFreshCache(cacheDateTime = "2024-06-11 21:56:32"))
}

@Test
fun isFreshCache_validDate() {
val now = LocalDateTime.now().minusHours(1)

assertTrue(isFreshCache(cacheDateTime = now.format(CACHE_TIME_FORMAT)))
}
}

0 comments on commit ee0b2cf

Please sign in to comment.