Skip to content

Commit

Permalink
Merge pull request #1111 from kiwix/1104-update-defaults-dependency
Browse files Browse the repository at this point in the history
Update defaults dependency to 8.2
  • Loading branch information
kelson42 authored Feb 25, 2025
2 parents 4de2be6 + 1cc64db commit 0ef04dc
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 54 deletions.
2 changes: 1 addition & 1 deletion App/CompactViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl

init(navigation: NavigationViewModel) {
self.navigation = navigation
searchViewModel = SearchViewModel()
searchViewModel = SearchViewModel.shared
let searchResult = SearchResults().environmentObject(searchViewModel)
searchController = UISearchController(searchResultsController: UIHostingController(rootView: searchResult))
super.init(rootView: AnyView(CompactView()))
Expand Down
2 changes: 1 addition & 1 deletion Model/Defaulting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol Defaulting: NSObjectProtocol {
}

final class UDefaults: NSObject, Defaulting {
subscript<Value>(key: Defaults.Key<Value>) -> Value where Value: DefaultsSerializable {
subscript<Value>(key: Defaults.Key<Value>) -> Value {
get {
Defaults[key]
}
Expand Down
35 changes: 0 additions & 35 deletions SwiftUI/Model/DefaultKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,3 @@ extension Defaults.Keys {
static let windowURLs = Key<[URL]>("windowURLs", default: [])
#endif
}

extension Defaults.Serializable where Self: Codable {
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
}

extension Defaults.Serializable where Self: Codable & NSSecureCoding {
public static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
}

extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
}

extension Defaults.Serializable where Self: Codable & RawRepresentable {
public static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
}

extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
}

extension Defaults.Serializable where Self: RawRepresentable {
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
}
extension Defaults.Serializable where Self: NSSecureCoding {
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
}

extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
public static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
}

extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
public static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
}
6 changes: 3 additions & 3 deletions Tests/TestDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Defaults

final class TestDefaults: NSObject, Defaulting {

var dict: [Defaults.AnyKey: any DefaultsSerializable] = [:]
var dict: [Defaults._AnyKey: AnyObject] = [:]

func setup() {
self[.categoriesToLanguages] = [:]
Expand All @@ -29,13 +29,13 @@ final class TestDefaults: NSObject, Defaulting {
self[.libraryLanguageCodes] = Set<String>()
}

subscript<Value>(key: Defaults.Key<Value>) -> Value where Value: DefaultsSerializable {
subscript<Value>(key: Defaults.Key<Value>) -> Value {
get {
// swiftlint:disable:next force_cast
dict[key] as! Value
}
set {
dict[key] = newValue
dict[key] = newValue as AnyObject
}
}
}
28 changes: 22 additions & 6 deletions ViewModel/BrowserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,33 @@ final class BrowserViewModel: NSObject, ObservableObject,

@MainActor
func updateLastOpened() {
guard let tab = try? Database.shared.viewContext.existingObject(with: tabID) as? Tab else { return }
tab.lastOpened = Date()
let currentTabID = tabID
Task {
Database.shared.performBackgroundTask { context in
guard let tab = try? context.existingObject(with: currentTabID) as? Tab else {
return
}
tab.lastOpened = Date()
try? context.save()
}
}
}

@MainActor
func persistState() {
guard let tab = try? Database.shared.viewContext.existingObject(with: tabID) as? Tab else {
return
let webData = webView.interactionState as? Data
let currentTabID = tabID
Task {
Database.shared.performBackgroundTask { context in
guard let tab = try? context.existingObject(with: currentTabID) as? Tab else {
return
}
tab.interactionState = webData
if context.hasChanges {
try? context.save()
}
}
}
tab.interactionState = webView.interactionState as? Data
try? Database.shared.viewContext.save()
}

// MARK: - Content Loading
Expand Down
3 changes: 2 additions & 1 deletion ViewModel/NavigationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ final class NavigationViewModel: ObservableObject {
/// Delete a single tab, and select another tab
/// - Parameter tabID: ID of the tab to delete
func deleteTab(tabID: NSManagedObjectID) {
let currentItemValue = currentItem
Database.shared.performBackgroundTask { context in
let sortByCreation = [NSSortDescriptor(key: "created", ascending: false)]
guard let tabs: [Tab] = try? context.fetch(Tab.fetchRequest(predicate: nil,
Expand All @@ -105,7 +106,7 @@ final class NavigationViewModel: ObservableObject {
return
}
let newlySelectedTab: Tab?
if case let .tab(selectedTabID) = self.currentItem, selectedTabID == tabID {
if case let .tab(selectedTabID) = currentItemValue, selectedTabID == tabID {
// select a closeBy tab if the currently selected tab is to be deleted
newlySelectedTab = tabs.closeBy(toWhere: { $0.objectID == tabID }) ?? Self.makeTab(context: context)
} else if tabs.count == 1 {
Expand Down
6 changes: 4 additions & 2 deletions ViewModel/SearchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ final class SearchViewModel: NSObject, ObservableObject, NSFetchedResultsControl
@Published private(set) var zimFiles: [UUID: ZimFile] // ID of zim files that are included in search
@Published private(set) var inProgress = false
@Published private(set) var results = [SearchResult]()

static let shared = SearchViewModel()

private let fetchedResultsController: NSFetchedResultsController<ZimFile>
private var searchSubscriber: AnyCancellable?
@ZimActor
private let queue = OperationQueue()

override init() {
override private init() {
// initialize fetched results controller
let predicate = NSPredicate(format: "includedInSearch == true AND fileURLBookmark != nil")
fetchedResultsController = NSFetchedResultsController(
Expand All @@ -39,7 +41,7 @@ final class SearchViewModel: NSObject, ObservableObject, NSFetchedResultsControl
cacheName: nil
)

// initilze zim file IDs
// initialize zim file IDs
try? fetchedResultsController.performFetch()
zimFiles = fetchedResultsController.fetchedObjects?.reduce(into: [:]) { result, zimFile in
result?[zimFile.fileID] = zimFile
Expand Down
6 changes: 3 additions & 3 deletions Views/Bookmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Bookmarks: View {

var body: some View {
LazyVGrid(columns: ([gridItem]), spacing: 12) {
ForEach(bookmarks) { bookmark in
ForEach(bookmarks, id: \.self) { bookmark in
Button {
NotificationCenter.openURL(bookmark.articleURL)
if horizontalSizeClass == .compact {
Expand All @@ -54,8 +54,8 @@ struct Bookmarks: View {
Message(text: LocalString.bookmark_overlay_empty_title)
}
}
#if os(iOS)
.toolbar {
#if os(iOS)
ToolbarItem(placement: .navigationBarLeading) {
if #unavailable(iOS 16), horizontalSizeClass == .regular {
Button {
Expand All @@ -65,8 +65,8 @@ struct Bookmarks: View {
}
}
}
#endif
}
#endif
}

private var gridItem: GridItem {
Expand Down
2 changes: 1 addition & 1 deletion Views/BrowserTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct BrowserTab: View {
@Environment(\.scenePhase) private var scenePhase
@EnvironmentObject private var browser: BrowserViewModel
@EnvironmentObject private var library: LibraryViewModel
@StateObject private var search = SearchViewModel()
@StateObject private var search = SearchViewModel.shared

var body: some View {
let model = if FeatureFlags.hasLibrary {
Expand Down
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ settings:
packages:
Defaults:
url: /~https://github.com/sindresorhus/Defaults
majorVersion: 6.0.0
majorVersion: 8.2.0
StripeApplePay:
url: /~https://github.com/CodeLikeW/stripe-apple-pay
majorVersion: 24.0.0
Expand Down

0 comments on commit 0ef04dc

Please sign in to comment.