diff --git a/pullBar/AppDelegate.swift b/pullBar/AppDelegate.swift index e6c1312..5ecd4df 100644 --- a/pullBar/AppDelegate.swift +++ b/pullBar/AppDelegate.swift @@ -13,19 +13,7 @@ import KeychainAccess @main class AppDelegate: NSObject, NSApplicationDelegate { - - @Default(.showAssigned) var showAssigned - @Default(.showCreated) var showCreated - @Default(.showRequested) var showRequested - - @Default(.showAvatar) var showAvatar - @Default(.showLabels) var showLabels - - @Default(.refreshRate) var refreshRate - @Default(.buildType) var buildType - @Default(.counterType) var counterType - - @Default(.githubUsername) var githubUsername + @FromKeychain(.githubToken) var githubToken let ghClient = GitHubClient() @@ -51,7 +39,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { statusBarItem.menu = menu timer = Timer.scheduledTimer( - timeInterval: Double(refreshRate * 60), + timeInterval: Double(Defaults[.refreshRate] * 60), target: self, selector: #selector(refreshMenu), userInfo: nil, @@ -86,7 +74,7 @@ extension AppDelegate { NSLog("Refreshing menu") self.menu.removeAllItems() - if (githubUsername == "" || githubToken == "") { + if (Defaults[.githubUsername] == "" || githubToken == "") { addMenuFooterItems() return } @@ -99,7 +87,7 @@ extension AppDelegate { let group = DispatchGroup() - if showAssigned { + if Defaults[.showAssigned] { group.enter() ghClient.getAssignedPulls() { pulls in assignedPulls?.append(contentsOf: pulls) @@ -107,7 +95,7 @@ extension AppDelegate { } } - if showCreated { + if Defaults[.showCreated] { group.enter() ghClient.getCreatedPulls() { pulls in createdPulls?.append(contentsOf: pulls) @@ -115,7 +103,7 @@ extension AppDelegate { } } - if showRequested { + if Defaults[.showRequested] { group.enter() ghClient.getReviewRequestedPulls() { pulls in reviewRequestedPulls?.append(contentsOf: pulls) @@ -128,8 +116,8 @@ extension AppDelegate { if let assignedPulls = assignedPulls, let createdPulls = createdPulls, let reviewRequestedPulls = reviewRequestedPulls { self.statusBarItem.button?.title = "" - if self.showAssigned && !assignedPulls.isEmpty { - if self.counterType == .assigned { + if Defaults[.showAssigned] && !assignedPulls.isEmpty { + if Defaults[.counterType] == .assigned { self.statusBarItem.button?.title = String(assignedPulls.count) } @@ -140,8 +128,8 @@ extension AppDelegate { self.menu.addItem(.separator()) } - if self.showCreated && !createdPulls.isEmpty { - if self.counterType == .created { + if Defaults[.showCreated] && !createdPulls.isEmpty { + if Defaults[.counterType] == .created { self.statusBarItem.button?.title = String(createdPulls.count) } @@ -152,8 +140,8 @@ extension AppDelegate { self.menu.addItem(.separator()) } - if self.showRequested && !reviewRequestedPulls.isEmpty { - if self.counterType == .reviewRequested { + if Defaults[.showRequested] && !reviewRequestedPulls.isEmpty { + if Defaults[.counterType] == .reviewRequested { self.statusBarItem.button?.title = String(reviewRequestedPulls.count) } @@ -195,7 +183,7 @@ extension AppDelegate { .appendIcon(iconName: "person") .appendString(string: pull.node.author.login) - if !pull.node.labels.nodes.isEmpty && self.showLabels { + if !pull.node.labels.nodes.isEmpty && Defaults[.showLabels] { issueItemTitle .appendNewLine() .appendIcon(iconName: "tag", color: NSColor(.secondary)) @@ -208,7 +196,7 @@ extension AppDelegate { issueItemTitle.appendNewLine() - let approvedByMe = pull.node.reviews.edges.contains{ $0.node.author.login == githubUsername } + let approvedByMe = pull.node.reviews.edges.contains{ $0.node.author.login == Defaults[.githubUsername] } issueItemTitle .appendIcon(iconName: "check-circle", color: approvedByMe ? NSColor(named: "green")! : NSColor.secondaryLabelColor) .appendString(string: " " + String(pull.node.reviews.totalCount)) @@ -219,7 +207,7 @@ extension AppDelegate { .appendIcon(iconName: "calendar") .appendString(string: pull.node.createdAt.getElapsedInterval()) - if showAvatar { + if Defaults[.showAvatar] { var image = NSImage() if let imageURL = pull.node.author.avatarUrl { image = NSImage.imageFromUrl(fromURL: imageURL) ?? NSImage(named: "person")! @@ -400,7 +388,7 @@ extension AppDelegate { if (windowTitle == "Preferences") { timer?.invalidate() timer = Timer.scheduledTimer( - timeInterval: Double(refreshRate * 60), + timeInterval: Double(Defaults[.refreshRate] * 60), target: self, selector: #selector(refreshMenu), userInfo: nil, diff --git a/pullBar/GitHub/GitHubClient.swift b/pullBar/GitHub/GitHubClient.swift index 92f7833..dd22d6f 100644 --- a/pullBar/GitHub/GitHubClient.swift +++ b/pullBar/GitHub/GitHubClient.swift @@ -12,16 +12,11 @@ import KeychainAccess public class GitHubClient { - @Default(.githubApiBaseUrl) var githubApiBaseUrl - @Default(.githubUsername) var githubUsername - @Default(.githubAdditionalQuery) var githubAdditionalQuery @FromKeychain(.githubToken) var githubToken - - @Default(.buildType) var buildType func getAssignedPulls(completion:@escaping (([Edge]) -> Void)) -> Void { - if (githubUsername == "" || githubToken == "") { + if (Defaults[.githubUsername] == "" || githubToken == "") { completion([Edge]()) } @@ -30,14 +25,14 @@ public class GitHubClient { .accept("application/json") ] - let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr assignee:\(githubUsername) archived:false \(githubAdditionalQuery)") + let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr assignee:\(Defaults[.githubUsername]) archived:false \(Defaults[.githubAdditionalQuery])") let parameters = [ "query": graphQlQuery, "variables":[] ] as [String: Any] - AF.request(githubApiBaseUrl + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) + AF.request(Defaults[.githubApiBaseUrl] + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) .validate(statusCode: 200..<300) .responseDecodable(of: GraphQlSearchResp.self, decoder: GithubDecoder()) { response in switch response.result { @@ -53,7 +48,7 @@ public class GitHubClient { func getCreatedPulls(completion:@escaping (([Edge]) -> Void)) -> Void { - if (githubUsername == "" || githubToken == "") { + if (Defaults[.githubUsername] == "" || githubToken == "") { completion([Edge]()) } @@ -61,14 +56,14 @@ public class GitHubClient { .authorization(bearerToken: githubToken), .accept("application/json") ] - let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr author:\(githubUsername) archived:false \(githubAdditionalQuery)") + let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr author:\(Defaults[.githubUsername]) archived:false \(Defaults[.githubAdditionalQuery])") let parameters = [ "query": graphQlQuery, "variables":[] ] as [String: Any] - AF.request(githubApiBaseUrl + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) + AF.request(Defaults[.githubApiBaseUrl] + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) .validate(statusCode: 200..<300) .responseDecodable(of: GraphQlSearchResp.self, decoder: GithubDecoder()) { response in switch response.result { @@ -83,7 +78,7 @@ public class GitHubClient { } func getReviewRequestedPulls(completion:@escaping (([Edge]) -> Void)) -> Void { - if (githubUsername == "" || githubToken == "") { + if (Defaults[.githubUsername] == "" || githubToken == "") { completion([Edge]()) } @@ -91,14 +86,14 @@ public class GitHubClient { .authorization(bearerToken: githubToken), .accept("application/json") ] - let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr review-requested:\(githubUsername) archived:false \(githubAdditionalQuery)") + let graphQlQuery = buildGraphQlQuery(queryString: "is:open is:pr review-requested:\(Defaults[.githubUsername]) archived:false \(Defaults[.githubAdditionalQuery])") let parameters = [ "query": graphQlQuery, "variables":[] ] as [String: Any] - AF.request(githubApiBaseUrl + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) + AF.request(Defaults[.githubApiBaseUrl] + "/graphql", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) .validate(statusCode: 200..<300) .responseDecodable(of: GraphQlSearchResp.self, decoder: GithubDecoder()) { response in switch response.result { @@ -115,7 +110,7 @@ public class GitHubClient { var build = "" - switch buildType { + switch Defaults[.buildType] { case .checks: build = """ commits(last: 1) { @@ -233,7 +228,7 @@ public class GitHubClient { .accept("application/json") ] - AF.request(githubApiBaseUrl + "/user", + AF.request(Defaults[.githubApiBaseUrl] + "/user", method: .get, headers: headers) .validate(statusCode: 200..<300) @@ -251,7 +246,7 @@ public class GitHubClient { func getLatestRelease(completion:@escaping (((LatestRelease?) -> Void))) -> Void { let headers: HTTPHeaders = [ - .authorization(username: githubUsername, password: githubToken), + .authorization(username: Defaults[.githubUsername], password: githubToken), .contentType("application/json"), .accept("application/json") ] diff --git a/pullBar/Views/PreferencesView.swift b/pullBar/Views/PreferencesView.swift index 78f3080..b143d96 100644 --- a/pullBar/Views/PreferencesView.swift +++ b/pullBar/Views/PreferencesView.swift @@ -29,8 +29,6 @@ struct PreferencesView: View { @Default(.counterType) var counterType @State private var showGhAlert = false - - @State private var selectedTab: Int = 1 @StateObject private var githubTokenValidator = GithubTokenValidator() @ObservedObject private var launchAtLogin = LaunchAtLogin.observable @@ -39,7 +37,7 @@ struct PreferencesView: View { var body: some View { - TabView (selection: $selectedTab) { + TabView { Form { HStack(alignment: .center) { Text("Pull Requests:").frame(width: 120, alignment: .trailing) @@ -88,14 +86,13 @@ struct PreferencesView: View { HStack(alignment: .center) { Text("Launch at login:").frame(width: 120, alignment: .trailing) Toggle("", isOn: $launchAtLogin.isEnabled) - } } .padding(8) .frame(maxWidth: .infinity) .tabItem{Text("General")} - + Form { HStack(alignment: .center) { Text("API Base URL:").frame(width: 120, alignment: .trailing) @@ -149,7 +146,6 @@ struct PreferencesView: View { githubTokenValidator.validate() } .tabItem{Text("Authentication")} - .tag(1) Form { HStack(alignment: .center) { @@ -191,6 +187,7 @@ struct PreferencesView: View { .tabItem{Text("Advanced")} } + .frame(width: 600) .padding() }