From 9058ef1ea325171f658529a890bd39cc7f0017e2 Mon Sep 17 00:00:00 2001 From: stoeffn <13511737+stoeffn@users.noreply.github.com> Date: Mon, 28 May 2018 22:18:20 +0200 Subject: [PATCH] :sparkles: Make app id and country editable --- TodayConnect.xcodeproj/project.pbxproj | 4 + TodayConnect/Base.lproj/Main.storyboard | 80 ++++++++++++++----- TodayConnect/ViewController.swift | 14 ++++ TodayConnectKit/App.swift | 10 ++- .../Authorization/DefaultsCookieStorage.swift | 11 +-- .../Extensions/Swift/String+Utils.swift | 24 ++++++ .../TodayViewController+WidgetProviding.swift | 13 ++- TodayExtension/TodayViewController.swift | 7 +- 8 files changed, 128 insertions(+), 35 deletions(-) create mode 100644 TodayConnectKit/Extensions/Swift/String+Utils.swift diff --git a/TodayConnect.xcodeproj/project.pbxproj b/TodayConnect.xcodeproj/project.pbxproj index b9216ba..c84f859 100644 --- a/TodayConnect.xcodeproj/project.pbxproj +++ b/TodayConnect.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 2421B59720BB0DFB00A7155A /* RatingStarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2421B59620BB0DFB00A7155A /* RatingStarView.swift */; }; 245FBEC720BC8D68000760A7 /* RatingsWidget-Expanded.png in Resources */ = {isa = PBXBuildFile; fileRef = 245FBEC520BC8D68000760A7 /* RatingsWidget-Expanded.png */; }; 245FBEC820BC8D68000760A7 /* RatingsWidget-Collapsed.png in Resources */ = {isa = PBXBuildFile; fileRef = 245FBEC620BC8D68000760A7 /* RatingsWidget-Collapsed.png */; }; + 245FBECC20BC9A70000760A7 /* String+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245FBECB20BC9A70000760A7 /* String+Utils.swift */; }; 24677D3020BAAD7A00F4D530 /* HTTPCookie+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24677D2F20BAAD7A00F4D530 /* HTTPCookie+Utils.swift */; }; 24677D3220BAB66900F4D530 /* ReviewSummaryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24677D3120BAB66900F4D530 /* ReviewSummaryViewController.swift */; }; 24677D3720BAB9F000F4D530 /* TodayViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 24677D3520BAB9F000F4D530 /* TodayViewController.xib */; }; @@ -133,6 +134,7 @@ 2421B59620BB0DFB00A7155A /* RatingStarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatingStarView.swift; sourceTree = ""; }; 245FBEC520BC8D68000760A7 /* RatingsWidget-Expanded.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RatingsWidget-Expanded.png"; sourceTree = ""; }; 245FBEC620BC8D68000760A7 /* RatingsWidget-Collapsed.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RatingsWidget-Collapsed.png"; sourceTree = ""; }; + 245FBECB20BC9A70000760A7 /* String+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Utils.swift"; sourceTree = ""; }; 24677D2F20BAAD7A00F4D530 /* HTTPCookie+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HTTPCookie+Utils.swift"; sourceTree = ""; }; 24677D3120BAB66900F4D530 /* ReviewSummaryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewSummaryViewController.swift; sourceTree = ""; }; 24677D3620BAB9F000F4D530 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = TodayExtension/Base.lproj/TodayViewController.xib; sourceTree = ""; }; @@ -422,6 +424,7 @@ isa = PBXGroup; children = ( 24BF901420B89B0D0098E71A /* Result.swift */, + 245FBECB20BC9A70000760A7 /* String+Utils.swift */, ); path = Swift; sourceTree = ""; @@ -750,6 +753,7 @@ 24BF902620B950590098E71A /* HttpMethods.swift in Sources */, 24BF901F20B8B19C0098E71A /* VerifySecurityCodeRequest.swift in Sources */, 24BF901320B89A200098E71A /* ConnectApi.swift in Sources */, + 245FBECC20BC9A70000760A7 /* String+Utils.swift in Sources */, 24BF903920B97FF60098E71A /* App.swift in Sources */, 24BF903720B978550098E71A /* Platforms.swift in Sources */, 24677D3020BAAD7A00F4D530 /* HTTPCookie+Utils.swift in Sources */, diff --git a/TodayConnect/Base.lproj/Main.storyboard b/TodayConnect/Base.lproj/Main.storyboard index 6265340..2c0d796 100644 --- a/TodayConnect/Base.lproj/Main.storyboard +++ b/TodayConnect/Base.lproj/Main.storyboard @@ -689,7 +689,7 @@ - + @@ -701,18 +701,28 @@ - + - + + - + @@ -723,7 +733,7 @@ - + @@ -734,7 +744,7 @@ - + @@ -745,7 +755,7 @@ - - + + + This login window is pretty much under construction and lacks proper error handling. +Only iOS apps are supported as of now. - + + + + + + + + + + + + + + + + + + + + + + + @@ -785,18 +821,26 @@ + + + + + + - + + + @@ -805,7 +849,7 @@ - + diff --git a/TodayConnect/ViewController.swift b/TodayConnect/ViewController.swift index 0703362..e089d62 100644 --- a/TodayConnect/ViewController.swift +++ b/TodayConnect/ViewController.swift @@ -32,8 +32,16 @@ final class ViewController: NSViewController { @IBOutlet var verifyButton: NSButton! + @IBOutlet var appIdTextField: NSTextField! + + @IBOutlet var countryCodeTextField: NSTextField! + override func viewDidLoad() { super.viewDidLoad() + + appIdTextField.stringValue = App.userDefaults.string(forKey: App.UserDefaultsKeys.appId.rawValue) ?? "" + countryCodeTextField.stringValue = App.userDefaults.string(forKey: App.UserDefaultsKeys.countryCode.rawValue) ?? "" + api.authorizationApi { self.authorizationApi = $0.value } } @@ -91,4 +99,10 @@ final class ViewController: NSViewController { } } } + + @IBAction + func updateSettings(_: Any) { + App.userDefaults.set(appIdTextField.stringValue.nilWhenEmpty, forKey: App.UserDefaultsKeys.appId.rawValue) + App.userDefaults.set(countryCodeTextField.stringValue.nilWhenEmpty, forKey: App.UserDefaultsKeys.countryCode.rawValue) + } } diff --git a/TodayConnectKit/App.swift b/TodayConnectKit/App.swift index a73fdab..0911fe3 100644 --- a/TodayConnectKit/App.swift +++ b/TodayConnectKit/App.swift @@ -17,6 +17,12 @@ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -enum App { - static let groupIdentifier = "group.SteffenRyll.TodayConnect" +public enum App { + public enum UserDefaultsKeys: String { + case appId, countryCode, isExpanded, cookies + } + + public static let groupIdentifier = "group.SteffenRyll.TodayConnect" + + public static let userDefaults = UserDefaults(suiteName: groupIdentifier)! } diff --git a/TodayConnectKit/Authorization/DefaultsCookieStorage.swift b/TodayConnectKit/Authorization/DefaultsCookieStorage.swift index 852f79a..b40a123 100644 --- a/TodayConnectKit/Authorization/DefaultsCookieStorage.swift +++ b/TodayConnectKit/Authorization/DefaultsCookieStorage.swift @@ -18,16 +18,11 @@ // public final class DefaultsCookieStorage: HTTPCookieStorage { - private let defaultsCookiesKey = "cookies" private let defaults: UserDefaults - // MARK: - Defaults - - public static let defaultDefaults = UserDefaults(suiteName: App.groupIdentifier)! - // MARK: - Life Cycle - public init(defaults: UserDefaults = defaultDefaults) { + public init(defaults: UserDefaults = App.userDefaults) { self.defaults = defaults } @@ -42,7 +37,7 @@ public final class DefaultsCookieStorage: HTTPCookieStorage { // MARK: - Retrieving Cookies private func cookies(from defaults: UserDefaults) -> [HTTPCookie] { - guard let rawCookies = defaults.array(forKey: defaultsCookiesKey) as? [[String: Any]] else { return [] } + guard let rawCookies = defaults.array(forKey: App.UserDefaultsKeys.cookies.rawValue) as? [[String: Any]] else { return [] } return rawCookies.compactMap(HTTPCookie.init) } @@ -53,7 +48,7 @@ public final class DefaultsCookieStorage: HTTPCookieStorage { // MARK: - Storing Cookies private func store(cookies: [HTTPCookie], to defaults: UserDefaults) { - defaults.set(cookies.map { $0.stringProperties }, forKey: defaultsCookiesKey) + defaults.set(cookies.map { $0.stringProperties }, forKey: App.UserDefaultsKeys.cookies.rawValue) } public override func storeCookies(_ newCookies: [HTTPCookie], for _: URLSessionTask) { diff --git a/TodayConnectKit/Extensions/Swift/String+Utils.swift b/TodayConnectKit/Extensions/Swift/String+Utils.swift new file mode 100644 index 0000000..a9ddfd1 --- /dev/null +++ b/TodayConnectKit/Extensions/Swift/String+Utils.swift @@ -0,0 +1,24 @@ +// +// TodayConnect +// +// Copyright © 2018 Steffen Ryll +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, +// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the +// Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +public extension String { + var nilWhenEmpty: String? { + return isEmpty ? nil : self + } +} diff --git a/TodayExtension/TodayViewController+WidgetProviding.swift b/TodayExtension/TodayViewController+WidgetProviding.swift index 9a7fc26..2638653 100644 --- a/TodayExtension/TodayViewController+WidgetProviding.swift +++ b/TodayExtension/TodayViewController+WidgetProviding.swift @@ -18,8 +18,17 @@ // import NotificationCenter +import TodayConnectKit extension TodayViewController: NCWidgetProviding { + var appId: String { + return App.userDefaults.string(forKey: App.UserDefaultsKeys.appId.rawValue) ?? "" + } + + var countryCode: String? { + return App.userDefaults.string(forKey: App.UserDefaultsKeys.countryCode.rawValue) + } + func widgetPerformUpdate(completionHandler completion: @escaping (NCUpdateResult) -> Void) { let group = DispatchGroup() var results: [NCUpdateResult] = [] @@ -51,7 +60,7 @@ extension TodayViewController: NCWidgetProviding { } func updateReviewSummary(completion: @escaping (NCUpdateResult) -> Void) { - api.reviewSummary(forAppId: appId, platform: platform) { result in + api.reviewSummary(forAppId: appId, platform: platform, countryCode: countryCode) { result in switch result { case let .success(reviewSummary) where reviewSummary != self.reviewSummary: DispatchQueue.main.async { self.reviewSummary = reviewSummary } @@ -65,7 +74,7 @@ extension TodayViewController: NCWidgetProviding { } func updateReviews(completion: @escaping (NCUpdateResult) -> Void) { - api.reviews(forAppId: appId, platform: platform) { result in + api.reviews(forAppId: appId, platform: platform, countryCode: countryCode) { result in switch result { case let .success(reviews) where reviews != self.reviews: DispatchQueue.main.async { self.reviews = reviews } diff --git a/TodayExtension/TodayViewController.swift b/TodayExtension/TodayViewController.swift index 6239c7f..8db0024 100644 --- a/TodayExtension/TodayViewController.swift +++ b/TodayExtension/TodayViewController.swift @@ -21,11 +21,8 @@ import Cocoa import TodayConnectKit final class TodayViewController: NSViewController { - private static let defaultsExpandedKey = "isExpanded" - private var contentViewControllerObservations: [NSKeyValueObservation] = [] let api = ConnectApi() - let appId = "1317593772" let platform = Platforms.iOS // MARK: - Life Cycle @@ -75,12 +72,12 @@ final class TodayViewController: NSViewController { contentViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true contentViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true contentViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true - contentViewController.isExpanded = UserDefaults.standard.bool(forKey: TodayViewController.defaultsExpandedKey) + contentViewController.isExpanded = App.userDefaults.bool(forKey: App.UserDefaultsKeys.isExpanded.rawValue) contentViewControllerObservations = [ contentViewController.observe(\.isExpanded, options: [.old, .new]) { [unowned self] _, change in guard change.newValue != change.oldValue else { return } - UserDefaults.standard.set(self.contentViewController.isExpanded, forKey: TodayViewController.defaultsExpandedKey) + App.userDefaults.set(self.contentViewController.isExpanded, forKey: App.UserDefaultsKeys.isExpanded.rawValue) }, ] }