diff --git a/ShiftWindow/Data/Entity/ShiftPattern.swift b/ShiftWindow/Data/Entity/ShiftPattern.swift index f3f47f9..9af29ac 100644 --- a/ShiftWindow/Data/Entity/ShiftPattern.swift +++ b/ShiftWindow/Data/Entity/ShiftPattern.swift @@ -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 diff --git a/ShiftWindow/Domain/Model/ShortcutModel.swift b/ShiftWindow/Domain/Model/ShortcutModel.swift index c09d14b..aa0633f 100644 --- a/ShiftWindow/Domain/Model/ShortcutModel.swift +++ b/ShiftWindow/Domain/Model/ShortcutModel.swift @@ -25,7 +25,7 @@ import SpiceKey protocol ShortcutModel: AnyObject { var showPanelPublisher: AnyPublisher { get } var fadeOutPanelPublisher: AnyPublisher { get } - var updatePatternsPublisher: AnyPublisher { get } + var patternsPublisher: AnyPublisher<[ShiftPattern], Never> { get } func initializeShortcuts() func updateShortcut(id: String, keyCombo: KeyCombination) @@ -42,9 +42,9 @@ final class ShortcutModelImpl { return fadeOutPanelSubject.eraseToAnyPublisher() } - private let updatePatternsSubject = PassthroughSubject() - var updatePatternsPublisher: AnyPublisher { - return updatePatternsSubject.eraseToAnyPublisher() + private let patternsSubject = CurrentValueSubject<[ShiftPattern], Never>([]) + var patternsPublisher: AnyPublisher<[ShiftPattern], Never> { + return patternsSubject.eraseToAnyPublisher() } private let userDefaultsRepository: UR @@ -53,10 +53,11 @@ final class ShortcutModelImpl 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) @@ -79,18 +84,18 @@ final class ShortcutModelImpl { Just(()).eraseToAnyPublisher() } - var updatePatternsPublisher: AnyPublisher { - Just(()).eraseToAnyPublisher() + var patternsPublisher: AnyPublisher<[ShiftPattern], Never> { + Just([]).eraseToAnyPublisher() } func initializeShortcuts() {} diff --git a/ShiftWindow/Domain/ShiftWindowAppModel.swift b/ShiftWindow/Domain/ShiftWindowAppModel.swift index cc94e9d..669d515 100644 --- a/ShiftWindow/Domain/ShiftWindowAppModel.swift +++ b/ShiftWindow/Domain/ShiftWindowAppModel.swift @@ -39,7 +39,7 @@ final class ShiftWindowAppModelImpl: NSObject, ShiftWindowAppModel { typealias SCM = ShortcutModelImpl typealias SCMConcrete = SCM typealias WMConcrete = WindowModelImpl - typealias MMConcrete = MenuBarModelImpl + typealias MMConcrete = MenuBarModelImpl @Published var settingsTab: SettingsTabType = .general @@ -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) diff --git a/ShiftWindow/Domain/ViewModel/MenuBarModel.swift b/ShiftWindow/Domain/ViewModel/MenuBarModel.swift index f0e9b76..25fe836 100644 --- a/ShiftWindow/Domain/ViewModel/MenuBarModel.swift +++ b/ShiftWindow/Domain/ViewModel/MenuBarModel.swift @@ -22,9 +22,8 @@ import AppKit import Combine protocol MenuBarModel: AnyObject { - var updateMenuItemsPublisher: AnyPublisher { get } + var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> { get } var resetIconsVisiblePublisher: AnyPublisher { get } - var patterns: [ShiftPattern] { get } func shiftWindow(shiftType: ShiftType) func toggleIconsVisible(flag: Bool) @@ -34,40 +33,33 @@ protocol MenuBarModel: AnyObject { func terminateApp() } -final class MenuBarModelImpl: NSObject, MenuBarModel { - private let updateMenuItemsSubject = PassthroughSubject() - var updateMenuItemsPublisher: AnyPublisher { + private let updateMenuItemsSubject = CurrentValueSubject<[ShiftPattern], Never>([]) + var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> { return updateMenuItemsSubject.eraseToAnyPublisher() } private let resetIconsVisibleSubject = PassthroughSubject() var resetIconsVisiblePublisher: AnyPublisher { 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() 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) } @@ -107,13 +99,12 @@ final class MenuBarModelImpl { - Just(()).eraseToAnyPublisher() + var updateMenuItemsPublisher: AnyPublisher<[ShiftPattern], Never> { + Just(ShiftPattern.defaults).eraseToAnyPublisher() } var resetIconsVisiblePublisher: AnyPublisher { Just(()).eraseToAnyPublisher() } - let patterns: [ShiftPattern] = ShiftPattern.defaults func shiftWindow(shiftType: ShiftType) {} func toggleIconsVisible(flag: Bool) {} diff --git a/ShiftWindow/Domain/ViewModel/ShortcutSettingsViewModel.swift b/ShiftWindow/Domain/ViewModel/ShortcutSettingsViewModel.swift index 8698ce3..a48d6e3 100644 --- a/ShiftWindow/Domain/ViewModel/ShortcutSettingsViewModel.swift +++ b/ShiftWindow/Domain/ViewModel/ShortcutSettingsViewModel.swift @@ -50,14 +50,12 @@ final class ShortcutSettingsViewModelImpl: 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) @@ -64,8 +63,8 @@ final class MenuBar: 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 @@ -100,9 +99,9 @@ final class MenuBar: 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) } }