Skip to content

Instantly share code, notes, and snippets.

View xajik's full-sized avatar
💡
Ideating

Igor xajik

💡
Ideating
View GitHub Profile
@xajik
xajik / build.gradle.kts
Last active February 27, 2021 07:33
Kotlin/Native 1.4 iOS unit test gradle task
tasks.register("iosTest") {
val device = project.findProperty("iosDevice") as? String ?: "iPhone 11 Pro Max"
dependsOn("linkDebugTestIosX64")
group = JavaBasePlugin.VERIFICATION_GROUP
description = "Runs tests for target 'iosX64' on an iOS simulator"
doLast {
val binary = (kotlin.targets["iosX64"] as KotlinNativeTarget).binaries.getTest("DEBUG").outputFile
exec {
commandLine("xcrun", "simctl", "spawn", "--standalone", device, binary.absolutePath)
}
@xajik
xajik / FlowUtils.kt
Last active February 27, 2021 07:37
/**
* @see [https://github.com/JetBrains/kotlinconf-app/blob/33f2d4e65f470d1444c5d4b46249af8feb243d03/common/src/mobileMain/kotlin/org/jetbrains/kotlinconf/FlowUtils.kt]
* */
@FlowPreview
@ExperimentalCoroutinesApi
fun <T> ConflatedBroadcastChannel<T>.wrap(): CFlow<T> = CFlow(asFlow())
fun <T> Flow<T>.wrap(coroutineScope: CoroutineScope? = null): CFlow<T> = CFlow(this, coroutineScope)
class CFlow<T>(
@xajik
xajik / DependencyManager.kt
Created February 25, 2021 15:38
Kodein DI examp;e for Kotlin/Native
private val databaseModule = DI.Module("database") {
bind<SqlDriver>() with singleton {
DatabaseFactory.createEngine(instance(), instance(), isDebug)
}
//...
}
private val viewModelModule = DI.Module("viewModel") {
bind<LoginViewModel>() with provider {
@xajik
xajik / AppDependencyProvider.kt
Created February 25, 2021 15:29
Kotlin/Native expected class with overloaded constructor
//K/N
expect class AppDependencyProvider
//Android
actual class AppDependencyProvider(var context: Context)
//iOS
actual class AppDependencyProvider()
@xajik
xajik / build.gradle.kts
Created February 25, 2021 15:25
Kotlin/Native shared project dependencies
val ktorVersion = "1.5.0"
val coroutinesVersion = "1.4.2-native-mt"
val serializationVersion = "1.0.1"
val kodeinVersion = "7.1.0"
val sqlDelightVersion = "1.4.4"
val datetimeVersion = "0.1.1"
val kryptoVersion = "1.12.0"
val junitVersion = "4.13.1"
sourceSets {
@xajik
xajik / swapi_load_people.swift
Last active May 8, 2020 05:02
Swift implementation of swapi loader
import Foundation
private let SwapiURL = "https://swapi.dev/api/people/";
class NativeSwapiClient {
func loadPeople(onSuccess: @escaping (([People]?) -> Void)) {
guard let url = URL(string: SwapiURL) else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
@xajik
xajik / cbindgen_build.rs
Created May 2, 2020 10:36
Build config example for cbindgen in rust
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_language(C)
.generate()
.expect("Unable to generate bindings")
.write_to_file("src/greetings.h");
@xajik
xajik / rust_ffi_getter.rs
Created May 2, 2020 10:33
Rust SwapiClient getter for fields
//return name
pub unsafe extern "C" fn people_get_name(person: *mut People) -> *mut c_char {
debug_assert!(!person.is_null());
let person = person.as_ref().unwrap();
return CString::new(person.name.to_owned()).unwrap().into_raw();
}
//Or you can accept pointer to name as param
#[no_mangle]
pub unsafe extern "C" fn people_get_name_(
@xajik
xajik / SwiftSwapiClient.swift
Last active May 7, 2020 06:30
Swift wrapper for Rust FFI Swapi client
/Wrapper for Rust SwapiClient
class SwapiLoader {
private let client: OpaquePointer
init() {
client = create_swapi_client()
}
deinit {
@xajik
xajik / rust_swapi_ffi_c_compat.rs
Last active April 1, 2022 06:30
Rust SwapiClient FFI
//Create client
#[no_mangle]
pub extern "C" fn create_swapi_client() -> *mut SwapiClient {
Box::into_raw(Box::new(SwapiClient::new()))
}
//Release memory
#[no_mangle]
pub unsafe extern "C" fn free_swapi_client(client: *mut SwapiClient) {
assert!(!client.is_null());