Skip to content

Commit

Permalink
Fixed a bug that fails to unregister spicekey.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Jan 28, 2023
1 parent 87f4092 commit bbf9daf
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 54 deletions.
2 changes: 1 addition & 1 deletion ShiftWindow/Data/Entity/ShiftPattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AppKit
import SwiftUI
import SpiceKey

final class ShiftPattern: Codable {
struct ShiftPattern: Codable {
enum CodingKeys: String, CodingKey {
case shiftType
case spiceKeyData
Expand Down
41 changes: 23 additions & 18 deletions ShiftWindow/Domain/Model/ShortcutModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import SpiceKey
protocol ShortcutModel: AnyObject {
var showPanelPublisher: AnyPublisher<String, Never> { get }
var fadeOutPanelPublisher: AnyPublisher<Void, Never> { get }
var updatePatternsPublisher: AnyPublisher<Void, Never> { get }
var patternsPublisher: AnyPublisher<[ShiftPattern], Never> { get }

func initializeShortcuts()
func updateShortcut(id: String, keyCombo: KeyCombination)
Expand All @@ -42,9 +42,9 @@ final class ShortcutModelImpl<UR: UserDefaultsRepository,
var fadeOutPanelPublisher: AnyPublisher<Void, Never> {
return fadeOutPanelSubject.eraseToAnyPublisher()
}
private let updatePatternsSubject = PassthroughSubject<Void, Never>()
var updatePatternsPublisher: AnyPublisher<Void, Never> {
return updatePatternsSubject.eraseToAnyPublisher()
private let patternsSubject = CurrentValueSubject<[ShiftPattern], Never>([])
var patternsPublisher: AnyPublisher<[ShiftPattern], Never> {
return patternsSubject.eraseToAnyPublisher()
}

private let userDefaultsRepository: UR
Expand All @@ -53,10 +53,11 @@ final class ShortcutModelImpl<UR: UserDefaultsRepository,
init(_ userDefaultsRepository: UR, _ shiftModel: SM) {
self.userDefaultsRepository = userDefaultsRepository
self.shiftModel = shiftModel
self.patternsSubject.value = userDefaultsRepository.patterns
}

func initializeShortcuts() {
userDefaultsRepository.patterns.forEach { pattern in
patternsSubject.value.forEach { pattern in
guard let keyCombo = pattern.spiceKeyData?.keyCombination else { return }
let spiceKey = SpiceKey(keyCombo) { [weak self] in
self?.showPanelSubject.send(keyCombo.string)
Expand All @@ -69,28 +70,32 @@ final class ShortcutModelImpl<UR: UserDefaultsRepository,
}
}

private func getIndex(id: String) -> Int? {
return patternsSubject.value.firstIndex(where: { $0.shiftType.id == id })
}

func updateShortcut(id: String, keyCombo: KeyCombination) {
let patterns = userDefaultsRepository.patterns
guard let pattern = patterns.first(where: { $0.shiftType.id == id }) else { return }
guard let index = getIndex(id: id) else { return }
let pattern = patternsSubject.value[index]
let spiceKey = SpiceKey(keyCombo) { [weak self] in
self?.showPanelSubject.send(keyCombo.string)
self?.shiftModel.shiftWindow(shiftType: pattern.shiftType)
} keyUpHandler: { [weak self] in
self?.fadeOutPanelSubject.send(())
}
spiceKey.register()
pattern.spiceKeyData = SpiceKeyData(id, keyCombo.key, keyCombo.modifierFlags, spiceKey)
userDefaultsRepository.patterns = patterns
updatePatternsSubject.send(())
patternsSubject.value[index].spiceKeyData = SpiceKeyData(id,
keyCombo.key,
keyCombo.modifierFlags,
spiceKey)
userDefaultsRepository.patterns = patternsSubject.value
}

func removeShortcut(id: String) {
let patterns = userDefaultsRepository.patterns
guard let pattern = patterns.first(where: { $0.shiftType.id == id }) else { return }
pattern.spiceKeyData?.spiceKey?.unregister()
pattern.spiceKeyData = nil
userDefaultsRepository.patterns = patterns
updatePatternsSubject.send(())
guard let index = getIndex(id: id) else { return }
patternsSubject.value[index].spiceKeyData?.spiceKey?.unregister()
patternsSubject.value[index].spiceKeyData = nil
userDefaultsRepository.patterns = patternsSubject.value
}
}

Expand All @@ -103,8 +108,8 @@ extension PreviewMock {
var fadeOutPanelPublisher: AnyPublisher<Void, Never> {
Just(()).eraseToAnyPublisher()
}
var updatePatternsPublisher: AnyPublisher<Void, Never> {
Just(()).eraseToAnyPublisher()
var patternsPublisher: AnyPublisher<[ShiftPattern], Never> {
Just([]).eraseToAnyPublisher()
}

func initializeShortcuts() {}
Expand Down
4 changes: 2 additions & 2 deletions ShiftWindow/Domain/ShiftWindowAppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class ShiftWindowAppModelImpl: NSObject, ShiftWindowAppModel {
typealias SCM = ShortcutModelImpl
typealias SCMConcrete = SCM<UR, ShiftModelImpl>
typealias WMConcrete = WindowModelImpl<UR, SCMConcrete>
typealias MMConcrete = MenuBarModelImpl<UR, ShiftModelImpl, SCMConcrete, WMConcrete>
typealias MMConcrete = MenuBarModelImpl<ShiftModelImpl, SCMConcrete, WMConcrete>

@Published var settingsTab: SettingsTabType = .general

Expand All @@ -58,7 +58,7 @@ final class ShiftWindowAppModelImpl: NSObject, ShiftWindowAppModel {
shiftModel = ShiftModelImpl()
shortcutModel = SCM(userDefaultsRepository, shiftModel)
windowModel = WindowModelImpl(userDefaultsRepository, shortcutModel)
menuBarModel = MenuBarModelImpl(userDefaultsRepository, shiftModel, shortcutModel, windowModel)
menuBarModel = MenuBarModelImpl(shiftModel, shortcutModel, windowModel)
super.init()

NotificationCenter.default.publisher(for: NSApplication.didFinishLaunchingNotification)
Expand Down
27 changes: 9 additions & 18 deletions ShiftWindow/Domain/ViewModel/MenuBarModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import AppKit
import Combine

protocol MenuBarModel: AnyObject {
var updateMenuItemsPublisher: AnyPublisher<Void, Never> { get }
var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> { get }
var resetIconsVisiblePublisher: AnyPublisher<Void, Never> { get }
var patterns: [ShiftPattern] { get }

func shiftWindow(shiftType: ShiftType)
func toggleIconsVisible(flag: Bool)
Expand All @@ -34,40 +33,33 @@ protocol MenuBarModel: AnyObject {
func terminateApp()
}

final class MenuBarModelImpl<UR: UserDefaultsRepository,
SM: ShiftModel,
final class MenuBarModelImpl<SM: ShiftModel,
SCM: ShortcutModel,
WM: WindowModel>: NSObject, MenuBarModel {
private let updateMenuItemsSubject = PassthroughSubject<Void, Never>()
var updateMenuItemsPublisher: AnyPublisher<Void, Never> {
private let updateMenuItemsSubject = CurrentValueSubject<[ShiftPattern], Never>([])
var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> {
return updateMenuItemsSubject.eraseToAnyPublisher()
}
private let resetIconsVisibleSubject = PassthroughSubject<Void, Never>()
var resetIconsVisiblePublisher: AnyPublisher<Void, Never> {
return resetIconsVisibleSubject.eraseToAnyPublisher()
}
var patterns: [ShiftPattern] {
return userDefaultsRepository.patterns
}

private let userDefaultsRepository: UR
private let shiftModel: SM
private let windowModel: WM
private var cancellables = Set<AnyCancellable>()

init(
_ userDefaultsRepository: UR,
_ shiftModel: SM,
_ shortcutModel: SCM,
_ windowModel: WM
) {
self.userDefaultsRepository = userDefaultsRepository
self.shiftModel = shiftModel
self.windowModel = windowModel
super.init()
shortcutModel.updatePatternsPublisher
.sink { [weak self] in
self?.updateMenuItemsSubject.send(())
shortcutModel.patternsPublisher
.sink { [weak self] patterns in
self?.updateMenuItemsSubject.send(patterns)
}
.store(in: &cancellables)
}
Expand Down Expand Up @@ -107,13 +99,12 @@ final class MenuBarModelImpl<UR: UserDefaultsRepository,
// MARK: - Preview Mock
extension PreviewMock {
final class MenuBarModelMock: MenuBarModel {
var updateMenuItemsPublisher: AnyPublisher<Void, Never> {
Just(()).eraseToAnyPublisher()
var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> {
Just(ShiftPattern.defaults).eraseToAnyPublisher()
}
var resetIconsVisiblePublisher: AnyPublisher<Void, Never> {
Just(()).eraseToAnyPublisher()
}
let patterns: [ShiftPattern] = ShiftPattern.defaults

func shiftWindow(shiftType: ShiftType) {}
func toggleIconsVisible(flag: Bool) {}
Expand Down
10 changes: 4 additions & 6 deletions ShiftWindow/Domain/ViewModel/ShortcutSettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ final class ShortcutSettingsViewModelImpl<UR: UserDefaultsRepository,
) {
self.userDefaultsRepository = userDefaultsRepository as! UR
self.shortcutModel = shortcutModel as! SCM
patterns = userDefaultsRepository.patterns
patterns = userDefaultsRepository.patterns
showShortcutPanel = userDefaultsRepository.showShortcutPanel
shortcutModel.updatePatternsPublisher
shortcutModel.patternsPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] in
if let self {
self.patterns = self.userDefaultsRepository.patterns
}
.sink { [weak self] patterns in
self?.patterns = patterns
}
.store(in: &cancellables)
}
Expand Down
6 changes: 3 additions & 3 deletions ShiftWindow/Domain/ViewModel/WindowModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ final class WindowModelImpl<UR: UserDefaultsRepository,
self.shortcutModel = shortcutModel
super.init()

self.shortcutModel.showPanelPublisher
shortcutModel.showPanelPublisher
.sink { [weak self] keyEquivalent in
self?.showShortcutPanel(keyEquivalent: keyEquivalent)
}
.store(in: &cancellables)
self.shortcutModel.fadeOutPanelPublisher
shortcutModel.fadeOutPanelPublisher
.sink { [weak self] in
self?.shortcutPanel?.fadeOut()
}
Expand Down Expand Up @@ -84,7 +84,7 @@ final class WindowModelImpl<UR: UserDefaultsRepository,
}
}

// NSWindowDelegate
// MARK: NSWindowDelegate
func windowWillClose(_ notification: Notification) {
guard let window = notification.object as? NSWindow else { return }
if window === shortcutPanel {
Expand Down
11 changes: 5 additions & 6 deletions ShiftWindow/Presentation/View/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ final class MenuBar<MM: MenuBarModel>: NSObject {
init(menuBarModel: MM) {
self.menuBarModel = menuBarModel
super.init()

menuBarModel.patterns.forEach { pattern in
ShiftPattern.defaults.forEach { pattern in
let item = ShiftMenuItem(pattern: pattern,
action: #selector(shiftWindow(_:)),
target: self)
Expand Down Expand Up @@ -64,8 +63,8 @@ final class MenuBar<MM: MenuBarModel>: NSObject {
statusItem.menu = menu

menuBarModel.updateMenuItemsPublisher
.sink { [weak self] in
self?.updateMenuItems()
.sink { [weak self] patterns in
self?.updateMenuItems(patterns)
}
.store(in: &cancellables)
menuBarModel.resetIconsVisiblePublisher
Expand Down Expand Up @@ -100,9 +99,9 @@ final class MenuBar<MM: MenuBarModel>: NSObject {
menuBarModel.terminateApp()
}

func updateMenuItems() {
func updateMenuItems(_ patterns: [ShiftPattern]) {
let items = menu.items.compactMap { $0 as? ShiftMenuItem }
for (pattern, item) in zip(menuBarModel.patterns, items) {
for (pattern, item) in zip(patterns, items) {
item.update(pattern)
}
}
Expand Down

0 comments on commit bbf9daf

Please sign in to comment.