Files
ucvl-zonghengjia-open-source/ios/App/ContentView.swift
T

462 lines
17 KiB
Swift

import SwiftUI
struct ContentView: View {
@EnvironmentObject private var state: AppState
var body: some View {
ZStack {
LandscapeArtwork()
.ignoresSafeArea()
if let profile = state.profile {
DashboardView(profile: profile)
} else {
LoginView()
}
}
.tint(UCVLColors.royal)
.foregroundColor(UCVLColors.navy)
.preferredColorScheme(.light)
.alert("提示", isPresented: Binding(
get: { !state.errorMessage.isEmpty },
set: { if !$0 { state.errorMessage = "" } }
)) {
Button("确定", role: .cancel) { state.errorMessage = "" }
} message: {
Text(state.errorMessage)
}
}
}
// Both placements draw the same original asset with a native aspect-fill crop.
// Artwork remains separate from the real controls and never changes VPN state.
private struct LandscapeArtwork: View {
var body: some View {
GeometryReader { geometry in
Image("MountainLandscape")
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
.background(UCVLColors.surface)
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
private struct ScenicPage<Content: View>: View {
let maxWidth: CGFloat
let content: Content
init(maxWidth: CGFloat, @ViewBuilder content: () -> Content) {
self.maxWidth = maxWidth
self.content = content()
}
var body: some View {
ScrollView {
content
.frame(maxWidth: maxWidth)
.padding(.horizontal, 18)
.padding(.vertical, 24)
.frame(maxWidth: .infinity)
}
}
}
private struct BrandHeader: View {
var compact = false
var body: some View {
VStack(spacing: compact ? 8 : 12) {
Image("UCVLLogo")
.resizable()
.scaledToFit()
.frame(width: compact ? 60 : 88, height: compact ? 60 : 88)
.accessibilityHidden(true)
Text("纵横家")
.font(compact ? .title2.bold() : .largeTitle.bold())
.foregroundStyle(UCVLColors.navy)
.fixedSize(horizontal: false, vertical: true)
Text(AppVersion.label)
.font(.caption)
.foregroundStyle(UCVLColors.muted)
.fixedSize(horizontal: false, vertical: true)
}
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
.padding(.horizontal, 16)
.padding(.vertical, compact ? 16 : 24)
.background {
LandscapeArtwork()
.overlay(Color.white.opacity(0.18))
.mask(LinearGradient(colors: [.black, .black, .clear], startPoint: .top, endPoint: .bottom))
}
}
}
private enum AppVersion {
static var label: String {
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
guard let version, !version.isEmpty else { return "版本信息不可用" }
guard let build, !build.isEmpty else { return "版本 \(version)" }
return "版本 \(version) (\(build))"
}
}
private struct LoginView: View {
@EnvironmentObject private var state: AppState
@State private var identifier = SecureStore.lastLogin
@State private var password = ""
@FocusState private var focusedField: Field?
private enum Field: Hashable { case identifier, password }
var body: some View {
ScenicPage(maxWidth: 540) {
VStack(spacing: 20) {
BrandHeader()
VStack(alignment: .leading, spacing: 18) {
Text("公司统一账号登录")
.font(.title2.bold())
.foregroundStyle(UCVLColors.navy)
TextField("用户名或邮箱", text: $identifier)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.keyboardType(.emailAddress)
.textContentType(.username)
.submitLabel(.next)
.focused($focusedField, equals: .identifier)
.ucvlField()
.onSubmit { focusedField = .password }
SecureField("密码", text: $password)
.textContentType(.password)
.submitLabel(.go)
.focused($focusedField, equals: .password)
.ucvlField()
.onSubmit { signIn() }
Button(action: signIn) {
HStack {
if state.busy { ProgressView().tint(.white) }
Text(state.busy ? "正在登录…" : "安全登录").fontWeight(.semibold)
}
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 14)
}
.buttonStyle(.borderedProminent)
.disabled(state.busy)
Text("账号停用后,设备授权与线路将自动失效。")
.font(.footnote)
.foregroundStyle(UCVLColors.muted)
.fixedSize(horizontal: false, vertical: true)
RegistrationPrompt()
}
.padding(20)
.scenicCard(cornerRadius: 24)
}
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("完成") { focusedField = nil }
}
}
}
private func signIn() {
focusedField = nil
Task {
await state.login(identifier: identifier, password: password)
if state.profile != nil { password = "" }
}
}
}
private struct RegistrationPrompt: View {
private var message: AttributedString {
var prompt = AttributedString("还没有账号?")
prompt.foregroundColor = UCVLColors.muted
var link = AttributedString("注册新用户")
link.link = URL(string: "https://vpn.example.invalid/register/")
link.foregroundColor = UCVLColors.royal
link.font = .footnote.weight(.semibold)
prompt.append(link)
return prompt
}
var body: some View {
// A single inline run shares a baseline and wraps naturally at large sizes.
Text(message)
.font(.footnote)
.lineSpacing(4)
.fixedSize(horizontal: false, vertical: true)
}
}
private struct DashboardHeader: View {
@Environment(\.horizontalSizeClass) private var sizeClass
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
let profile: ClientProfile
private var account: some View {
VStack(alignment: .leading, spacing: 6) {
Text("你好,\(profile.displayName.isEmpty ? profile.username : profile.displayName)")
.font(.headline)
Text(profile.email)
.font(.subheadline)
.foregroundStyle(UCVLColors.muted)
Text(profile.identityType == "employee" ? "内部员工" : "外部联系人")
.font(.caption)
.foregroundStyle(UCVLColors.muted)
}
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
}
var body: some View {
Group {
if sizeClass == .regular && !dynamicTypeSize.isAccessibilitySize {
HStack(spacing: 24) {
BrandHeader(compact: true)
account
}
} else {
VStack(spacing: 16) {
BrandHeader(compact: true)
account
}
}
}
.padding(18)
.scenicCard()
}
}
private struct DashboardView: View {
@EnvironmentObject private var state: AppState
@Environment(\.horizontalSizeClass) private var sizeClass
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
let profile: ClientProfile
var body: some View {
ScenicPage(maxWidth: 760) {
VStack(spacing: 18) {
DashboardHeader(profile: profile)
StatusCard()
UCVLCard(title: "工作节点与代理模式", systemImage: "point.3.connected.trianglepath.dotted") {
VStack(spacing: 14) {
AdaptiveDetailRow(title: "当前工作节点") {
Text(state.currentNode?.displayName ?? "由 vpn-ai 自动选择")
.foregroundStyle(state.currentNode == nil ? UCVLColors.muted : UCVLColors.royal)
.fixedSize(horizontal: false, vertical: true)
}
Divider()
AdaptiveDetailRow(title: "代理模式") {
Picker("代理模式", selection: $state.selectedRouteID) {
ForEach(profile.routeModes) { route in Text(route.name).tag(route.id) }
}
.pickerStyle(.menu)
.disabled(state.vpn.isConnected || state.busy)
}
Button {
Task { await state.toggleConnection() }
} label: {
HStack {
if state.busy { ProgressView().tint(.white) }
Image(systemName: state.vpn.isConnected ? "stop.fill" : "bolt.shield.fill")
Text(state.vpn.isConnected ? "断开连接" : "连接")
}
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
}
.buttonStyle(.borderedProminent)
.tint(state.vpn.isConnected ? UCVLColors.navy : UCVLColors.royal)
.disabled(state.busy || profile.nodes.isEmpty)
}
}
UsageCard(subscription: profile.subscription)
Group {
if sizeClass == .regular && !dynamicTypeSize.isAccessibilitySize {
HStack(spacing: 12) { accountActions }
} else {
VStack(spacing: 12) { accountActions }
}
}
.padding(16)
.scenicCard()
}
}
}
@ViewBuilder private var accountActions: some View {
Button { Task { await state.refresh() } } label: {
Label("刷新", systemImage: "arrow.clockwise")
.frame(maxWidth: .infinity)
.padding(.vertical, 6)
}
.buttonStyle(.bordered)
.disabled(state.busy)
Button(role: .destructive) { Task { await state.logout() } } label: {
Label("退出账号", systemImage: "rectangle.portrait.and.arrow.right")
.frame(maxWidth: .infinity)
.padding(.vertical, 6)
}
.buttonStyle(.bordered)
.disabled(state.busy)
}
}
private struct AdaptiveDetailRow<Content: View>: View {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
let title: String
let content: Content
init(title: String, @ViewBuilder content: () -> Content) {
self.title = title
self.content = content()
}
var body: some View {
Group {
if dynamicTypeSize.isAccessibilitySize {
VStack(alignment: .leading, spacing: 8) {
Text(title)
content
.frame(maxWidth: .infinity, alignment: .leading)
}
} else {
HStack(alignment: .firstTextBaseline, spacing: 12) {
Text(title)
.fixedSize(horizontal: true, vertical: false)
content
.multilineTextAlignment(.trailing)
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
private struct StatusCard: View {
@EnvironmentObject private var state: AppState
var body: some View {
UCVLCard(title: "连接状态", systemImage: "network.badge.shield.half.filled") {
HStack(alignment: .top, spacing: 14) {
Circle()
.fill(state.statusIsActive ? UCVLColors.success : UCVLColors.muted)
.frame(width: 13, height: 13)
.padding(.top, 6)
VStack(alignment: .leading, spacing: 4) {
Text(state.statusTitle).font(.title3.bold())
Text(state.message.isEmpty ? "点击连接,vpn-ai 将自动选择并验证工作节点" : state.message)
.font(.subheadline)
.foregroundStyle(UCVLColors.muted)
}
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
}
private struct UsageCard: View {
let subscription: SubscriptionProfile
private var fraction: Double {
guard subscription.trafficLimitBytes > 0 else { return 0 }
return min(1, max(0, Double(subscription.usedBytes) / Double(subscription.trafficLimitBytes)))
}
var body: some View {
UCVLCard(title: "本期流量", systemImage: "chart.bar.fill") {
VStack(alignment: .leading, spacing: 12) {
AdaptiveDetailRow(title: "已用流量") {
Text(format(subscription.usedBytes))
.fixedSize(horizontal: false, vertical: true)
}
AdaptiveDetailRow(title: "套餐流量") {
Text(subscription.trafficLimitBytes > 0 ? format(subscription.trafficLimitBytes) : "不限流量")
.foregroundStyle(UCVLColors.royal)
.fixedSize(horizontal: false, vertical: true)
}
ProgressView(value: fraction)
.tint(UCVLColors.royal)
Text("套餐:\(subscription.plan) · 设备上限 \(subscription.deviceLimit) 台\(expiryText)")
.font(.footnote)
.foregroundStyle(UCVLColors.muted)
.fixedSize(horizontal: false, vertical: true)
}
}
}
private var expiryText: String {
guard let expiresAt = subscription.expiresAt else { return " · 长期有效" }
return " · 到期 \(String(expiresAt.prefix(10)))"
}
private func format(_ value: Int64) -> String {
ByteCountFormatter.string(fromByteCount: value, countStyle: .file)
}
}
private struct UCVLCard<Content: View>: View {
let title: String
let systemImage: String
let content: Content
init(title: String, systemImage: String, @ViewBuilder content: () -> Content) {
self.title = title
self.systemImage = systemImage
self.content = content()
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Label(title, systemImage: systemImage)
.font(.headline)
.foregroundStyle(UCVLColors.navy)
.fixedSize(horizontal: false, vertical: true)
content
}
.padding(20)
.frame(maxWidth: .infinity, alignment: .leading)
.scenicCard()
}
}
private enum UCVLColors {
static let navy = Color(red: 10 / 255, green: 34 / 255, blue: 91 / 255)
static let royal = Color(red: 0, green: 80 / 255, blue: 221 / 255)
static let muted = Color(red: 52 / 255, green: 79 / 255, blue: 116 / 255)
static let surface = Color(red: 237 / 255, green: 248 / 255, blue: 1)
static let success = Color(red: 0, green: 113 / 255, blue: 110 / 255)
}
private extension View {
func scenicCard(cornerRadius: CGFloat = 22) -> some View {
self
.background(Color.white.opacity(0.50), in: RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
.overlay(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous).stroke(Color.white.opacity(0.55)))
.shadow(color: UCVLColors.navy.opacity(0.08), radius: 14, y: 7)
}
func ucvlField() -> some View {
self
.foregroundColor(UCVLColors.navy)
.padding(.horizontal, 14)
.padding(.vertical, 14)
.frame(minHeight: 52)
.background(Color.white.opacity(0.72), in: RoundedRectangle(cornerRadius: 13))
.overlay(RoundedRectangle(cornerRadius: 13).stroke(UCVLColors.navy.opacity(0.16)))
}
}