Multiple accounts

This commit is contained in:
Shadowfacts 2022-01-12 13:48:52 -05:00
parent 4c4044c382
commit 9deafa4b33
4 changed files with 75 additions and 16 deletions

View File

@ -18,6 +18,7 @@ class FervorController {
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FervorController") private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FervorController")
let client: FervorClient let client: FervorClient
private(set) var account: LocalData.Account?
private(set) var clientID: String? private(set) var clientID: String?
private(set) var clientSecret: String? private(set) var clientSecret: String?
private(set) var accessToken: String? private(set) var accessToken: String?
@ -31,6 +32,7 @@ class FervorController {
convenience init(account: LocalData.Account) { convenience init(account: LocalData.Account) {
self.init(instanceURL: account.instanceURL) self.init(instanceURL: account.instanceURL)
self.account = account
self.clientID = account.clientID self.clientID = account.clientID
self.clientSecret = account.clientSecret self.clientSecret = account.clientSecret
self.accessToken = account.accessToken self.accessToken = account.accessToken

View File

@ -14,19 +14,39 @@ struct LocalData {
private static let encoder = JSONEncoder() private static let encoder = JSONEncoder()
private static let decoder = JSONDecoder() private static let decoder = JSONDecoder()
static var account: Account? { static var accounts: [Account] {
get { get {
guard let data = UserDefaults.standard.data(forKey: "account") else { guard let data = UserDefaults.standard.data(forKey: "accounts"),
return nil let accounts = try? decoder.decode([Account].self, from: data) else {
return []
} }
return try? decoder.decode(Account.self, from: data) return accounts
} }
set { set {
let data = try! encoder.encode(newValue) let data = try! encoder.encode(newValue)
UserDefaults.standard.set(data, forKey: "account") UserDefaults.standard.set(data, forKey: "accounts")
} }
} }
static var mostRecentAccountID: UUID? {
get {
guard let str = UserDefaults.standard.string(forKey: "mostRecentAccountID") else {
return nil
}
return UUID(uuidString: str)
}
set {
UserDefaults.standard.set(newValue?.uuidString, forKey: "mostRecentAccountID")
}
}
static func mostRecentAccount() -> Account? {
guard let id = mostRecentAccountID else {
return nil
}
return accounts.first(where: { $0.id == id })
}
struct Account: Codable { struct Account: Codable {
let id: UUID let id: UUID
let instanceURL: URL let instanceURL: URL

View File

@ -22,7 +22,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
window = UIWindow(windowScene: windowScene) window = UIWindow(windowScene: windowScene)
window!.tintColor = .appTintColor window!.tintColor = .appTintColor
if let account = LocalData.account { if let account = LocalData.mostRecentAccount() {
fervorController = FervorController(account: account) fervorController = FervorController(account: account)
createAppUI() createAppUI()
} else { } else {
@ -68,6 +68,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private func createAppUI() { private func createAppUI() {
let home = HomeViewController(fervorController: fervorController) let home = HomeViewController(fervorController: fervorController)
home.delegate = self
let nav = AppNavigationController(rootViewController: home) let nav = AppNavigationController(rootViewController: home)
nav.navigationBar.prefersLargeTitles = true nav.navigationBar.prefersLargeTitles = true
window!.rootViewController = nav window!.rootViewController = nav
@ -81,8 +82,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
extension SceneDelegate: LoginViewControllerDelegate { extension SceneDelegate: LoginViewControllerDelegate {
func didLogin(with controller: FervorController) { func didLogin(with controller: FervorController) {
LocalData.account = .init(instanceURL: controller.instanceURL, clientID: controller.clientID!, clientSecret: controller.clientSecret!, accessToken: controller.accessToken!) let account = LocalData.Account(instanceURL: controller.instanceURL, clientID: controller.clientID!, clientSecret: controller.clientSecret!, accessToken: controller.accessToken!)
fervorController = FervorController(account: LocalData.account!) LocalData.accounts.append(account)
LocalData.mostRecentAccountID = account.id
fervorController = FervorController(account: account)
createAppUI()
}
}
extension SceneDelegate: HomeViewControllerDelegate {
func switchToAccount(_ account: LocalData.Account) {
LocalData.mostRecentAccountID = account.id
fervorController = FervorController(account: account)
createAppUI() createAppUI()
} }
} }

View File

@ -8,8 +8,14 @@
import UIKit import UIKit
import CoreData import CoreData
protocol HomeViewControllerDelegate: AnyObject {
func switchToAccount(_ account: LocalData.Account)
}
class HomeViewController: UIViewController { class HomeViewController: UIViewController {
weak var delegate: HomeViewControllerDelegate?
let fervorController: FervorController let fervorController: FervorController
private var collectionView: UICollectionView! private var collectionView: UICollectionView!
@ -246,13 +252,33 @@ extension HomeViewController: StretchyMenuInteractionDelegate {
return "Switch Accounts" return "Switch Accounts"
} }
func stretchyMenuItems() -> [StretchyMenuItem] { func stretchyMenuItems() -> [StretchyMenuItem] {
return [ var items: [StretchyMenuItem] = LocalData.accounts.map { account in
StretchyMenuItem(title: "foo", subtitle: "bar", action: { var title = account.instanceURL.host!
print("foo") if let port = account.instanceURL.port, port != 80 && port != 443 {
}), title += ":\(port)"
StretchyMenuItem(title: "baz", subtitle: "qux", action: { }
print("baz") let subtitle = account.id == fervorController.account!.id ? "Currently logged in" : nil
}), return StretchyMenuItem(title: title, subtitle: subtitle) { [unowned self] in
] guard account.id != self.fervorController.account!.id else { return }
self.delegate?.switchToAccount(account)
}
}
items.append(StretchyMenuItem(title: "Add Account", subtitle: nil, action: { [unowned self] in
let login = LoginViewController()
login.delegate = self
login.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction(handler: { (_) in
self.dismiss(animated: true)
}), menu: nil)
let nav = UINavigationController(rootViewController: login)
self.present(nav, animated: true)
}))
return items
}
}
extension HomeViewController: LoginViewControllerDelegate {
func didLogin(with controller: FervorController) {
self.dismiss(animated: true)
(view.window!.windowScene!.delegate as! SceneDelegate).didLogin(with: controller)
} }
} }