MastoSearch/MastoSearch/WindowController.swift

59 lines
1.6 KiB
Swift

//
// WindowController.swift
// MastoSearch
//
// Created by Shadowfacts on 12/14/21.
//
import Cocoa
import Combine
import MastoSearchCore
class WindowController: NSWindowController {
@IBOutlet weak var searchField: NSSearchField!
private var querySubject = PassthroughSubject<String, Never>()
private var cancellables = Set<AnyCancellable>()
private var viewController: ViewController {
contentViewController as! ViewController
}
override func windowDidLoad() {
super.windowDidLoad()
querySubject
.debounce(for: .seconds(0.5), scheduler: RunLoop.main)
.sink { [unowned self] (val) in
self.viewController.search(val)
}
.store(in: &cancellables)
SyncController.shared.onSync
.sink { [unowned self] in self.updateSubtitle() }
.store(in: &cancellables)
DatabaseController.shared.onInitialize
.sink { [unowned self] in self.updateSubtitle() }
.store(in: &cancellables)
}
private func updateSubtitle() {
guard let window = window,
DatabaseController.shared.isInitialized else {
return
}
window.subtitle = "\(DatabaseController.shared.countStatuses().formatted(.number.grouping(.automatic))) statuses"
}
@IBAction func searchFieldTextChanged(_ sender: Any) {
querySubject.send(searchField.stringValue)
}
@IBAction func searchMenuItemActivated(_ sender: Any) {
searchField.becomeFirstResponder()
}
}