Wrapper for Defaults

This commit is contained in:
Balazs Perlaki-Horvath 2025-01-06 11:21:59 +01:00 committed by Kelson
parent 2ade82fb64
commit 4d3f2bf333
2 changed files with 59 additions and 12 deletions

View File

@ -13,17 +13,23 @@
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
//
// CategoriesToLanguage.swift
// Kiwix
//
import Foundation
import Defaults
struct CategoriesToLanguages {
protocol CategoriesProtocol {
func has(category: Category, inLanguages langCodes: Set<String>) -> Bool
func save(_ dictionary: [Category: Set<String>])
func allCategories() -> [Category]
}
private let dictionary: [Category: Set<String>] = Defaults[.categoriesToLanguages]
struct CategoriesToLanguages: CategoriesProtocol {
private let defaults: Defaulting
private let dictionary: [Category: Set<String>]
init(withDefaults defaults: Defaulting = UDefaults()) {
self.defaults = defaults
self.dictionary = defaults[.categoriesToLanguages]
}
func has(category: Category, inLanguages langCodes: Set<String>) -> Bool {
guard !langCodes.isEmpty, !dictionary.isEmpty else {
@ -35,13 +41,13 @@ struct CategoriesToLanguages {
return !languages.isDisjoint(with: langCodes)
}
static func save(_ dictionary: [Category: Set<String>]) {
Defaults[.categoriesToLanguages] = dictionary
func save(_ dictionary: [Category: Set<String>]) {
defaults[.categoriesToLanguages] = dictionary
}
static func allCategories() -> [Category] {
func allCategories() -> [Category] {
let categoriesToLanguages = CategoriesToLanguages()
let contentLanguages = Defaults[.libraryLanguageCodes]
let contentLanguages = defaults[.libraryLanguageCodes]
return Category.allCases.filter { (category: Category) in
categoriesToLanguages.has(category: category, inLanguages: contentLanguages)
}

41
Model/Defaulting.swift Normal file
View File

@ -0,0 +1,41 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
import Foundation
import Defaults
protocol Defaulting: NSObjectProtocol {
subscript<Value: Defaults.Serializable>(key: Defaults.Key<Value>) -> Value { get set }
}
final class UDefaults: NSObject, Defaulting {
subscript<Value>(key: Defaults.Key<Value>) -> Value where Value : DefaultsSerializable {
get {
Defaults[key]
}
set {
Defaults[key] = newValue
}
}
}
/*
static subscript<Value: Serializable>(key: Key<Value>) -> Value {
get { key.suite[key] }
set {
key.suite[key] = newValue
}
}
*/