2024-10-31 22:35:22 +08:00
|
|
|
//
|
2024-11-05 20:30:18 +08:00
|
|
|
// Adobe Downloader
|
2024-10-31 22:35:22 +08:00
|
|
|
//
|
|
|
|
|
// Created by X1a0He on 2024/10/30.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct InstallProgressView: View {
|
2024-11-04 17:40:01 +08:00
|
|
|
@EnvironmentObject private var networkManager: NetworkManager
|
2024-10-31 22:35:22 +08:00
|
|
|
let productName: String
|
|
|
|
|
let progress: Double
|
|
|
|
|
let status: String
|
|
|
|
|
let onCancel: () -> Void
|
2024-11-04 00:29:08 +08:00
|
|
|
let onRetry: (() -> Void)?
|
2024-10-31 22:35:22 +08:00
|
|
|
|
|
|
|
|
private var isCompleted: Bool {
|
2024-11-07 16:14:42 +08:00
|
|
|
progress >= 1.0 || status == String(localized: "安装完成")
|
2024-10-31 22:35:22 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 17:28:23 +08:00
|
|
|
private var isFailed: Bool {
|
2024-11-07 16:14:42 +08:00
|
|
|
status.contains(String(localized: "失败"))
|
2024-11-01 17:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-31 22:35:22 +08:00
|
|
|
private var progressText: String {
|
|
|
|
|
if isCompleted {
|
2024-11-07 16:14:42 +08:00
|
|
|
return String(localized: "安装完成")
|
2024-10-31 22:35:22 +08:00
|
|
|
} else {
|
|
|
|
|
return "\(Int(progress * 100))%"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
private var statusIcon: String {
|
|
|
|
|
if isCompleted {
|
|
|
|
|
return "checkmark.circle.fill"
|
|
|
|
|
} else if isFailed {
|
|
|
|
|
return "xmark.circle.fill"
|
|
|
|
|
} else {
|
|
|
|
|
return "arrow.down.circle.fill"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var statusColor: Color {
|
|
|
|
|
if isCompleted {
|
|
|
|
|
return .green
|
|
|
|
|
} else if isFailed {
|
|
|
|
|
return .red
|
|
|
|
|
} else {
|
|
|
|
|
return .blue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var statusTitle: String {
|
|
|
|
|
if isCompleted {
|
2024-11-07 16:14:42 +08:00
|
|
|
return String(localized: "\(productName) 安装完成")
|
2024-11-05 09:22:40 +08:00
|
|
|
} else if isFailed {
|
2024-11-07 16:14:42 +08:00
|
|
|
return String(localized: "\(productName) 安装失败")
|
2024-11-05 09:22:40 +08:00
|
|
|
} else {
|
2024-11-07 16:14:42 +08:00
|
|
|
return String(localized: "正在安装 \(productName)")
|
2024-11-05 09:22:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 22:35:22 +08:00
|
|
|
var body: some View {
|
|
|
|
|
VStack(spacing: 16) {
|
|
|
|
|
HStack {
|
2024-11-05 09:22:40 +08:00
|
|
|
Image(systemName: statusIcon)
|
2024-10-31 22:35:22 +08:00
|
|
|
.font(.title2)
|
2024-11-05 09:22:40 +08:00
|
|
|
.foregroundColor(statusColor)
|
2024-10-31 22:35:22 +08:00
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
Text(statusTitle)
|
2024-10-31 22:35:22 +08:00
|
|
|
.font(.headline)
|
|
|
|
|
}
|
2024-11-04 17:40:01 +08:00
|
|
|
.padding(.horizontal, 20)
|
2024-10-31 22:35:22 +08:00
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
if !isFailed {
|
|
|
|
|
ProgressSection(progress: progress, progressText: progressText)
|
2024-10-31 22:35:22 +08:00
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
|
|
|
|
|
LogSection(logs: networkManager.installationLogs)
|
|
|
|
|
|
|
|
|
|
if isFailed {
|
2024-11-07 16:14:42 +08:00
|
|
|
ErrorSection(
|
|
|
|
|
status: status, isFailed: isFailed
|
|
|
|
|
)
|
2024-11-05 09:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ButtonSection(
|
|
|
|
|
isCompleted: isCompleted,
|
|
|
|
|
isFailed: isFailed,
|
|
|
|
|
onCancel: onCancel,
|
|
|
|
|
onRetry: onRetry
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
.padding()
|
2024-11-07 16:14:42 +08:00
|
|
|
.frame(minWidth: 500, minHeight: 400)
|
2024-11-05 09:22:40 +08:00
|
|
|
.background(Color(NSColor.windowBackgroundColor))
|
|
|
|
|
.cornerRadius(8)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct ProgressSection: View {
|
|
|
|
|
let progress: Double
|
|
|
|
|
let progressText: String
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
VStack(spacing: 4) {
|
|
|
|
|
ProgressView(value: progress)
|
|
|
|
|
.progressViewStyle(.linear)
|
|
|
|
|
.frame(maxWidth: .infinity)
|
2024-11-04 17:40:01 +08:00
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
Text(progressText)
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
|
}
|
|
|
|
|
.padding(.horizontal, 20)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct LogSection: View {
|
|
|
|
|
let logs: [String]
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
ScrollViewReader { proxy in
|
2024-11-06 15:18:57 +08:00
|
|
|
ScrollView(showsIndicators: true) {
|
|
|
|
|
LazyVStack(alignment: .leading, spacing: 2) {
|
|
|
|
|
ForEach(Array(logs.suffix(1000).enumerated()), id: \.offset) { index, log in
|
2024-11-05 09:22:40 +08:00
|
|
|
Text(log)
|
|
|
|
|
.font(.system(.caption, design: .monospaced))
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.id(index)
|
|
|
|
|
.padding(.horizontal, 8)
|
|
|
|
|
.padding(.vertical, 2)
|
2024-11-06 15:18:57 +08:00
|
|
|
.textSelection(.enabled)
|
2024-11-04 17:40:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
|
}
|
|
|
|
|
.frame(height: 150)
|
|
|
|
|
.background(Color(NSColor.textBackgroundColor))
|
|
|
|
|
.cornerRadius(4)
|
|
|
|
|
.overlay(
|
|
|
|
|
RoundedRectangle(cornerRadius: 4)
|
|
|
|
|
.stroke(Color.secondary.opacity(0.2), lineWidth: 1)
|
|
|
|
|
)
|
|
|
|
|
.padding(.horizontal, 20)
|
2024-11-06 21:57:17 +08:00
|
|
|
.onChange(of: logs) { newValue in
|
2024-11-06 15:18:57 +08:00
|
|
|
if !newValue.isEmpty {
|
|
|
|
|
withAnimation {
|
|
|
|
|
proxy.scrollTo(newValue.count - 1, anchor: .bottom)
|
|
|
|
|
}
|
2024-11-04 17:40:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-31 22:35:22 +08:00
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
private struct ErrorSection: View {
|
|
|
|
|
let status: String
|
2024-11-07 16:14:42 +08:00
|
|
|
let isFailed: Bool
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
|
|
|
Text("错误详情:")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.fontWeight(.medium)
|
|
|
|
|
Text(status)
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.textSelection(.enabled)
|
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
.padding(8)
|
|
|
|
|
.background(Color.secondary.opacity(0.1))
|
|
|
|
|
.cornerRadius(6)
|
|
|
|
|
if isFailed {
|
|
|
|
|
HStack {
|
|
|
|
|
Text("自行安装命令:")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.fontWeight(.medium)
|
|
|
|
|
CommandPopover()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
.padding(.horizontal, 20)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct CommandSection: View {
|
|
|
|
|
let command: String
|
|
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
var body: some View {
|
|
|
|
|
ScrollView {
|
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
2024-11-07 16:14:42 +08:00
|
|
|
Text("自行安装命令:")
|
2024-11-05 09:22:40 +08:00
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.fontWeight(.medium)
|
|
|
|
|
|
2024-11-07 16:14:42 +08:00
|
|
|
Text(command)
|
2024-11-05 09:22:40 +08:00
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.textSelection(.enabled)
|
2024-11-07 16:14:42 +08:00
|
|
|
.frame(maxWidth: .infinity,alignment: .leading)
|
|
|
|
|
.frame(minHeight: 200)
|
2024-11-05 09:22:40 +08:00
|
|
|
.padding(8)
|
|
|
|
|
.background(Color.secondary.opacity(0.1))
|
|
|
|
|
.cornerRadius(6)
|
2024-11-01 17:28:23 +08:00
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-01 17:28:23 +08:00
|
|
|
|
2024-11-05 09:22:40 +08:00
|
|
|
private struct ButtonSection: View {
|
|
|
|
|
let isCompleted: Bool
|
|
|
|
|
let isFailed: Bool
|
|
|
|
|
let onCancel: () -> Void
|
|
|
|
|
let onRetry: (() -> Void)?
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
HStack(spacing: 12) {
|
|
|
|
|
if isFailed {
|
|
|
|
|
if let onRetry = onRetry {
|
|
|
|
|
Button(action: onRetry) {
|
|
|
|
|
Label("重试", systemImage: "arrow.clockwise.circle.fill")
|
2024-11-01 17:28:23 +08:00
|
|
|
}
|
|
|
|
|
.buttonStyle(.borderedProminent)
|
2024-11-05 09:22:40 +08:00
|
|
|
.tint(.blue)
|
2024-10-31 22:35:22 +08:00
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
|
|
|
|
|
Button(action: onCancel) {
|
|
|
|
|
Label("关闭", systemImage: "xmark.circle.fill")
|
|
|
|
|
}
|
|
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
|
.tint(.red)
|
|
|
|
|
} else if isCompleted {
|
|
|
|
|
Button(action: onCancel) {
|
|
|
|
|
Label("关闭", systemImage: "xmark.circle.fill")
|
|
|
|
|
}
|
|
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
|
.tint(.green)
|
|
|
|
|
} else {
|
|
|
|
|
Button(action: onCancel) {
|
|
|
|
|
Label("取消", systemImage: "xmark.circle.fill")
|
|
|
|
|
}
|
|
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
|
.tint(.red)
|
2024-10-31 22:35:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 09:22:40 +08:00
|
|
|
.padding(.horizontal, 20)
|
2024-10-31 22:35:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 16:14:42 +08:00
|
|
|
private struct CommandPopover: View {
|
|
|
|
|
@EnvironmentObject private var networkManager: NetworkManager
|
|
|
|
|
@State private var showPopover = false
|
2024-11-07 21:31:29 +08:00
|
|
|
@State private var showCopiedAlert = false
|
2024-11-07 16:14:42 +08:00
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
Button(action: { showPopover.toggle() }) {
|
|
|
|
|
Image(systemName: "terminal.fill")
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
}
|
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
|
.popover(isPresented: $showPopover, arrowEdge: .bottom) {
|
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
|
|
|
Button("复制命令") {
|
2024-11-07 21:31:29 +08:00
|
|
|
let command = networkManager.installCommand
|
|
|
|
|
let pasteboard = NSPasteboard.general
|
|
|
|
|
pasteboard.clearContents()
|
|
|
|
|
pasteboard.setString(command, forType: .string)
|
|
|
|
|
showCopiedAlert = true
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
|
|
|
|
showCopiedAlert = false
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-07 16:14:42 +08:00
|
|
|
|
2024-11-07 21:31:29 +08:00
|
|
|
if showCopiedAlert {
|
|
|
|
|
Text("已复制")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.green)
|
2024-11-07 16:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-07 21:31:29 +08:00
|
|
|
let command = networkManager.installCommand
|
|
|
|
|
Text(command)
|
2024-11-07 16:14:42 +08:00
|
|
|
.font(.system(.caption, design: .monospaced))
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
.textSelection(.enabled)
|
|
|
|
|
.padding(8)
|
|
|
|
|
.background(Color.secondary.opacity(0.1))
|
|
|
|
|
.cornerRadius(6)
|
|
|
|
|
}
|
|
|
|
|
.padding()
|
|
|
|
|
.frame(width: 400)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 17:40:01 +08:00
|
|
|
#Preview("安装中带日志") {
|
|
|
|
|
let networkManager = NetworkManager()
|
|
|
|
|
return InstallProgressView(
|
|
|
|
|
productName: "Adobe Photoshop",
|
|
|
|
|
progress: 0.45,
|
|
|
|
|
status: "正在安装核心组件...",
|
|
|
|
|
onCancel: {},
|
|
|
|
|
onRetry: nil
|
|
|
|
|
)
|
|
|
|
|
.environmentObject(networkManager)
|
|
|
|
|
.onAppear {
|
|
|
|
|
let previewLogs = [
|
|
|
|
|
"正在准备安装...",
|
|
|
|
|
"Progress: 10%",
|
|
|
|
|
"Progress: 20%",
|
|
|
|
|
"Progress: 30%",
|
|
|
|
|
"Progress: 40%",
|
|
|
|
|
"Progress: 45%",
|
|
|
|
|
"正在安装核心组件...",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for (index, log) in previewLogs.enumerated() {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.5) {
|
|
|
|
|
networkManager.installationLogs.append(log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Preview("安装失败带日志") {
|
|
|
|
|
let networkManager = NetworkManager()
|
|
|
|
|
return InstallProgressView(
|
|
|
|
|
productName: "Adobe Photoshop",
|
|
|
|
|
progress: 0.0,
|
|
|
|
|
status: "安装失败: 权限被拒绝",
|
|
|
|
|
onCancel: {},
|
|
|
|
|
onRetry: {}
|
|
|
|
|
)
|
|
|
|
|
.environmentObject(networkManager)
|
|
|
|
|
.onAppear {
|
2024-11-07 16:14:42 +08:00
|
|
|
networkManager.installCommand = "sudo \"/Library/Application Support/Adobe/Adobe Desktop Common/HDBox/Setup\" --install=1 --driverXML=\"/Users/demo/Downloads/Adobe Photoshop/driver.xml\""
|
|
|
|
|
|
2024-11-04 17:40:01 +08:00
|
|
|
let previewLogs = [
|
|
|
|
|
"正在准备安装...",
|
|
|
|
|
"Progress: 10%",
|
|
|
|
|
"Progress: 20%",
|
|
|
|
|
"检查权限...",
|
|
|
|
|
"权限检查失败",
|
|
|
|
|
"安装失败: 权限被拒绝"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for (index, log) in previewLogs.enumerated() {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.5) {
|
|
|
|
|
networkManager.installationLogs.append(log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Preview("安装完成带日志") {
|
|
|
|
|
let networkManager = NetworkManager()
|
|
|
|
|
return InstallProgressView(
|
|
|
|
|
productName: "Adobe Photoshop",
|
|
|
|
|
progress: 1.0,
|
|
|
|
|
status: "安装完成",
|
|
|
|
|
onCancel: {},
|
|
|
|
|
onRetry: nil
|
|
|
|
|
)
|
|
|
|
|
.environmentObject(networkManager)
|
|
|
|
|
.onAppear {
|
|
|
|
|
let previewLogs = [
|
|
|
|
|
"正在准备安装...",
|
|
|
|
|
"Progress: 25%",
|
|
|
|
|
"Progress: 50%",
|
|
|
|
|
"Progress: 75%",
|
|
|
|
|
"Progress: 100%",
|
|
|
|
|
"正在完成安装...",
|
|
|
|
|
"安装完成"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for (index, log) in previewLogs.enumerated() {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.5) {
|
|
|
|
|
networkManager.installationLogs.append(log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Preview("在深色模式下带日志") {
|
|
|
|
|
let networkManager = NetworkManager()
|
|
|
|
|
return InstallProgressView(
|
|
|
|
|
productName: "Adobe Photoshop",
|
|
|
|
|
progress: 0.75,
|
|
|
|
|
status: "正在安装...",
|
|
|
|
|
onCancel: {},
|
|
|
|
|
onRetry: nil
|
|
|
|
|
)
|
|
|
|
|
.environmentObject(networkManager)
|
|
|
|
|
.preferredColorScheme(.dark)
|
|
|
|
|
.onAppear {
|
|
|
|
|
let previewLogs = [
|
|
|
|
|
"正在准备安装...",
|
|
|
|
|
"Progress: 25%",
|
|
|
|
|
"Progress: 50%",
|
|
|
|
|
"Progress: 75%",
|
|
|
|
|
"正在安装..."
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for (index, log) in previewLogs.enumerated() {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.5) {
|
|
|
|
|
networkManager.installationLogs.append(log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|