Skip to content

Commit

Permalink
atomicfu usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Borowy committed Dec 19, 2022
1 parent 8474643 commit e04cc44
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
classpath(libs.org.jetbrains.kotlin.serialization.plugin)
classpath(libs.com.squareup.sqldelight.gradle.plugin)
classpath(libs.com.google.osdetector.gradle.plugin)
classpath(libs.org.jetbrains.kotlinx.atomicfu.plugin)
}
}

Expand Down
2 changes: 1 addition & 1 deletion data/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("com.squareup.sqldelight")
id("social.androiddev.code-quality")
kotlin("plugin.serialization")
id("kotlinx-atomicfu")
}


Expand Down Expand Up @@ -52,7 +53,6 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(libs.multiplatform.settings)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.io.insert.koin.core)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ interface DodoAuthStorage {
/**
* Save the @param token keyed by @param server
*/
suspend fun saveAccessToken(server: String, token: String)
fun saveAccessToken(server: String, token: String)

/**
* Get the Access token for @param server
*/
suspend fun getAccessToken(server: String): String?
fun getAccessToken(server: String): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ package social.androiddev.common.persistence.localstorage
import com.russhwolf.settings.Settings
import com.russhwolf.settings.get
import com.russhwolf.settings.set
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.atomicfu.locks.ReentrantLock
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json

internal class DodoAuthStorageImpl(
private val settings: Settings,
private val json: Json,
private val lock: Mutex = Mutex()
private val lock: ReentrantLock = reentrantLock()
) : DodoAuthStorage {
override var currentDomain: String?
get() = settings[KEY_DOMAIN_CACHE]
Expand Down Expand Up @@ -50,10 +51,10 @@ internal class DodoAuthStorageImpl(
}
private val memCache: LinkedHashMap<String, AccessToken> by lazy { diskCache }

override suspend fun getAccessToken(server: String): String? =
override fun getAccessToken(server: String): String? =
lock.withLock { memCache[server]?.token }

override suspend fun saveAccessToken(server: String, token: String) {
override fun saveAccessToken(server: String, token: String) {
lock.withLock {
memCache[server] = AccessToken(token = token, server = server)
diskCache = memCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class AuthenticationRepositoryImpl(
redirectUris: String,
scopes: String,
website: String?
): NewAppOAuthToken? {
): NewAppOAuthToken? = withContext(ioCoroutineContext) {
val application = mastodonApi.createApplication(
domain = domain,
clientName = clientName,
Expand All @@ -41,7 +41,7 @@ internal class AuthenticationRepositoryImpl(
website = website
).getOrNull()

return application?.let {
application?.let {
NewAppOAuthToken(
clientId = it.clientId,
clientSecret = it.clientSecret,
Expand All @@ -50,7 +50,7 @@ internal class AuthenticationRepositoryImpl(
}
}

override suspend fun saveApplication(token: NewAppOAuthToken, domain: String) {
override suspend fun saveApplication(token: NewAppOAuthToken, domain: String) = withContext(ioCoroutineContext) {
// save our new application oauth token to our DB
database.applicationQueries.insertApplication(
Application(
Expand All @@ -65,20 +65,21 @@ internal class AuthenticationRepositoryImpl(
settings.currentDomain = domain
}

override suspend fun getApplicationOAuthToken(server: String): ApplicationOAuthToken? {
return database
.applicationQueries
.selectByServer(server)
.executeAsOneOrNull()
?.let {
ApplicationOAuthToken(
server = it.instance,
clientId = it.client_id,
clientSecret = it.client_secret,
redirectUri = it.redirect_uri,
)
}
}
override suspend fun getApplicationOAuthToken(server: String): ApplicationOAuthToken? =
withContext(ioCoroutineContext) {
database
.applicationQueries
.selectByServer(server)
.executeAsOneOrNull()
?.let {
ApplicationOAuthToken(
server = it.instance,
clientId = it.client_id,
clientSecret = it.client_secret,
redirectUri = it.redirect_uri,
)
}
}

override suspend fun createAccessToken(
authCode: String,
Expand All @@ -102,10 +103,8 @@ internal class AuthenticationRepositoryImpl(
}
}

override suspend fun saveAccessToken(server: String, token: String) {
withContext(ioCoroutineContext) {
settings.saveAccessToken(server = server, token = token)
}
override fun saveAccessToken(server: String, token: String) {
settings.saveAccessToken(server = server, token = token)
}

override val selectedServer: String? = settings.currentDomain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface AuthenticationRepository {
grantType: String,
): String?

suspend fun saveAccessToken(server: String, token: String)
fun saveAccessToken(server: String, token: String)

val selectedServer: String?

Expand Down
10 changes: 7 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# plugins
org-jetbrains-compose = "1.2.1"
org-jetbrains-kotlin = "1.7.20"
org-jetbrains-kotlinx-coroutines = "1.6.4"

# Need to match with kotlin version used: https://github.com/Kotlin/kotlinx-atomicfu/blob/0.18.5/gradle.properties
org-jetbrains-kotlinx-atomicfu = "0.18.5"
org-jetbrains-kotlinx-serialization = "1.4.1"
com-android-tools-build = "7.2.2"
com-squareup-sqldelight = "1.5.3"
com-diffplug-spotless = "6.11.0"
com-google-osdetector = "1.7.1"
# libraries
io-ktor = "2.1.3"
com-google-truth = "1.1.3"
org-jetbrains-kotlinx = "1.4.1"
org-jetbrains-kotlinx-coroutines = "1.6.4"
androidx-core = "1.3.1"
androidx-appcompat = "1.5.1"
androidx-activity = "1.6.1"
Expand All @@ -28,6 +31,7 @@ org-jetbrains-compose-gradle-plugin = { module = "org.jetbrains.compose:compose-
org-jetbrains-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "org-jetbrains-kotlin" }
com-android-tools-build-gradle = { module = "com.android.tools.build:gradle", version.ref = "com-android-tools-build" }
org-jetbrains-kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlinx-atomicfu-plugin = { module = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "org-jetbrains-kotlinx-atomicfu" }
com-squareup-sqldelight-gradle-plugin = { module = "com.squareup.sqldelight:gradle-plugin", version.ref = "com-squareup-sqldelight" }
com-diffplug-spotless-gradle-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "com-diffplug-spotless" }
com-google-osdetector-gradle-plugin = { module = "com.google.gradle:osdetector-gradle-plugin", version.ref = "com-google-osdetector" }
Expand All @@ -49,7 +53,7 @@ io-insert-koin-android = { module = "io.insert-koin:koin-android", version.ref =
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "org-jetbrains-kotlinx-coroutines" }
kotlinx-coroutines-javafx = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-javafx", version.ref = "org-jetbrains-kotlinx-coroutines" }
org-jetbrains-kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "org-jetbrains-kotlinx-coroutines" }
org-jetbrains-kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "org-jetbrains-kotlinx" }
org-jetbrains-kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "org-jetbrains-kotlinx-serialization" }

org-jetbrains-kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common", version.ref = "org-jetbrains-kotlin" }
Expand Down

0 comments on commit e04cc44

Please sign in to comment.