I have a singleton instance of a class that (among other things) is managing which subset of words
will be available to users.
The contents of availableWords
will always be a subset of words
and is always a function of three userDefaults that are bound to user settings (using @AppStorage
)
I could dynamically reconstruct availableWords
every time it is needed, but it will be read much more frequently than it changes. Because of this, I want to cache the updated list every time a user changes one of the settings that will change its contents.
But the only way I can see to do this is to create an update
function and rely on the UI code to call the function any time a user updates one of the settings that will require availableWords
to be updated. And this feels more like something out of UIKit.
Do any of you see a better way of managing the updates of availableWords
?
class WordsManager {
static let shared = WordsManager()
let words: Words // defined in init
var availableWords: Words // updated anytime scriptPickers, languageChoice or cardPile changes
@AppStorage("scriptPickers") var scriptPickers: ScriptPickers = ScriptPickers.defaultDictionary
@AppStorage("languageChoice") var languageChoice: LanguageChoice = .all
@AppStorage("cardPile") var cardPile: CardPile = .allRandom
func updateAvailableWords() {
var result = words.filtered(by: cardPile.wordList)
let askIdentifiers = languageChoice.askLanguages
let answerIdentifiers = languageChoice.answerLanguages
result = result.matching(askIdentifiers: askIdentifiers, answerIdentifiers: answerIdentifiers)
self.availableWords = result
}
// other stuff
}