HomeView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import SwiftUI
  2. import SwiftData
  3. // MARK: - HomeView
  4. /// The main launch screen of CelestiaTrace.
  5. /// Features a minimalist, line-drawn aesthetic with a central record button
  6. /// and a subtle technical grid background.
  7. struct HomeView: View {
  8. @Environment(\.modelContext) private var modelContext
  9. @ObservedObject private var bleManager: BLEManager = .shared
  10. @ObservedObject private var authManager: AuthManager = .shared
  11. @State private var activeRecording: ActiveRecordingPresentation?
  12. @State private var completedRecordingSession: CelestiaSession?
  13. @State private var selectedSessionForDetail: CelestiaSession?
  14. @State private var detector = RecordingEnvironmentDetector()
  15. @State private var dismissedWarnings: Set<String> = []
  16. @State private var showRecordingSourcePicker = false
  17. @State private var pendingRecordingSource: RecordingSourceChoice?
  18. var body: some View {
  19. NavigationStack {
  20. ZStack {
  21. // Dynamic clean background
  22. Color.spaceBlack
  23. .ignoresSafeArea()
  24. // Subtle technical grid background
  25. businessGrid
  26. // Main content
  27. VStack(spacing: 0) {
  28. topBar
  29. .padding(.top, 16)
  30. if let firstWarning = detector.activeWarnings.first(where: { !dismissedWarnings.contains($0) }) {
  31. DiscreetWarningBanner(warning: firstWarning) {
  32. withAnimation {
  33. _ = dismissedWarnings.insert(firstWarning)
  34. }
  35. }
  36. .padding(.top, 12)
  37. }
  38. Spacer()
  39. recordSection
  40. Spacer()
  41. }
  42. .padding(.horizontal, 24)
  43. }
  44. .navigationBarHidden(true)
  45. .onAppear {
  46. detector.checkEnvironment()
  47. }
  48. .fullScreenCover(item: $activeRecording, onDismiss: {
  49. if let session = completedRecordingSession {
  50. selectedSessionForDetail = session
  51. completedRecordingSession = nil
  52. }
  53. }) { recording in
  54. ActiveRecordingView(
  55. session: recording.session,
  56. recordingSource: recording.source
  57. ) {
  58. completedRecordingSession = recording.session
  59. }
  60. }
  61. .sheet(isPresented: $showRecordingSourcePicker, onDismiss: {
  62. guard let source = pendingRecordingSource else { return }
  63. pendingRecordingSource = nil
  64. createSessionAndNavigate(source: source)
  65. }) {
  66. RecordingSourcePickerView(devices: connectedSparkDevices) { source in
  67. pendingRecordingSource = source
  68. showRecordingSourcePicker = false
  69. }
  70. }
  71. .navigationDestination(item: $selectedSessionForDetail) { session in
  72. SessionDetailView(session: session)
  73. }
  74. }
  75. }
  76. // MARK: - Top Bar
  77. private var topBar: some View {
  78. HStack(alignment: .top) {
  79. VStack(alignment: .leading, spacing: 4) {
  80. Text("星痕现场记录")
  81. .font(.system(size: 11, weight: .bold, design: .monospaced))
  82. .foregroundStyle(Color.primary.opacity(0.8))
  83. .tracking(3)
  84. Text(currentDateFormatted)
  85. .font(.system(size: 13, weight: .regular))
  86. .foregroundStyle(Color.secondary)
  87. }
  88. Spacer()
  89. }
  90. }
  91. // MARK: - Record Section
  92. private var recordSection: some View {
  93. VStack(spacing: 24) {
  94. PulsingRecordButton {
  95. beginNewRecording()
  96. }
  97. Text("开始新的现场记录")
  98. .font(.system(size: 16, weight: .medium))
  99. .foregroundStyle(Color.primary.opacity(0.88))
  100. .tracking(0.4)
  101. }
  102. }
  103. // MARK: - Technical Grid Background
  104. private var businessGrid: some View {
  105. Canvas { context, size in
  106. let step: CGFloat = 40
  107. let cols = Int(size.width / step)
  108. let rows = Int(size.height / step)
  109. for col in 0...cols {
  110. let x = CGFloat(col) * step
  111. var path = Path()
  112. path.move(to: CGPoint(x: x, y: 0))
  113. path.addLine(to: CGPoint(x: x, y: size.height))
  114. context.stroke(path, with: .color(Color.primary.opacity(0.015)), lineWidth: 0.5)
  115. }
  116. for row in 0...rows {
  117. let y = CGFloat(row) * step
  118. var path = Path()
  119. path.move(to: CGPoint(x: 0, y: y))
  120. path.addLine(to: CGPoint(x: size.width, y: y))
  121. context.stroke(path, with: .color(Color.primary.opacity(0.015)), lineWidth: 0.5)
  122. }
  123. }
  124. .ignoresSafeArea()
  125. .allowsHitTesting(false)
  126. }
  127. // MARK: - Actions
  128. private func beginNewRecording() {
  129. if connectedSparkDevices.isEmpty {
  130. createSessionAndNavigate(source: .iPhone)
  131. } else {
  132. showRecordingSourcePicker = true
  133. }
  134. }
  135. private func createSessionAndNavigate(source: RecordingSourceChoice) {
  136. let formatter = DateFormatter()
  137. formatter.dateFormat = "yyyyMMdd_HHmm"
  138. let title = "现场记录_\(formatter.string(from: Date()))"
  139. let session = CelestiaSession(title: title)
  140. modelContext.insert(session)
  141. try? modelContext.save()
  142. HapticManager.trigger(.recordStart)
  143. activeRecording = ActiveRecordingPresentation(session: session, source: source)
  144. }
  145. private var connectedSparkDevices: [BoundDevice] {
  146. guard let userID = authManager.currentUser?.id else { return [] }
  147. return bleManager.connectedDevices(forUserId: userID)
  148. .sorted { $0.boundAt < $1.boundAt }
  149. }
  150. // MARK: - Helpers
  151. private var currentDateFormatted: String {
  152. let formatter = DateFormatter()
  153. formatter.locale = Locale(identifier: "zh_Hans")
  154. formatter.dateFormat = "yyyy年M月d日 EEEE"
  155. return formatter.string(from: Date())
  156. }
  157. }
  158. private struct ActiveRecordingPresentation: Identifiable {
  159. let id = UUID()
  160. let session: CelestiaSession
  161. let source: RecordingSourceChoice
  162. }
  163. // MARK: - Recording source picker
  164. /// Shared by new recordings and continuation recordings. When at least one
  165. /// connected Spark exists, the first Spark is selected by default.
  166. struct RecordingSourcePickerView: View {
  167. @Environment(\.dismiss) private var dismiss
  168. let devices: [BoundDevice]
  169. let onConfirm: (RecordingSourceChoice) -> Void
  170. @State private var selection: RecordingSourceChoice
  171. init(devices: [BoundDevice], onConfirm: @escaping (RecordingSourceChoice) -> Void) {
  172. self.devices = devices
  173. self.onConfirm = onConfirm
  174. if let first = devices.first {
  175. _selection = State(initialValue: .spark(deviceID: first.id, displayName: first.name))
  176. } else {
  177. _selection = State(initialValue: .iPhone)
  178. }
  179. }
  180. var body: some View {
  181. NavigationStack {
  182. ZStack {
  183. Color.spaceBlack.ignoresSafeArea()
  184. VStack(alignment: .leading, spacing: 14) {
  185. Text("选择本次现场记录使用的录音设备")
  186. .font(.system(size: 13))
  187. .foregroundStyle(Color.secondary)
  188. ForEach(Array(devices.enumerated()), id: \.element.id) { index, device in
  189. sourceRow(
  190. source: .spark(deviceID: device.id, displayName: device.name),
  191. title: device.name,
  192. subtitle: sparkSubtitle(device),
  193. badge: index == 0 ? "默认" : nil
  194. )
  195. }
  196. sourceRow(
  197. source: .iPhone,
  198. title: "iPhone 麦克风",
  199. subtitle: "使用手机内置或当前系统音频输入",
  200. badge: devices.isEmpty ? "默认" : nil
  201. )
  202. Button {
  203. onConfirm(selection)
  204. } label: {
  205. Text("开始现场记录")
  206. .font(.system(size: 14, weight: .semibold))
  207. .foregroundStyle(Color.spaceBlack)
  208. .frame(maxWidth: .infinity)
  209. .padding(.vertical, 12)
  210. .background(Color.primary)
  211. .cornerRadius(8)
  212. }
  213. .padding(.top, 8)
  214. Spacer()
  215. }
  216. .padding(20)
  217. }
  218. .navigationTitle("录音设备")
  219. .navigationBarTitleDisplayMode(.inline)
  220. .toolbar {
  221. ToolbarItem(placement: .cancellationAction) {
  222. Button("取消") { dismiss() }
  223. .foregroundStyle(Color.secondary)
  224. }
  225. }
  226. }
  227. .presentationDetents([.medium, .large])
  228. .presentationBackground(Color.spaceBlack)
  229. }
  230. private func sourceRow(
  231. source: RecordingSourceChoice,
  232. title: String,
  233. subtitle: String,
  234. badge: String?
  235. ) -> some View {
  236. Button {
  237. selection = source
  238. } label: {
  239. HStack(spacing: 12) {
  240. Image(systemName: source.systemImage)
  241. .font(.system(size: 19, weight: .light))
  242. .foregroundStyle(Color.primary)
  243. .frame(width: 34)
  244. VStack(alignment: .leading, spacing: 4) {
  245. HStack(spacing: 7) {
  246. Text(title)
  247. .font(.system(size: 14, weight: .semibold))
  248. .foregroundStyle(Color.primary)
  249. if let badge {
  250. Text(badge)
  251. .font(.system(size: 9, weight: .bold))
  252. .foregroundStyle(Color.spaceBlack)
  253. .padding(.horizontal, 6)
  254. .padding(.vertical, 2)
  255. .background(Color.primary)
  256. .cornerRadius(4)
  257. }
  258. }
  259. Text(subtitle)
  260. .font(.system(size: 11))
  261. .foregroundStyle(Color.secondary)
  262. }
  263. Spacer()
  264. Image(systemName: selection == source ? "checkmark.circle.fill" : "circle")
  265. .font(.system(size: 18))
  266. .foregroundStyle(selection == source ? Color.primary : Color.secondary)
  267. }
  268. .padding(14)
  269. .background(Color.cardBackground.opacity(selection == source ? 0.55 : 0.25))
  270. .businessBorder(cornerRadius: 10)
  271. }
  272. .buttonStyle(.plain)
  273. }
  274. private func sparkSubtitle(_ device: BoundDevice) -> String {
  275. var parts = ["已连接"]
  276. if let battery = device.batteryLevel { parts.append("电量 \(battery)%") }
  277. if let free = device.freeStorageMB { parts.append("剩余 \(free) MB") }
  278. return parts.joined(separator: " · ")
  279. }
  280. }
  281. // MARK: - Preview
  282. #Preview {
  283. HomeView()
  284. .modelContainer(for: CelestiaSession.self, inMemory: true)
  285. }