Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Oct 10, 2024
1 parent 588031d commit ebd7b9b
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 154 deletions.
File renamed without changes.
3 changes: 0 additions & 3 deletions Ice/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

// Allow the app to set the cursor in the background.
appState.setsCursorInBackground = true

// Set up the shared screen state manager.
ScreenStateManager.setUpSharedManager()
}

func applicationDidFinishLaunching(_ notification: Notification) {
Expand Down
2 changes: 1 addition & 1 deletion Ice/Main/IceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct IceApp: App {

init() {
NSSplitViewItem.swizzle()
MigrationManager(appState: appState).migrateAll()
MigrationManager.migrateAll(appState: appState)
appDelegate.assignAppState(appState)
}

Expand Down
24 changes: 13 additions & 11 deletions Ice/Utilities/MigrationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ struct MigrationManager {

extension MigrationManager {
/// Performs all migrations.
func migrateAll() {
static func migrateAll(appState: AppState) {
let manager = MigrationManager(appState: appState)

do {
try performAll(blocks: [
migrate0_8_0,
migrate0_10_0,
manager.migrate0_8_0,
manager.migrate0_10_0,
])
} catch {
logError(error)
}

let results = [
migrate0_10_1(),
manager.migrate0_10_1(),
]

for result in results {
Expand All @@ -40,7 +42,7 @@ extension MigrationManager {
}
}

private func logError(_ error: any Error) {
private static func logError(_ error: any Error) {
Logger.migration.error("Migration failed with error: \(error)")
}
}
Expand All @@ -54,7 +56,7 @@ extension MigrationManager {
guard !Defaults.bool(forKey: .hasMigrated0_8_0) else {
return
}
try performAll(blocks: [
try MigrationManager.performAll(blocks: [
migrateHotkeys0_8_0,
migrateControlItems0_8_0,
migrateSections0_8_0,
Expand Down Expand Up @@ -149,7 +151,7 @@ extension MigrationManager {

// migrate the old autosave name to the new autosave name in UserDefaults
StatusItemDefaults.migrate(key: .preferredPosition, from: autosaveName, to: identifier)
StatusItemDefaults.migrate(key: .isVisible, from: autosaveName, to: identifier)
StatusItemDefaults.migrate(key: .visible, from: autosaveName, to: identifier)

// replace the old "controlItem" dictionary with the new one
sectionDict["controlItem"] = controlItemDict
Expand Down Expand Up @@ -181,7 +183,7 @@ extension MigrationManager {
guard !Defaults.bool(forKey: .hasMigrated0_10_0) else {
return
}
try performAll(blocks: [
try MigrationManager.performAll(blocks: [
migrateControlItems0_10_0,
])
Defaults.set(true, forKey: .hasMigrated0_10_0)
Expand Down Expand Up @@ -223,12 +225,12 @@ extension MigrationManager {

for identifier in ControlItem.Identifier.allCases {
if
StatusItemDefaults[.isVisible, identifier.rawValue] == false,
StatusItemDefaults[.visible, identifier.rawValue] == false,
StatusItemDefaults[.preferredPosition, identifier.rawValue] == nil
{
needsResetPreferredPositions = true
}
StatusItemDefaults[.isVisible, identifier.rawValue] = nil
StatusItemDefaults[.visible, identifier.rawValue] = nil
}

if needsResetPreferredPositions {
Expand All @@ -252,7 +254,7 @@ extension MigrationManager {
extension MigrationManager {
/// Performs every block in the given array, catching any thrown
/// errors and rethrowing them as a combined error.
private func performAll(blocks: [() throws -> Void]) throws {
private static func performAll(blocks: [() throws -> Void]) throws {
let results = blocks.map { block in
Result(catching: block)
}
Expand Down
27 changes: 0 additions & 27 deletions Ice/Utilities/ScreenState/ScreenState.swift

This file was deleted.

59 changes: 0 additions & 59 deletions Ice/Utilities/ScreenState/ScreenStateManager.swift

This file was deleted.

58 changes: 58 additions & 0 deletions Ice/Utilities/StatusItemDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// StatusItemDefaults.swift
// Ice
//

import Cocoa

// MARK: - StatusItemDefaults

/// Proxy getters and setters for a status item's user defaults values.
enum StatusItemDefaults {
/// Accesses the value associated with the specified key and autosave name.
static subscript<Value>(key: Key<Value>, autosaveName: String) -> Value? {
get {
let stringKey = key.stringKey(for: autosaveName)
return UserDefaults.standard.object(forKey: stringKey) as? Value
}
set {
let stringKey = key.stringKey(for: autosaveName)
return UserDefaults.standard.set(newValue, forKey: stringKey)
}
}

/// Migrates the given status item defaults key from an old autosave name
/// to a new autosave name.
static func migrate<Value>(key: Key<Value>, from oldAutosaveName: String, to newAutosaveName: String) {
guard newAutosaveName != oldAutosaveName else {
return
}
Self[key, newAutosaveName] = Self[key, oldAutosaveName]
Self[key, oldAutosaveName] = nil
}
}

// MARK: - StatusItemDefaults.Key

extension StatusItemDefaults {
/// Keys used to look up user defaults values for status items.
struct Key<Value> {
/// The raw value of the key.
let rawValue: String

/// Returns the full string key for the given autosave name.
func stringKey(for autosaveName: String) -> String {
return "NSStatusItem \(rawValue) \(autosaveName)"
}
}
}

extension StatusItemDefaults.Key<CGFloat> {
/// String key: "NSStatusItem Preferred Position autosaveName"
static let preferredPosition = Self(rawValue: "Preferred Position")
}

extension StatusItemDefaults.Key<Bool> {
/// String key: "NSStatusItem Visible autosaveName"
static let visible = Self(rawValue: "Visible")
}
34 changes: 0 additions & 34 deletions Ice/Utilities/StatusItemDefaults/StatusItemDefaults.swift

This file was deleted.

19 changes: 0 additions & 19 deletions Ice/Utilities/StatusItemDefaults/StatusItemDefaultsKey.swift

This file was deleted.

0 comments on commit ebd7b9b

Please sign in to comment.