DeviceListView.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import SwiftUI
  2. /// Component embedded in ProfileView displaying user's bound BLE recording devices and empty state.
  3. struct DeviceListView: View {
  4. @ObservedObject var bleManager: BLEManager = .shared
  5. @ObservedObject var authManager: AuthManager = .shared
  6. var onAddDeviceClicked: () -> Void
  7. @State private var deviceToUnbind: BoundDevice?
  8. @State private var deviceToRename: BoundDevice?
  9. @State private var proposedDeviceName = ""
  10. private var userBoundDevices: [BoundDevice] {
  11. guard let userId = authManager.currentUser?.id else { return [] }
  12. return bleManager.devices(forUserId: userId)
  13. }
  14. var body: some View {
  15. VStack(alignment: .leading, spacing: 14) {
  16. // Header Row
  17. HStack {
  18. HStack(spacing: 8) {
  19. Image(systemName: "mic.badge.plus")
  20. .font(.system(size: 14, weight: .light))
  21. .foregroundStyle(Color.secondary)
  22. Text("我的录音设备")
  23. .font(.system(size: 15, weight: .semibold))
  24. .foregroundStyle(Color.primary)
  25. }
  26. Spacer()
  27. Button {
  28. onAddDeviceClicked()
  29. } label: {
  30. HStack(spacing: 4) {
  31. Image(systemName: "plus")
  32. .font(.system(size: 11, weight: .bold))
  33. Text("添加设备")
  34. .font(.system(size: 12, weight: .medium))
  35. }
  36. .foregroundStyle(Color.primary)
  37. .padding(.horizontal, 10)
  38. .padding(.vertical, 5)
  39. .background(Color.primary.opacity(0.08))
  40. .cornerRadius(6)
  41. }
  42. }
  43. // Content State
  44. if userBoundDevices.isEmpty {
  45. emptyDeviceCard
  46. } else {
  47. VStack(spacing: 10) {
  48. ForEach(userBoundDevices) { device in
  49. boundDeviceCard(device)
  50. }
  51. }
  52. }
  53. }
  54. .confirmationDialog(
  55. "确定要解绑设备「\(deviceToUnbind?.name ?? "")」吗?",
  56. isPresented: Binding(
  57. get: { deviceToUnbind != nil },
  58. set: { if !$0 { deviceToUnbind = nil } }
  59. ),
  60. titleVisibility: .visible
  61. ) {
  62. Button("解绑设备", role: .destructive) {
  63. if let dev = deviceToUnbind {
  64. bleManager.unbindDevice(id: dev.id)
  65. }
  66. }
  67. Button("取消", role: .cancel) { }
  68. } message: {
  69. Text("解绑后该设备将从你的账号名下移除。")
  70. }
  71. .alert(
  72. "给微光取个名字",
  73. isPresented: Binding(
  74. get: { deviceToRename != nil },
  75. set: { if !$0 { deviceToRename = nil } }
  76. )
  77. ) {
  78. TextField("设备名称", text: $proposedDeviceName)
  79. Button("保存") {
  80. if let device = deviceToRename {
  81. bleManager.renameDevice(id: device.id, to: proposedDeviceName)
  82. }
  83. deviceToRename = nil
  84. }
  85. Button("取消", role: .cancel) {
  86. deviceToRename = nil
  87. }
  88. } message: {
  89. Text("这个名称只保存在你的 App 中,不会修改设备的蓝牙广播名。")
  90. }
  91. }
  92. // MARK: - Empty Device Card
  93. private var emptyDeviceCard: some View {
  94. VStack(spacing: 14) {
  95. ZStack {
  96. Circle()
  97. .fill(Color.primary.opacity(0.04))
  98. .frame(width: 56, height: 56)
  99. Image(systemName: "mic.slash")
  100. .font(.system(size: 24, weight: .light))
  101. .foregroundStyle(Color.secondary.opacity(0.6))
  102. }
  103. VStack(spacing: 4) {
  104. Text("暂未绑定录音设备")
  105. .font(.system(size: 14, weight: .medium))
  106. .foregroundStyle(Color.primary)
  107. Text("通过低功耗蓝牙绑定设备后,可同步硬件录音及状态管理")
  108. .font(.system(size: 11))
  109. .foregroundStyle(Color.secondary)
  110. .multilineTextAlignment(.center)
  111. }
  112. Button {
  113. onAddDeviceClicked()
  114. } label: {
  115. HStack(spacing: 6) {
  116. Image(systemName: "wave.3.right")
  117. .font(.system(size: 12))
  118. Text("搜索并绑定蓝牙设备")
  119. .font(.system(size: 13, weight: .medium))
  120. }
  121. .foregroundStyle(Color.spaceBlack)
  122. .padding(.horizontal, 16)
  123. .padding(.vertical, 9)
  124. .background(Color.primary)
  125. .cornerRadius(6)
  126. }
  127. .padding(.top, 4)
  128. }
  129. .frame(maxWidth: .infinity)
  130. .padding(.vertical, 24)
  131. .padding(.horizontal, 16)
  132. .background(Color.cardBackground.opacity(0.2))
  133. .businessBorder(cornerRadius: 10)
  134. }
  135. // MARK: - Bound Device Card
  136. private func boundDeviceCard(_ device: BoundDevice) -> some View {
  137. HStack(spacing: 14) {
  138. // Icon
  139. ZStack {
  140. RoundedRectangle(cornerRadius: 8)
  141. .fill(Color.primary.opacity(0.06))
  142. .frame(width: 44, height: 44)
  143. Image(systemName: "mic.fill")
  144. .font(.system(size: 18))
  145. .foregroundStyle(Color.primary)
  146. }
  147. // Device Details
  148. VStack(alignment: .leading, spacing: 4) {
  149. HStack(spacing: 8) {
  150. Text(device.name)
  151. .font(.system(size: 14, weight: .semibold))
  152. .foregroundStyle(Color.primary)
  153. // Connection Badge
  154. HStack(spacing: 4) {
  155. Circle()
  156. .fill(device.isConnected ? Color.green : Color.gray)
  157. .frame(width: 6, height: 6)
  158. Text(device.isConnected ? "已连接" : "未连接")
  159. .font(.system(size: 10, weight: .medium))
  160. .foregroundStyle(device.isConnected ? Color.green : Color.secondary)
  161. }
  162. }
  163. HStack(spacing: 12) {
  164. if let battery = device.batteryLevel {
  165. HStack(spacing: 3) {
  166. Image(systemName: "battery.75")
  167. .font(.system(size: 10))
  168. Text("\(battery)%")
  169. .font(.system(size: 11, design: .monospaced))
  170. }
  171. .foregroundStyle(Color.secondary)
  172. }
  173. if let fw = device.firmwareVersion {
  174. Text("固件: \(fw)")
  175. .font(.system(size: 11, design: .monospaced))
  176. .foregroundStyle(Color.secondary)
  177. }
  178. if let freeStorage = device.freeStorageMB {
  179. Text("剩余: \(freeStorage) MB")
  180. .font(.system(size: 11, design: .monospaced))
  181. .foregroundStyle(Color.secondary)
  182. }
  183. }
  184. }
  185. Spacer()
  186. VStack(spacing: 8) {
  187. Button {
  188. proposedDeviceName = device.name
  189. deviceToRename = device
  190. } label: {
  191. Text("重命名")
  192. .font(.system(size: 12, weight: .regular))
  193. .foregroundStyle(Color.primary)
  194. }
  195. Button {
  196. deviceToUnbind = device
  197. } label: {
  198. Text("解绑")
  199. .font(.system(size: 12, weight: .regular))
  200. .foregroundStyle(Color.recordingRed)
  201. }
  202. }
  203. .padding(.horizontal, 10)
  204. .padding(.vertical, 7)
  205. .overlay(
  206. RoundedRectangle(cornerRadius: 6)
  207. .stroke(Color.primary.opacity(0.2), lineWidth: 1)
  208. )
  209. }
  210. .padding(14)
  211. .background(Color.cardBackground.opacity(0.35))
  212. .businessBorder(cornerRadius: 10)
  213. }
  214. }
  215. #Preview {
  216. DeviceListView(onAddDeviceClicked: {})
  217. }