Skip to content

Instantly share code, notes, and snippets.

View jeremyrempel's full-sized avatar

Jeremy Rempel jeremyrempel

View GitHub Profile
val client = HttpClient { install(ExpectSuccess }
val endPoint = "https://api.unsplash.com"
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(JSON.nonstrict).apply {
setMapper(PhotoResponse::class, PhotoResponse.serializer())
}
}
install(ExpectSuccess)
}
commonMainImplementation "io.ktor:ktor-client-core:$ktor_version"
androidMainImplementation "io.ktor:ktor-client-android:$ktor_version"
androidMainImplementation "io.ktor:ktor-client-json-jvm:$ktor_version"
iosMainImplementation "io.ktor:ktor-client-ios:$ktor_version"
iosMainImplementation "io.ktor:ktor-client-json-native:$ktor_version"
commonMainImplementation "io.ktor:ktor-client-core:$ktor_version"
androidMainImplementation "io.ktor:ktor-client-android:$ktor_version"
androidMainImplementation "io.ktor:ktor-client-json-jvm:$ktor_version"
iosMainImplementation "io.ktor:ktor-client-ios:$ktor_version"
iosMainImplementation "io.ktor:ktor-client-json-native:$ktor_version"
@Serializable
data class PhotoResponse(val id: String, val description: String?, val exif: Exif?, val urls: Urls, val user: User) {
@Serializable
data class Exif(val make: String?, val model: String?)
@Serializable
data class Urls(val raw: String, val full: String, val regular: String, val thumb: String)
@Serializable
interface PhotoView : BaseView {
var isUpdating: Boolean
fun onUpdate(data: PhotoResponse)
}
interface PhotoActions {
fun onRequestData()
}
class PhotoPresenter(
uiContext: CoroutineContext,
val view: PhotoView
) : CoroutinePresenter(uiContext, view), PhotoActions {
val api: PhotoApi by kodein.instance("Api")
commonMainImplementation "org.kodein.di:kodein-di-erased:$kodein_version"
// provide dependency Api
bind<PhotoApi>("Api") with provider {
val client by kodein.instance<HttpClient>()
val clientId by kodein.instance<String>("ClientId")
val apiHost by kodein.instance<String>("ApiHost")
PhotoApiService(client, apiHost, clientId)
}
// request dependency Api
sealed class LogLevel {
object DEBUG : LogLevel()
object INFO : LogLevel()
object WARN : LogLevel()
object ERROR : LogLevel()
}
expect fun log(level: LogLevel, tag: String, message: String, error: Throwable)
import platform.Foundation.NSLog
actual fun log(level: LogLevel, tag: String, message: String, error: Throwable) =
NSLog("[$level]: ($tag), $message, $error")