Skip to content

Fix categories parsing, when eTag 304 is used #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading