Skip to content

Commit

Permalink
✨ Make app id and country editable
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffn committed May 28, 2018
1 parent f5f1737 commit 9058ef1
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 35 deletions.
4 changes: 4 additions & 0 deletions TodayConnect.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -133,6 +134,7 @@
2421B59620BB0DFB00A7155A /* RatingStarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatingStarView.swift; sourceTree = "<group>"; };
245FBEC520BC8D68000760A7 /* RatingsWidget-Expanded.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RatingsWidget-Expanded.png"; sourceTree = "<group>"; };
245FBEC620BC8D68000760A7 /* RatingsWidget-Collapsed.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RatingsWidget-Collapsed.png"; sourceTree = "<group>"; };
245FBECB20BC9A70000760A7 /* String+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Utils.swift"; sourceTree = "<group>"; };
24677D2F20BAAD7A00F4D530 /* HTTPCookie+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HTTPCookie+Utils.swift"; sourceTree = "<group>"; };
24677D3120BAB66900F4D530 /* ReviewSummaryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewSummaryViewController.swift; sourceTree = "<group>"; };
24677D3620BAB9F000F4D530 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = TodayExtension/Base.lproj/TodayViewController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -422,6 +424,7 @@
isa = PBXGroup;
children = (
24BF901420B89B0D0098E71A /* Result.swift */,
245FBECB20BC9A70000760A7 /* String+Utils.swift */,
);
path = Swift;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
80 changes: 62 additions & 18 deletions TodayConnect/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions TodayConnect/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -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)
}
}
10 changes: 8 additions & 2 deletions TodayConnectKit/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
}
11 changes: 3 additions & 8 deletions TodayConnectKit/Authorization/DefaultsCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand All @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions TodayConnectKit/Extensions/Swift/String+Utils.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 11 additions & 2 deletions TodayExtension/TodayViewController+WidgetProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
7 changes: 2 additions & 5 deletions TodayExtension/TodayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
},
]
}
Expand Down

0 comments on commit 9058ef1

Please sign in to comment.