mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-01-12 22:47:34 +08:00
migrate to get username for session, display authd sessions in settings
This commit is contained in:
@@ -10,9 +10,14 @@ import Foundation
|
||||
|
||||
extension GithubClient {
|
||||
|
||||
struct AccessTokenUser {
|
||||
let token: String
|
||||
let username: String
|
||||
}
|
||||
|
||||
enum AccessTokenResult {
|
||||
case failure
|
||||
case success(String)
|
||||
case success(AccessTokenUser)
|
||||
}
|
||||
|
||||
func requestAccessToken(
|
||||
@@ -35,7 +40,12 @@ extension GithubClient {
|
||||
completion: { (response, _) in
|
||||
if let json = response.value as? [String: Any],
|
||||
let token = json["access_token"] as? String {
|
||||
completion(.success(token))
|
||||
self.verifyPersonalAccessToken(token: token, completion: { result in
|
||||
switch result {
|
||||
case .success(let user): completion(.success(user))
|
||||
case .failure: completion(.failure)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
completion(.failure)
|
||||
}
|
||||
@@ -55,10 +65,9 @@ extension GithubClient {
|
||||
method: .get,
|
||||
headers: headers,
|
||||
completion: { (response, _) in
|
||||
let json = response.value as? [String: Any]
|
||||
let username = json?["login"] as? String
|
||||
if username != nil {
|
||||
completion(.success(token))
|
||||
if let json = response.value as? [String: Any],
|
||||
let username = json["login"] as? String {
|
||||
completion(.success(AccessTokenUser(token: token, username: username)))
|
||||
} else {
|
||||
completion(.failure)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ final class LoginSplashViewController: UIViewController, GithubSessionListener {
|
||||
private func handle(result: GithubClient.AccessTokenResult, authMethod: GithubUserSession.AuthMethod) {
|
||||
switch result {
|
||||
case .failure: handleError()
|
||||
case .success(let token): finishLogin(token: token, authMethod: authMethod)
|
||||
case .success(let user): finishLogin(token: user.token, authMethod: authMethod, username: user.username)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +98,8 @@ final class LoginSplashViewController: UIViewController, GithubSessionListener {
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
private func finishLogin(token: String, authMethod: GithubUserSession.AuthMethod) {
|
||||
client.sessionManager.authenticate(token, authMethod: authMethod)
|
||||
private func finishLogin(token: String, authMethod: GithubUserSession.AuthMethod, username: String) {
|
||||
client.sessionManager.authenticate(token, authMethod: authMethod, username: username)
|
||||
}
|
||||
|
||||
// MARK: GithubSessionListener
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
<color key="separatorColor" red="0.73725490196078436" green="0.73333333333333328" blue="0.75686274509803919" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
<view key="tableFooterView" contentMode="scaleToFill" id="jkq-3p-p02">
|
||||
<rect key="frame" x="0.0" y="429.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="473.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||
<subviews>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Version 1.4.0 (1207)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2bb-Lj-p2k">
|
||||
@@ -57,12 +57,40 @@
|
||||
</tableViewCellContentView>
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</tableViewCell>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="NzM-oA-wbA" detailTextLabel="Rk9-Tv-PGT" style="IBUITableViewCellStyleValue1" id="64O-sw-wTx" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="99.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="64O-sw-wTx" id="Vxj-xW-he4">
|
||||
<rect key="frame" x="0.0" y="0.0" width="342" height="43.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Accounts" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="NzM-oA-wbA">
|
||||
<rect key="frame" x="15" y="12" width="68" height="19.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="16"/>
|
||||
<color key="textColor" red="0.14117647059999999" green="0.16078431369999999" blue="0.18039215689999999" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="username" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Rk9-Tv-PGT">
|
||||
<rect key="frame" x="268" y="12" width="72" height="19.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="16"/>
|
||||
<color key="textColor" red="0.63921568627450975" green="0.66666666666666663" blue="0.69411764705882351" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
</subviews>
|
||||
</tableViewCellContentView>
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
<connections>
|
||||
<segue destination="Pin-sZ-TSO" kind="show" id="v7n-Wc-t3h"/>
|
||||
</connections>
|
||||
</tableViewCell>
|
||||
</cells>
|
||||
</tableViewSection>
|
||||
<tableViewSection headerTitle="Githawk" id="3QH-oa-l2C">
|
||||
<cells>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="kdx-s1-NXl" style="IBUITableViewCellStyleDefault" id="WwO-m1-s3q" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="155.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="199.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="WwO-m1-s3q" id="DTP-jg-lt7">
|
||||
<rect key="frame" x="0.0" y="0.0" width="342" height="43.5"/>
|
||||
@@ -80,7 +108,7 @@
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</tableViewCell>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="LOz-Oo-yFl" style="IBUITableViewCellStyleDefault" id="SAx-6B-29G" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="199.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="243.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="SAx-6B-29G" id="NM8-rc-RzD">
|
||||
<rect key="frame" x="0.0" y="0.0" width="342" height="43.5"/>
|
||||
@@ -98,7 +126,7 @@
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</tableViewCell>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" id="pnm-5p-yC5" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="243.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="287.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="pnm-5p-yC5" id="Cu2-kJ-7ah">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
|
||||
@@ -139,7 +167,7 @@
|
||||
<inset key="separatorInset" minX="15" minY="0.0" maxX="0.0" maxY="0.0"/>
|
||||
</tableViewCell>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" id="U6l-Zw-17s" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="287.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="331.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="U6l-Zw-17s" id="cMZ-jh-esi">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
|
||||
@@ -173,7 +201,7 @@
|
||||
<tableViewSection id="GTg-1u-cvV">
|
||||
<cells>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="ku5-ZI-OG9" style="IBUITableViewCellStyleDefault" id="Y2k-Zq-AUW" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="367.5" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="411.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Y2k-Zq-AUW" id="XiT-sq-B22">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
|
||||
@@ -206,6 +234,7 @@
|
||||
</barButtonItem>
|
||||
</navigationItem>
|
||||
<connections>
|
||||
<outlet property="accountsCell" destination="64O-sw-wTx" id="Erb-Qs-t0m"/>
|
||||
<outlet property="backgroundFetchSwitch" destination="FvK-93-raA" id="Huy-vc-MoM"/>
|
||||
<outlet property="badgeCell" destination="pnm-5p-yC5" id="hdC-aO-QSy"/>
|
||||
<outlet property="markReadSwitch" destination="8dQ-YU-Yuw" id="5jz-5d-osk"/>
|
||||
@@ -221,12 +250,69 @@
|
||||
</objects>
|
||||
<point key="canvasLocation" x="-940" y="-131"/>
|
||||
</scene>
|
||||
<!--Accounts-->
|
||||
<scene sceneID="Xjx-IC-OZR">
|
||||
<objects>
|
||||
<tableViewController id="Pin-sZ-TSO" customClass="SettingsAccountsViewController" customModule="Freetime" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="-1" sectionHeaderHeight="18" sectionFooterHeight="18" id="vrj-05-S8l">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" red="0.96470588239999999" green="0.97254901959999995" blue="0.98039215690000003" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<color key="separatorColor" red="0.73725490199999999" green="0.73333333329999995" blue="0.75686274509999996" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<prototypes>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="account" textLabel="ASM-DA-gBk" style="IBUITableViewCellStyleDefault" id="lpN-QC-8cv" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="55.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="lpN-QC-8cv" id="yMj-Hq-uAw">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="username" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="ASM-DA-gBk">
|
||||
<rect key="frame" x="15" y="0.0" width="345" height="43.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="16"/>
|
||||
<nil key="textColor"/>
|
||||
<color key="highlightedColor" red="0.14117647058823529" green="0.16078431372549018" blue="0.1803921568627451" alpha="1" colorSpace="calibratedRGB"/>
|
||||
</label>
|
||||
</subviews>
|
||||
</tableViewCellContentView>
|
||||
<color key="tintColor" red="0.011764705882352941" green="0.40000000000000002" blue="0.83921568627450982" alpha="1" colorSpace="calibratedRGB"/>
|
||||
</tableViewCell>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="add-account" textLabel="I8s-EO-d9s" style="IBUITableViewCellStyleDefault" id="gi2-tl-wiw" customClass="StyledTableCell" customModule="Freetime" customModuleProvider="target">
|
||||
<rect key="frame" x="0.0" y="99.5" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="gi2-tl-wiw" id="YwN-fl-jII">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<subviews>
|
||||
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Add Account" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="I8s-EO-d9s">
|
||||
<rect key="frame" x="15" y="0.0" width="345" height="43.5"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="16"/>
|
||||
<color key="textColor" red="0.011764705882352941" green="0.40000000000000002" blue="0.83921568627450982" alpha="1" colorSpace="calibratedRGB"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
</subviews>
|
||||
</tableViewCellContentView>
|
||||
</tableViewCell>
|
||||
</prototypes>
|
||||
<connections>
|
||||
<outlet property="dataSource" destination="Pin-sZ-TSO" id="uiT-RG-hsT"/>
|
||||
<outlet property="delegate" destination="Pin-sZ-TSO" id="wQZ-LB-ZNX"/>
|
||||
</connections>
|
||||
</tableView>
|
||||
<navigationItem key="navigationItem" title="Accounts" id="56A-2S-vCm"/>
|
||||
</tableViewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="YHM-PS-ThT" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="-90" y="-131"/>
|
||||
</scene>
|
||||
<!--Navigation Controller-->
|
||||
<scene sceneID="D9x-gg-9aE">
|
||||
<objects>
|
||||
<navigationController id="d42-bu-Upj" sceneMemberID="viewController">
|
||||
<navigationBar key="navigationBar" contentMode="scaleToFill" id="ldN-13-vej">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
|
||||
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
</navigationBar>
|
||||
<connections>
|
||||
|
||||
61
Classes/Settings/SettingsAccountsViewController.swift
Normal file
61
Classes/Settings/SettingsAccountsViewController.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// SettingsAccountsViewController.swift
|
||||
// Freetime
|
||||
//
|
||||
// Created by Ryan Nystrom on 8/25/17.
|
||||
// Copyright © 2017 Ryan Nystrom. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
final class SettingsAccountsViewController: UITableViewController {
|
||||
|
||||
var sessionManager: GithubSessionManager!
|
||||
|
||||
// MARK: UITableViewDataSource
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return 2
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
if section == 0 {
|
||||
return sessionManager.userSessions.count
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let isAccounts = indexPath.section == 0
|
||||
let identifier = isAccounts ? "account" : "add-account"
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
|
||||
if isAccounts {
|
||||
let session = sessionManager.userSessions[indexPath.row]
|
||||
cell.textLabel?.text = session.username ?? session.token
|
||||
cell.accessoryType = sessionManager.focusedUserSession == session ? .checkmark : .none
|
||||
}
|
||||
return cell
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||
if section == 0 {
|
||||
return NSLocalizedString("Authenticated Accounts", comment: "")
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UITableViewDelegate
|
||||
|
||||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
|
||||
if indexPath.section == 0 {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ final class SettingsViewController: UITableViewController {
|
||||
@IBOutlet weak var openSettingsButton: UIButton!
|
||||
@IBOutlet weak var badgeCell: UITableViewCell!
|
||||
@IBOutlet weak var markReadSwitch: UISwitch!
|
||||
@IBOutlet weak var accountsCell: StyledTableCell!
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
@@ -43,6 +44,14 @@ final class SettingsViewController: UITableViewController {
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
rz_smoothlyDeselectRows(tableView: tableView)
|
||||
|
||||
accountsCell.detailTextLabel?.text = sessionManager.focusedUserSession?.username ?? Strings.unknown
|
||||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||
if let controller = segue.destination as? SettingsAccountsViewController {
|
||||
controller.sessionManager = sessionManager
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UITableViewDelegate
|
||||
|
||||
@@ -75,9 +75,13 @@ final class GithubSessionManager: NSObject, ListDiffable {
|
||||
listeners.append(wrapper)
|
||||
}
|
||||
|
||||
public func authenticate(_ token: String, authMethod: GithubUserSession.AuthMethod) {
|
||||
let userSession = GithubUserSession(token: token, authMethod: authMethod)
|
||||
_userSessions.insert(GithubUserSession(token: token, authMethod: authMethod), at: 0)
|
||||
public func authenticate(
|
||||
_ token: String,
|
||||
authMethod: GithubUserSession.AuthMethod,
|
||||
username: String
|
||||
) {
|
||||
let userSession = GithubUserSession(token: token, authMethod: authMethod, username: username)
|
||||
_userSessions.insert(userSession, at: 0)
|
||||
save()
|
||||
for wrapper in listeners {
|
||||
wrapper.listener?.didAuthenticate(manager: self, userSession: userSession)
|
||||
|
||||
@@ -13,6 +13,7 @@ final class GithubUserSession: NSObject, NSCoding {
|
||||
enum Keys {
|
||||
static let token = "token"
|
||||
static let authMethod = "authMethod"
|
||||
static let username = "username"
|
||||
}
|
||||
|
||||
enum AuthMethod: String {
|
||||
@@ -23,12 +24,18 @@ final class GithubUserSession: NSObject, NSCoding {
|
||||
let token: String
|
||||
let authMethod: AuthMethod
|
||||
|
||||
// mutable to handle migration from time when username wasn't captured
|
||||
// can freely mutate and manually update. caller must then save updated session.
|
||||
var username: String?
|
||||
|
||||
init(
|
||||
token: String,
|
||||
authMethod: AuthMethod
|
||||
authMethod: AuthMethod,
|
||||
username: String?
|
||||
) {
|
||||
self.token = token
|
||||
self.authMethod = authMethod
|
||||
self.username = username
|
||||
}
|
||||
|
||||
// MARK: NSCoding
|
||||
@@ -40,15 +47,19 @@ final class GithubUserSession: NSObject, NSCoding {
|
||||
let storedAuthMethod = aDecoder.decodeObject(forKey: Keys.authMethod) as? String
|
||||
let authMethod = storedAuthMethod.flatMap(AuthMethod.init) ?? .oauth
|
||||
|
||||
let username = aDecoder.decodeObject(forKey: Keys.username) as? String
|
||||
|
||||
self.init(
|
||||
token: token,
|
||||
authMethod: authMethod
|
||||
authMethod: authMethod,
|
||||
username: username
|
||||
)
|
||||
}
|
||||
|
||||
func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(token, forKey: Keys.token)
|
||||
aCoder.encode(authMethod.rawValue, forKey: Keys.authMethod)
|
||||
aCoder.encode(username, forKey: Keys.username)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ final class RootNavigationManager: GithubSessionListener {
|
||||
|
||||
let client = newGithubClient(sessionManager: sessionManager, userSession: userSession)
|
||||
|
||||
fetchUsernameForMigrationIfNecessary(client: client, userSession: userSession, sessionManager: sessionManager)
|
||||
|
||||
let notifications = newNotificationsRootViewController(client: client)
|
||||
let settingsBarButtonItem = UIBarButtonItem(
|
||||
image: UIImage(named: "bullets-hollow"),
|
||||
@@ -85,6 +87,27 @@ final class RootNavigationManager: GithubSessionListener {
|
||||
|
||||
// MARK: Private API
|
||||
|
||||
private func fetchUsernameForMigrationIfNecessary(
|
||||
client: GithubClient,
|
||||
userSession: GithubUserSession,
|
||||
sessionManager: GithubSessionManager
|
||||
) {
|
||||
// only required when there is no username
|
||||
guard userSession.username == nil else { return }
|
||||
|
||||
client.verifyPersonalAccessToken(token: userSession.token) { result in
|
||||
switch result {
|
||||
case .success(let user):
|
||||
userSession.username = user.username
|
||||
|
||||
// user session ref is same session that manager should be using
|
||||
// update w/ mutated session
|
||||
sessionManager.save()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var masterNavigationController: UINavigationController? {
|
||||
return rootViewController?.viewControllers.first as? UINavigationController
|
||||
}
|
||||
|
||||
@@ -208,6 +208,7 @@
|
||||
297AE8831EC0D5C200B44A1F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 297AE8711EC0D5C200B44A1F /* Main.storyboard */; };
|
||||
297DD5E11F061BBE006E7E63 /* CreateProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 297DD5E01F061BBE006E7E63 /* CreateProfileViewController.swift */; };
|
||||
297DD5E31F06922A006E7E63 /* NotificationClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 297DD5E21F06922A006E7E63 /* NotificationClient.swift */; };
|
||||
297FB7781F51128A00F2E618 /* SettingsAccountsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 297FB7771F51128A00F2E618 /* SettingsAccountsViewController.swift */; };
|
||||
2980E0921F073E8B000E02C6 /* NotificationSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2980E0911F073E8B000E02C6 /* NotificationSectionController.swift */; };
|
||||
2981A8A41EFE9FC700E25EF1 /* GithubEmoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2981A8A31EFE9FC700E25EF1 /* GithubEmoji.swift */; };
|
||||
298744CB1F24B75300D5DF30 /* SlackTextViewController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 298744CA1F24B75300D5DF30 /* SlackTextViewController.framework */; };
|
||||
@@ -487,6 +488,7 @@
|
||||
297AE8731EC0D5C200B44A1F /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
297DD5E01F061BBE006E7E63 /* CreateProfileViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateProfileViewController.swift; sourceTree = "<group>"; };
|
||||
297DD5E21F06922A006E7E63 /* NotificationClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationClient.swift; sourceTree = "<group>"; };
|
||||
297FB7771F51128A00F2E618 /* SettingsAccountsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsAccountsViewController.swift; sourceTree = "<group>"; };
|
||||
2980E0911F073E8B000E02C6 /* NotificationSectionController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationSectionController.swift; sourceTree = "<group>"; };
|
||||
2981A8A31EFE9FC700E25EF1 /* GithubEmoji.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubEmoji.swift; sourceTree = "<group>"; };
|
||||
2981A8A61EFEBEF900E25EF1 /* EmojiTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1176,6 +1178,7 @@
|
||||
children = (
|
||||
292FF8AB1F2FD3EC009E63F7 /* Settings.storyboard */,
|
||||
292FF8AD1F2FD4A8009E63F7 /* SettingsViewController.swift */,
|
||||
297FB7771F51128A00F2E618 /* SettingsAccountsViewController.swift */,
|
||||
);
|
||||
path = Settings;
|
||||
sourceTree = "<group>";
|
||||
@@ -1665,6 +1668,7 @@
|
||||
292FF8B91F303DB0009E63F7 /* IssuePreviewModel.swift in Sources */,
|
||||
295840691EE8A328007723C6 /* IssueStatusEventSectionController.swift in Sources */,
|
||||
29EE444A1F19D85800B05ED3 /* ShowErrorStatusBar.swift in Sources */,
|
||||
297FB7781F51128A00F2E618 /* SettingsAccountsViewController.swift in Sources */,
|
||||
29C0E7071ECBC6C50051D756 /* GithubClient.swift in Sources */,
|
||||
29A195111EC7AC9500C3E289 /* NotificationRepoCell.swift in Sources */,
|
||||
29416BFF1F1195D400D03E1A /* GithubClient+AccessToken.swift in Sources */,
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1458</string>
|
||||
<string>1469</string>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>LSApplicationQueriesSchemes</key>
|
||||
|
||||
@@ -14,7 +14,7 @@ func runningInSample() -> Bool {
|
||||
|
||||
func sampleUserSession() -> GithubUserSession? {
|
||||
guard runningInSample() else { return nil }
|
||||
return GithubUserSession(token: "1234", authMethod: .oauth)
|
||||
return GithubUserSession(token: "1234", authMethod: .oauth, username: "tester")
|
||||
}
|
||||
|
||||
func loadSample(path: String) -> Any? {
|
||||
|
||||
Reference in New Issue
Block a user