-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.swift
More file actions
94 lines (74 loc) · 2.53 KB
/
Copy pathMainActivity.swift
File metadata and controls
94 lines (74 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Activity.swift
// AndroidSwiftUI
//
// Created by Alsey Coleman Miller on 6/8/25.
//
import Foundation
import AndroidKit
import JavaLang
@JavaClass("com.pureswift.swiftandroid.MainActivity")
open class MainActivity: AndroidApp.Activity {
public internal(set) static var shared: MainActivity!
@JavaMethod
open func setRootView(_ view: AndroidView.View?)
}
@JavaImplementation("com.pureswift.swiftandroid.MainActivity")
extension MainActivity {
@JavaMethod
public func onCreateSwift(_ savedInstanceState: BaseBundle?) {
log("\(self).\(#function)")
MainActivity.shared = self
// Point @AppStorage at a file in the app's private storage before any
// view is built, so the first evaluation already reads saved values.
// The path comes through the existing Context binding — no new bridge.
if let directory = (self as AndroidContent.Context).getFilesDir()?.getAbsolutePath() {
AppStorageStore.backend = FileAppStorage(directory: directory)
} else {
log("MainActivity: no files directory; @AppStorage stays in memory")
}
// start app
AndroidSwiftUIMain()
runAsync()
}
@JavaMethod
public func onActivityResultSwift(_ requestCode: Int32, _ resultCode: Int32, _ data: AndroidContent.Intent?) {
log("\(self).\(#function) requestCode \(requestCode) resultCode \(resultCode)")
}
}
private extension MainActivity {
func runAsync() {
RunLoop.main.run(until: Date() + 0.1)
DispatchQueue.main.async {
Self.log("\(self).\(#function) Main Thread Async")
}
DispatchQueue.global(qos: .default).async {
Self.log("\(self).\(#function) Default Dispatch Queue Async")
}
Task {
Self.log("\(self).\(#function) Task Started")
await MainActor.run {
RunLoop.main.run(until: Date() + 0.1)
}
}
}
}
extension MainActivity {
static var logTag: String { "MainActivity" }
static let log = try! JavaClass<AndroidUtil.Log>()
static func log(_ string: String) {
_ = Self.log.d(Self.logTag, string)
}
static func logInfo(_ string: String) {
_ = Self.log.i(Self.logTag, string)
}
static func logError(_ string: String) {
_ = Self.log.e(Self.logTag, string)
}
func log(_ string: String) {
Self.log(string)
}
func logError(_ string: String) {
Self.logError(string)
}
}