Skip to content

Commit

Permalink
Implement action for resetting ranges to default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Oct 27, 2024
1 parent d943c98 commit f4df120
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
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

0 comments on commit f4df120

Please sign in to comment.