diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.pbxproj b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.pbxproj new file mode 100644 index 0000000..ce1de1c --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.pbxproj @@ -0,0 +1,329 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXFileReference section */ + 976F818A2E16F953000D4A5B /* HAM Operator Quiz iPhone.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "HAM Operator Quiz iPhone.app"; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 976F818C2E16F953000D4A5B /* HAM Operator Quiz iPhone */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = "HAM Operator Quiz iPhone"; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 976F81872E16F953000D4A5B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 976F81812E16F953000D4A5B = { + isa = PBXGroup; + children = ( + 976F818C2E16F953000D4A5B /* HAM Operator Quiz iPhone */, + 976F818B2E16F953000D4A5B /* Products */, + ); + sourceTree = ""; + }; + 976F818B2E16F953000D4A5B /* Products */ = { + isa = PBXGroup; + children = ( + 976F818A2E16F953000D4A5B /* HAM Operator Quiz iPhone.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 976F81892E16F953000D4A5B /* HAM Operator Quiz iPhone */ = { + isa = PBXNativeTarget; + buildConfigurationList = 976F81952E16F955000D4A5B /* Build configuration list for PBXNativeTarget "HAM Operator Quiz iPhone" */; + buildPhases = ( + 976F81862E16F953000D4A5B /* Sources */, + 976F81872E16F953000D4A5B /* Frameworks */, + 976F81882E16F953000D4A5B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 976F818C2E16F953000D4A5B /* HAM Operator Quiz iPhone */, + ); + name = "HAM Operator Quiz iPhone"; + packageProductDependencies = ( + ); + productName = "HAM Operator Quiz iPhone"; + productReference = 976F818A2E16F953000D4A5B /* HAM Operator Quiz iPhone.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 976F81822E16F953000D4A5B /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1640; + LastUpgradeCheck = 1640; + TargetAttributes = { + 976F81892E16F953000D4A5B = { + CreatedOnToolsVersion = 16.4; + }; + }; + }; + buildConfigurationList = 976F81852E16F953000D4A5B /* Build configuration list for PBXProject "HAM Operator Quiz iPhone" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 976F81812E16F953000D4A5B; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 976F818B2E16F953000D4A5B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 976F81892E16F953000D4A5B /* HAM Operator Quiz iPhone */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 976F81882E16F953000D4A5B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 976F81862E16F953000D4A5B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 976F81932E16F955000D4A5B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DEVELOPMENT_TEAM = 5RZLK3W7ZH; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 18.5; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 976F81942E16F955000D4A5B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEVELOPMENT_TEAM = 5RZLK3W7ZH; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 18.5; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 976F81962E16F955000D4A5B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 5RZLK3W7ZH; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.justinoros.HAM-Operator-Quiz-iPhone"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 976F81972E16F955000D4A5B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = 5RZLK3W7ZH; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.justinoros.HAM-Operator-Quiz-iPhone"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 976F81852E16F953000D4A5B /* Build configuration list for PBXProject "HAM Operator Quiz iPhone" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 976F81932E16F955000D4A5B /* Debug */, + 976F81942E16F955000D4A5B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 976F81952E16F955000D4A5B /* Build configuration list for PBXNativeTarget "HAM Operator Quiz iPhone" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 976F81962E16F955000D4A5B /* Debug */, + 976F81972E16F955000D4A5B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 976F81822E16F953000D4A5B /* Project object */; +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/xcuserdata/justinoros.xcuserdatad/UserInterfaceState.xcuserstate b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/xcuserdata/justinoros.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..479f751 Binary files /dev/null and b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/project.xcworkspace/xcuserdata/justinoros.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/xcuserdata/justinoros.xcuserdatad/xcschemes/xcschememanagement.plist b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/xcuserdata/justinoros.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..4a5b4ab --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone.xcodeproj/xcuserdata/justinoros.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + HAM Operator Quiz iPhone.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AccentColor.colorset/Contents.json b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AppIcon.appiconset/Contents.json b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2305880 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,35 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/Contents.json b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/HAM_Operator_Quiz_iPhoneApp.swift b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/HAM_Operator_Quiz_iPhoneApp.swift new file mode 100644 index 0000000..77521f2 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/HAM_Operator_Quiz_iPhoneApp.swift @@ -0,0 +1,12 @@ +import SwiftUI + +@main +struct HamOperatorQuizApp: App { + var body: some Scene { + WindowGroup { + NavigationStack { + TestSelectionView() + } + } + } +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/QuizView.swift b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/QuizView.swift new file mode 100644 index 0000000..b2127c7 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/QuizView.swift @@ -0,0 +1,238 @@ +import SwiftUI + +struct QuizQuestion: Codable, Identifiable { + let id: String + let question: String + let correct: Int + let answers: [String] +} + +struct QuizView: View { + let testName: String + + @State private var questions: [QuizQuestion] = [] + @State private var currentIndex = 0 + @State private var selectedAnswer: String? = nil + @State private var score = 0 + @State private var feedbackText = "" + @State private var feedbackColor = Color.primary + @State private var loadError = false + @State private var showResults = false + @State private var showSummaryAlert = false + + var body: some View { + VStack(alignment: .leading, spacing: 20) { + if loadError { + Text("Could not load questions for \(testName). Make sure the .json file is included in the app bundle or Documents folder.") + .foregroundColor(.red) + .padding() + } else if questions.isEmpty { + Text("Loading questions...") + .padding() + } else if currentIndex < questions.count { + Text(questions[currentIndex].question) + .font(.headline) + .fixedSize(horizontal: false, vertical: true) + + ForEach(questions[currentIndex].answers, id: \.self) { answer in + Button(action: { + selectedAnswer = answer + checkAnswer() + + let correctAnswer = questions[currentIndex].answers[questions[currentIndex].correct] + let delay = (answer == correctAnswer) ? 3.0 : 5.0 + + DispatchQueue.main.asyncAfter(deadline: .now() + delay) { + if selectedAnswer != nil { + nextQuestion() + } + } + }) { + Text(answer) + .padding(10) + .frame(maxWidth: .infinity, alignment: .leading) + .background( + Capsule() + .fill(bubbleColor(for: answer)) + ) + .foregroundColor(.white) + } + .buttonStyle(PlainButtonStyle()) + } + + Text(feedbackText) + .foregroundColor(feedbackColor) + + HStack { + Button("Next") { + nextQuestion() + } + .disabled(selectedAnswer == nil) + .tint(Color.blue) + + Spacer() + + Button("Quit") { + showSummaryAlert = true + } + .tint(Color.gray) + } + .padding(.top, 10) + + } else { + VStack(spacing: 10) { + Text("Quiz Complete!") + .font(.title) + + let percentage = questions.count > 0 ? Int((Double(score) / Double(questions.count)) * 100) : 0 + Text("You got \(score) out of \(questions.count) correct. (\(percentage)%)") + } + } + } + .padding() + .onAppear(perform: syncAndLoadQuestions) + .navigationTitle(testName + " Test") + .alert(isPresented: $showSummaryAlert) { + let percentage = currentIndex > 0 ? Int((Double(score) / Double(currentIndex + 1)) * 100) : 0 + return Alert( + title: Text("Quiz Summary"), + message: Text("You answered \(score) out of \(currentIndex + 1) correctly. (\(percentage)% correct)"), + dismissButton: .default(Text("OK"), action: { + exit(0) + }) + ) + } + } + + func bubbleColor(for answer: String) -> Color { + let imessageBlue = Color(red: 0.0, green: 122/255, blue: 1.0) + let correctAnswer = questions[currentIndex].answers[questions[currentIndex].correct] + if let selected = selectedAnswer { + if selected == answer && selected == correctAnswer { + return .green + } else if selected == answer && selected != correctAnswer { + return .red + } else if answer == correctAnswer && selected != correctAnswer { + return .green + } + } + return imessageBlue + } + + func syncAndLoadQuestions() { + guard let documentsDir = getDocumentsDirectory() else { + loadError = true + return + } + + let lowerName = testName.lowercased() + let filename = "\(lowerName).json" + let urlMap = [ + "technician": "https://raw.githubusercontent.com/russolsen/ham_radio_question_pool/master/technician-2022-2026/technician.json", + "general": "https://raw.githubusercontent.com/russolsen/ham_radio_question_pool/master/general-2023-2027/general.json", + "extra": "https://raw.githubusercontent.com/russolsen/ham_radio_question_pool/master/extra-2024-2028/extra.json" + ] + + guard let remoteUrlString = urlMap[lowerName], + let remoteUrl = URL(string: remoteUrlString) else { + loadError = true + return + } + + let localFileURL = documentsDir.appendingPathComponent(filename) + + let fileExists = FileManager.default.fileExists(atPath: localFileURL.path) + + if !fileExists { + downloadAndSave(remoteUrl: remoteUrl, to: localFileURL) + } else { + URLSession.shared.dataTask(with: remoteUrl) { data, response, error in + if let data = data, + let httpResponse = response as? HTTPURLResponse, + httpResponse.statusCode == 200 { + do { + let remoteQuestions = try JSONDecoder().decode([QuizQuestion].self, from: data) + if let localData = try? Data(contentsOf: localFileURL), + let localQuestions = try? JSONDecoder().decode([QuizQuestion].self, from: localData), + localQuestions.count == remoteQuestions.count { + loadQuestions(from: localFileURL) + } else { + try data.write(to: localFileURL) + loadQuestions(from: localFileURL) + } + } catch { + print("Failed sync check: \(error)") + loadQuestions(from: localFileURL) + } + } else { + print("Remote check failed: \(error?.localizedDescription ?? "Unknown")") + loadQuestions(from: localFileURL) + } + }.resume() + } + } + + func downloadAndSave(remoteUrl: URL, to localUrl: URL) { + URLSession.shared.dataTask(with: remoteUrl) { data, response, error in + if let data = data, + let httpResponse = response as? HTTPURLResponse, + httpResponse.statusCode == 200 { + do { + try data.write(to: localUrl) + loadQuestions(from: localUrl) + } catch { + print("Failed to save downloaded JSON: \(error)") + loadError = true + } + } else { + print("Download failed: \(error?.localizedDescription ?? "Unknown")") + loadError = true + } + }.resume() + } + + func loadQuestions(from fileURL: URL) { + DispatchQueue.global(qos: .background).async { + do { + let data = try Data(contentsOf: fileURL) + let loadedQuestions = try JSONDecoder().decode([QuizQuestion].self, from: data) + + DispatchQueue.main.async { + if loadedQuestions.isEmpty { + loadError = true + } else { + questions = loadedQuestions.shuffled() + loadError = false + } + } + } catch { + print("Failed to parse questions: \(error)") + DispatchQueue.main.async { + loadError = true + } + } + } + } + + func checkAnswer() { + let correctAnswer = questions[currentIndex].answers[questions[currentIndex].correct] + if selectedAnswer == correctAnswer { + score += 1 + feedbackText = "Correct!" + feedbackColor = .green + } else { + feedbackText = "Incorrect! Correct answer: \(correctAnswer)" + feedbackColor = .red + } + } + + func nextQuestion() { + currentIndex += 1 + selectedAnswer = nil + feedbackText = "" + } + + func getDocumentsDirectory() -> URL? { + FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first + } +} diff --git a/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/TestSelectionView.swift b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/TestSelectionView.swift new file mode 100644 index 0000000..3c03205 --- /dev/null +++ b/HAM Operator Quiz iPhone/HAM Operator Quiz iPhone/TestSelectionView.swift @@ -0,0 +1,34 @@ +import SwiftUI + +struct TestSelectionView: View { + let testOptions = ["Technician Class", "General Class", "Extra Class"] + + var body: some View { + NavigationStack { + VStack(spacing: 30) { + Spacer() // Push content down for vertical centering + + ForEach(testOptions, id: \.self) { test in + NavigationLink(value: test) { + Text(test) + .frame(width: 250, height: 50) + .background(Color.blue.opacity(0.7)) + .foregroundColor(.white) + .cornerRadius(8) + } + } + + Link("Find an Exam Center", destination: URL(string: "https://www.arrl.org/find-an-amateur-radio-license-exam-session")!) + .foregroundColor(.blue) + .padding(.top, 20) + + Spacer() // Push content up for vertical centering + } + .navigationTitle("Select Test") + .navigationDestination(for: String.self) { selectedTest in + QuizView(testName: selectedTest.replacingOccurrences(of: " Class", with: "")) + } + .padding() + } + } +}