TabCustomizationModal.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // LoopFollow
  2. // TabCustomizationModal.swift
  3. import SwiftUI
  4. // Represents either a TabItem or Settings in the drag-and-drop list
  5. enum TabCustomizationItem: Identifiable, Equatable, Hashable {
  6. case tabItem(TabItem)
  7. case settings
  8. var id: String {
  9. switch self {
  10. case let .tabItem(item): return item.rawValue
  11. case .settings: return "settings"
  12. }
  13. }
  14. var displayName: String {
  15. switch self {
  16. case let .tabItem(item): return item.displayName
  17. case .settings: return "Menu"
  18. }
  19. }
  20. var icon: String {
  21. switch self {
  22. case let .tabItem(item): return item.icon
  23. case .settings: return "line.3.horizontal"
  24. }
  25. }
  26. }
  27. struct TabCustomizationModal: View {
  28. @Binding var isPresented: Bool
  29. let onApply: () -> Void
  30. // All items including Settings - top 4 go to tab bar, rest to menu
  31. @State private var allItems: [TabCustomizationItem]
  32. private let originalItems: [TabCustomizationItem]
  33. init(isPresented: Binding<Bool>, onApply: @escaping () -> Void) {
  34. _isPresented = isPresented
  35. self.onApply = onApply
  36. let sortedTabItems = TabItem.movableItems.sorted { item1, item2 in
  37. let pos1 = Storage.shared.position(for: item1).normalized
  38. let pos2 = Storage.shared.position(for: item2).normalized
  39. let isInTabBar1 = TabPosition.customizablePositions.contains(pos1)
  40. let isInTabBar2 = TabPosition.customizablePositions.contains(pos2)
  41. // Tab bar positions (1-4) come before menu
  42. if isInTabBar1, isInTabBar2 {
  43. return (pos1.tabIndex ?? 99) < (pos2.tabIndex ?? 99)
  44. } else if isInTabBar1 {
  45. return true // pos1 is in tab bar, pos2 is in menu
  46. } else if isInTabBar2 {
  47. return false // pos2 is in tab bar, pos1 is in menu
  48. } else {
  49. // Both in menu - maintain original order from movableItems
  50. let idx1 = TabItem.movableItems.firstIndex(of: item1) ?? 0
  51. let idx2 = TabItem.movableItems.firstIndex(of: item2) ?? 0
  52. return idx1 < idx2
  53. }
  54. }
  55. // Convert to TabCustomizationItem array and add Settings at the start of menu items
  56. var items: [TabCustomizationItem] = sortedTabItems.map { .tabItem($0) }
  57. // Find where menu items start (after position 4)
  58. let menuStartIndex = items.firstIndex { item in
  59. if case let .tabItem(tabItem) = item {
  60. let pos = Storage.shared.position(for: tabItem).normalized
  61. return !TabPosition.customizablePositions.contains(pos)
  62. }
  63. return false
  64. } ?? items.count
  65. // Insert Settings at the start of menu items
  66. items.insert(.settings, at: menuStartIndex)
  67. _allItems = State(initialValue: items)
  68. originalItems = items
  69. }
  70. var body: some View {
  71. NavigationStack {
  72. List {
  73. // Instructions
  74. Section {
  75. VStack(alignment: .leading, spacing: 8) {
  76. Text("Drag to reorder")
  77. .font(.subheadline)
  78. .fontWeight(.medium)
  79. Text("The top 4 items appear in the tab bar. Items 5+ appear in the Menu.")
  80. .font(.caption)
  81. .foregroundColor(.secondary)
  82. }
  83. .padding(.vertical, 4)
  84. }
  85. // All items - Settings appears at position 5 as a divider
  86. Section {
  87. // Build display list: first 4 TabItems, then Settings, then remaining TabItems
  88. let tabItems = allItems.compactMap { item -> TabItem? in
  89. if case let .tabItem(tabItem) = item { return tabItem }
  90. return nil
  91. }
  92. // Display items in order: tab bar items, Settings, menu items
  93. ForEach(Array(allItems.enumerated()), id: \.element) { _, item in
  94. switch item {
  95. case let .tabItem(tabItem):
  96. // Determine if this TabItem is in tab bar or menu
  97. let tabItemIndex = tabItems.firstIndex(of: tabItem) ?? 0
  98. let isInTabBar = tabItemIndex < 4
  99. TabItemRow(
  100. item : tabItem,
  101. position: isInTabBar ? tabItemIndex + 1: nil,
  102. isInMenu: !isInTabBar
  103. )
  104. case .settings:
  105. SettingsRow()
  106. .moveDisabled(true)
  107. }
  108. }
  109. .onMove { source, destination in
  110. // Check if Settings (at index 4) is being moved - prevent it
  111. if source.contains(4) {
  112. return
  113. }
  114. // Get all TabItems (excluding Settings)
  115. var tabItemsOnly: [TabItem] = allItems.compactMap { item -> TabItem? in
  116. if case let .tabItem(tabItem) = item { return tabItem }
  117. return nil
  118. }
  119. // Adjust source indices: if any are after Settings (index 4), subtract 1
  120. var adjustedSource = source
  121. if source.contains(where: { $0 > 4 }) {
  122. adjustedSource = IndexSet(source.map { $0 > 4 ? $0 - 1 : $0 })
  123. }
  124. // Adjust destination: if it's after Settings (position 5), subtract 1
  125. let adjustedDestination = destination > 4 ? destination - 1 : destination
  126. // Move TabItems
  127. tabItemsOnly.move(fromOffsets: adjustedSource, toOffset: adjustedDestination)
  128. // Rebuild allItems with Settings at position 5
  129. var newItems: [TabCustomizationItem] = []
  130. for (index, tabItem) in tabItemsOnly.enumerated() {
  131. newItems.append(.tabItem(tabItem))
  132. // Insert Settings after the 4th TabItem (at position 5)
  133. if index == 3 {
  134. newItems.append(.settings)
  135. }
  136. }
  137. // If there are fewer than 4 TabItems, add Settings after the last one
  138. if tabItemsOnly.count < 4 {
  139. newItems.append(.settings)
  140. }
  141. allItems = newItems
  142. }
  143. } header: {
  144. Text("Tab Order")
  145. }
  146. }
  147. .listStyle(.insetGrouped)
  148. .environment(\.editMode, .constant(.active))
  149. .navigationTitle("Tab Settings")
  150. .navigationBarTitleDisplayMode(.inline)
  151. .toolbar {
  152. ToolbarItem(placement: .navigationBarLeading) {
  153. Button {
  154. allItems = originalItems
  155. isPresented = false
  156. } label: {
  157. HStack(spacing: 4) {
  158. Image(systemName: "chevron.left")
  159. Text("Back")
  160. }
  161. }
  162. }
  163. ToolbarItem(placement: .navigationBarTrailing) {
  164. Button {
  165. applyChangesSilently()
  166. onApply()
  167. isPresented = false
  168. } label: {
  169. Image(systemName: "checkmark")
  170. }
  171. .disabled(allItems == originalItems)
  172. .foregroundColor(.blue)
  173. }
  174. }
  175. }
  176. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  177. }
  178. // MARK: - Actions
  179. private func applyChangesSilently() {
  180. // Count only TabItems (not Settings) to determine tab bar positions
  181. // First 4 TabItems go to tab bar, rest go to menu
  182. var tabItemCount = 0
  183. for item in allItems {
  184. switch item {
  185. case let .tabItem(tabItem):
  186. let position: TabPosition
  187. if tabItemCount < 4 {
  188. switch tabItemCount {
  189. case 0: position = .position1
  190. case 1: position = .position2
  191. case 2: position = .position3
  192. case 3: position = .position4
  193. default: position = .menu
  194. }
  195. } else {
  196. position = .menu
  197. }
  198. Storage.shared.setPosition(position, for: tabItem)
  199. tabItemCount += 1
  200. case .settings:
  201. break
  202. }
  203. }
  204. // Don't call onApply() - let the tab position observers handle the rebuild naturally
  205. }
  206. }
  207. // MARK: - Row Views
  208. struct SettingsRow: View {
  209. var body: some View {
  210. HStack {
  211. Image(systemName: "line.3.horizontal")
  212. .frame(width: 28)
  213. .foregroundColor(.secondary)
  214. Text("Menu")
  215. .foregroundColor(.secondary)
  216. Spacer()
  217. Text("Tab 5")
  218. .font(.caption)
  219. .foregroundColor(.white)
  220. .padding(.horizontal, 8)
  221. .padding(.vertical, 4)
  222. .background(Color.gray)
  223. .cornerRadius(4)
  224. }
  225. .contentShape(Rectangle())
  226. }
  227. }
  228. struct TabItemRow: View {
  229. let item: TabItem
  230. let position: Int?
  231. let isInMenu: Bool
  232. var body: some View {
  233. HStack {
  234. Image(systemName: item.icon)
  235. .frame(width: 28)
  236. .foregroundColor(isInMenu ? .secondary : .accentColor)
  237. VStack(alignment: .leading, spacing: 2) {
  238. Text(item.displayName)
  239. .foregroundColor(isInMenu ? .secondary : .primary)
  240. }
  241. Spacer()
  242. if let pos = position {
  243. Text("Tab \(pos)")
  244. .font(.caption)
  245. .foregroundColor(.white)
  246. .padding(.horizontal, 8)
  247. .padding(.vertical, 4)
  248. .background(Color.accentColor)
  249. .cornerRadius(4)
  250. } else {
  251. Text("In Menu")
  252. .font(.caption)
  253. .foregroundColor(.orange)
  254. .padding(.horizontal, 8)
  255. .padding(.vertical, 4)
  256. .background(Color.orange.opacity(0.15))
  257. .cornerRadius(4)
  258. }
  259. }
  260. .contentShape(Rectangle())
  261. }
  262. }