Skip to content

Commit ec278d6

Browse files
committed
Fix an issue with the script sheet
1 parent 577e9ba commit ec278d6

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

StikDebug/JSSupport/RunJSView.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import idevice
1212
typealias RemoteServerHandle = OpaquePointer
1313
typealias ScreenshotClientHandle = OpaquePointer
1414

15-
final class RunJSViewModel: ObservableObject, @unchecked Sendable {
15+
final class RunJSViewModel: ObservableObject, Identifiable, @unchecked Sendable {
16+
let id = UUID()
1617
var context: JSContext?
1718
@Published var logs: [String] = []
1819
@Published var scriptName: String = "Script"
@@ -34,8 +35,14 @@ final class RunJSViewModel: ObservableObject, @unchecked Sendable {
3435
}
3536

3637
func runScript(data: Data, name: String? = nil) throws {
37-
let scriptContent = String(data: data, encoding: .utf8)
38-
scriptName = name ?? "Script"
38+
let displayName = name ?? "Script"
39+
DispatchQueue.main.async {
40+
self.scriptName = displayName
41+
}
42+
43+
guard let scriptContent = String(data: data, encoding: .utf8) else {
44+
throw CocoaError(.fileReadCorruptFile)
45+
}
3946

4047
let getPidFunction: @convention(block) () -> Int = {
4148
return self.pid
@@ -210,9 +217,18 @@ struct RunJSView: View {
210217
var body: some View {
211218
ScrollViewReader { proxy in
212219
List {
213-
ForEach(Array(model.logs.enumerated()), id: \.offset) { index, logStr in
214-
Text(logStr)
215-
.id(index)
220+
if model.logs.isEmpty {
221+
HStack(spacing: 12) {
222+
ProgressView()
223+
.controlSize(.small)
224+
Text("Starting script...")
225+
.foregroundStyle(.secondary)
226+
}
227+
} else {
228+
ForEach(Array(model.logs.enumerated()), id: \.offset) { index, logStr in
229+
Text(logStr)
230+
.id(index)
231+
}
216232
}
217233
}
218234
.navigationTitle("Running \(model.scriptName)")

StikDebug/Views/HomeView.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ struct HomeView: View {
1919
@State private var isShowingPairingFilePicker = false
2020
@State private var debugFeedback: DebugFeedback?
2121
@State private var pendingExternalURLAction: HomeExternalAction?
22-
@State private var isShowingScriptRunner = false
2322
@State private var scriptRunModel: RunJSViewModel?
2423

2524
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@@ -80,18 +79,15 @@ struct HomeView: View {
8079
allowedContentTypes: PairingFileStore.supportedContentTypes,
8180
onCompletion: importPairingFile
8281
)
83-
.sheet(isPresented: $isShowingScriptRunner) {
82+
.sheet(item: $scriptRunModel) { model in
8483
NavigationStack {
85-
if let scriptRunModel {
86-
RunJSView(model: scriptRunModel)
87-
.toolbar {
88-
ToolbarItem(placement: .topBarTrailing) {
89-
Button("Done") { isShowingScriptRunner = false }
90-
}
84+
RunJSView(model: model)
85+
.toolbar {
86+
ToolbarItem(placement: .topBarTrailing) {
87+
Button("Done") { scriptRunModel = nil }
9188
}
92-
.navigationTitle(selectedScript)
93-
.navigationBarTitleDisplayMode(.inline)
94-
}
89+
}
90+
.navigationBarTitleDisplayMode(.inline)
9591
}
9692
}
9793
}
@@ -118,11 +114,10 @@ struct HomeView: View {
118114
return
119115
}
120116

121-
scriptRunModel = model
122117
if let name = notification.userInfo?["scriptName"] as? String {
123118
selectedScript = name
124119
}
125-
isShowingScriptRunner = true
120+
scriptRunModel = model
126121
}
127122

128123
private func refreshMountStatusIfNeeded() {
@@ -271,7 +266,6 @@ struct HomeView: View {
271266

272267
DispatchQueue.main.async {
273268
scriptRunModel = model
274-
isShowingScriptRunner = true
275269
}
276270

277271
do {

0 commit comments

Comments
 (0)