refactor: switch from metro to koin
Honestly metro looks too overcomplicated and I still don't know how to use it properly. Switching to koin for now as I'm more comfortable with it.
This commit is contained in:
parent
53abaccd21
commit
2725342c3f
23 changed files with 165 additions and 199 deletions
|
|
@ -2,9 +2,9 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
|||
|
||||
plugins {
|
||||
alias(libs.plugins.androidLibrary)
|
||||
alias(libs.plugins.koinCompiler)
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
alias(libs.plugins.kotlinSerialization)
|
||||
alias(libs.plugins.metro)
|
||||
alias(libs.plugins.sqldelight)
|
||||
}
|
||||
|
||||
|
|
@ -23,6 +23,9 @@ kotlin {
|
|||
implementation(libs.ktor.client.websockets)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
|
||||
implementation(project.dependencies.platform(libs.koin.bom))
|
||||
implementation(libs.koin.core)
|
||||
|
||||
implementation(libs.kermit)
|
||||
implementation(libs.settings)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,7 @@ package moe.lava.neon.core
|
|||
|
||||
import com.russhwolf.settings.Settings
|
||||
import com.russhwolf.settings.nullableString
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@Inject
|
||||
class AppSettings {
|
||||
private val settings = Settings()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
package moe.lava.neon.core.api
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.HttpSend
|
||||
|
|
@ -23,8 +20,6 @@ import kotlinx.serialization.json.JsonNamingStrategy
|
|||
import moe.lava.neon.core.api.captcha.CaptchaRequest
|
||||
import moe.lava.neon.core.api.captcha.CaptchaResponse
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@Inject
|
||||
class ApiClient {
|
||||
private val logger = Logger.withTag("neon.core.api/client")
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
package moe.lava.neon.core.api.gateway
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import dev.zacsweers.metro.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import moe.lava.neon.core.di.EventHandlerGraph
|
||||
import moe.lava.neon.core.api.gateway.handlers.EventHandlers
|
||||
import moe.lava.neon.core.repository.AuthRepository
|
||||
import kotlin.math.pow
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@Inject
|
||||
class GatewayHandler(
|
||||
private val auth: AuthRepository,
|
||||
private val handlers: EventHandlerGraph,
|
||||
private val eventHandlers: EventHandlers,
|
||||
) {
|
||||
private val logger = Logger.withTag("neon.core.api.gateway/handler")
|
||||
private val scope = CoroutineScope(Dispatchers.IO)
|
||||
|
|
@ -35,7 +33,7 @@ class GatewayHandler(
|
|||
|
||||
session = GatewaySession.start(
|
||||
token = token,
|
||||
eventHandlers = handlers,
|
||||
eventHandlers = eventHandlers,
|
||||
resumeProps = resumeProps,
|
||||
onSuccess = {
|
||||
logger.d { "Successful session start" }
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ import kotlinx.coroutines.isActive
|
|||
import kotlinx.coroutines.launch
|
||||
import moe.lava.neon.core.api.ApiConstants
|
||||
import moe.lava.neon.core.api.ApiConstants.json
|
||||
import moe.lava.neon.core.di.EventHandlerGraph
|
||||
import moe.lava.neon.core.api.gateway.handlers.EventHandlers
|
||||
import kotlin.random.Random
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ private val logger = Logger.withTag("neon.core.api.gateway/session")
|
|||
class GatewaySession private constructor(
|
||||
private var ws: DefaultClientWebSocketSession,
|
||||
private val token: String,
|
||||
private val handlers: EventHandlerGraph,
|
||||
private val handlers: EventHandlers,
|
||||
private val scope: CoroutineScope,
|
||||
private var resumeProps: ResumeProperties?,
|
||||
private val onDestroy: (GatewayCloseReason, ResumeProperties?) -> Unit,
|
||||
|
|
@ -46,7 +46,7 @@ class GatewaySession private constructor(
|
|||
companion object {
|
||||
suspend fun start(
|
||||
token: String,
|
||||
eventHandlers: EventHandlerGraph,
|
||||
eventHandlers: EventHandlers,
|
||||
client: HttpClient = HttpClient {
|
||||
install(HttpCookies)
|
||||
install(WebSockets)
|
||||
|
|
|
|||
|
|
@ -3,3 +3,7 @@ package moe.lava.neon.core.api.gateway.handlers
|
|||
import moe.lava.neon.core.api.gateway.Event
|
||||
|
||||
sealed interface Handler<T: Event.Incoming>
|
||||
|
||||
class EventHandlers(
|
||||
val ready: ReadyHandler
|
||||
)
|
||||
|
|
@ -1,13 +1,11 @@
|
|||
package moe.lava.neon.core.api.gateway.handlers
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import dev.zacsweers.metro.Inject
|
||||
import moe.lava.neon.core.api.gateway.Event
|
||||
import moe.lava.neon.core.api.gateway.ResumeProperties
|
||||
|
||||
private val logger = Logger.withTag("neon.core.api.events/ready")
|
||||
|
||||
@Inject
|
||||
class ReadyHandler : Handler<Event.Ready> {
|
||||
fun handle(event: Event.Ready, updateResumeProps: (ResumeProperties) -> Unit) {
|
||||
logger.i { "Received payload $event" }
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
package moe.lava.neon.core.di
|
||||
|
||||
import dev.zacsweers.metro.GraphExtension
|
||||
import moe.lava.neon.core.AppSettings
|
||||
import moe.lava.neon.core.api.ApiClient
|
||||
import moe.lava.neon.core.repository.AuthRepository
|
||||
import moe.lava.neon.core.repository.UserRepository
|
||||
|
||||
@GraphExtension
|
||||
interface AppGraph {
|
||||
val api: ApiClient
|
||||
val settings: AppSettings
|
||||
|
||||
val auth: AuthRepository
|
||||
val users: UserRepository
|
||||
|
||||
val gatewayHandlers: EventHandlerGraph
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package moe.lava.neon.core.di
|
||||
|
||||
import moe.lava.neon.core.AppSettings
|
||||
import moe.lava.neon.core.api.ApiClient
|
||||
import moe.lava.neon.core.api.gateway.GatewayHandler
|
||||
import moe.lava.neon.core.api.gateway.handlers.EventHandlers
|
||||
import moe.lava.neon.core.api.gateway.handlers.ReadyHandler
|
||||
import moe.lava.neon.core.repository.AuthRepository
|
||||
import moe.lava.neon.core.repository.UserRepository
|
||||
import org.koin.dsl.module
|
||||
import org.koin.plugin.module.dsl.single
|
||||
|
||||
val coreModule = module {
|
||||
single<ApiClient>()
|
||||
single<AppSettings>()
|
||||
|
||||
single<AuthRepository>()
|
||||
single<UserRepository>()
|
||||
|
||||
single<GatewayHandler>()
|
||||
|
||||
single<ReadyHandler>()
|
||||
single<EventHandlers>()
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
package moe.lava.neon.core.di
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesTo
|
||||
import dev.zacsweers.metro.GraphExtension
|
||||
import moe.lava.neon.core.api.gateway.handlers.ReadyHandler
|
||||
|
||||
@GraphExtension
|
||||
@ContributesTo(AppScope::class)
|
||||
interface EventHandlerGraph {
|
||||
val ready: ReadyHandler
|
||||
}
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
package moe.lava.neon.core.repository
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
|
|
@ -46,8 +43,6 @@ sealed class AuthResponse {
|
|||
// data class MFARequested() : AuthResponse()
|
||||
}
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
class AuthRepository(
|
||||
private val settings: AppSettings,
|
||||
private val api: ApiClient,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,4 @@
|
|||
package moe.lava.neon.core.repository
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
|
||||
@Inject
|
||||
@SingleIn(AppScope::class)
|
||||
class UserRepository {
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue