Skip to content

Commit

Permalink
migration to kotlin-inject partial 3
Browse files Browse the repository at this point in the history
  • Loading branch information
danielyrovas committed Apr 10, 2024
1 parent 0e51805 commit bf3406d
Show file tree
Hide file tree
Showing 20 changed files with 409 additions and 218 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ android {
dependencies {
implementation(libs.core.ktx)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.lifecycle.viewmodel.ktx)
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
implementation(libs.ui)
Expand Down Expand Up @@ -90,4 +91,5 @@ dependencies {
androidTestImplementation(libs.ui.test.junit4)
debugImplementation(libs.ui.tooling)
debugImplementation(libs.ui.test.manifest)
implementation(kotlin("script-runtime"))
}
73 changes: 73 additions & 0 deletions app/src/main/java/org/yrovas/linklater/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.yrovas.linklater

import android.content.Context
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.DefaultRequest
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.request.header
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import me.tatarka.inject.annotations.Scope
import org.yrovas.linklater.data.local.PrefDataStore
import org.yrovas.linklater.data.local.PrefStore
import org.yrovas.linklater.data.remote.BookmarkAPI
import org.yrovas.linklater.data.remote.LinkDingAPI
import org.yrovas.linklater.ui.activity.DestinationHost

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")

@Scope
annotation class AppScope

@Component
@AppScope
abstract class AppComponent(
@get:Provides val context: Context,
) {
abstract val destinationHost: DestinationHost
abstract val prefStore: PrefDataStore

private val store: DataStore<Preferences>
get() = context.dataStore

@Provides
fun providePrefStore(): PrefDataStore = PrefStore(store)

@Provides
fun provideHttpClient(): HttpClient = HttpClient(Android) {
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Log.i("Ktor =>", message)
}
}
level = LogLevel.ALL
}
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}

abstract val linkDingAPI: LinkDingAPI
val bookmarkAPI: BookmarkAPI
@Provides get() = linkDingAPI
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.yrovas.linklater.data
package org.yrovas.linklater.data.local

import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.flow.Flow
import org.yrovas.linklater.AppScope

@AppScope
interface PrefDataStore {
suspend fun <T> getPrefs(key: Preferences.Key<T>, default: T): Flow<T>
suspend fun <T> getPref(key: Preferences.Key<T>, default: T): T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.yrovas.linklater.data
package org.yrovas.linklater.data.local

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
Expand All @@ -21,7 +21,7 @@ object Prefs {

@AppScope
@Inject
class PreferenceStore(private val store: DataStore<Preferences>) : PrefDataStore {
class PrefStore(private val store: DataStore<Preferences>) : PrefDataStore {
override suspend fun <T> getPrefs(
key: Preferences.Key<T>,
default: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.yrovas.linklater.domain
package org.yrovas.linklater.data.remote

import android.content.Context
import org.yrovas.linklater.data.*
import org.yrovas.linklater.domain.*
import org.yrovas.linklater.data.Bookmark
import org.yrovas.linklater.data.LocalBookmark
import org.yrovas.linklater.domain.APIError
import org.yrovas.linklater.domain.Res

interface BookmarkAPI {
suspend fun authenticate(endpoint: String? = null, token: String? = null, validate: Boolean = false): Res<Unit, APIError>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.yrovas.linklater.domain
package org.yrovas.linklater.data.remote

import android.content.Context
import kotlinx.coroutines.delay
import org.yrovas.linklater.data.Bookmark
import org.yrovas.linklater.data.LocalBookmark
import org.yrovas.linklater.domain.APIError
import org.yrovas.linklater.domain.Err
import org.yrovas.linklater.domain.Ok
import org.yrovas.linklater.domain.Res

class EmptyBookmarkAPI : BookmarkAPI {
override suspend fun authenticate(
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/org/yrovas/linklater/data/remote/LinkDingAPI.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.yrovas.linklater.domain
package org.yrovas.linklater.data.remote

import android.content.Context
import android.util.Log
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.*
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
Expand All @@ -14,6 +17,11 @@ import org.yrovas.linklater.checkBookmarkAPIToken
import org.yrovas.linklater.checkURL
import org.yrovas.linklater.data.Bookmark
import org.yrovas.linklater.data.LocalBookmark
import org.yrovas.linklater.domain.APIError
import org.yrovas.linklater.domain.Err
import org.yrovas.linklater.domain.Ok
import org.yrovas.linklater.domain.Res
import org.yrovas.linklater.domain.toRes
import java.io.File

const val BOOKMARKS_CACHE_PATH = "bookmark_page_cache.json"
Expand All @@ -27,14 +35,19 @@ class LinkDingAPI(
private var token: String? = null,
private val pageSize: Int = 20,
) : BookmarkAPI {
init {
Log.d("DEBUG", "CREATING BOOKMARK API: ")
}

private val authProvided = !endpoint.isNullOrBlank() && !token.isNullOrBlank()
private val authProvided
get() = !endpoint.isNullOrBlank() && !token.isNullOrBlank()

override suspend fun authenticate(
endpoint: String?,
token: String?,
validate: Boolean,
): Res<Unit, APIError> {
Log.d("DEBUG", "authenticate: with endpoint: $endpoint")
if (!endpoint.isNullOrBlank()) {
if (!checkURL(endpoint)) return Err(APIError.AUTH)
this.endpoint = endpoint
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/org/yrovas/linklater/domain/Res.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ fun <D, T : Any, E : Error> Res<D, E>.then(ok: (data: D) -> T, err: (error: E) -
}
}

fun <D, T : Any, E : Error> Res<D, E>?.then(
ok: (data: D) -> T,
err: (error: E) -> T,
nil: () -> T,
): T {
return this?.then(ok, err) ?: nil()
}

@Composable
fun <D, T : Any, E : Error> Res<D, E>.apply(
ok: @Composable (data: D) -> T,
err: @Composable (error: E) -> T,
): T {
return when (this) {
is Res.Err -> err(error)
is Res.Ok -> ok(data)
}
}

@Composable
fun <D, T : Any, E : Error> Res<D, E>?.apply(
ok: @Composable (data: D) -> T,
err: @Composable (error: E) -> T,
nil: @Composable () -> T,
): T {
return this?.apply(ok, err) ?: nil()
}

fun <D, E : Error> Res<D, E>.getOrNull(): D? {
return when (this) {
is Res.Err -> null
Expand Down
Loading

0 comments on commit bf3406d

Please sign in to comment.