-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from twocentstudios/time-attack
Add question/time limit quiz modes
- Loading branch information
Showing
14 changed files
with
776 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import DependenciesAdditions | ||
import Foundation | ||
|
||
struct SessionSettingsClient { | ||
var get: @Sendable () -> (SessionSettings) | ||
var set: @Sendable (SessionSettings) throws -> Void | ||
} | ||
|
||
struct SessionSettings: Equatable, Codable { | ||
enum QuizMode: Equatable, Identifiable, Codable, CaseIterable { | ||
case infinite | ||
case questionLimit | ||
case timeLimit | ||
|
||
var id: Self { self } | ||
var title: String { | ||
switch self { | ||
case .infinite: "Infinite" | ||
case .questionLimit: "Question Limit" | ||
case .timeLimit: "Time Limit" | ||
} | ||
} | ||
} | ||
var quizMode: QuizMode | ||
var questionLimit: Int | ||
var timeLimit: Int // seconds | ||
|
||
var showProgress: Bool | ||
var showBiki: Bool | ||
var showConfetti: Bool | ||
var playHaptics: Bool | ||
} | ||
|
||
extension SessionSettings { | ||
static let `default`: Self = .init(quizMode: .infinite, questionLimit: 10, timeLimit: 60, showProgress: true, showBiki: true, showConfetti: true, playHaptics: true) | ||
|
||
static let questionLimitValues: [Int] = [5, 10, 20, 30, 50, 100] | ||
static let timeLimitValues: [Int] = [30, 1 * 60, 3 * 60, 5 * 60, 10 * 60, 20 * 60] | ||
} | ||
|
||
extension SessionSettingsClient: DependencyKey { | ||
static let settingsKey = "SessionSettingsClient.settings" | ||
static var liveValue: Self { | ||
@Dependency(\.userDefaults) var userDefaults | ||
@Dependency(\.encode) var encode | ||
@Dependency(\.decode) var decode | ||
return .init( | ||
get: { | ||
let data = userDefaults.data(forKey: settingsKey) | ||
let value: SessionSettings | ||
if let data { | ||
value = (try? decode(SessionSettings.self, from: data)) ?? SessionSettings.default | ||
} else { | ||
value = SessionSettings.default | ||
} | ||
return value | ||
}, | ||
set: { newValue in | ||
let data = try encode(newValue) | ||
userDefaults.set(data, forKey: settingsKey) | ||
} | ||
) | ||
} | ||
} | ||
|
||
extension SessionSettingsClient: TestDependencyKey { | ||
static var previewValue: Self { | ||
let storage = LockIsolated(SessionSettings.default) | ||
return .init( | ||
get: { storage.value }, | ||
set: { storage.setValue($0) } | ||
) | ||
} | ||
|
||
static var testValue: Self { | ||
let storage = LockIsolated(SessionSettings.default) | ||
return .init( | ||
get: { storage.value }, | ||
set: { storage.setValue($0) } | ||
) | ||
} | ||
} | ||
|
||
extension DependencyValues { | ||
var sessionSettingsClient: SessionSettingsClient { | ||
get { self[SessionSettingsClient.self] } | ||
set { self[SessionSettingsClient.self] = newValue } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// https://stackoverflow.com/a/40868784 | ||
extension Comparable { | ||
func clamped(to limits: ClosedRange<Self>) -> Self { | ||
min(max(self, limits.lowerBound), limits.upperBound) | ||
} | ||
} | ||
|
||
extension BinaryFloatingPoint { | ||
func clampedPercentage() -> Self { | ||
clamped(to: 0.0 ... 1.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.