mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-10-04 01:27:26 -04:00
35 lines
1.0 KiB
Swift
35 lines
1.0 KiB
Swift
//
|
|
// CategoriesToLanguage.swift
|
|
// Kiwix
|
|
//
|
|
|
|
import Foundation
|
|
import Defaults
|
|
|
|
struct CategoriesToLanguages {
|
|
|
|
private let dictionary: [Category: Set<String>] = Defaults[.categoriesToLanguages]
|
|
|
|
func has(category: Category, inLanguages langCodes: Set<String>) -> Bool {
|
|
guard !langCodes.isEmpty, !dictionary.isEmpty else {
|
|
return true // no languages or category filters provided, do not filter
|
|
}
|
|
guard let languages = dictionary[category] else {
|
|
return false
|
|
}
|
|
return !languages.isDisjoint(with: langCodes)
|
|
}
|
|
|
|
static func save(_ dictionary: [Category: Set<String>]) {
|
|
Defaults[.categoriesToLanguages] = dictionary
|
|
}
|
|
|
|
static func allCategories() -> [Category] {
|
|
let categoriesToLanguages = CategoriesToLanguages()
|
|
let contentLanguages = Defaults[.libraryLanguageCodes]
|
|
return Category.allCases.filter { (category: Category) in
|
|
categoriesToLanguages.has(category: category, inLanguages: contentLanguages)
|
|
}
|
|
}
|
|
}
|