Skip to content

Commit

Permalink
Refactor UserDefaults extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jakob committed Oct 27, 2016
1 parent 0da8860 commit 368bc8a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
8 changes: 8 additions & 0 deletions Postgres.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
360E961C1DC20D5E005D8C69 /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 360E961B1DC20D5E005D8C69 /* UserDefaults.swift */; };
360E961D1DC20EF2005D8C69 /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 360E961B1DC20D5E005D8C69 /* UserDefaults.swift */; };
360E961E1DC20EF3005D8C69 /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 360E961B1DC20D5E005D8C69 /* UserDefaults.swift */; };
830658E51D536F2E00BA1752 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830658E41D536F2E00BA1752 /* Extensions.swift */; };
830B4B261D1C132300F16762 /* ServerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830B4B251D1C132300F16762 /* ServerManager.swift */; };
830B4B281D1C289A00F16762 /* Sidebar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830B4B271D1C289A00F16762 /* Sidebar.swift */; };
Expand Down Expand Up @@ -90,6 +93,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
360E961B1DC20D5E005D8C69 /* UserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaults.swift; sourceTree = "<group>"; };
830658E41D536F2E00BA1752 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
830B4B251D1C132300F16762 /* ServerManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerManager.swift; sourceTree = "<group>"; };
830B4B271D1C289A00F16762 /* Sidebar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Sidebar.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -230,6 +234,7 @@
83F3647D1D5204D30096F1F5 /* SettingsWindow.swift */,
8358D53B1D26EBE7007B7904 /* Server.swift */,
830B4B251D1C132300F16762 /* ServerManager.swift */,
360E961B1DC20D5E005D8C69 /* UserDefaults.swift */,
830658E41D536F2E00BA1752 /* Extensions.swift */,
83A544E21D6682C700603068 /* KeyValueObserver.swift */,
830D62CB1D77297800570BFC /* Preferences.swift */,
Expand Down Expand Up @@ -428,6 +433,7 @@
files = (
83999C6A1D7DA8DF00B8A1D6 /* Server.swift in Sources */,
83999C6D1D7DA9B000B8A1D6 /* ErrorRecoveryAttempter.m in Sources */,
360E961D1DC20EF2005D8C69 /* UserDefaults.swift in Sources */,
83999C6B1D7DA8E300B8A1D6 /* ServerManager.swift in Sources */,
83C19C2D1D7D977000F4862D /* main.swift in Sources */,
83999C6C1D7DA8EA00B8A1D6 /* Extensions.swift in Sources */,
Expand All @@ -443,6 +449,7 @@
83C19C691D7D9D1E00F4862D /* ServerManager.swift in Sources */,
83C19C5C1D7D9AAE00F4862D /* MenuItemView.swift in Sources */,
83C19C681D7D9D1900F4862D /* Server.swift in Sources */,
360E961E1DC20EF3005D8C69 /* UserDefaults.swift in Sources */,
8348A1F21DB52E9A0039A024 /* ValueTransformers.swift in Sources */,
83C19C6C1D7D9D2E00F4862D /* KeyValueObserver.swift in Sources */,
83C19C511D7D9A7D00F4862D /* AppDelegate.swift in Sources */,
Expand All @@ -458,6 +465,7 @@
839A38281D88494A00E9591A /* main.swift in Sources */,
8353FBE01D1D5E4C002F77E7 /* SplitView.swift in Sources */,
83969DC31D36A3EA005B9F1C /* ClientLauncher.applescript in Sources */,
360E961C1DC20D5E005D8C69 /* UserDefaults.swift in Sources */,
830B4B281D1C289A00F16762 /* Sidebar.swift in Sources */,
83FB573C1D1B18C900A903A3 /* MainWindow.swift in Sources */,
83F3647E1D5204D30096F1F5 /* SettingsWindow.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Postgres/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SUUpdaterDelegate {


func applicationWillFinishLaunching(_ notification: Notification) {
UserDefaults.standard.register(defaults: ["ClientAppName": "Terminal"])
UserDefaults.standard.registerPostgresDefaults()
}

func applicationDidFinishLaunching(_ notification: Notification) {
Expand Down
13 changes: 1 addition & 12 deletions Postgres/ServerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ServerManager: NSObject {

func loadServers() {
servers.removeAll()
guard let plists = UserDefaults.shared().array(forKey: "Servers") as? [[AnyHashable: Any]] else {
guard let plists = UserDefaults.shared.array(forKey: "Servers") as? [[AnyHashable: Any]] else {
NSLog("PostgresApp could not load servers from user defaults.")
return
}
Expand Down Expand Up @@ -95,14 +95,3 @@ class ServerManager: NSObject {

}



extension UserDefaults {
static func shared() -> UserDefaults {
if Bundle.main.bundleIdentifier == "com.postgresapp.Postgres2" {
return UserDefaults.standard
} else {
return UserDefaults(suiteName: "com.postgresapp.Postgres2")!
}
}
}
23 changes: 23 additions & 0 deletions Postgres/UserDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// UserDefaults.swift
// Postgres
//
// Created by Jakob Egger on 27.10.16.
// Copyright © 2016 postgresapp. All rights reserved.
//

import Cocoa

extension UserDefaults {
// @nonobjc is necessary to prevent the following compiler error (which might be a bug):
// A declaration cannot be both 'final' and 'dynamic'
@nonobjc static var shared: UserDefaults = {
let sharedDefaults = Bundle.main.bundleIdentifier == "com.postgresapp.Postgres2" ? UserDefaults.standard : UserDefaults(suiteName: "com.postgresapp.Postgres2")!
sharedDefaults.registerPostgresDefaults()
return sharedDefaults
}()

func registerPostgresDefaults() {
self.register(defaults: ["ClientAppName": "Terminal"])
}
}

0 comments on commit 368bc8a

Please sign in to comment.