Skip to content

Instantly share code, notes, and snippets.

View danailalexiev's full-sized avatar

Danail Alexiev danailalexiev

  • Sofia, Bulgaria
View GitHub Profile
@danailalexiev
danailalexiev / JokeClient.swift
Created October 5, 2022 08:30
Kotlin Multiplatform Default Completion handler
func getRandomJoke(jokeCategory: JokeCategory?, completionHandler: (Result<Joke>?, Error?) -> ()) {
...
}
@danailalexiev
danailalexiev / ApiClient.kt
Created October 5, 2022 08:00
Kotlin Multiplatform Common API Client
internal interface ApiClient {
suspend fun getRandomJoke(category: JokeCategory? = null): Result<Joke>
suspend fun getJokeCategories(): Result<List<JokeCategory>>
suspend fun findJoke(query: String): Result<JokeSearchResult>
}
@danailalexiev
danailalexiev / JokeClient.kt
Created October 5, 2022 07:58
Kotlin Multiplatform JS Interop
interface JokeClient {
suspend fun getRandomJoke(category: JokeCategory? = null): Result<Joke>
suspend fun getJokeCategories(): Result<List<JokeCategory>>
suspend fun findJoke(query: String): Result<JokeSearchResult>
}
@danailalexiev
danailalexiev / JokeClientExt.swift
Created October 5, 2022 07:57
Kotlin Interface Swift Extension
import Foundation
import sdk
extension JokeClient {
func getRandomJokeAsync(jokeCategory: JokeCategory? = nil) async throws -> Joke {
return try await convertToAsync(kotlinCall: { completionHandler in
self.getRandomJoke(category: jokeCategory, completionHandler: completionHandler)
})
}
@danailalexiev
danailalexiev / KotlinInterop.swift
Created October 5, 2022 07:55
Kotlin Interop Swift
import Foundation
import sdk
typealias NativeCompletionHandler<Result> = (Result?, Error?) -> ()
typealias NativeCall<Result> = (@escaping NativeCompletionHandler<Result>) -> NativeCancellable
func convertToAsync<Result>(kotlinCall call: @escaping NativeCall<Result>) async throws -> Result {
let cancellableActor: NativeCancellableActor = NativeCancellableActor()
@danailalexiev
danailalexiev / JokeClient.kt
Created October 5, 2022 07:52
Kotlin Multiplatform iOS Interop
interface JokeClient {
fun getRandomJoke(
category: JokeCategory? = null,
completionHandler: CompletionHandler<Joke?>
): NativeCancellable
fun getJokeCategories(completionHandler: CompletionHandler<List<JokeCategory>?>): NativeCancellable
fun findJoke(
@danailalexiev
danailalexiev / CoroutineInterop.kt
Created October 5, 2022 07:50
Kotlin Multiplatform iOS Coroutine Interop
package com.infinitelambda.chuck.util
import kotlinx.coroutines.*
import platform.Foundation.NSError
import platform.Foundation.NSLocalizedDescriptionKey
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.native.concurrent.SharedImmutable
import kotlin.native.concurrent.ThreadLocal
import kotlin.native.concurrent.freeze
@danailalexiev
danailalexiev / JokeClient.kt
Created October 5, 2022 07:44
Kotlin Multiplatform Android Interop
interface JokeClient {
suspend fun getRandomJoke(category: JokeCategory? = null): Result<Joke>
suspend fun getJokeCategories(): Result<List<JokeCategory>>
suspend fun findJoke(query: String): Result<JokeSearchResult>
}
@danailalexiev
danailalexiev / HttpLogger.kt
Created October 5, 2022 07:41
Kotlin Multiplatform Platform-specific Logging JavaScript
package com.infinitelambda.chuck.util
import io.ktor.client.plugins.logging.*
internal actual object HttpLogger : Logger {
override fun log(message: String) {
console.log("$TAG: $message")
}
@danailalexiev
danailalexiev / HttpLogger.kt
Created October 5, 2022 07:40
Kotlin Multiplatform Platform-specific Logging iOS
package com.infinitelambda.chuck.util
import io.ktor.client.plugins.logging.*
import platform.Foundation.NSLog
internal actual object HttpLogger: Logger {
override fun log(message: String) {
NSLog("$TAG: $message")
}