Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MeoAsstMac/Model/MAAViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import SwiftUI

@Published var copilot: CopilotConfiguration?
@Published var downloadCopilot: String?
@Published var downloadCopilotSet: String?
@Published var showImportCopilot = false
@Published var copilotDetailMode: CopilotDetailMode = .log

Expand Down
48 changes: 48 additions & 0 deletions MeoAsstMac/Navigation/CopilotContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct CopilotContent: View {
.onDrop(of: [.fileURL], isTargeted: .none, perform: addCopilots)
.onReceive(viewModel.$copilotDetailMode, perform: deselectCopilot)
.onReceive(viewModel.$downloadCopilot, perform: downloadCopilot)
.onReceive(viewModel.$downloadCopilotSet, perform: downloadCopilotSet)
.onReceive(viewModel.$videoRecoginition, perform: selectNewCopilot)
.fileImporter(
isPresented: $viewModel.showImportCopilot,
Expand Down Expand Up @@ -166,6 +167,45 @@ struct CopilotContent: View {
}
}

private func downloadCopilotSet(id: String?) {
guard let id, let copilotID = Int(id) else { return }

let url = URL(string: "https://prts.maa.plus/set/get?id=\(copilotID)")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
Task {
self.downloading = true
do {
let (data, _) = try await URLSession.shared.data(for: request)
let response = try JSONDecoder().decode(CopilotSetResult.self, from: data)

if response.data.copilot_ids.isEmpty {
print("该作业集不包含任何作业")
self.downloading = false
return
}

for id in response.data.copilot_ids {
let file = externalDirectory.appendingPathComponent("\(id)").appendingPathExtension("json")
let url = URL(string: "https://prts.maa.plus/copilot/get/\(id)")!
do {
let data = try await URLSession.shared.data(from: url).0
let response = try JSONDecoder().decode(CopilotResponse.self, from: data)
try response.data.content.write(toFile: file.path, atomically: true, encoding: .utf8)
copilots.insert(file)
} catch {
print("Failed to fetch copilot \(id):", error)
}
}

self.selection = copilots.urls.last
} catch {
print(error)
}
self.downloading = false
}
}

private func deleteCopilot(url: URL) {
guard canDelete(url) else { return }
copilots.remove(url)
Expand Down Expand Up @@ -290,6 +330,14 @@ private struct CopilotResponse: Codable {
}
}

private struct CopilotSetResult: Codable {
let data: CopilotSetDetail
}

private struct CopilotSetDetail: Codable {
let copilot_ids: [Int]
}

// MARK: - Convenience Methods

extension NSItemProvider {
Expand Down
6 changes: 5 additions & 1 deletion MeoAsstMac/Navigation/CopilotDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ struct CopilotDetail: View {
viewModel.downloadCopilot = prtsCode.parsedID
}
.disabled(prtsCode.parsedID == nil)


Button("下载作业集") {
viewModel.downloadCopilotSet = prtsCode.parsedID
}
.disabled(prtsCode.parsedID == nil)
Button("选择本地文件…") {
viewModel.showImportCopilot = true
}
Expand Down
22 changes: 19 additions & 3 deletions Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,22 @@
},
"下载作业" : {

},
"下载作业集" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Download Copilot Set"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "下载作业集"
}
}
}
},
"不使用无人机" : {

Expand Down Expand Up @@ -2643,9 +2659,6 @@
}
}
}
},
"繁中服(txwy)" : {

},
"组装批次数:" : {
"localizations" : {
Expand All @@ -2666,6 +2679,9 @@
}
}
}
},
"繁中服(txwy)" : {

},
"自动下载更新" : {
"localizations" : {
Expand Down