iBook/ipad_app/Sources/ViewModels/LibraryViewModel.swift

20 lines
752 B
Swift

import Foundation
import Combine
@available(macOS 10.15, iOS 13.0, *)
@MainActor
final class LibraryViewModel: ObservableObject {
@Published var bundleIndex: BundleIndex?
@Published var searchText: String = ""
var filteredBooks: [BookMeta] {
guard let books = bundleIndex?.books else { return [] }
let q = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
if q.isEmpty { return books.sorted { ($0.last_open ?? 0) > ($1.last_open ?? 0) } }
return books.filter { $0.title.localizedCaseInsensitiveContains(q) || $0.author.localizedCaseInsensitiveContains(q) }
.sorted { ($0.last_open ?? 0) > ($1.last_open ?? 0) }
}
func attach(index: BundleIndex) { self.bundleIndex = index }
}