TabCustomizationModal.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // All items including Settings - top 4 go to tab bar, rest to menu
  29. @State private var allItems: [TabCustomizationItem]
  30. private let originalItems: [TabCustomizationItem]
  31. init() {
  32. let sortedTabItems = TabItem.movableItems.sorted { item1, item2 in
  33. let pos1 = Storage.shared.position(for: item1).normalized
  34. let pos2 = Storage.shared.position(for: item2).normalized
  35. let isInTabBar1 = TabPosition.customizablePositions.contains(pos1)
  36. let isInTabBar2 = TabPosition.customizablePositions.contains(pos2)
  37. // Tab bar positions (1-4) come before menu
  38. if isInTabBar1, isInTabBar2 {
  39. return (pos1.tabIndex ?? 99) < (pos2.tabIndex ?? 99)
  40. } else if isInTabBar1 {
  41. return true // pos1 is in tab bar, pos2 is in menu
  42. } else if isInTabBar2 {
  43. return false // pos2 is in tab bar, pos1 is in menu
  44. } else {
  45. // Both in menu - maintain original order from movableItems
  46. let idx1 = TabItem.movableItems.firstIndex(of: item1) ?? 0
  47. let idx2 = TabItem.movableItems.firstIndex(of: item2) ?? 0
  48. return idx1 < idx2
  49. }
  50. }
  51. // Convert to TabCustomizationItem array and add Settings at the start of menu items
  52. var items: [TabCustomizationItem] = sortedTabItems.map { .tabItem($0) }
  53. // Find where menu items start (after position 4)
  54. let menuStartIndex = items.firstIndex { item in
  55. if case let .tabItem(tabItem) = item {
  56. let pos = Storage.shared.position(for: tabItem).normalized
  57. return !TabPosition.customizablePositions.contains(pos)
  58. }
  59. return false
  60. } ?? items.count
  61. // Insert Settings at the start of menu items
  62. items.insert(.settings, at: menuStartIndex)
  63. _allItems = State(initialValue: items)
  64. originalItems = items
  65. }
  66. var body: some View {
  67. List {
  68. // Instructions
  69. Section {
  70. VStack(alignment: .leading, spacing: 8) {
  71. Text("Drag to reorder")
  72. .font(.subheadline)
  73. .fontWeight(.medium)
  74. Text("The top 4 items appear in the tab bar. The Menu can always open every feature.")
  75. .font(.caption)
  76. .foregroundColor(.secondary)
  77. }
  78. .padding(.vertical, 4)
  79. }
  80. // All items - Settings appears at position 5 as a divider
  81. Section {
  82. // Build display list: first 4 TabItems, then Settings, then remaining TabItems
  83. let tabItems = allItems.compactMap { item -> TabItem? in
  84. if case let .tabItem(tabItem) = item { return tabItem }
  85. return nil
  86. }
  87. // Display items in order: tab bar items, Settings, menu items
  88. ForEach(Array(allItems.enumerated()), id: \.element) { _, item in
  89. switch item {
  90. case let .tabItem(tabItem):
  91. // Determine if this TabItem is in tab bar or menu
  92. let tabItemIndex = tabItems.firstIndex(of: tabItem) ?? 0
  93. let isInTabBar = tabItemIndex < 4
  94. TabItemRow(
  95. item : tabItem,
  96. position: isInTabBar ? tabItemIndex + 1: nil,
  97. isInMenu: !isInTabBar
  98. )
  99. case .settings:
  100. SettingsRow()
  101. .moveDisabled(true)
  102. }
  103. }
  104. .onMove { source, destination in
  105. // Check if Settings (at index 4) is being moved - prevent it
  106. if source.contains(4) {
  107. return
  108. }
  109. // Get all TabItems (excluding Settings)
  110. var tabItemsOnly: [TabItem] = allItems.compactMap { item -> TabItem? in
  111. if case let .tabItem(tabItem) = item { return tabItem }
  112. return nil
  113. }
  114. // Adjust source indices: if any are after Settings (index 4), subtract 1
  115. var adjustedSource = source
  116. if source.contains(where: { $0 > 4 }) {
  117. adjustedSource = IndexSet(source.map { $0 > 4 ? $0 - 1 : $0 })
  118. }
  119. // Adjust destination: if it's after Settings (position 5), subtract 1
  120. let adjustedDestination = destination > 4 ? destination - 1 : destination
  121. // Move TabItems
  122. tabItemsOnly.move(fromOffsets: adjustedSource, toOffset: adjustedDestination)
  123. // Rebuild allItems with Settings at position 5
  124. var newItems: [TabCustomizationItem] = []
  125. for (index, tabItem) in tabItemsOnly.enumerated() {
  126. newItems.append(.tabItem(tabItem))
  127. // Insert Settings after the 4th TabItem (at position 5)
  128. if index == 3 {
  129. newItems.append(.settings)
  130. }
  131. }
  132. // If there are fewer than 4 TabItems, add Settings after the last one
  133. if tabItemsOnly.count < 4 {
  134. newItems.append(.settings)
  135. }
  136. allItems = newItems
  137. persistTabOrder(tabItemsOnly)
  138. }
  139. } header: {
  140. Text("Tab Order")
  141. }
  142. }
  143. .listStyle(.insetGrouped)
  144. .environment(\.editMode, .constant(.active))
  145. .navigationTitle("Tabs")
  146. .navigationBarTitleDisplayMode(.inline)
  147. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  148. }
  149. // MARK: - Actions
  150. private func persistTabOrder(_ tabItems: [TabItem]) {
  151. for (index, tabItem) in tabItems.enumerated() {
  152. let position: TabPosition
  153. switch index {
  154. case 0: position = .position1
  155. case 1: position = .position2
  156. case 2: position = .position3
  157. case 3: position = .position4
  158. default: position = .menu
  159. }
  160. Storage.shared.setPosition(position, for: tabItem)
  161. }
  162. }
  163. }
  164. // MARK: - Row Views
  165. struct SettingsRow: View {
  166. var body: some View {
  167. HStack {
  168. Image(systemName: "line.3.horizontal")
  169. .frame(width: 28)
  170. .foregroundColor(.secondary)
  171. Text("Menu")
  172. .foregroundColor(.secondary)
  173. Spacer()
  174. Text("Tab 5")
  175. .font(.caption)
  176. .foregroundColor(.white)
  177. .padding(.horizontal, 8)
  178. .padding(.vertical, 4)
  179. .background(Color.gray)
  180. .cornerRadius(4)
  181. }
  182. .contentShape(Rectangle())
  183. }
  184. }
  185. struct TabItemRow: View {
  186. let item: TabItem
  187. let position: Int?
  188. let isInMenu: Bool
  189. var body: some View {
  190. HStack {
  191. Image(systemName: item.icon)
  192. .frame(width: 28)
  193. .foregroundColor(isInMenu ? .secondary : .accentColor)
  194. VStack(alignment: .leading, spacing: 2) {
  195. Text(item.displayName)
  196. .foregroundColor(isInMenu ? .secondary : .primary)
  197. }
  198. Spacer()
  199. if let pos = position {
  200. Text("Tab \(pos)")
  201. .font(.caption)
  202. .foregroundColor(.white)
  203. .padding(.horizontal, 8)
  204. .padding(.vertical, 4)
  205. .background(Color.accentColor)
  206. .cornerRadius(4)
  207. } else {
  208. Text("In Menu")
  209. .font(.caption)
  210. .foregroundColor(.orange)
  211. .padding(.horizontal, 8)
  212. .padding(.vertical, 4)
  213. .background(Color.orange.opacity(0.15))
  214. .cornerRadius(4)
  215. }
  216. }
  217. .contentShape(Rectangle())
  218. }
  219. }