Skip to content

Commit

Permalink
Update filtering logic for duplicate hiding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 26, 2024
1 parent cfef6a7 commit 82a8dcf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
19 changes: 7 additions & 12 deletions Spellbook/SpellFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,12 @@ func filteredSpellList(state: SpellbookAppState) -> [Spell] {

let spellList = SpellbookAppState.allSpells
var keptIDs = Set<Int>()
var filteredSpellList: [Spell] = []
let filter = createFilter(state: state)
let hideDuplicates = state.profile?.sortFilterStatus.hideDuplicateSpells ?? true

//
for spell in spellList {
if !filter(spell) {
filteredSpellList.append(spell)
if hideDuplicates {
keptIDs.insert(spell.id)
}
}
var filteredSpellList = spellList.filter(filter)
if hideDuplicates {
keptIDs = Set(filteredSpellList.map { $0.id })
}

// I'd rather avoid a second pass, but since linked spells won't necessarily
Expand All @@ -165,11 +159,12 @@ func filteredSpellList(state: SpellbookAppState) -> [Spell] {
let prefer2024Spells = state.profile?.sortFilterStatus.prefer2024Spells ?? true
let rulesetToIgnore = prefer2024Spells ? Ruleset.Rules2014 : Ruleset.Rules2024
let duplicatesFilter = { (spell: Spell) in
var filter = false
if spell.ruleset == rulesetToIgnore {
return false
guard let linkedID = Spellbook.linkedSpellID(for: spell) else { return true }
filter = keptIDs.contains(linkedID)
}
guard let linkedID = Spellbook.linkedSpellID(for: spell) else { return false }
return !keptIDs.contains(linkedID)
return !filter
}
filteredSpellList = filteredSpellList.filter(duplicatesFilter)
}
Expand Down
3 changes: 1 addition & 2 deletions Spellbook/SpellbookAppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ struct SpellbookAppState: StateType {
}

mutating func filterAndSortSpells() {
let filter = createFilter(state: self)
currentSpellList = spellList.filter(filter)
currentSpellList = filteredSpellList(state: self)
self.sortSpells()
}
}

0 comments on commit 82a8dcf

Please sign in to comment.