Add logout action on iPhone

This commit is contained in:
Shadowfacts 2023-01-14 13:41:45 -05:00
parent ad90eba9b6
commit 3990997e6e
4 changed files with 53 additions and 4 deletions

View File

@ -71,6 +71,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
NotificationCenter.default.addObserver(self, selector: #selector(updateAppearance), name: .appearanceChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateAppearance), name: .appearanceChanged, object: nil)
updateAppearance() updateAppearance()
NotificationCenter.default.addObserver(self, selector: #selector(logoutIfNecessary), name: .logoutAccount, object: nil)
} }
func sceneDidDisconnect(_ scene: UIScene) { func sceneDidDisconnect(_ scene: UIScene) {
@ -256,6 +257,21 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
createAppUI() createAppUI()
syncFromServer() syncFromServer()
} }
@objc private func logoutIfNecessary(_ notification: Notification) {
guard let account = notification.object as? LocalData.Account else {
return
}
if fervorController.account?.id == account.id {
if let next = LocalData.mostRecentAccount() ?? LocalData.accounts.first {
Task {
await switchToAccount(next)
}
} else {
createLoginUI()
}
}
}
} }
@ -307,3 +323,7 @@ extension SceneDelegate: NSToolbarDelegate {
} }
} }
#endif #endif
extension Notification.Name {
static let logoutAccount = Notification.Name("logoutAccount")
}

View File

@ -306,12 +306,21 @@ extension HomeViewController: StretchyMenuInteractionDelegate {
title += ":\(port)" title += ":\(port)"
} }
let subtitle = account.id == fervorController.account!.id ? "Currently logged in" : nil let subtitle = account.id == fervorController.account!.id ? "Currently logged in" : nil
return StretchyMenuItem(title: title, subtitle: subtitle) { [unowned self] in let menu = UIMenu(children: [
UIAction(title: "Log Out", image: UIImage(systemName: "door.left.hand.open"), handler: { _ in
guard let index = LocalData.accounts.firstIndex(where: { $0.id == account.id }) else {
return
}
LocalData.accounts.remove(at: index)
NotificationCenter.default.post(name: .logoutAccount, object: account)
})
])
return StretchyMenuItem(title: title, subtitle: subtitle, menu: menu) { [unowned self] in
guard account.id != self.fervorController.account!.id else { return } guard account.id != self.fervorController.account!.id else { return }
self.delegate?.switchToAccount(account) self.delegate?.switchToAccount(account)
} }
} }
items.append(StretchyMenuItem(title: "Add Account", subtitle: nil, action: { [unowned self] in items.append(StretchyMenuItem(title: "Add Account", action: { [unowned self] in
let login = LoginViewController() let login = LoginViewController()
login.delegate = self login.delegate = self
login.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction(handler: { (_) in login.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction(handler: { (_) in

View File

@ -393,7 +393,7 @@ extension ReadViewController: StretchyMenuInteractionDelegate {
return [] return []
} }
var items = [ var items = [
StretchyMenuItem(title: "Open in Safari", subtitle: nil, action: { [unowned self] in StretchyMenuItem(title: "Open in Safari", action: { [unowned self] in
self.present(createSafariVC(url: url), animated: true) self.present(createSafariVC(url: url), animated: true)
}), }),
StretchyMenuItem(title: item.read ? "Mark as Unread" : "Mark as Read", subtitle: nil, action: { [unowned self] in StretchyMenuItem(title: item.read ? "Mark as Unread" : "Mark as Read", subtitle: nil, action: { [unowned self] in
@ -403,7 +403,7 @@ extension ReadViewController: StretchyMenuInteractionDelegate {
}), }),
] ]
#if !targetEnvironment(macCatalyst) #if !targetEnvironment(macCatalyst)
items.insert(StretchyMenuItem(title: "Share", subtitle: nil, action: { [unowned self] in items.insert(StretchyMenuItem(title: "Share", action: { [unowned self] in
self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true) self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true)
}), at: 1) }), at: 1)
#endif #endif

View File

@ -10,7 +10,15 @@ import UIKit
struct StretchyMenuItem { struct StretchyMenuItem {
let title: String let title: String
let subtitle: String? let subtitle: String?
let menu: UIMenu?
let action: () -> Void let action: () -> Void
init(title: String, subtitle: String? = nil, menu: UIMenu? = nil, action: @escaping () -> Void) {
self.title = title
self.subtitle = subtitle
self.menu = menu
self.action = action
}
} }
protocol StretchyMenuInteractionDelegate: AnyObject { protocol StretchyMenuInteractionDelegate: AnyObject {
@ -350,6 +358,10 @@ private class MenuItemView: UIView {
]) ])
} }
if item.menu != nil {
addInteraction(UIContextMenuInteraction(delegate: self))
}
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemTapped))) addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemTapped)))
} }
@ -403,3 +415,11 @@ private class MenuItemView: UIView {
} }
} }
extension MenuItemView: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(actionProvider: { [unowned self] _ in
return self.item.menu!
})
}
}