diff --git a/.gitignore b/.gitignore index 714b5d9..57b84ea 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ xcuserdata/ *.moved-aside *.xccheckout *.xcscmblueprint +*.DS_Store ## Obj-C/Swift specific *.hmap @@ -70,4 +71,9 @@ AndroidBluetooth.xcodeproj/* Package.resolved # VS Code -.vscode \ No newline at end of file +.vscode + +# Android +*.so +.gradle +.idea \ No newline at end of file diff --git a/Demo/Package.swift b/Demo/Package.swift new file mode 100644 index 0000000..ab35f25 --- /dev/null +++ b/Demo/Package.swift @@ -0,0 +1,44 @@ +// swift-tools-version: 6.2 +import PackageDescription + +let package = Package( + name: "SwiftAndroidApp", + platforms: [ + .macOS(.v15), + ], + products: [ + .library( + name: "SwiftAndroidApp", + type: .dynamic, + targets: ["SwiftAndroidApp"] + ), + ], + dependencies: [ + .package( + path: "../" + ), + .package( + url: "https://github.com/PureSwift/Android.git", + branch: "master" + ), + ], + targets: [ + .target( + name: "SwiftAndroidApp", + dependencies: [ + .product( + name: "AndroidBluetooth", + package: "AndroidBluetooth" + ), + .product( + name: "AndroidKit", + package: "Android" + ) + ], + path: "./app/src/main/swift", + swiftSettings: [ + .swiftLanguageMode(.v5) + ] + ) + ] +) diff --git a/Demo/app/.gitignore b/Demo/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/Demo/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/Demo/app/build.gradle.kts b/Demo/app/build.gradle.kts new file mode 100644 index 0000000..4bed26e --- /dev/null +++ b/Demo/app/build.gradle.kts @@ -0,0 +1,89 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) +} + +android { + namespace = "com.pureswift.swiftandroid" + compileSdk = 35 + + defaultConfig { + applicationId = "com.pureswift.swiftandroid" + minSdk = 29 + targetSdk = 35 + versionCode = 1 + versionName = "1.0" + ndk { + //noinspection ChromeOsAbiSupport + abiFilters += listOf("arm64-v8a") + } + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + compose = true + } + packaging { + resources { + excludes += listOf("/META-INF/{AL2.0,LGPL2.1}") + } + jniLibs { + keepDebugSymbols += listOf( + "*/arm64-v8a/*.so", + "*/armeabi-v7a/*.so", + "*/x86_64/*.so" + ) + } + } +} + +// Compile native Swift code for the demo app with `skip android build`. +val buildSwift by tasks.registering(Exec::class) { + group = "build" + description = "Build native Swift sources for Android" + workingDir(rootProject.projectDir) + commandLine("bash", "build-swift.sh") +} + +tasks.named("preBuild") { + dependsOn(buildSwift) +} + +dependencies { + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.recyclerview) + implementation(libs.androidx.navigation.runtime) + implementation(libs.androidx.material3) + implementation(libs.material) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.ui.test.junit4) + debugImplementation(libs.androidx.ui.tooling) + debugImplementation(libs.androidx.ui.test.manifest) +} diff --git a/Demo/app/proguard-rules.pro b/Demo/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/Demo/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/Demo/app/src/androidTest/java/com/pureswift/swiftandroid/ExampleInstrumentedTest.kt b/Demo/app/src/androidTest/java/com/pureswift/swiftandroid/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..0b3aecb --- /dev/null +++ b/Demo/app/src/androidTest/java/com/pureswift/swiftandroid/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.pureswift.swiftandroid + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.pureswift.swiftandroid", appContext.packageName) + } +} \ No newline at end of file diff --git a/Demo/app/src/main/AndroidManifest.xml b/Demo/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d9a561f --- /dev/null +++ b/Demo/app/src/main/AndroidManifest.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Demo/app/src/main/java/com/example/swift/HelloSubclass.java b/Demo/app/src/main/java/com/example/swift/HelloSubclass.java new file mode 100644 index 0000000..2312d8f --- /dev/null +++ b/Demo/app/src/main/java/com/example/swift/HelloSubclass.java @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +public class HelloSubclass extends HelloSwift { + private String greeting; + + public HelloSubclass(String greeting) { + this.greeting = greeting; + } + + public void greetMe() { + super.greet(greeting); + } +} diff --git a/Demo/app/src/main/java/com/example/swift/HelloSwift.java b/Demo/app/src/main/java/com/example/swift/HelloSwift.java new file mode 100644 index 0000000..5a1ba2e --- /dev/null +++ b/Demo/app/src/main/java/com/example/swift/HelloSwift.java @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +import java.util.function.Predicate; + +public class HelloSwift { + public double value; + public static double initialValue = 3.14159; + public String name = "Java"; + + static { + System.loadLibrary("SwiftAndroidApp"); + } + + public HelloSwift() { + this.value = initialValue; + } + + public native int sayHello(int x, int y); + public native String throwMessageFromSwift(String message) throws Exception; + + // To be called back by the native code + public double sayHelloBack(int i) { + System.out.println("And hello back from " + name + "! You passed me " + i); + return value; + } + + public void greet(String name) { + System.out.println("Salutations, " + name); + } + + public Predicate lessThanTen() { + Predicate predicate = i -> (i < 10); + return predicate; + } + + public String[] doublesToStrings(double[] doubles) { + int size = doubles.length; + String[] strings = new String[size]; + + for(int i = 0; i < size; i++) { + strings[i] = "" + doubles[i]; + } + + return strings; + } + + public void throwMessage(String message) throws Exception { + throw new Exception(message); + } +} diff --git a/Demo/app/src/main/java/com/example/swift/ThreadSafe.java b/Demo/app/src/main/java/com/example/swift/ThreadSafe.java new file mode 100644 index 0000000..2b1b358 --- /dev/null +++ b/Demo/app/src/main/java/com/example/swift/ThreadSafe.java @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface ThreadSafe { +} diff --git a/Demo/app/src/main/java/com/example/swift/ThreadSafeHelperClass.java b/Demo/app/src/main/java/com/example/swift/ThreadSafeHelperClass.java new file mode 100644 index 0000000..3b7793f --- /dev/null +++ b/Demo/app/src/main/java/com/example/swift/ThreadSafeHelperClass.java @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +import java.util.Optional; +import java.util.OptionalLong; +import java.util.OptionalInt; +import java.util.OptionalDouble; + +@ThreadSafe +public class ThreadSafeHelperClass { + public ThreadSafeHelperClass() { } + + public Optional text = Optional.of(""); + + public final OptionalDouble val = OptionalDouble.of(2); + + public String getValue(Optional name) { + return name.orElse(""); + } + + public Optional getText() { + return text; + } + + public OptionalLong from(OptionalInt value) { + return OptionalLong.of(value.getAsInt()); + } +} diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Application.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Application.kt new file mode 100644 index 0000000..4075c97 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Application.kt @@ -0,0 +1,29 @@ +package com.pureswift.swiftandroid + +import com.example.swift.HelloSubclass + +class Application: android.app.Application() { + + init { + NativeLibrary.shared() + } + + override fun onCreate() { + super.onCreate() + onCreateSwift() + } + + private external fun onCreateSwift() + + override fun onTerminate() { + super.onTerminate() + onTerminateSwift() + } + + private external fun onTerminateSwift() + + fun sayHello() { + val result = HelloSubclass("Swift").sayHello(17, 25) + println("sayHello(17, 25) = $result") + } +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/EmbeddedAndroidViewDemo.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/EmbeddedAndroidViewDemo.kt new file mode 100644 index 0000000..5b20617 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/EmbeddedAndroidViewDemo.kt @@ -0,0 +1,63 @@ +package com.pureswift.swiftandroid + +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.material3.Text +import androidx.compose.material3.Button +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import androidx.core.content.ContextCompat + +@Preview(showBackground = true) +@Composable +fun EmbeddedAndroidViewDemo() { + Column { + val state = remember { mutableIntStateOf(0) } + + //widget.ImageView + AndroidView(factory = { ctx -> + ImageView(ctx).apply { + val drawable = ContextCompat.getDrawable(ctx, R.drawable.ic_launcher_foreground) + setImageDrawable(drawable) + } + }) + + //Compose Button + Button(onClick = { state.value++ }) { + Text("MyComposeButton") + } + + //widget.Button + AndroidView(factory = { ctx -> + //Here you can construct your View + android.widget.Button(ctx).apply { + text = "MyAndroidButton" + layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT) + setOnClickListener { + state.value++ + } + } + }, modifier = Modifier.padding(8.dp)) + + //widget.TextView + AndroidView(factory = { ctx -> + //Here you can construct your View + TextView(ctx).apply { + layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT) + } + }, update = { + it.text = "You have clicked the buttons: " + state.value.toString() + " times" + }) + } +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Fragment.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Fragment.kt new file mode 100644 index 0000000..26a6c39 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Fragment.kt @@ -0,0 +1,24 @@ +package com.pureswift.swiftandroid + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout + +class Fragment(val swiftObject: SwiftObject): android.app.Fragment() { + + @Deprecated("Deprecated in Java") + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val context = this.context + checkNotNull(context) + val linearLayout = LinearLayout(context) + return linearLayout + } + + external override fun onViewCreated(view: View, savedInstanceState: Bundle?) +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ListViewAdapter.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ListViewAdapter.kt new file mode 100644 index 0000000..722b2b0 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ListViewAdapter.kt @@ -0,0 +1,14 @@ +package com.pureswift.swiftandroid + +import android.R +import android.content.Context +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import androidx.recyclerview.widget.RecyclerView + +class ListViewAdapter(context: Context, val swiftObject: SwiftObject, val objects: ArrayList) : + ArrayAdapter(context, 0, objects) { + + external override fun getView(position: Int, convertView: View?, parent: ViewGroup): View +} diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/MainActivity.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/MainActivity.kt new file mode 100644 index 0000000..2f3c2eb --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/MainActivity.kt @@ -0,0 +1,83 @@ +package com.pureswift.swiftandroid + +import android.Manifest +import android.os.Bundle +import android.util.Log +import android.view.View +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.width +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.core.app.ActivityCompat +import java.util.Date + +class MainActivity : ComponentActivity() { + + init { + NativeLibrary.shared() + } + + val emitter = UnitEmitter() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + onCreateSwift(savedInstanceState) + //enableEdgeToEdge() + + // Request permissions on startup. + val permissions = listOf( + Manifest.permission.BLUETOOTH_SCAN, + Manifest.permission.BLUETOOTH_CONNECT, + Manifest.permission.BLUETOOTH_ADVERTISE, + Manifest.permission.INTERNET + ) + val requestTag = 1 + ActivityCompat.requestPermissions(this, permissions.toTypedArray(), requestTag) + } + + external fun onCreateSwift(savedInstanceState: Bundle?) + + fun setRootView(view: View) { + Log.d("MainActivity", "AndroidSwiftUI.MainActivity.setRootView(_:)") + setContentView(view) + } +} + +@Composable +fun EventReceiver(emitter: UnitEmitter) { + + val tick by emitter.flow.collectAsState(initial = Unit) + + var date by remember { mutableStateOf(Date()) } + + LaunchedEffect(Unit) { + emitter.flow.collect { + date = Date() + } + } + + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center + ) { + Column( + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text("Hello Swift!") + Text(date.toString()) + } + } +} diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NativeLibrary.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NativeLibrary.kt new file mode 100644 index 0000000..871a45c --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NativeLibrary.kt @@ -0,0 +1,35 @@ +package com.pureswift.swiftandroid + +import android.util.Log + +class NativeLibrary private constructor() { + + companion object { + + @Volatile + var shared : NativeLibrary? = null + + fun shared(): NativeLibrary { + return shared?: synchronized(this){ + val instance = NativeLibrary() + shared = instance + return instance + } + } + } + + init { + loadNativeLibrary() + } + + private fun loadNativeLibrary() { + try { + System.loadLibrary("SwiftAndroidApp") + System.loadLibrary("SwiftJava") + } catch (error: UnsatisfiedLinkError) { + Log.e("NativeLibrary", "Unable to load native libraries: $error") + return + } + Log.d("NativeLibrary", "Loaded Swift library") + } +} diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NavigationBarViewOnItemSelectedListener.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NavigationBarViewOnItemSelectedListener.kt new file mode 100644 index 0000000..cf8014f --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/NavigationBarViewOnItemSelectedListener.kt @@ -0,0 +1,9 @@ +package com.pureswift.swiftandroid + +import android.view.MenuItem +import com.google.android.material.navigation.NavigationBarView + +class NavigationBarViewOnItemSelectedListener(val action: SwiftObject): NavigationBarView.OnItemSelectedListener { + + external override fun onNavigationItemSelected(menuItem: MenuItem): Boolean +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/RecyclerViewAdapter.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/RecyclerViewAdapter.kt new file mode 100644 index 0000000..d11648d --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/RecyclerViewAdapter.kt @@ -0,0 +1,40 @@ +package com.pureswift.swiftandroid + +import android.util.Log +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.ViewHolder + +class RecyclerViewAdapter(val swiftObject: SwiftObject) : + RecyclerView.Adapter() { + + class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + } + + // Create new views (invoked by the layout manager) + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewAdapter.ViewHolder { + Log.d("RecyclerViewAdapter", "SwiftAndroidApp.RecyclerViewAdapter.onCreateViewHolderSwift(_:_:) $viewType") + val view = LinearLayout(parent.context) + val viewHolder = ViewHolder(view) + checkNotNull(viewHolder) + checkNotNull(viewHolder.itemView) + return viewHolder + } + + // Replace the contents of a view (invoked by the layout manager) + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + onBindViewHolderSwift(holder as RecyclerViewAdapter.ViewHolder, position) + } + + external fun onBindViewHolderSwift(holder: RecyclerViewAdapter.ViewHolder, position: Int) + + // Return the size of your dataset (invoked by the layout manager) + override fun getItemCount(): Int { + return getItemCountSwift() + } + + external fun getItemCountSwift(): Int +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Runnable.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Runnable.kt new file mode 100644 index 0000000..08a0987 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/Runnable.kt @@ -0,0 +1,6 @@ +package com.pureswift.swiftandroid + +class Runnable(val block: SwiftObject): java.lang.Runnable { + + external override fun run() +} diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/SwiftObject.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/SwiftObject.kt new file mode 100644 index 0000000..94ff2db --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/SwiftObject.kt @@ -0,0 +1,17 @@ +package com.pureswift.swiftandroid + +/// Swift object retained by JVM +class SwiftObject(val swiftObject: Long, val type: String) { + + override fun toString(): String { + return toStringSwift() + } + + external fun toStringSwift(): String + + fun finalize() { + finalizeSwift() + } + + external fun finalizeSwift() +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/UnitEmitter.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/UnitEmitter.kt new file mode 100644 index 0000000..93af004 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/UnitEmitter.kt @@ -0,0 +1,15 @@ +package com.pureswift.swiftandroid + +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow + +class UnitEmitter() { + + private val _flow = MutableSharedFlow(extraBufferCapacity = 64) + val flow: SharedFlow get() = _flow + + fun emit() { + //println("Emit") + _flow.tryEmit(Unit) + } +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ViewOnClickListener.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ViewOnClickListener.kt new file mode 100644 index 0000000..39b5ce3 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ViewOnClickListener.kt @@ -0,0 +1,8 @@ +package com.pureswift.swiftandroid + +import android.view.View + +class ViewOnClickListener(val action: SwiftObject): View.OnClickListener { + + external override fun onClick(view: View) +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Color.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Color.kt new file mode 100644 index 0000000..4a8371b --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.pureswift.swiftandroid.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Theme.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Theme.kt new file mode 100644 index 0000000..47f8f81 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.pureswift.swiftandroid.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun SwiftAndroidTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Type.kt b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Type.kt new file mode 100644 index 0000000..4de94b7 --- /dev/null +++ b/Demo/app/src/main/kotlin/com/pureswift/swiftandroid/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.pureswift.swiftandroid.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/Demo/app/src/main/kotlin/org/pureswift/bluetooth/le/ScanCallback.kt b/Demo/app/src/main/kotlin/org/pureswift/bluetooth/le/ScanCallback.kt new file mode 100644 index 0000000..f4d701a --- /dev/null +++ b/Demo/app/src/main/kotlin/org/pureswift/bluetooth/le/ScanCallback.kt @@ -0,0 +1,87 @@ +package org.pureswift.bluetooth.le + +import android.bluetooth.le.ScanCallback as AndroidScanCallback +import android.bluetooth.le.ScanResult +import android.util.Log + +/** + * Bluetooth LE Scan Callback for AndroidBluetooth Swift package. + * + * This class is referenced by AndroidBluetooth's LowEnergyScanCallback + * via the @JavaClass("org.pureswift.bluetooth.le.ScanCallback") annotation. + * It extends Android's ScanCallback and provides the bridge between + * Android's Bluetooth LE scanning and the Swift AndroidBluetooth package. + */ +open class ScanCallback( + private var swiftPeer: Long = 0L +) : AndroidScanCallback() { + + fun setSwiftPeer(swiftPeer: Long) { + this.swiftPeer = swiftPeer + } + + fun getSwiftPeer(): Long { + return swiftPeer + } + + fun finalize() { + swiftRelease(swiftPeer) + swiftPeer = 0L + } + + private external fun swiftRelease(swiftPeer: Long) + + companion object { + private const val TAG = "PureSwift.ScanCallback" + } + + /** + * Callback when a BLE advertisement has been found. + * + * @param callbackType Determines how this callback was triggered + * @param result A Bluetooth LE scan result + */ + override fun onScanResult(callbackType: Int, result: ScanResult?) { + super.onScanResult(callbackType, result) + swiftOnScanResult(swiftPeer, callbackType, result) + } + + external fun swiftOnScanResult( + swiftPeer: Long, + callbackType: Int, + result: ScanResult? + ) + + /** + * Callback when batch results are delivered. + * + * @param results List of scan results that are previously scanned + */ + override fun onBatchScanResults(results: MutableList?) { + super.onBatchScanResults(results) + if (swiftPeer != 0L) { + swiftOnBatchScanResults(swiftPeer, results) + } else { + Log.d(TAG, "onBatchScanResults: ${results?.size ?: 0} results") + } + } + private external fun swiftOnBatchScanResults( + swiftPeer: Long, + results: MutableList? + ) + + /** + * Callback when scan could not be started. + * + * @param errorCode Error code (one of SCAN_FAILED_*) + */ + override fun onScanFailed(errorCode: Int) { + super.onScanFailed(errorCode) + if (swiftPeer != 0L) { + swiftOnScanFailed(swiftPeer, errorCode) + } else { + Log.e(TAG, "onScanFailed: errorCode=$errorCode") + } + } + private external fun swiftOnScanFailed(swiftPeer: Long, errorCode: Int) +} diff --git a/Demo/app/src/main/res/drawable/ic_launcher_background.xml b/Demo/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/Demo/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Demo/app/src/main/res/drawable/ic_launcher_foreground.xml b/Demo/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/Demo/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Demo/app/src/main/res/layout/list_item.xml b/Demo/app/src/main/res/layout/list_item.xml new file mode 100644 index 0000000..eaf5504 --- /dev/null +++ b/Demo/app/src/main/res/layout/list_item.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/Demo/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Demo/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/Demo/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/Demo/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/Demo/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/Demo/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/Demo/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/Demo/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/Demo/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/Demo/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/Demo/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/Demo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/Demo/app/src/main/res/values/colors.xml b/Demo/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/Demo/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/Demo/app/src/main/res/values/strings.xml b/Demo/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..7de10d2 --- /dev/null +++ b/Demo/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + SwiftAndroid + \ No newline at end of file diff --git a/Demo/app/src/main/res/values/themes.xml b/Demo/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..c81f2c7 --- /dev/null +++ b/Demo/app/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +