Add logout action on iPhone
This commit is contained in:
parent
ad90eba9b6
commit
3990997e6e
|
@ -71,6 +71,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateAppearance), name: .appearanceChanged, object: nil)
|
||||
updateAppearance()
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(logoutIfNecessary), name: .logoutAccount, object: nil)
|
||||
}
|
||||
|
||||
func sceneDidDisconnect(_ scene: UIScene) {
|
||||
|
@ -256,6 +257,21 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|||
createAppUI()
|
||||
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
|
||||
|
||||
extension Notification.Name {
|
||||
static let logoutAccount = Notification.Name("logoutAccount")
|
||||
}
|
||||
|
|
|
@ -306,12 +306,21 @@ extension HomeViewController: StretchyMenuInteractionDelegate {
|
|||
title += ":\(port)"
|
||||
}
|
||||
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 }
|
||||
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()
|
||||
login.delegate = self
|
||||
login.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction(handler: { (_) in
|
||||
|
|
|
@ -393,7 +393,7 @@ extension ReadViewController: StretchyMenuInteractionDelegate {
|
|||
return []
|
||||
}
|
||||
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)
|
||||
}),
|
||||
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)
|
||||
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)
|
||||
}), at: 1)
|
||||
#endif
|
||||
|
|
|
@ -10,7 +10,15 @@ import UIKit
|
|||
struct StretchyMenuItem {
|
||||
let title: String
|
||||
let subtitle: String?
|
||||
let menu: UIMenu?
|
||||
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 {
|
||||
|
@ -350,6 +358,10 @@ private class MenuItemView: UIView {
|
|||
])
|
||||
}
|
||||
|
||||
if item.menu != nil {
|
||||
addInteraction(UIContextMenuInteraction(delegate: self))
|
||||
}
|
||||
|
||||
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!
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue