Skip to content

Commit 5045eed

Browse files
authored
Merge pull request #11 from Kyome22/fix-sonoma-issue
Fix Sonoma issue
2 parents 06bf348 + 31d154c commit 5045eed

10 files changed

Lines changed: 113 additions & 70 deletions

File tree

ShiftWindow.xcodeproj/xcshareddata/xcschemes/ShiftWindow.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "DomainTests"
36+
BuildableName = "DomainTests"
37+
BlueprintName = "DomainTests"
38+
ReferencedContainer = "container:ShiftWindowPackages">
39+
</BuildableReference>
40+
</TestableReference>
3141
</Testables>
3242
</TestAction>
3343
<LaunchAction

ShiftWindowPackages/Sources/Domain/AppDelegate.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
4141
}
4242

4343
public func applicationWillTerminate(_ notification: Notification) {
44-
let executeClient = appDependencies.executeClient
45-
Task.detached(priority: .background) {
46-
if try executeClient.checkIconsVisible() {
47-
try executeClient.toggleIconsVisible(false)
48-
}
44+
if (try? appDependencies.executeClient.checkIconsVisible()) == true {
45+
try? appDependencies.executeClient.toggleIconsVisible(false)
4946
}
5047
}
5148
}

ShiftWindowPackages/Sources/Domain/ViewModel/GeneralSettingsViewModel.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ import Observation
2828
private let launchAtLoginRepository: LaunchAtLoginRepository
2929
private let logService: LogService
3030

31-
public var launchAtLoginIsEnabled: Bool
32-
public var checkForUpdatesIsEnabled: Bool {
33-
didSet { checkForUpdatesRepository.switchStatus(checkForUpdatesIsEnabled) }
34-
}
31+
public var launchAtLogin: Bool
32+
public var checkForUpdates: Bool
3533

3634
public init(
3735
_ nsWorkspaceClient: NSWorkspaceClient,
@@ -43,8 +41,8 @@ import Observation
4341
self.checkForUpdatesRepository = .init(spuUpdaterClient)
4442
self.launchAtLoginRepository = .init(smAppServiceClient)
4543
self.logService = logService
46-
launchAtLoginIsEnabled = launchAtLoginRepository.isEnabled
47-
checkForUpdatesIsEnabled = checkForUpdatesRepository.isEnabled
44+
launchAtLogin = launchAtLoginRepository.isEnabled
45+
checkForUpdates = checkForUpdatesRepository.isEnabled
4846
}
4947

5048
public func onAppear(screenName: String) {
@@ -56,12 +54,17 @@ import Observation
5654
_ = nsWorkspaceClient.open(URL(string: path)!)
5755
}
5856

59-
public func launchAtLoginSwitched(_ isEnabled: Bool) {
60-
switch launchAtLoginRepository.switchStatus(isEnabled) {
57+
public func toggleLaunchAtLogin(_ isOn: Bool) {
58+
switch launchAtLoginRepository.switchStatus(isOn) {
6159
case .success:
62-
launchAtLoginIsEnabled = isEnabled
60+
launchAtLogin = isOn
6361
case let .failure(.switchFailed(value)):
64-
launchAtLoginIsEnabled = value
62+
launchAtLogin = value
6563
}
6664
}
65+
66+
public func toggleCheckForUpdates(_ isOn: Bool) {
67+
checkForUpdates = isOn
68+
checkForUpdatesRepository.switchStatus(isOn)
69+
}
6770
}

ShiftWindowPackages/Sources/Domain/ViewModel/MenuViewModel.swift

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ import Observation
3333
@ObservationIgnored private var task: Task<Void, Never>?
3434

3535
public var patterns = [ShiftPattern]()
36-
public var hideIcons: Bool {
37-
didSet { toggleIconsVisible(hideIcons) }
38-
}
36+
public var hideIcons: Bool
3937
public var canChecksForUpdates = false
4038

4139
public init(
@@ -53,32 +51,28 @@ import Observation
5351
self.shortcutService = shortcutService
5452
self.updateService = updateService
5553
hideIcons = (try? executeClient.checkIconsVisible()) ?? false
56-
}
57-
58-
public func onAppear(screenName: String) {
59-
logService.notice(.screenView(name: screenName))
60-
task = Task {
54+
task = Task { [shortcutService, updateService] in
6155
await withTaskGroup(of: Void.self) { group in
62-
group.addTask {
63-
for await value in await self.shortcutService.patternsStream() {
64-
await MainActor.run {
65-
self.patterns = value
66-
}
56+
group.addTask { [weak self] in
57+
for await value in await shortcutService.patternsStream() {
58+
await self?.updatePatterns(value)
6759
}
6860
}
69-
group.addTask {
70-
for await value in await self.updateService.canChecksForUpdatesStream() {
71-
await MainActor.run {
72-
self.canChecksForUpdates = value
73-
}
61+
group.addTask { [weak self] in
62+
for await value in await updateService.canChecksForUpdatesStream() {
63+
await self?.updateCanChecksForUpdates(value)
7464
}
7565
}
7666
}
7767
}
7868
}
7969

80-
public func onDisappear() {
81-
task?.cancel()
70+
private func updatePatterns(_ patterns: [ShiftPattern]) {
71+
self.patterns = patterns
72+
}
73+
74+
private func updateCanChecksForUpdates(_ canChecksForUpdates: Bool) {
75+
self.canChecksForUpdates = canChecksForUpdates
8276
}
8377

8478
public func shiftWindow(shiftType: ShiftType) async {
@@ -102,13 +96,12 @@ import Observation
10296
nsAppClient.terminate(nil)
10397
}
10498

105-
func toggleIconsVisible(_ value: Bool) {
106-
Task.detached(priority: .background) {
107-
do {
108-
try self.executeClient.toggleIconsVisible(value)
109-
} catch {
110-
self.logService.critical(.failedExecuteScript(error))
111-
}
99+
public func toggleIconsVisible(_ value: Bool) {
100+
do {
101+
try executeClient.toggleIconsVisible(value)
102+
hideIcons = value
103+
} catch {
104+
logService.critical(.failedExecuteScript(error))
112105
}
113106
}
114107
}

ShiftWindowPackages/Sources/Domain/ViewModel/ShortcutSettingsViewModel.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ import SpiceKey
3131
@ObservationIgnored private var task: Task<Void, Never>?
3232

3333
public var patterns: [ShiftPattern]
34-
public var showShortcutPanel: Bool {
35-
didSet { userDefaultsRepository.showShortcutPanel = showShortcutPanel }
36-
}
34+
public var showShortcutPanel: Bool
3735

3836
public init(
3937
_ userDefaultsClient: UserDefaultsClient,
@@ -49,9 +47,9 @@ import SpiceKey
4947

5048
public func onAppear(screenName: String) {
5149
logService.notice(.screenView(name: screenName))
52-
task = Task {
50+
task = Task { [weak self, shortcutService] in
5351
for await patterns in await shortcutService.patternsStream() {
54-
self.patterns = patterns
52+
self?.patterns = patterns
5553
}
5654
}
5755
}
@@ -68,4 +66,9 @@ import SpiceKey
6866
await shortcutService.removeShortcut(id: id)
6967
}
7068
}
69+
70+
public func toggleShowShortcutPanel(_ isOn: Bool) {
71+
showShortcutPanel = isOn
72+
userDefaultsRepository.showShortcutPanel = isOn
73+
}
7174
}

ShiftWindowPackages/Sources/Presentation/View/GeneralSettingsView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ struct GeneralSettingsView: View {
3838
Form {
3939
LabeledContent {
4040
Toggle(isOn: Binding<Bool>(
41-
get: { viewModel.launchAtLoginIsEnabled },
42-
set: { viewModel.launchAtLoginSwitched($0) }
41+
get: { viewModel.launchAtLogin },
42+
set: { viewModel.toggleLaunchAtLogin($0) }
4343
)) {
4444
Text("AutomaticallyLaunchAtLogin", bundle: .module)
4545
}
4646
} label: {
4747
Text("launch", bundle: .module)
4848
}
4949
LabeledContent {
50-
Toggle(isOn: $viewModel.checkForUpdatesIsEnabled) {
50+
Toggle(isOn: Binding<Bool>(
51+
get: { viewModel.checkForUpdates },
52+
set: { viewModel.toggleCheckForUpdates($0) }
53+
)) {
5154
Text("AutomaticallyCheckForUpdates", bundle: .module)
5255
}
5356
} label: {

ShiftWindowPackages/Sources/Presentation/View/MenuView.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ struct MenuView: View {
5858
}
5959
}
6060
Divider()
61-
Toggle(isOn: $viewModel.hideIcons) {
61+
Toggle(isOn: Binding<Bool>(
62+
get: { viewModel.hideIcons },
63+
set: { viewModel.toggleIconsVisible($0) }
64+
)) {
6265
Text("hideDesktopIcons", bundle: .module)
6366
}
6467
Divider()
@@ -88,12 +91,6 @@ struct MenuView: View {
8891
Text("terminateApp", bundle: .module)
8992
}
9093
}
91-
.onAppear {
92-
viewModel.onAppear(screenName: String(describing: Self.self))
93-
}
94-
.onDisappear {
95-
viewModel.onDisappear()
96-
}
9794
}
9895
}
9996

ShiftWindowPackages/Sources/Presentation/View/ShortcutSettingsView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ struct ShortcutSettingsView: View {
6161
}
6262
Divider()
6363
LabeledContent {
64-
Toggle(isOn: $viewModel.showShortcutPanel) {
64+
Toggle(isOn: Binding<Bool>(
65+
get: { viewModel.showShortcutPanel },
66+
set: { viewModel.toggleShowShortcutPanel($0) }
67+
)) {
6568
Text("enable", bundle: .module)
6669
}
6770
} label: {

ShiftWindowPackages/Tests/DomainTests/ViewModelTests/GeneralSettingsViewModelTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,48 @@ final class GeneralSettingsViewModelTests: XCTestCase {
2323
}
2424

2525
@MainActor
26-
func test_launchAtLoginSwitched_有効の要求_成功した_有効に変更される() async {
26+
func test_toggleLaunchAtLogin_有効の要求_成功した_有効に変更される() async {
2727
let currentStatus = OSAllocatedUnfairLock(initialState: SMAppService.Status.notRegistered)
2828
let smAppServiceClient = testDependency(of: SMAppServiceClient.self) {
2929
$0.status = { currentStatus.withLock(\.self) }
3030
$0.register = { currentStatus.withLock { $0 = .enabled } }
3131
}
3232
let sut = GeneralSettingsViewModel(.testValue, .testValue, smAppServiceClient, .init(.testValue))
33-
sut.launchAtLoginSwitched(true)
34-
XCTAssertTrue(sut.launchAtLoginIsEnabled)
33+
sut.toggleLaunchAtLogin(true)
34+
XCTAssertTrue(sut.launchAtLogin)
3535
}
3636

3737
@MainActor
38-
func test_launchAtLoginSwitched_有効の要求_失敗した_無効に戻される() async {
38+
func test_toggleLaunchAtLogin_有効の要求_失敗した_無効に戻される() async {
3939
let smAppServiceClient = testDependency(of: SMAppServiceClient.self) {
4040
$0.status = { .notRegistered }
4141
$0.register = { throw NSError(domain: "", code: kSMErrorInternalFailure) }
4242
}
4343
let sut = GeneralSettingsViewModel(.testValue, .testValue, smAppServiceClient, .init(.testValue))
44-
sut.launchAtLoginSwitched(true)
45-
XCTAssertFalse(sut.launchAtLoginIsEnabled)
44+
sut.toggleLaunchAtLogin(true)
45+
XCTAssertFalse(sut.launchAtLogin)
4646
}
4747

4848
@MainActor
49-
func test_launchAtLoginSwitched_無効の要求_成功した_無効に変更される() async {
49+
func test_toggleLaunchAtLogin_無効の要求_成功した_無効に変更される() async {
5050
let currentStatus = OSAllocatedUnfairLock(initialState: SMAppService.Status.enabled)
5151
let smAppServiceClient = testDependency(of: SMAppServiceClient.self) {
5252
$0.status = { currentStatus.withLock(\.self) }
5353
$0.unregister = { currentStatus.withLock { $0 = .notRegistered } }
5454
}
5555
let sut = GeneralSettingsViewModel(.testValue, .testValue, smAppServiceClient, .init(.testValue))
56-
sut.launchAtLoginSwitched(false)
57-
XCTAssertFalse(sut.launchAtLoginIsEnabled)
56+
sut.toggleLaunchAtLogin(false)
57+
XCTAssertFalse(sut.launchAtLogin)
5858
}
5959

6060
@MainActor
61-
func test_launchAtLoginSwitched_無効の要求_失敗した_有効に戻される() async {
61+
func test_toggleLaunchAtLogin_無効の要求_失敗した_有効に戻される() async {
6262
let smAppServiceClient = testDependency(of: SMAppServiceClient.self) {
6363
$0.status = { .enabled }
6464
$0.unregister = { throw NSError(domain: "", code: kSMErrorInternalFailure) }
6565
}
6666
let sut = GeneralSettingsViewModel(.testValue, .testValue, smAppServiceClient, .init(.testValue))
67-
sut.launchAtLoginSwitched(false)
68-
XCTAssertTrue(sut.launchAtLoginIsEnabled)
67+
sut.toggleLaunchAtLogin(false)
68+
XCTAssertTrue(sut.launchAtLogin)
6969
}
7070
}

ShiftWindowPackages/Tests/DomainTests/ViewModelTests/MenuViewModelTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,38 @@ final class MenuViewModelTests: XCTestCase {
8383
sut.terminateApp()
8484
XCTAssertEqual(count.withLock(\.self), 1)
8585
}
86+
87+
@MainActor
88+
func test_toggleIconsVisible_変更の要求_成功した_変更される() {
89+
let executeClient = testDependency(of: ExecuteClient.self) {
90+
$0.toggleIconsVisible = { _ in }
91+
}
92+
let sut = MenuViewModel(
93+
executeClient,
94+
.testValue,
95+
.init(.testValue),
96+
.init(.testValue, .testValue, .testValue, .testValue, .testValue),
97+
.init(.testValue, .testValue, .testValue),
98+
.init(.testValue)
99+
)
100+
sut.toggleIconsVisible(true)
101+
XCTAssertTrue(sut.hideIcons)
102+
}
103+
104+
@MainActor
105+
func test_toggleIconsVisible_変更の要求_失敗した_変更されない() {
106+
let executeClient = testDependency(of: ExecuteClient.self) {
107+
$0.toggleIconsVisible = { _ in throw CocoaError(.fileNoSuchFile) }
108+
}
109+
let sut = MenuViewModel(
110+
executeClient,
111+
.testValue,
112+
.init(.testValue),
113+
.init(.testValue, .testValue, .testValue, .testValue, .testValue),
114+
.init(.testValue, .testValue, .testValue),
115+
.init(.testValue)
116+
)
117+
sut.toggleIconsVisible(true)
118+
XCTAssertFalse(sut.hideIcons)
119+
}
86120
}

0 commit comments

Comments
 (0)