Skip to content
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

Implement reducer for resetting ranges to default values #41

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Spellbook/AppReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func specificActionReducer(action: Action, state: inout SpellbookAppState) -> Sp
return rangeValueUpdateReducer(action: action, state: &state)
case let action as RangeUnitUpdateAction:
return rangeUnitUpdateReducer(action: action, state: &state)

// Resetting ranges to defaults
case let action as CastingTimeDefaultAction:
return defaultCastingTimeRangeReducer(action: action, state: &state)
case let action as DurationDefaultAction:
return defaultDurationRangeReducer(action: action, state: &state)
case let action as RangeDefaultAction:
return defaultRangeRangeReducer(action: action, state: &state)

// Updating filter flags
case let action as SetFlagAction:
Expand Down
3 changes: 2 additions & 1 deletion Spellbook/RangeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ class RangeView: UIView, HeightProvider {
minUnitChoice.delegate = minUnitDelegate
maxUnitChoice.delegate = maxUnitDelegate

// Create the function that will get the values for the unit bounds
// Create the functions that will get the values for the unit bounds
boundsGetter = { () in return store.state.profile?.sortFilterStatus.getStringBounds(Q.self) ?? (0, "", 1, "") }
defaultBoundsGetter = { () in return store.state.profile?.sortFilterStatus.getDefaultStringBounds(Q.self) ?? (0, "", 1, "") }

// Create and set the delegates for the values
minValueDelegate = NumberFieldDelegate<ValueActionType>(
Expand Down
32 changes: 32 additions & 0 deletions Spellbook/SortFilterStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,38 @@ class SortFilterStatus {
self.maxRangeValue = maxValue
self.maxRangeUnit = maxUnit
}

func setCastingTimeBoundsToDefault() {
setCastingTimeBounds(minValue: SortFilterStatus.defaultMinCastingTimeValue,
minUnit: SortFilterStatus.defaultMinCastingTimeUnit,
maxValue: SortFilterStatus.defaultMaxCastingTimeValue,
maxUnit: SortFilterStatus.defaultMaxCastingTimeUnit)
}
func setDurationBoundsToDefault() {
setDurationBounds(minValue: SortFilterStatus.defaultMinDurationValue,
minUnit: SortFilterStatus.defaultMinDurationUnit,
maxValue: SortFilterStatus.defaultMaxDurationValue,
maxUnit: SortFilterStatus.defaultMaxDurationUnit)
}
func setRangeBoundsToDefault() {
setRangeBounds(minValue: SortFilterStatus.defaultMinRangeValue,
minUnit: SortFilterStatus.defaultMinRangeUnit,
maxValue: SortFilterStatus.defaultMaxRangeValue,
maxUnit: SortFilterStatus.defaultMaxRangeUnit)
}
func setBoundsToDefault<T:QuantityType, U:Unit, Q:Quantity<T,U>>(_ type: Q.Type) {
switch type {
case is CastingTime.Type:
return setCastingTimeBoundsToDefault()
case is Duration.Type:
return setDurationBoundsToDefault()
case is Range.Type:
return setRangeBoundsToDefault()
default:
return
}
}

private func setBoundsFromSION<U: Unit>(sion: SION, setter: BoundSetter<U>,
minValueKey: String, maxValueKey: String) {
let minValue = intFromSION(sion[minValueKey], defaultValue: 0)
Expand Down
16 changes: 16 additions & 0 deletions Spellbook/SpellbookReducers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ func castingTimeUnitUpdateReducer(action: CastingTimeUnitUpdateAction, state: in
return unitUpdateReducer(action: action, state: &state, minSetter: SortFilterStatus.setMinCastingTimeUnit, maxSetter: SortFilterStatus.setMaxCastingTimeUnit)
}

func defaultRangeReducer<T: QuantityType, U: Unit, Q: Quantity<T,U>>(action: QuantityRangeDefaultAction<T,U,Q>, state: inout SpellbookAppState, type: Q.Type) -> SpellbookAppState {
guard let status = state.profile?.sortFilterStatus else { return state }
status.setBoundsToDefault(type)
state.filterAndSortSpells()
return state
}
func defaultCastingTimeRangeReducer(action: CastingTimeDefaultAction, state: inout SpellbookAppState) -> SpellbookAppState {
return defaultRangeReducer(action: action, state: &state, type: CastingTime.self)
}
func defaultDurationRangeReducer(action: DurationDefaultAction, state: inout SpellbookAppState) -> SpellbookAppState {
return defaultRangeReducer(action: action, state: &state, type: Duration.self)
}
func defaultRangeRangeReducer(action: RangeDefaultAction, state: inout SpellbookAppState) -> SpellbookAppState {
return defaultRangeReducer(action: action, state: &state, type: Range.self)
}

func setFlagFilterReducer(action: SetFlagAction, state: inout SpellbookAppState) -> SpellbookAppState {
guard let status = state.profile?.sortFilterStatus else { return state }
switch (action.flag) {
Expand Down