-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add question/time limit quiz modes #3
Changes from all commits
358b632
442444c
6463828
cf8d650
feca37e
6aa8403
dad3752
84f4ee6
aad229c
7ff5989
638e2f8
6e78002
9aaf9d0
c792eb6
6767b28
48714ed
42c7fdc
45736cc
f7642db
b1161cd
4ae45b8
0b2d969
981bd4b
f2608ab
fa00643
6ae7139
c3b5b31
41ec9d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
Comment on lines
+10
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
||
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 | ||
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to save these values as raw |
||
|
||
var showProgress: Bool | ||
var showBiki: Bool | ||
var showConfetti: Bool | ||
var playHaptics: Bool | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add full support for these in another PR. |
||
} | ||
|
||
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.
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This file was technically renamed from the unused
TopicSettingsClient
for convenience.