Skip to content

Commit

Permalink
Setting up cache + users local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Jan 30, 2024
1 parent 2dcbb4f commit e2fe6b6
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.nathanfallet.extopy.di

import me.nathanfallet.extopy.BuildConfig
import me.nathanfallet.extopy.database.DatabaseDriverFactory
import me.nathanfallet.extopy.models.application.ExtopyEnvironment
import me.nathanfallet.extopy.repositories.application.ITokenRepository
import me.nathanfallet.extopy.repositories.application.TokenRepository
Expand All @@ -13,11 +14,16 @@ val environmentModule = module {
}
}

val databaseModule = module {
single { DatabaseDriverFactory(get()) }
}

val repositoryModule = module {
single<ITokenRepository> { TokenRepository(get()) }
}

val androidModule = listOf(
databaseModule,
environmentModule,
repositoryModule
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun RootView() {
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()

LaunchedEffect("user") {
LaunchedEffect(Unit) {
viewModel.fetchUser()
}

Expand Down
2 changes: 1 addition & 1 deletion ios/Extopy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
children = (
6AB01AE32B3605A1001AF7FF /* AuthView.swift */,
6A7D0795291D8C5F0023BE5B /* AuthSheet.swift */,
6A7D0793291D81950023BE5B /* SafariView.swift */,
);
path = Auth;
sourceTree = "<group>";
Expand Down Expand Up @@ -231,6 +230,7 @@
children = (
6AC04BBE2B35B682009938B9 /* Posts */,
6AC04BBD2B35B67A009938B9 /* Users */,
6A7D0793291D81950023BE5B /* SafariView.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver

actual class DatabaseDriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
return AndroidSqliteDriver(AppDatabase.Schema, context, "solar.db")
}

actual fun createDriver(): SqlDriver =
AndroidSqliteDriver(AppDatabase.Schema, context, "extopy.db")

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package me.nathanfallet.extopy.database
class Database(databaseDriverFactory: DatabaseDriverFactory) {

private val database = AppDatabase(databaseDriverFactory.createDriver())
//private val dbQuery = database.appDatabaseQueries

val usersQueries = database.usersDatabaseQueries
val postsQueries = database.postsDatabaseQueries

Check warning on line 8 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/database/Database.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/database/Database.kt#L7-L8

Added lines #L7 - L8 were not covered by tests

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package me.nathanfallet.extopy.di

import me.nathanfallet.extopy.client.ExtopyClient
import me.nathanfallet.extopy.client.IExtopyClient
import me.nathanfallet.extopy.database.Database
import me.nathanfallet.extopy.repositories.users.IUsersRepository
import me.nathanfallet.extopy.repositories.users.UsersRepository
import me.nathanfallet.extopy.usecases.auth.*
import me.nathanfallet.extopy.usecases.posts.*
import me.nathanfallet.extopy.usecases.timelines.FetchTimelinePostsUseCase
Expand All @@ -19,8 +22,16 @@ import me.nathanfallet.extopy.viewmodels.users.ProfileViewModel
import me.nathanfallet.ktorx.usecases.api.IGetTokenUseCase
import org.koin.dsl.module

val databaseModule = module {
single { Database(get()) }

Check warning on line 26 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt#L25-L26

Added lines #L25 - L26 were not covered by tests
}

val repositoryModule = module {
// Remote client
single<IExtopyClient> { ExtopyClient(get(), get()) }

// Local cache
single<IUsersRepository> { UsersRepository(get()) }

Check warning on line 34 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt#L34

Added line #L34 was not covered by tests
}

val useCaseModule = module {
Expand All @@ -36,7 +47,7 @@ val useCaseModule = module {
single<IFetchTimelinePostsUseCase> { FetchTimelinePostsUseCase(get()) }

// Users
single<IFetchUserUseCase> { FetchUserUseCase(get()) }
single<IFetchUserUseCase> { FetchUserUseCase(get(), get()) }

Check warning on line 50 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt#L50

Added line #L50 was not covered by tests
single<IUpdateFollowInUserUseCase> { UpdateFollowInUserUseCase(get(), get()) }
single<IFetchUserPostsUseCase> { FetchUserPostsUseCase(get()) }

Expand All @@ -58,6 +69,7 @@ val viewModelModule = module {
}

val sharedModule = listOf(
databaseModule,

Check warning on line 72 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/di/SharedModule.kt#L72

Added line #L72 was not covered by tests
repositoryModule,
useCaseModule,
viewModelModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.nathanfallet.extopy.repositories.users

import me.nathanfallet.extopy.models.users.User

interface IUsersRepository {

fun upsert(user: User)
fun get(id: String): User?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.nathanfallet.extopy.repositories.users

import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.TimeZone
import kotlinx.datetime.plus
import me.nathanfallet.extopy.database.Database
import me.nathanfallet.extopy.database.Users
import me.nathanfallet.extopy.models.users.User

class UsersRepository(
private val database: Database,

Check warning on line 12 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt#L11-L12

Added lines #L11 - L12 were not covered by tests
) : IUsersRepository {

override fun upsert(user: User) = database.usersQueries.upsert(
id = user.id,
displayName = user.displayName,
username = user.username,
email = user.email,
biography = user.biography,
avatar = user.avatar,
expiresFromCacheAt = Clock.System.now()
.plus(60, DateTimeUnit.SECOND, TimeZone.currentSystemDefault())
.toString()

Check warning on line 24 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt#L15-L24

Added lines #L15 - L24 were not covered by tests
)

override fun get(id: String): User? = database.usersQueries.get(id)

Check warning on line 27 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt#L27

Added line #L27 was not covered by tests
.executeAsOneOrNull()?.toUser()

private fun Users.toUser() = User(
id = id,
displayName = displayName,
username = username,
email = email,
biography = biography,
avatar = avatar

Check warning on line 36 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/repositories/users/UsersRepository.kt#L30-L36

Added lines #L30 - L36 were not covered by tests
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package me.nathanfallet.extopy.usecases.users

import me.nathanfallet.extopy.client.IExtopyClient
import me.nathanfallet.extopy.models.users.User
import me.nathanfallet.extopy.repositories.users.IUsersRepository

class FetchUserUseCase(
private val client: IExtopyClient,
private val usersRepository: IUsersRepository,

Check warning on line 9 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/FetchUserUseCase.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/FetchUserUseCase.kt#L9

Added line #L9 was not covered by tests
) : IFetchUserUseCase {

override suspend fun invoke(input: String): User? {
// TODO: Read from cache (local db)
return client.users.get(input)?.also {
// TODO: Save in cache (local db)
return usersRepository.get(input) ?: client.users.get(input)?.also {
usersRepository.upsert(it)

Check warning on line 14 in shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/FetchUserUseCase.kt

View check run for this annotation

Codecov / codecov/patch

shared/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/FetchUserUseCase.kt#L14

Added line #L14 was not covered by tests
}
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE posts (
id TEXT PRIMARY KEY,
userId TEXT,
body TEXT
);

insert:
INSERT INTO posts (id, userId, body)
VALUES (?, ? , ?);

get:
SELECT * FROM posts WHERE id = ?;

delete:
DELETE FROM posts WHERE id = ?;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CREATE TABLE users (
id TEXT PRIMARY KEY NOT NULL,
displayName TEXT NOT NULL,
username TEXT NOT NULL,
email TEXT,
biography TEXT,
avatar TEXT,
expiresFromCacheAt TEXT NOT NULL
);

upsert {
UPDATE users
SET displayName = :displayName,
username = :username,
email = :email,
biography = :biography,
avatar = :avatar,
expiresFromCacheAt = :expiresFromCacheAt
WHERE id = :id;

INSERT OR IGNORE INTO users (id, displayName, username, email, biography, avatar, expiresFromCacheAt)
VALUES (
:id,
:displayName,
:username,
:email,
:biography,
:avatar,
:expiresFromCacheAt
);
}

get:
SELECT * FROM users WHERE id = ?;

delete:
DELETE FROM users WHERE id = ?;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import app.cash.sqldelight.driver.native.NativeSqliteDriver

actual class DatabaseDriverFactory {

actual fun createDriver(): SqlDriver {
return NativeSqliteDriver(AppDatabase.Schema, "solar.db")
}
actual fun createDriver(): SqlDriver =
NativeSqliteDriver(AppDatabase.Schema, "extopy.db")

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun KoinApplication.Companion.start(): KoinApplication = startKoin {
modules(sharedModule)
}

// MARK: - View models (we should not call any other class from iOS directly, only use cases)
// MARK: - View models (we should not call any other class from iOS directly, only view models)

val Koin.rootViewModel: RootViewModel get() = get()
val Koin.authViewModel: AuthViewModel get() = get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.nathanfallet.extopy.di

import me.nathanfallet.extopy.database.DatabaseDriverFactory
import me.nathanfallet.extopy.models.application.ExtopyEnvironment
import me.nathanfallet.extopy.repositories.application.ITokenRepository

Expand All @@ -9,6 +10,8 @@ object SwiftModule {
lateinit var tokenRepository: ITokenRepository

fun module() = org.koin.dsl.module {
single { DatabaseDriverFactory() }

single { environment }
single { tokenRepository }
}
Expand Down

0 comments on commit e2fe6b6

Please sign in to comment.