Skip to content

Commit

Permalink
Fix categories parsing, when eTag 304 is used
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH committed Jan 2, 2025
1 parent 93f80a9 commit 9a52eaa
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions ViewModel/LibraryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,29 @@ final class LibraryViewModel: ObservableObject {

// refresh library
guard let data = try await fetchData() else {
error = nil
process.state = .complete
return
// this is the case when we have no new data (304 http)
// but we still need to refresh the memory only stored
// zimfile categories to languages dictionary
if CategoriesToLanguages.allCategories().count < 2 {
let context = Database.shared.viewContext
if let zimFiles: [ZimFile] = try? context.fetch(ZimFile.fetchRequest()) {
saveCategoryAvailableInLanguages(fromDBZimFiles: zimFiles)
// populate library language code if there isn't one set already
await setDefaultContentFilterLanguage()

error = nil
process.state = .complete
return
} else {
error = LibraryRefreshError.process
process.state = .error
return
}
} else {
error = nil
process.state = .complete
return
}
}
let parser = try await parse(data: data)
// delete all old ISO Lang Code entries if needed, by passing in an empty parser
Expand Down Expand Up @@ -138,6 +158,22 @@ final class LibraryViewModel: ObservableObject {
}
CategoriesToLanguages.save(dictionary)
}

private func saveCategoryAvailableInLanguages(fromDBZimFiles zimFiles: [ZimFile]) {
var dictionary: [Category: Set<String>] = [:]
for zimFile in zimFiles {
let category = Category(rawValue: zimFile.category) ?? .other
let allLanguagesForCategory: Set<String>
let categoryLanguages: Set<String> = Set<String>(zimFile.languageCode.components(separatedBy: ","))
if let existingLanguages = dictionary[category] {
allLanguagesForCategory = existingLanguages.union(categoryLanguages)
} else {
allLanguagesForCategory = categoryLanguages
}
dictionary[category] = allLanguagesForCategory
}
CategoriesToLanguages.save(dictionary)
}

/// The fetched content is filtered by the languages set in settings.
/// We need to make sure, whatever was set by the user is
Expand Down

0 comments on commit 9a52eaa

Please sign in to comment.