ContentView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import SwiftUI
  2. enum MainTab: String, CaseIterable {
  3. case home = "Home", daily = "Daily Challenge", profile = "Me"
  4. var icon: String {
  5. switch self {
  6. case .home: "house.fill"
  7. case .daily: CompatibleSymbol.name("calendar.badge.checkmark", fallback: "calendar")
  8. case .profile: "person.crop.circle.fill"
  9. }
  10. }
  11. }
  12. struct ContentView: View {
  13. @EnvironmentObject private var language: LanguageStore
  14. @EnvironmentObject private var store: GameStore
  15. @EnvironmentObject private var platform: PlatformBootstrap
  16. @Environment(\.scenePhase) private var scenePhase
  17. @State private var tab = MainTab.home
  18. @State private var showDifficulty = false
  19. @State private var dailyResumeDate: Date?
  20. init() {
  21. #if DEBUG
  22. switch DevelopmentPreview.screen {
  23. case "daily": _tab = State(initialValue: .daily)
  24. case "daily-resume":
  25. _tab = State(initialValue: .daily)
  26. _dailyResumeDate = State(initialValue: .now)
  27. case "profile", "profile-bottom", "settings", "privacy": _tab = State(initialValue: .profile)
  28. case "difficulty": _showDifficulty = State(initialValue: true)
  29. default: break
  30. }
  31. #endif
  32. }
  33. var body: some View {
  34. ZStack {
  35. if let game = store.currentGame {
  36. GameView(gameID: game.id).accessibilityHidden(platform.activity != nil)
  37. } else {
  38. Group {
  39. switch tab {
  40. case .home: HomeView(openDaily: { tab = .daily }, newGame: { withAnimation { showDifficulty = true } })
  41. case .daily: DailyChallengeView { date in withAnimation { dailyResumeDate = date } }
  42. case .profile: ProfileView()
  43. }
  44. }
  45. .safeAreaInset(edge: .bottom, spacing: 0) { tabBar }
  46. .accessibilityHidden(showDifficulty || dailyResumeDate != nil || store.isGenerating || platform.activity != nil)
  47. }
  48. if showDifficulty {
  49. ActionPanel(title: "Select Difficulty", dismiss: { withAnimation { showDifficulty = false } }) {
  50. AnyView(VStack(spacing: 0) {
  51. ForEach(Difficulty.allCases) { difficulty in
  52. PanelRow(title: difficulty.title, showDivider: difficulty != .master) {
  53. withAnimation { showDifficulty = false }
  54. Task { await store.startGame(difficulty: difficulty) }
  55. }
  56. }
  57. })
  58. }.zIndex(2)
  59. }
  60. if let date = dailyResumeDate {
  61. ActionPanel(dismiss: { withAnimation { dailyResumeDate = nil } }) {
  62. AnyView(VStack(spacing: 0) {
  63. PanelRow(title: "Continue Game") {
  64. withAnimation { dailyResumeDate = nil }
  65. if let session = store.dailySession(for: date) { store.resume(session) }
  66. }
  67. PanelRow(title: "Restart") {
  68. withAnimation { dailyResumeDate = nil }
  69. Task { await store.startGame(difficulty: .hard, day: date) }
  70. }
  71. PanelRow(title: "Cancel", showDivider: false) { withAnimation { dailyResumeDate = nil } }
  72. })
  73. }.zIndex(2)
  74. }
  75. if store.isGenerating {
  76. Color.black.opacity(0.15).ignoresSafeArea()
  77. VStack(spacing: 18) {
  78. ProgressView().tint(Palette.blue).scaleEffect(1.3)
  79. Text(language.text("Preparing your puzzle…")).font(.system(size: 15)).foregroundStyle(Palette.ink)
  80. }.padding(30).background(.regularMaterial, in: RoundedRectangle(cornerRadius: 22)).zIndex(4)
  81. }
  82. if let notice = platform.activity {
  83. ActivityNoticeView(notice: notice, showsControls: false, didLoad: {
  84. platform.recordActivityOpened(notice.url)
  85. }) { platform.dismissActivity() }.id(notice.id).zIndex(10)
  86. }
  87. }
  88. .environment(\.isRemoteActivityVisible, platform.activity != nil)
  89. .tint(Palette.blue)
  90. #if DEBUG
  91. .modifier(DevelopmentPreviewViewport())
  92. #endif
  93. .task {
  94. #if DEBUG
  95. await DevelopmentPreview.prepare(store)
  96. if ["activity", "activity-bridge-probe"].contains(DevelopmentPreview.screen ?? "") { platform.presentPreviewActivity() }
  97. #endif
  98. }
  99. .onChange(of: scenePhase) { phase in if phase != .active { store.save() } }
  100. .alert(language.text("Save Notice"), isPresented: Binding(get: { store.storageWarning != nil }, set: { if !$0 { store.storageWarning = nil } })) {
  101. Button(language.text("Got It")) { store.storageWarning = nil }
  102. } message: { Text(language.text(store.storageWarning ?? "")) }
  103. }
  104. private var tabBar: some View {
  105. HStack(spacing: 0) {
  106. ForEach(MainTab.allCases, id: \.self) { item in
  107. Button { ClickAudio.shared.play(); tab = item } label: {
  108. VStack(spacing: 3) {
  109. Image(systemName: item.icon).font(.system(size: 22, weight: .semibold)).frame(height: 24)
  110. Text(language.text(item.rawValue)).font(.system(size: 11, weight: tab == item ? .semibold : .regular))
  111. }
  112. .foregroundStyle(tab == item ? Palette.blue : Palette.muted)
  113. .frame(maxWidth: .infinity).frame(minHeight: 52)
  114. .contentShape(Rectangle())
  115. }
  116. .buttonStyle(.plain)
  117. .accessibilityAddTraits(tab == item ? [.isSelected] : [])
  118. .accessibilityIdentifier("tab.\(item.rawValue)")
  119. }
  120. }
  121. .background(.white)
  122. .overlay(alignment: .top) { Rectangle().fill(Palette.line.opacity(0.65)).frame(height: 0.5) }
  123. }
  124. }
  125. struct ContentView_Previews: PreviewProvider {
  126. static var previews: some View {
  127. ContentView().environmentObject(GameStore()).environmentObject(LanguageStore())
  128. .environmentObject(FacebookTracking.shared).environmentObject(PlatformBootstrap.live())
  129. }
  130. }