Skip to content

Commit

Permalink
Update to use new swift-android-native modules
Browse files Browse the repository at this point in the history
  • Loading branch information
marcprux committed Dec 14, 2024
1 parent 6107a53 commit 2c96a47
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
.package(url: "https://source.skip.tools/skip.git", from: "1.2.1"),
.package(url: "https://source.skip.tools/skip-foundation.git", from: "1.2.0"),
.package(url: "https://source.skip.tools/skip-bridge.git", "0.0.0"..<"2.0.0"),
.package(url: "https://source.skip.tools/swift-android-native.git", "0.0.0"..<"2.0.0")
.package(url: "https://source.skip.tools/swift-android-native.git", from: "1.1.0")
],
targets: [
// mode=kotlin
Expand All @@ -27,7 +27,7 @@ let package = Package(
// mode=swift
.target(name: "SkipAndroidBridge", dependencies: [
"SkipAndroidSDKBridge",
.product(name: "AndroidNative", package: "swift-android-native", condition: .when(platforms: [.android])),
.product(name: "AndroidNative", package: "swift-android-native"/*, condition: .when(platforms: [.android])*/),
], plugins: [.plugin(name: "skipstone", package: "skip")]),
// mode=kotlin
.target(name: "SkipAndroidBridgeKt",
Expand All @@ -36,12 +36,17 @@ let package = Package(
],
plugins: [.plugin(name: "skipstone", package: "skip")]),

// .target(name: "SkipAndroidBridgeTestsSupport",
// dependencies: ["SkipAndroidBridgeKt"],
// plugins: [.plugin(name: "skipstone", package: "skip")]),

.testTarget(name: "SkipAndroidSDKBridgeTests", dependencies: [
"SkipAndroidSDKBridge",
.product(name: "SkipTest", package: "skip"),
], plugins: [.plugin(name: "skipstone", package: "skip")]),
.testTarget(name: "SkipAndroidBridgeTests", dependencies: [
"SkipAndroidBridge",
//"SkipAndroidBridgeTestsSupport",
.product(name: "SkipBridgeKt", package: "skip-bridge"),
.product(name: "SkipTest", package: "skip"),
], plugins: [.plugin(name: "skipstone", package: "skip")]),
Expand Down
16 changes: 16 additions & 0 deletions Sources/SkipAndroidBridge/AndroidBridgeBootstrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import SkipAndroidSDKBridge
#elseif canImport(OSLog)
@_exported import OSLog
#endif
#if canImport(AndroidLooper)
@_exported import AndroidLooper
#endif
#if canImport(AndroidChoreographer)
@_exported import AndroidChoreographer
#endif

import Dispatch

fileprivate let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "AndroidBridgeToKotlin")
#endif
Expand All @@ -39,6 +47,14 @@ public class AndroidBridgeBootstrap {
if androidBridgeInit == true { return }
defer { androidBridgeInit = true }

precondition(Thread.isMainThread, "initAndroidBridge must be called from main thread")

#if os(Android)
// set up the main thread looper; note that we do not need to do this for Robolectric, since it uses the standard CFRunLoop
AndroidLooper.setupMainLooper()
AndroidChoreographer.setupMainChoreographer()
#endif

let start = Date.now
logger.log("initAndroidBridge started")
guard let context = AndroidContext.shared as AndroidContext? else {
Expand Down
15 changes: 15 additions & 0 deletions Sources/SkipAndroidBridge/AndroidBridgeMainActor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Skip
//
// This is free software: you can redistribute and/or modify it
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

#if !SKIP
#if os(Android)
import AndroidLooper

// this mechanism overrides the MainActor with an AndroidMainActor that uses the Looper API to enqueue events
public typealias MainActor = AndroidMainActor

#endif
#endif
4 changes: 4 additions & 0 deletions Sources/SkipAndroidBridgeTestsSupport/Skip/skip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configuration file for https://skip.tools project
skip:
mode: 'native'
bridging: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 Skip
//
// This is free software: you can redistribute and/or modify it
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

import Foundation
import SkipAndroidBridge

@MainActor public class MainActorSample {
public init() {
}

public func callMainActorFunction() -> String {
assert(Thread.isMainThread)
return "ABC"
}
}
10 changes: 8 additions & 2 deletions Tests/SkipAndroidBridgeTests/AndroidBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import OSLog
import Foundation
import SkipBridgeKt
@testable import SkipAndroidBridge
//import SkipAndroidBridgeTestsSupport

let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "Tests")

Expand All @@ -17,6 +18,7 @@ final class AndroidBridgeTests: XCTestCase {
override func setUp() {
#if SKIP
loadPeerLibrary(packageName: "skip-android-bridge", moduleName: "SkipAndroidBridge")
//try! AndroidBridgeBootstrap.initAndroidBridge()
#endif
}

Expand Down Expand Up @@ -56,7 +58,11 @@ final class AndroidBridgeTests: XCTestCase {
// make sure we can read and write to the filesDir
try "ABC".write(to: filesDir.appendingPathComponent("test.txt"), atomically: true, encoding: .utf8)
try "XYZ".write(to: cacheDir.appendingPathComponent("test.txt"), atomically: true, encoding: .utf8)

try AndroidBridgeBootstrap.initAndroidBridge()
}

// func testMainActor() async {
// let mainActorSample = await MainActorSample()
// let result = await mainActorSample.callMainActorFunction()
// XCTAssertEqual("ABC", result)
// }
}

0 comments on commit 2c96a47

Please sign in to comment.