From 87b99f65248e61535e5ea045c41eaa5dd1c56342 Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Wed, 30 Oct 2024 16:40:04 -0400 Subject: [PATCH] Add AndroidContext --- Package.swift | 14 +- Sources/AndroidBridge/AndroidBridge.swift | 13 + .../Kotlin/AndroidKotlinBridge.swift | 26 ++ .../Skip/skip.yml | 0 .../Kotlin/AndroidKotlinBridge.swift | 14 - .../Resources/Localizable.xcstrings | 342 ------------------ .../SkipAndroidBridge/SkipAndroidBridge.swift | 6 - .../AndroidBridgeTests.swift | 17 + .../Skip/skip.yml | 0 .../XCSkipTests.swift | 0 .../Resources/TestData.json | 3 - .../SkipAndroidBridgeTests.swift | 27 -- 12 files changed, 63 insertions(+), 399 deletions(-) create mode 100644 Sources/AndroidBridge/AndroidBridge.swift create mode 100644 Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift rename Sources/{SkipAndroidBridge => AndroidBridge}/Skip/skip.yml (100%) delete mode 100644 Sources/SkipAndroidBridge/Kotlin/AndroidKotlinBridge.swift delete mode 100644 Sources/SkipAndroidBridge/Resources/Localizable.xcstrings delete mode 100644 Sources/SkipAndroidBridge/SkipAndroidBridge.swift create mode 100644 Tests/AndroidBridgeTests/AndroidBridgeTests.swift rename Tests/{SkipAndroidBridgeTests => AndroidBridgeTests}/Skip/skip.yml (100%) rename Tests/{SkipAndroidBridgeTests => AndroidBridgeTests}/XCSkipTests.swift (100%) delete mode 100644 Tests/SkipAndroidBridgeTests/Resources/TestData.json delete mode 100644 Tests/SkipAndroidBridgeTests/SkipAndroidBridgeTests.swift diff --git a/Package.swift b/Package.swift index 19cbf2d..2cb9174 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.10 +// swift-tools-version: 5.9 import PackageDescription let package = Package( @@ -6,7 +6,7 @@ let package = Package( defaultLocalization: "en", platforms: [.iOS(.v16), .macOS(.v13), .tvOS(.v16), .watchOS(.v9), .macCatalyst(.v16)], products: [ - .library(name: "SkipAndroidBridge", type: .dynamic, targets: ["SkipAndroidBridge"]), + .library(name: "AndroidBridge", type: .dynamic, targets: ["AndroidBridge"]), ], dependencies: [ .package(url: "https://source.skip.tools/skip.git", from: "1.1.16"), @@ -15,14 +15,14 @@ let package = Package( .package(url: "https://source.skip.tools/swift-android-native.git", branch: "main") ], targets: [ - .target(name: "SkipAndroidBridge", dependencies: [ + .target(name: "AndroidBridge", dependencies: [ .product(name: "SkipBridge", package: "skip-bridge"), .product(name: "SkipFoundation", package: "skip-foundation"), .product(name: "AndroidNative", package: "swift-android-native"), - ], resources: [.process("Resources")], plugins: [.plugin(name: "skipstone", package: "skip")]), - .testTarget(name: "SkipAndroidBridgeTests", dependencies: [ - "SkipAndroidBridge", + ], plugins: [.plugin(name: "skipstone", package: "skip")]), + .testTarget(name: "AndroidBridgeTests", dependencies: [ + "AndroidBridge", .product(name: "SkipTest", package: "skip"), - ], resources: [.process("Resources")], plugins: [.plugin(name: "skipstone", package: "skip")]), + ], plugins: [.plugin(name: "skipstone", package: "skip")]), ] ) diff --git a/Sources/AndroidBridge/AndroidBridge.swift b/Sources/AndroidBridge/AndroidBridge.swift new file mode 100644 index 0000000..07317f8 --- /dev/null +++ b/Sources/AndroidBridge/AndroidBridge.swift @@ -0,0 +1,13 @@ +// 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 +#if canImport(AndroidNative) +import AndroidNative +#else +import OSLog +#endif + +fileprivate let logger: Logger = Logger(subsystem: "AndroidBridge", category: "AndroidSwiftBridge") + diff --git a/Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift b/Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift new file mode 100644 index 0000000..648721b --- /dev/null +++ b/Sources/AndroidBridge/Kotlin/AndroidKotlinBridge.swift @@ -0,0 +1,26 @@ +import Foundation +import OSLog + +fileprivate let logger: Logger = Logger(subsystem: "AndroidBridge", category: "AndroidKotlinBridge") + +// SKIP @BridgeToSwift +func getJavaSystemProperty(_ name: String) -> String? { + #if SKIP + return java.lang.System.getProperty(name) + #else + return nil + #endif +} + +// SKIP @BridgeToSwift +public class AndroidContext { + #if SKIP + public static let shared: AndroidContext = AndroidContext(context: ProcessInfo.processInfo.androidContext) + + private let context: android.content.Context + + private init(context: android.content.Context) { + self.context = context + } + #endif +} diff --git a/Sources/SkipAndroidBridge/Skip/skip.yml b/Sources/AndroidBridge/Skip/skip.yml similarity index 100% rename from Sources/SkipAndroidBridge/Skip/skip.yml rename to Sources/AndroidBridge/Skip/skip.yml diff --git a/Sources/SkipAndroidBridge/Kotlin/AndroidKotlinBridge.swift b/Sources/SkipAndroidBridge/Kotlin/AndroidKotlinBridge.swift deleted file mode 100644 index b6c58b6..0000000 --- a/Sources/SkipAndroidBridge/Kotlin/AndroidKotlinBridge.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Foundation -import OSLog - -fileprivate let logger: Logger = Logger(subsystem: "AppDroid", category: "AppDroidKotlin") - -// SKIP @BridgeToSwift -func getJavaSystemProperty(_ name: String) -> String? { - #if SKIP - return java.lang.System.getProperty(name) - #else - return nil - #endif -} - diff --git a/Sources/SkipAndroidBridge/Resources/Localizable.xcstrings b/Sources/SkipAndroidBridge/Resources/Localizable.xcstrings deleted file mode 100644 index e9c0b9a..0000000 --- a/Sources/SkipAndroidBridge/Resources/Localizable.xcstrings +++ /dev/null @@ -1,342 +0,0 @@ -{ - "sourceLanguage" : "en", - "strings" : { - "Appearance" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Apariencia" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Apparence" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "外観" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "外观" - } - } - } - }, - "Dark" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Oscuro" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Sombre" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "ダーク" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "暗" - } - } - } - }, - "Hello [%@](https://skip.tools)!" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "¡Hola [%@](https://skip.tools)!" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Bonjour [%@](https://skip.tools)!" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "こんにちは、[%@](https://skip.tools)!" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "你好,[%@](https://skip.tools)!" - } - } - } - }, - "Home" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Inicio" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Accueil" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "ホーム" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "主页" - } - } - } - }, - "Item %lld" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Elemento %lld" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Élément %lld" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "アイテム %lld" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "项目 %lld" - } - } - } - }, - "Light" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Claro" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Clair" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "ライト" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "光" - } - } - } - }, - "Name" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Nombre" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Nom" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "名前" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "姓名" - } - } - } - }, - "Powered by %@" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Potenciado por %@" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Propulsé par %@" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "%@動力" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "由%@提供动力" - } - } - } - }, - "Screen %lld" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Pantalla %lld" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Écran %lld" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "画面 %lld" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "屏幕 %lld" - } - } - } - }, - "Settings" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Configuración" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Paramètres" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "設定" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "设置" - } - } - } - }, - "System" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Sistema" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Système" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "システム" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "系统" - } - } - } - }, - "Welcome" : { - "localizations" : { - "es" : { - "stringUnit" : { - "state" : "translated", - "value" : "Bienvenido" - } - }, - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Bienvenue" - } - }, - "ja" : { - "stringUnit" : { - "state" : "translated", - "value" : "ようこそ" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "欢迎" - } - } - } - } - }, - "version" : "1.0" -} \ No newline at end of file diff --git a/Sources/SkipAndroidBridge/SkipAndroidBridge.swift b/Sources/SkipAndroidBridge/SkipAndroidBridge.swift deleted file mode 100644 index 89efa6d..0000000 --- a/Sources/SkipAndroidBridge/SkipAndroidBridge.swift +++ /dev/null @@ -1,6 +0,0 @@ -// 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 - -public class SkipAndroidBridgeModule { -} diff --git a/Tests/AndroidBridgeTests/AndroidBridgeTests.swift b/Tests/AndroidBridgeTests/AndroidBridgeTests.swift new file mode 100644 index 0000000..993916b --- /dev/null +++ b/Tests/AndroidBridgeTests/AndroidBridgeTests.swift @@ -0,0 +1,17 @@ +// 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 XCTest +import OSLog +import Foundation +@testable import AndroidBridge + +let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "Tests") + +@available(macOS 13, *) +final class AndroidBridgeTests: XCTestCase { + func testAndroidBridge() throws { + logger.log("running testSkipAndroidBridge") + } +} diff --git a/Tests/SkipAndroidBridgeTests/Skip/skip.yml b/Tests/AndroidBridgeTests/Skip/skip.yml similarity index 100% rename from Tests/SkipAndroidBridgeTests/Skip/skip.yml rename to Tests/AndroidBridgeTests/Skip/skip.yml diff --git a/Tests/SkipAndroidBridgeTests/XCSkipTests.swift b/Tests/AndroidBridgeTests/XCSkipTests.swift similarity index 100% rename from Tests/SkipAndroidBridgeTests/XCSkipTests.swift rename to Tests/AndroidBridgeTests/XCSkipTests.swift diff --git a/Tests/SkipAndroidBridgeTests/Resources/TestData.json b/Tests/SkipAndroidBridgeTests/Resources/TestData.json deleted file mode 100644 index 3daf207..0000000 --- a/Tests/SkipAndroidBridgeTests/Resources/TestData.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "testModuleName": "SkipAndroidBridge" -} \ No newline at end of file diff --git a/Tests/SkipAndroidBridgeTests/SkipAndroidBridgeTests.swift b/Tests/SkipAndroidBridgeTests/SkipAndroidBridgeTests.swift deleted file mode 100644 index 200c526..0000000 --- a/Tests/SkipAndroidBridgeTests/SkipAndroidBridgeTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -// 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 XCTest -import OSLog -import Foundation -@testable import SkipAndroidBridge - -let logger: Logger = Logger(subsystem: "SkipAndroidBridge", category: "Tests") - -@available(macOS 13, *) -final class SkipAndroidBridgeTests: XCTestCase { - func testSkipAndroidBridge() throws { - logger.log("running testSkipAndroidBridge") - XCTAssertEqual(1 + 2, 3, "basic test") - - // load the TestData.json file from the Resources folder and decode it into a struct - let resourceURL: URL = try XCTUnwrap(Bundle.module.url(forResource: "TestData", withExtension: "json")) - let testData = try JSONDecoder().decode(TestData.self, from: Data(contentsOf: resourceURL)) - XCTAssertEqual("SkipAndroidBridge", testData.testModuleName) - } -} - -struct TestData : Codable, Hashable { - var testModuleName: String -} \ No newline at end of file