90 lines
3.3 KiB
Swift
90 lines
3.3 KiB
Swift
//
|
|
// AbortView.swift
|
|
// sdlan
|
|
//
|
|
// Created by 安礼成 on 2024/6/5.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
struct AbortView: View {
|
|
struct AlertShow: Identifiable {
|
|
enum ShowContent {
|
|
case error(String)
|
|
case upgrade(String, String)
|
|
}
|
|
|
|
var id: String
|
|
var content: ShowContent
|
|
}
|
|
|
|
@State private var alertShow: AlertShow?
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Image("logo")
|
|
|
|
Text("sdlan")
|
|
|
|
Text("Version1.1")
|
|
|
|
Button {
|
|
Task {
|
|
guard let response = try? await SDLAPI.checkVersion(clientId: "test", version: 1, channel: "macos") else {
|
|
DispatchQueue.main.async {
|
|
self.alertShow = AlertShow(id: "network_error", content: .error("Network Error"))
|
|
}
|
|
return
|
|
}
|
|
|
|
if let result = response.result {
|
|
if result.upgrade_type == 0 {
|
|
DispatchQueue.main.async {
|
|
self.alertShow = AlertShow(id: "upgrade_0", content: .upgrade(result.upgrade_prompt, ""))
|
|
}
|
|
} else if result.upgrade_type == 1 {
|
|
DispatchQueue.main.async {
|
|
self.alertShow = AlertShow(id: "upgrade_1", content: .upgrade(result.upgrade_prompt, result.upgrade_address))
|
|
}
|
|
} else if result.upgrade_type == 2 {
|
|
DispatchQueue.main.async {
|
|
self.alertShow = AlertShow(id: "upgrade_1", content: .upgrade(result.upgrade_prompt, result.upgrade_address))
|
|
}
|
|
}
|
|
} else if let error = response.error {
|
|
DispatchQueue.main.async {
|
|
self.alertShow = AlertShow(id: "response_error", content: .error(error.message))
|
|
}
|
|
}
|
|
}
|
|
|
|
} label: {
|
|
Text("版本检测")
|
|
.font(.system(size: 16, weight: .regular))
|
|
.foregroundColor(.white)
|
|
.cornerRadius(5.0)
|
|
}
|
|
.frame(width: 138, height: 33)
|
|
.buttonStyle(PlainButtonStyle())
|
|
.background(Color(red: 74 / 255, green: 207 / 255, blue: 154 / 255))
|
|
.cornerRadius(5.0)
|
|
|
|
}
|
|
.alert(item: $alertShow) { show in
|
|
switch show.content {
|
|
case .error(let errorMessage):
|
|
Alert(title: Text("错误提示"), message: Text(errorMessage))
|
|
case .upgrade(let prompt, let address):
|
|
Alert(title: Text("版本升级"), message: Text(prompt), primaryButton: .default(Text("升级版本"), action: {
|
|
if let url = URL(string: address) {
|
|
// schema: "macappstore://apps.apple.com/app/idYOUR_APP_ID"
|
|
NSWorkspace.shared.open(url)
|
|
}
|
|
}), secondaryButton: .cancel())
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|