mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-08-03 04:27:00 -04:00
Use a test database for library tests
This commit is contained in:
parent
ba5b12ce54
commit
1ff560a8ad
103
Tests/DatabaseTests.swift
Normal file
103
Tests/DatabaseTests.swift
Normal file
@ -0,0 +1,103 @@
|
||||
// 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 XCTest
|
||||
@testable import Kiwix
|
||||
|
||||
// swiftlint:disable force_try
|
||||
final class DatabaseTests: XCTestCase {
|
||||
|
||||
/// Make sure our test database behaves the same way as the real one
|
||||
func xtestDatabases() throws {
|
||||
let database: Databasing = Database.shared
|
||||
let testDB = TestDatabase()
|
||||
|
||||
// insertion
|
||||
let id1 = UUID()
|
||||
let id2 = UUID()
|
||||
|
||||
var zimFileIDs = Set([id1, id2])
|
||||
let insertCount = try database.context.bulkInsert { zimFile in
|
||||
while !zimFileIDs.isEmpty {
|
||||
guard let id = zimFileIDs.popFirst() else { continue }
|
||||
let metadata = Self.metadata(for: id)
|
||||
LibraryOperations.configureZimFile(zimFile, metadata: metadata)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var uuids = Set([id1, id2])
|
||||
let testInsertCount = try testDB.context.bulkInsert { zimFile in
|
||||
while !uuids.isEmpty {
|
||||
guard let id = uuids.popFirst() else { continue }
|
||||
let metadata = Self.metadata(for: id)
|
||||
LibraryOperations.configureZimFile(zimFile, metadata: metadata)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
XCTAssertEqual(insertCount, testInsertCount)
|
||||
|
||||
// test fetching
|
||||
let zimFiles = try! database.context.fetchZimFiles()
|
||||
XCTAssertEqual(zimFiles.count, 2)
|
||||
XCTAssertEqual(try! testDB.context.fetchZimFiles().count, 2)
|
||||
|
||||
// test deletion
|
||||
let noDeleteCount = try! database.context.bulkDeleteNotDownloadedZims(notIncludedIn: [id1, id2])
|
||||
let testNoDeleteCount = try! testDB.context.bulkDeleteNotDownloadedZims(notIncludedIn: [id1, id2])
|
||||
|
||||
XCTAssertEqual(noDeleteCount, 0)
|
||||
XCTAssertEqual(testNoDeleteCount, 0)
|
||||
|
||||
let deleteCount = try! database.context.bulkDeleteNotDownloadedZims(notIncludedIn: [])
|
||||
let testDeleteCount = try! testDB.context.bulkDeleteNotDownloadedZims(notIncludedIn: [])
|
||||
|
||||
XCTAssertEqual(deleteCount, 2)
|
||||
XCTAssertEqual(testDeleteCount, 2)
|
||||
|
||||
XCTAssertEqual(try! database.context.fetchZimFiles().count, 0)
|
||||
XCTAssertEqual(try! testDB.context.fetchZimFiles().count, 0)
|
||||
|
||||
}
|
||||
|
||||
private static func metadata(for uuid: UUID) -> ZimFileMetaData {
|
||||
ZimFileMetaData(
|
||||
fileID: uuid,
|
||||
groupIdentifier: "group",
|
||||
title: "test-title",
|
||||
fileDescription: "desc",
|
||||
languageCodes: "en",
|
||||
category: "meta",
|
||||
creationDate: .now,
|
||||
size: 11,
|
||||
articleCount: 1,
|
||||
mediaCount: 1,
|
||||
creator: "me",
|
||||
publisher: "me",
|
||||
downloadURL: nil,
|
||||
faviconURL: nil,
|
||||
faviconData: nil,
|
||||
flavor: nil,
|
||||
hasDetails: false,
|
||||
hasPictures: false,
|
||||
hasVideos: false,
|
||||
requiresServiceWorkers: false
|
||||
)
|
||||
}
|
||||
}
|
||||
// swiftlint:enable force_try
|
@ -109,7 +109,8 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
let viewModel = LibraryViewModel(urlSession: urlSession,
|
||||
processFactory: { LibraryProcess(defaultState: .initial) },
|
||||
defaults: testDefaults,
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults))
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults),
|
||||
database: TestDatabase())
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
XCTAssert(viewModel.error is LibraryRefreshError)
|
||||
XCTAssertEqual(
|
||||
@ -135,7 +136,8 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
let viewModel = LibraryViewModel(urlSession: urlSession,
|
||||
processFactory: { LibraryProcess(defaultState: .initial) },
|
||||
defaults: testDefaults,
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults))
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults),
|
||||
database: TestDatabase())
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
XCTAssert(viewModel.error is LibraryRefreshError)
|
||||
XCTAssertEqual(
|
||||
@ -162,7 +164,8 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
let viewModel = LibraryViewModel(urlSession: urlSession,
|
||||
processFactory: { LibraryProcess(defaultState: .initial) },
|
||||
defaults: testDefaults,
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults))
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults),
|
||||
database: TestDatabase())
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
XCTExpectFailure("Requires work in dependency to resolve the issue.")
|
||||
XCTAssertEqual(
|
||||
@ -189,23 +192,28 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
}
|
||||
let testDefaults = TestDefaults()
|
||||
testDefaults.setup()
|
||||
let database = TestDatabase()
|
||||
let context = database.context
|
||||
|
||||
let viewModel = LibraryViewModel(urlSession: urlSession,
|
||||
processFactory: { LibraryProcess(defaultState: .initial) },
|
||||
defaults: testDefaults,
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults))
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults),
|
||||
database: database)
|
||||
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
|
||||
// check no error has happened
|
||||
XCTAssertNil(viewModel.error)
|
||||
|
||||
// check one zim file is in the database
|
||||
let context = Database.shared.viewContext
|
||||
let zimFiles = try context.fetch(ZimFile.fetchRequest())
|
||||
let zimFiles = try context.fetchZimFiles()
|
||||
XCTAssertEqual(zimFiles.count, 1)
|
||||
XCTAssertEqual(zimFiles[0].id, zimFileID)
|
||||
|
||||
// check zim file can be retrieved by id, and properties are populated
|
||||
let zimFile = try XCTUnwrap(try context.fetch(ZimFile.fetchRequest(fileID: zimFileID)).first)
|
||||
// swiftlint:disable:next force_try
|
||||
let zimFile = try! XCTUnwrap(zimFiles.first { $0.fileID == zimFileID })
|
||||
XCTAssertEqual(zimFile.id, zimFileID)
|
||||
XCTAssertEqual(zimFile.articleCount, 50001)
|
||||
XCTAssertEqual(zimFile.category, Category.wikipedia.rawValue)
|
||||
@ -237,8 +245,6 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
XCTAssertEqual(zimFile.requiresServiceWorkers, false)
|
||||
XCTAssertEqual(zimFile.size, 6515656704)
|
||||
|
||||
// clean up
|
||||
context.delete(zimFile)
|
||||
}
|
||||
|
||||
/// Test zim file deprecation
|
||||
@ -246,21 +252,43 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
func testZimFileDeprecation() async throws {
|
||||
let testDefaults = TestDefaults()
|
||||
testDefaults.setup()
|
||||
let context = Database.shared.viewContext
|
||||
let oldZimFiles = try? context.fetch(ZimFile.fetchRequest())
|
||||
oldZimFiles?.forEach { zimFile in
|
||||
context.delete(zimFile)
|
||||
}
|
||||
if context.hasChanges {
|
||||
try? context.save()
|
||||
}
|
||||
|
||||
let database = TestDatabase()
|
||||
let context = database.context
|
||||
|
||||
// refresh library for the first time, which should create one zim file
|
||||
let viewModel = LibraryViewModel(urlSession: urlSession,
|
||||
processFactory: { LibraryProcess(defaultState: .initial) },
|
||||
defaults: testDefaults,
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults))
|
||||
categories: CategoriesToLanguages(withDefaults: testDefaults),
|
||||
database: database)
|
||||
|
||||
await forceRefresh(viewModel: viewModel)
|
||||
|
||||
let zimFile1 = try XCTUnwrap(try context.fetchZimFiles().first)
|
||||
|
||||
// refresh library for the second time, which should replace the old zim file with a new one
|
||||
await forceRefresh(viewModel: viewModel)
|
||||
|
||||
var zimFiles = try context.fetchZimFiles()
|
||||
XCTAssertEqual(zimFiles.count, 1)
|
||||
let zimFile2 = try XCTUnwrap(zimFiles.first)
|
||||
XCTAssertNotEqual(zimFile1.fileID, zimFile2.fileID)
|
||||
|
||||
// set fileURLBookmark of zim file 2
|
||||
zimFile2.fileURLBookmark = Data("/Users/tester/Downloads/file_url.zim".utf8)
|
||||
|
||||
// refresh library for the third time
|
||||
await forceRefresh(viewModel: viewModel)
|
||||
|
||||
zimFiles = try context.fetchZimFiles()
|
||||
|
||||
// check there are two zim files in the database, and zim file 2 is not deprecated
|
||||
XCTAssertEqual(zimFiles.count, 2)
|
||||
XCTAssertEqual(zimFiles.filter({ $0.fileID == zimFile2.fileID }).count, 1)
|
||||
}
|
||||
|
||||
private func forceRefresh(viewModel: LibraryViewModel) async {
|
||||
let expectationVMComplete = XCTestExpectation(description: "viewModel completed")
|
||||
viewModel.$state.sink { value in
|
||||
switch value {
|
||||
@ -272,44 +300,6 @@ final class LibraryRefreshViewModelTest: XCTestCase {
|
||||
}.store(in: &cancellables)
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
await fulfillment(of: [expectationVMComplete], timeout: 2)
|
||||
|
||||
let zimFile1 = try XCTUnwrap(try context.fetch(ZimFile.fetchRequest()).first)
|
||||
|
||||
// refresh library for the second time, which should replace the old zim file with a new one
|
||||
let expectationVMComplete2 = XCTestExpectation(description: "viewModel completed")
|
||||
viewModel.$state.sink { value in
|
||||
switch value {
|
||||
case .complete:
|
||||
expectationVMComplete2.fulfill()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}.store(in: &cancellables)
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
await fulfillment(of: [expectationVMComplete2], timeout: 2)
|
||||
|
||||
var zimFiles = try context.fetch(ZimFile.fetchRequest())
|
||||
XCTAssertEqual(zimFiles.count, 1)
|
||||
let zimFile2 = try XCTUnwrap(zimFiles.first)
|
||||
XCTAssertNotEqual(zimFile1.fileID, zimFile2.fileID)
|
||||
|
||||
// set fileURLBookmark of zim file 2
|
||||
zimFile2.fileURLBookmark = Data("/Users/tester/Downloads/file_url.zim".utf8)
|
||||
try context.save()
|
||||
|
||||
// refresh library for the third time
|
||||
await viewModel.start(isUserInitiated: true)
|
||||
zimFiles = try context.fetch(ZimFile.fetchRequest())
|
||||
|
||||
// check there are two zim files in the database, and zim file 2 is not deprecated
|
||||
XCTAssertEqual(zimFiles.count, 2)
|
||||
XCTAssertEqual(zimFiles.filter({ $0.fileID == zimFile2.fileID }).count, 1)
|
||||
|
||||
// clean up
|
||||
context.delete(zimFile1)
|
||||
context.delete(zimFile2)
|
||||
|
||||
try? context.save()
|
||||
}
|
||||
}
|
||||
|
||||
|
59
Tests/TestDatabase.swift
Normal file
59
Tests/TestDatabase.swift
Normal file
@ -0,0 +1,59 @@
|
||||
// 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 CoreData
|
||||
@testable import Kiwix
|
||||
|
||||
final class TestContext: DBObjectContext {
|
||||
|
||||
private let objectContext = NSManagedObjectContext(.privateQueue)
|
||||
|
||||
var zimFiles: [ZimFile] = []
|
||||
|
||||
func fetchZimFiles() throws -> [ZimFile] {
|
||||
zimFiles
|
||||
}
|
||||
|
||||
func bulkInsert(handler: @escaping (ZimFile) -> Bool) throws -> Int {
|
||||
var count = 0
|
||||
var zimFile = ZimFile(context: objectContext)
|
||||
while handler(zimFile) == false {
|
||||
zimFiles.append(zimFile)
|
||||
zimFile = ZimFile(context: objectContext)
|
||||
count += 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func bulkDeleteNotDownloadedZims(notIncludedIn: Set<UUID>) throws -> Int {
|
||||
let oldCount = zimFiles.count
|
||||
zimFiles = zimFiles.filter { zimFile in
|
||||
notIncludedIn.contains(zimFile.fileID) || zimFile.fileURLBookmark != nil
|
||||
}
|
||||
let newCount = zimFiles.count
|
||||
return oldCount - newCount
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class TestDatabase: Databasing {
|
||||
|
||||
var context: DBObjectContext = TestContext()
|
||||
|
||||
func backgroundTask(_ block: @escaping (any DBObjectContext) -> Void) {
|
||||
block(context)
|
||||
}
|
||||
}
|
@ -43,48 +43,67 @@ enum LibraryState {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class MultiSelectedZimFilesViewModel: ObservableObject {
|
||||
@Published private(set) var selectedZimFiles = Set<ZimFile>()
|
||||
|
||||
func toggleMultiSelect(of zimFile: ZimFile) {
|
||||
if selectedZimFiles.contains(zimFile) {
|
||||
selectedZimFiles.remove(zimFile)
|
||||
} else {
|
||||
selectedZimFiles.insert(zimFile)
|
||||
}
|
||||
// MARK: database protocols
|
||||
|
||||
protocol Databasing {
|
||||
var context: DBObjectContext { get }
|
||||
func backgroundTask(_ block: @escaping (DBObjectContext) -> Void)
|
||||
}
|
||||
|
||||
extension Database: Databasing {
|
||||
var context: any DBObjectContext {
|
||||
viewContext
|
||||
}
|
||||
|
||||
func singleSelect(zimFile: ZimFile) {
|
||||
selectedZimFiles = Set([zimFile])
|
||||
}
|
||||
|
||||
func reset() {
|
||||
selectedZimFiles.removeAll()
|
||||
}
|
||||
|
||||
func isSelected(_ zimFile: ZimFile) -> Bool {
|
||||
selectedZimFiles.contains(zimFile)
|
||||
}
|
||||
|
||||
func intersection(with zimFiles: Set<ZimFile>) {
|
||||
selectedZimFiles = selectedZimFiles.intersection(zimFiles)
|
||||
func backgroundTask(_ block: @escaping (any DBObjectContext) -> Void) {
|
||||
performBackgroundTask(block)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class SelectedZimFileViewModel: ObservableObject {
|
||||
@Published var selectedZimFile: ZimFile?
|
||||
|
||||
func reset() {
|
||||
selectedZimFile = nil
|
||||
protocol DBObjectContext {
|
||||
func fetchZimFiles() throws -> [ZimFile]
|
||||
func bulkInsert(handler: @escaping (ZimFile) -> Bool) throws -> Int
|
||||
func bulkDeleteNotDownloadedZims(notIncludedIn: Set<UUID>) throws -> Int
|
||||
}
|
||||
|
||||
extension NSManagedObjectContext: DBObjectContext {
|
||||
func bulkInsert(handler: @escaping (ZimFile) -> Bool) throws -> Int {
|
||||
let insertRequest = NSBatchInsertRequest(
|
||||
entity: ZimFile.entity(),
|
||||
managedObjectHandler: { zimFile in
|
||||
guard let zimFile = zimFile as? ZimFile else { return true }
|
||||
return handler(zimFile)
|
||||
}
|
||||
)
|
||||
insertRequest.resultType = .count
|
||||
if let result = try execute(insertRequest) as? NSBatchInsertResult {
|
||||
return result.result as? Int ?? 0
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func isSelected(_ zimFile: ZimFile) -> Bool {
|
||||
selectedZimFile == zimFile
|
||||
func bulkDeleteNotDownloadedZims(notIncludedIn: Set<UUID>) throws -> Int {
|
||||
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = ZimFile.fetchRequest()
|
||||
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
|
||||
ZimFile.Predicate.notDownloaded,
|
||||
NSPredicate(format: "NOT fileID IN %@", notIncludedIn)
|
||||
])
|
||||
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
||||
deleteRequest.resultType = .resultTypeCount
|
||||
if let result = try execute(deleteRequest) as? NSBatchDeleteResult {
|
||||
return result.result as? Int ?? 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func fetchZimFiles() throws -> [ZimFile] {
|
||||
try fetch(ZimFile.fetchRequest())
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: LibraryViewModel
|
||||
|
||||
final class LibraryViewModel: ObservableObject {
|
||||
@MainActor @Published private(set) var error: Error?
|
||||
/// Note: due to multiple instances of LibraryViewModel,
|
||||
@ -94,6 +113,7 @@ final class LibraryViewModel: ObservableObject {
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private let defaults: Defaulting
|
||||
private let categories: CategoriesProtocol
|
||||
private let database: Databasing
|
||||
|
||||
private let urlSession: URLSession
|
||||
private var insertionCount = 0
|
||||
@ -106,12 +126,14 @@ final class LibraryViewModel: ObservableObject {
|
||||
urlSession: URLSession = URLSession.shared,
|
||||
processFactory: @MainActor () -> LibraryProcess = { .shared },
|
||||
defaults: Defaulting = UDefaults(),
|
||||
categories: CategoriesProtocol = CategoriesToLanguages(withDefaults: UDefaults())
|
||||
categories: CategoriesProtocol = CategoriesToLanguages(withDefaults: UDefaults()),
|
||||
database: Databasing = Database.shared
|
||||
) {
|
||||
self.urlSession = urlSession
|
||||
self.process = processFactory()
|
||||
self.defaults = defaults
|
||||
self.categories = categories
|
||||
self.database = database
|
||||
state = process.state
|
||||
process.$state.sink { [weak self] newState in
|
||||
self?.state = newState
|
||||
@ -140,8 +162,8 @@ final class LibraryViewModel: ObservableObject {
|
||||
// but we still need to refresh the memory only stored
|
||||
// zimfile categories to languages dictionary
|
||||
if categories.allCategories().count < 2 {
|
||||
let context = Database.shared.viewContext
|
||||
if let zimFiles: [ZimFile] = try? context.fetch(ZimFile.fetchRequest()) {
|
||||
let context = database.context
|
||||
if let zimFiles: [ZimFile] = try? context.fetchZimFiles() {
|
||||
saveCategoryAvailableInLanguages(fromDBZimFiles: zimFiles)
|
||||
// populate library language code if there isn't one set already
|
||||
await setDefaultContentFilterLanguage()
|
||||
@ -298,44 +320,33 @@ final class LibraryViewModel: ObservableObject {
|
||||
|
||||
private func process(parser: Parser) async throws {
|
||||
try await withCheckedThrowingContinuation { [weak self] continuation in
|
||||
Database.shared.performBackgroundTask { [weak self] context in
|
||||
guard let self else {
|
||||
continuation.resume()
|
||||
return
|
||||
}
|
||||
self.database.backgroundTask { [weak self] context in
|
||||
guard let self else {
|
||||
continuation.resume()
|
||||
return
|
||||
}
|
||||
do {
|
||||
// insert new zim files
|
||||
let existing = try context.fetch(ZimFile.fetchRequest()).map { $0.fileID }
|
||||
let existing = try context.fetchZimFiles().map { $0.fileID }
|
||||
var zimFileIDs = parser.zimFileIDs.subtracting(existing)
|
||||
let insertRequest = NSBatchInsertRequest(
|
||||
entity: ZimFile.entity(),
|
||||
managedObjectHandler: { zimFile in
|
||||
guard let zimFile = zimFile as? ZimFile else { return true }
|
||||
while !zimFileIDs.isEmpty {
|
||||
guard let id = zimFileIDs.popFirst(),
|
||||
let metadata = parser.getMetaData(id: id) else { continue }
|
||||
LibraryOperations.configureZimFile(zimFile, metadata: metadata)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
self.insertionCount = try context.bulkInsert { zimFile in
|
||||
while !zimFileIDs.isEmpty {
|
||||
guard let id = zimFileIDs.popFirst(),
|
||||
let metadata = parser.getMetaData(id: id) else { continue }
|
||||
LibraryOperations.configureZimFile(zimFile, metadata: metadata)
|
||||
return false
|
||||
}
|
||||
)
|
||||
insertRequest.resultType = .count
|
||||
if let result = try context.execute(insertRequest) as? NSBatchInsertResult {
|
||||
self.insertionCount = result.result as? Int ?? 0
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// delete old zim entries not included in the feed
|
||||
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = ZimFile.fetchRequest()
|
||||
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
|
||||
ZimFile.Predicate.notDownloaded,
|
||||
NSPredicate(format: "NOT fileID IN %@", parser.zimFileIDs)
|
||||
])
|
||||
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
||||
deleteRequest.resultType = .resultTypeCount
|
||||
if let result = try context.execute(deleteRequest) as? NSBatchDeleteResult {
|
||||
self.deletionCount = result.result as? Int ?? 0
|
||||
}
|
||||
self.deletionCount = try context.bulkDeleteNotDownloadedZims(notIncludedIn: parser.zimFileIDs)
|
||||
|
||||
continuation.resume()
|
||||
} catch {
|
||||
os_log("Error saving OPDS Data: %s", log: Log.OPDS, type: .error, error.localizedDescription)
|
||||
|
58
ViewModel/ZimSelectionViewModels.swift
Normal file
58
ViewModel/ZimSelectionViewModels.swift
Normal file
@ -0,0 +1,58 @@
|
||||
// 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 SwiftUI
|
||||
|
||||
@MainActor
|
||||
final class MultiSelectedZimFilesViewModel: ObservableObject {
|
||||
@Published private(set) var selectedZimFiles = Set<ZimFile>()
|
||||
|
||||
func toggleMultiSelect(of zimFile: ZimFile) {
|
||||
if selectedZimFiles.contains(zimFile) {
|
||||
selectedZimFiles.remove(zimFile)
|
||||
} else {
|
||||
selectedZimFiles.insert(zimFile)
|
||||
}
|
||||
}
|
||||
|
||||
func singleSelect(zimFile: ZimFile) {
|
||||
selectedZimFiles = Set([zimFile])
|
||||
}
|
||||
|
||||
func reset() {
|
||||
selectedZimFiles.removeAll()
|
||||
}
|
||||
|
||||
func isSelected(_ zimFile: ZimFile) -> Bool {
|
||||
selectedZimFiles.contains(zimFile)
|
||||
}
|
||||
|
||||
func intersection(with zimFiles: Set<ZimFile>) {
|
||||
selectedZimFiles = selectedZimFiles.intersection(zimFiles)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class SelectedZimFileViewModel: ObservableObject {
|
||||
@Published var selectedZimFile: ZimFile?
|
||||
|
||||
func reset() {
|
||||
selectedZimFile = nil
|
||||
}
|
||||
|
||||
func isSelected(_ zimFile: ZimFile) -> Bool {
|
||||
selectedZimFile == zimFile
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user