| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import SwiftUI
- import UIKit
- private enum ProfilePage: String, Hashable {
- case awards = "Awards", statistics = "Statistics", settings = "Settings", tutorial = "How to Play", rules = "Rules"
- case help = "Help", feedback = "My Tickets", about = "About", privacy = "Privacy", preferences = "Privacy Preferences"
- }
- struct ProfileView: View {
- @EnvironmentObject private var language: LanguageStore
- @State private var selection: ProfilePage?
- init() {
- #if DEBUG
- if DevelopmentPreview.screen == "settings" { _selection = State(initialValue: .settings) }
- if DevelopmentPreview.screen == "privacy" { _selection = State(initialValue: .privacy) }
- #endif
- }
- var body: some View {
- NavigationView {
- ScrollView(showsIndicators: false) {
- VStack(spacing: 23) {
- VStack(spacing: 10) {
- largeRow(.awards, icon: "trophy.fill", color: Palette.gold)
- largeRow(.statistics, icon: "chart.bar.fill", color: Palette.blue)
- }
- VStack(spacing: 0) {
- row(.settings, icon: "gearshape.fill", color: .red)
- row(.tutorial, icon: "graduationcap.fill", color: .orange)
- row(.rules, icon: "book.fill", color: .cyan, divider: false)
- }.background(.white, in: RoundedRectangle(cornerRadius: 17))
- VStack(spacing: 0) {
- row(.help, icon: "questionmark", color: .green)
- row(.feedback, icon: "envelope.fill", color: .indigo)
- row(.about, icon: "info", color: Palette.blue)
- row(.privacy, icon: "checkmark.shield.fill", color: .indigo)
- row(.preferences, icon: "person.fill.checkmark", color: .cyan, divider: false)
- }.background(.white, in: RoundedRectangle(cornerRadius: 17))
- Text("JOKER SUDOKU · 1.0").font(.system(size: 10, weight: .medium)).tracking(2).foregroundStyle(Palette.muted.opacity(0.6)).padding(.bottom, 10)
- }.padding(.horizontal, 17).padding(.top, 24).padding(.bottom, 24).frame(maxWidth: 760).frame(maxWidth: .infinity)
- }
- #if DEBUG
- .jokerPreviewScrollAnchor(atBottom: DevelopmentPreview.screen == "profile-bottom")
- #endif
- .background(Color(red: 0.92, green: 0.94, blue: 0.97))
- .navigationTitle(language.text("Me")).navigationBarTitleDisplayMode(.inline)
- .jokerNavigationBarBackground()
- }.navigationViewStyle(.stack)
- }
- @ViewBuilder private func destination(_ page: ProfilePage) -> some View {
- switch page {
- case .awards: AwardsView()
- case .statistics: StatisticsView()
- case .settings: SettingsView()
- case .feedback: FeedbackView()
- case .preferences: PrivacyPreferencesView()
- case .privacy: PrivacyPolicyView()
- default: InformationView(page: page)
- }
- }
- private func largeRow(_ page: ProfilePage, icon: String, color: Color) -> some View {
- NavigationLink(destination: destination(page), tag: page, selection: $selection) {
- HStack(spacing: 18) {
- Image(systemName: icon).font(.system(size: 29, weight: .medium)).foregroundStyle(color)
- .frame(width: 58, height: 58).background(Palette.blue.opacity(0.13), in: Circle())
- Text(language.text(page.rawValue)).font(.system(size: 22, weight: .medium)).foregroundStyle(Palette.ink)
- Spacer()
- Image(systemName: "chevron.right").font(.system(size: 16, weight: .semibold)).foregroundStyle(Palette.line)
- }.padding(18).frame(maxWidth: .infinity, minHeight: 92).background(.white, in: RoundedRectangle(cornerRadius: 17))
- }.buttonStyle(.plain)
- }
- private func row(_ page: ProfilePage, icon: String, color: Color, divider: Bool = true) -> some View {
- VStack(spacing: 0) {
- NavigationLink(destination: destination(page), tag: page, selection: $selection) {
- HStack(spacing: 17) {
- Image(systemName: icon).font(.system(size: 18, weight: .bold)).foregroundStyle(.white)
- .frame(width: 33, height: 33).background(color, in: RoundedRectangle(cornerRadius: 8))
- Text(language.text(page.rawValue)).font(.system(size: 18)).foregroundStyle(Palette.ink)
- Spacer()
- Image(systemName: "chevron.right").font(.system(size: 15, weight: .semibold)).foregroundStyle(Palette.line)
- }.padding(.horizontal, 12).frame(height: 56).contentShape(Rectangle())
- }.buttonStyle(.plain).accessibilityIdentifier("profile.\(page.rawValue)")
- if divider { Divider().padding(.leading, 62) }
- }
- }
- }
- struct SettingsView: View {
- @EnvironmentObject private var language: LanguageStore
- @EnvironmentObject private var store: GameStore
- @Environment(\.dismiss) private var dismiss
- @AppStorage(ClickAudio.preferenceKey) private var soundEnabled = true
- private func setting(_ key: WritableKeyPath<PlayerSettings, Bool>) -> Binding<Bool> {
- Binding(get: { store.settings[keyPath: key] }, set: { store.settings[keyPath: key] = $0 })
- }
- var body: some View {
- Form {
- Section {
- Picker(language.text("App Language"), selection: Binding(get: { language.selected }, set: { language.selected = $0 })) {
- ForEach(AppLanguage.allCases) { option in
- Text(option.nativeName).tag(option)
- }
- }
- .pickerStyle(.inline)
- .accessibilityIdentifier("settings.language")
- } header: {
- Text(language.text("Language"))
- } footer: {
- Text(language.text("Changes take effect immediately. Your current game and saved progress are kept."))
- }
- Section(language.text("Board Assistance")) {
- Toggle(language.text("Highlight row, column, and box"), isOn: setting(\.highlightPeers))
- Toggle(language.text("Highlight matching numbers"), isOn: setting(\.highlightSameNumbers))
- Toggle(language.text("Automatically clear related notes"), isOn: setting(\.removeNotes))
- }
- Section(language.text("Experience")) {
- Toggle(language.text("Sound effects"), isOn: $soundEnabled)
- .accessibilityIdentifier("settings.soundEffects")
- .onChange(of: soundEnabled) { enabled in if enabled { ClickAudio.shared.play() } }
- Toggle(language.text("Haptic feedback"), isOn: setting(\.haptics))
- }
- Section {
- Label(language.text("Progress is saved on this device"), systemImage: "checkmark.circle").foregroundStyle(Palette.muted)
- } footer: { Text(language.text("Use Continue Game to resume after returning home, switching apps, or closing the game.")) }
- }
- .navigationTitle(language.text("Settings")).navigationBarTitleDisplayMode(.inline)
- .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(language.text("Done")) { ClickAudio.shared.play(); dismiss() } } }
- }
- }
- struct StatisticsView: View {
- @EnvironmentObject private var language: LanguageStore
- @EnvironmentObject private var store: GameStore
- var body: some View {
- List {
- Section(language.text("All Games")) {
- stat("Games Started", value: "\(store.statistics.started)")
- stat("Games Won", value: "\(store.statistics.won)")
- stat("Completion Rate", value: store.statistics.started == 0 ? "—" : "\(Int(Double(store.statistics.won) / Double(store.statistics.started) * 100))%")
- stat("Best Score", value: store.statistics.bestScore.formatted())
- stat("Daily Challenges Completed", value: language.text("%d days", store.completedDailyCount))
- stat("Total Winning Time", value: GameSession.formatTime(store.statistics.totalSeconds))
- }
- Section(language.text("Best Time by Difficulty")) {
- ForEach(Difficulty.allCases) { difficulty in
- stat(difficulty.title, value: store.statistics.bestTimes[difficulty.rawValue].map(GameSession.formatTime) ?? "—")
- }
- }
- }.navigationTitle(language.text("Statistics")).navigationBarTitleDisplayMode(.inline)
- }
- private func stat(_ title: String, value: String) -> some View {
- HStack { Text(language.text(title)).foregroundStyle(Palette.ink); Spacer(); Text(value).monospacedDigit().foregroundStyle(Palette.blue) }.padding(.vertical, 4)
- }
- }
- struct AwardsView: View {
- @EnvironmentObject private var language: LanguageStore
- @EnvironmentObject private var store: GameStore
- var body: some View {
- ScrollView {
- VStack(spacing: 18) {
- Image(systemName: "trophy.fill").font(.system(size: 68)).foregroundStyle(Palette.gold).padding(.top, 25)
- Text(language.text("Every finish is worth keeping")).font(.system(size: 16)).foregroundStyle(Palette.muted).padding(.bottom, 15)
- award("First Steps", detail: "Complete your first Sudoku", icon: "sparkles", unlocked: store.statistics.won >= 1)
- award("Daily Star", detail: "Complete 1 daily challenge", icon: "star.fill", unlocked: store.completedDailyCount >= 1)
- award("Focused Mind", detail: "Complete 10 Sudoku games", icon: "flame.fill", unlocked: store.statistics.won >= 10)
- award("Master Class", detail: "Complete a Master puzzle", icon: "crown.fill", unlocked: store.statistics.winsByDifficulty[Difficulty.master.rawValue, default: 0] > 0)
- award("Star Collector", detail: "Complete 30 daily challenges", icon: "star.circle.fill", unlocked: store.completedDailyCount >= 30)
- }.padding(.horizontal, 22).padding(.bottom, 25)
- }.background(Palette.background).navigationTitle(language.text("Awards")).navigationBarTitleDisplayMode(.inline)
- }
- private func award(_ title: String, detail: String, icon: String, unlocked: Bool) -> some View {
- HStack(spacing: 16) {
- Image(systemName: icon).font(.system(size: 27)).foregroundStyle(unlocked ? Palette.gold : Palette.line)
- .frame(width: 52, height: 52).background(unlocked ? Palette.gold.opacity(0.12) : Palette.background, in: Circle())
- VStack(alignment: .leading, spacing: 7) {
- Text(language.text(title)).font(.system(size: 17, weight: .medium)).foregroundStyle(Palette.ink)
- Text(language.text(detail)).font(.system(size: 12)).foregroundStyle(Palette.muted)
- }
- Spacer()
- Image(systemName: unlocked ? "checkmark.seal.fill" : "lock.fill").foregroundStyle(unlocked ? Palette.blue : Palette.line)
- }.padding(16).background(.white, in: RoundedRectangle(cornerRadius: 18))
- }
- }
- private struct InformationView: View {
- @EnvironmentObject private var language: LanguageStore
- let page: ProfilePage
- private var paragraphs: [(String, String)] {
- switch page {
- case .tutorial:
- [("01 Choose an empty square", "Tap an empty square, then tap a number below the board. The original clues cannot be changed."),
- ("02 Think with notes", "Tap Notes to turn on pencil mode, then tap numbers to record candidates. Tap the same number again to remove it."),
- ("03 Find your way to the answer", "The selected row, column, and box are highlighted. Incorrect entries appear in red. Three mistakes end the game, and Undo does not remove mistakes."),
- ("04 Need a little inspiration?", "Each game has 3 hints. A hint fills the correct answer in a square, uses one hint, and reduces your score.")]
- case .rules:
- [("Nine rows, nine columns, nine boxes", "Fill the 9 × 9 board with numbers from 1 to 9."),
- ("No repeats in a row", "Every row must contain each number from 1 to 9 exactly once."),
- ("No repeats in a column", "Every column must also contain each number from 1 to 9 exactly once."),
- ("No repeats in a box", "Each 3 × 3 box outlined with bold lines must contain numbers 1 to 9 without repeats. Every puzzle has one unique solution.")]
- case .help:
- [("How do I resume a game?", "Continue Game appears on Home when you have an unfinished game. To resume a daily challenge, select its date on the calendar."),
- ("Why can’t I change a number?", "The original numbers are fixed clues. Numbers you enter can be erased or undone."),
- ("How do daily challenges work?", "Each date has a fixed puzzle. Play today or any past date, and earn a star when you finish."),
- ("Where is my progress saved?", "Progress is saved automatically on this device. Deleting the app removes local data. Cross-device sync is not currently supported.")]
- case .about:
- [("JokerSudoku", "Every square holds a possibility.\nA Sudoku game for logic and a moment of calm."),
- ("Designed for focus", "Five difficulty levels, daily challenges, and automatic local saves. No account needed. Play offline anytime."),
- ("Version", "1.0 (1)")]
- default: []
- }
- }
- var body: some View {
- ScrollView {
- VStack(alignment: .leading, spacing: 25) {
- ForEach(paragraphs, id: \.0) { title, body in
- VStack(alignment: .leading, spacing: 11) {
- Text(language.text(title)).font(.system(size: 19, weight: .semibold)).foregroundStyle(Palette.ink)
- Text(language.text(body)).font(.system(size: 16)).foregroundStyle(Palette.muted).lineSpacing(7).fixedSize(horizontal: false, vertical: true)
- }.frame(maxWidth: .infinity, alignment: .leading)
- }
- }.padding(25)
- }.background(Palette.background).navigationTitle(language.text(page.rawValue)).navigationBarTitleDisplayMode(.inline)
- }
- }
- private struct PrivacyPreferencesView: View {
- @EnvironmentObject private var language: LanguageStore
- @EnvironmentObject private var tracking: FacebookTracking
- @Environment(\.openURL) private var openURL
- var body: some View {
- Form {
- Section {
- HStack {
- Text(language.text("Tracking permission"))
- Spacer()
- Text(language.text(tracking.authorization.title)).foregroundStyle(Palette.muted)
- }
- if tracking.isConfigured && tracking.authorization == .notDetermined {
- Button(language.text("Request Tracking Permission")) {
- Task { await tracking.requestTrackingAuthorizationIfNeeded() }
- }
- }
- Button(language.text("Manage in iOS Settings")) {
- if let url = URL(string: UIApplication.openSettingsURLString) { openURL(url) }
- }
- } footer: { Text(language.text("Tracking is optional. You can change permission in iOS Settings. Declining does not affect Sudoku or your saved progress.")) }
- }
- .onAppear { tracking.refreshAuthorization() }
- .navigationTitle(language.text("Privacy Preferences")).navigationBarTitleDisplayMode(.inline)
- }
- }
- private struct FeedbackView: View {
- @EnvironmentObject private var language: LanguageStore
- @AppStorage("JokerSudoku.feedbackDraft") private var savedDraft = ""
- @State private var draft = ""
- @State private var saved = false
- @State private var showingShare = false
- var body: some View {
- Form {
- Section {
- Text(language.text("Write down a problem or an idea. Your feedback draft stays on this device, ready to copy or share anytime."))
- .font(.system(size: 14)).foregroundStyle(Palette.muted)
- TextEditor(text: $draft).frame(minHeight: 160).accessibilityLabel(language.text("Feedback"))
- } header: { Text(language.text("Feedback Draft")) }
- Section {
- Button(language.text("Save Draft")) { savedDraft = draft; saved = true }.disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
- Button { showingShare = true } label: {
- Label(language.text("Share Feedback"), systemImage: "square.and.arrow.up")
- }.disabled(draft.isEmpty)
- }
- }
- .navigationTitle(language.text("My Tickets")).navigationBarTitleDisplayMode(.inline)
- .onAppear { draft = savedDraft }
- .sheet(isPresented: $showingShare) { FeedbackShareSheet(text: draft) }
- .alert(language.text("Draft Saved"), isPresented: $saved) { Button(language.text("OK"), role: .cancel) { } }
- }
- }
|