Cleanup previewing actions code

This commit is contained in:
Shadowfacts 2022-11-11 23:34:41 -05:00
parent eb7fe22863
commit 072a77b58e
1 changed files with 23 additions and 52 deletions

View File

@ -149,12 +149,7 @@ extension MenuActionProvider {
self.mastodonController?.persistentContainer.addOrUpdate(status: status) self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error): case .failure(let error):
if let toastable = self.toastableViewController { self.handleError(error, title: "Error \(bookmarked ? "Unb" : "B")ookmarking")
let config = ToastConfiguration(from: error, with: "Error \(bookmarked ? "Unb" : "B")ookmarking", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
} }
} }
}), }),
@ -210,12 +205,7 @@ extension MenuActionProvider {
case .success(let status, _): case .success(let status, _):
self.mastodonController?.persistentContainer.addOrUpdate(status: status) self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error): case .failure(let error):
if let toastable = self.toastableViewController { self.handleError(error, title: "Error \(muted ? "Unm" : "M")uting")
let config = ToastConfiguration(from: error, with: "Error \(muted ? "Unm" : "M")uting", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
} }
} }
})) }))
@ -234,12 +224,7 @@ extension MenuActionProvider {
case .success(let status, _): case .success(let status, _):
self.mastodonController?.persistentContainer.addOrUpdate(status: status) self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error): case .failure(let error):
if let toastable = self.toastableViewController { self.handleError(error, title: "Error \(pinned ? "Unp" :"P")inning")
let config = ToastConfiguration(from: error, with: "Error \(pinned ? "Unp" :"P")inning", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
} }
}) })
})) }))
@ -259,12 +244,7 @@ extension MenuActionProvider {
mastodonController.persistentContainer.addOrUpdate(status: status, context: mastodonController.persistentContainer.viewContext) mastodonController.persistentContainer.addOrUpdate(status: status, context: mastodonController.persistentContainer.viewContext)
} }
case .failure(let error): case .failure(let error):
if let toastable = self?.toastableViewController { self?.handleError(error, title: "Error Refreshing Poll")
let config = ToastConfiguration(from: error, with: "Error Refreshing Poll", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
} }
}) })
}), at: 0) }), at: 0)
@ -350,6 +330,15 @@ extension MenuActionProvider {
}) })
} }
private func handleError(_ error: Client.Error, title: String) {
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: title, in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
}
private func relationshipAction(accountID: String, mastodonController: MastodonController, builder: @escaping @MainActor (RelationshipMO, MastodonController) -> UIMenuElement) -> UIDeferredMenuElement { private func relationshipAction(accountID: String, mastodonController: MastodonController, builder: @escaping @MainActor (RelationshipMO, MastodonController) -> UIMenuElement) -> UIDeferredMenuElement {
return UIDeferredMenuElement.uncached({ @MainActor elementHandler in return UIDeferredMenuElement.uncached({ @MainActor elementHandler in
let relationship = Task { let relationship = Task {
@ -375,17 +364,12 @@ extension MenuActionProvider {
private func followAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { private func followAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement {
let accountID = relationship.accountID let accountID = relationship.accountID
let following = relationship.following let following = relationship.following
return createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus") { _ in return createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus") { [weak self] _ in
let request = (following ? Account.unfollow : Account.follow)(accountID) let request = (following ? Account.unfollow : Account.follow)(accountID)
mastodonController.run(request) { response in mastodonController.run(request) { response in
switch response { switch response {
case .failure(let error): case .failure(let error):
if let toastable = self.toastableViewController { self?.handleError(error, title: "Error \(following ? "Unf" : "F")ollowing")
let config = ToastConfiguration(from: error, with: "Error \(following ? "Unf" : "F")ollowing", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
case .success(let relationship, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship) mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
} }
@ -398,21 +382,13 @@ extension MenuActionProvider {
let accountID = relationship.accountID let accountID = relationship.accountID
let displayName = relationship.account!.displayOrUserName let displayName = relationship.account!.displayOrUserName
let host = relationship.account!.url.host! let host = relationship.account!.url.host!
let handleFailure = { (isActionBlock: Bool, error: Client.Error) in
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error \(isActionBlock ? "B" : "Unb")locking", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
}
let handler = { (block: Bool) in let handler = { (block: Bool) in
return { (_: UIAction) in return { [weak self] (_: UIAction) in
let req = block ? Account.block(accountID) : Account.unblock(accountID) let req = block ? Account.block(accountID) : Account.unblock(accountID)
_ = mastodonController.run(req) { response in _ = mastodonController.run(req) { response in
switch response { switch response {
case .failure(let error): case .failure(let error):
handleFailure(block, error) self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking")
case .success(let relationship, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship) mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
} }
@ -420,11 +396,11 @@ extension MenuActionProvider {
} }
} }
let domainHandler = { (block: Bool) in let domainHandler = { (block: Bool) in
return { (_: UIAction) in return { [weak self] (_: UIAction) in
let req = block ? Client.block(domain: host) : Client.unblock(domain: host) let req = block ? Client.block(domain: host) : Client.unblock(domain: host)
mastodonController.run(req) { response in mastodonController.run(req) { response in
if case .failure(let error) = response { if case .failure(let error) = response {
handleFailure(block, error) self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking")
} }
} }
} }
@ -446,27 +422,22 @@ extension MenuActionProvider {
@MainActor @MainActor
private func muteAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { private func muteAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement {
if relationship.muting || relationship.mutingNotifications { if relationship.muting || relationship.mutingNotifications {
return UIAction(title: "Unmute", image: UIImage(systemName: "speaker")) { [unowned self] _ in return UIAction(title: "Unmute", image: UIImage(systemName: "speaker")) { [weak self] _ in
let req = Account.unmute(relationship.accountID) let req = Account.unmute(relationship.accountID)
mastodonController.run(req) { response in mastodonController.run(req) { response in
switch response { switch response {
case .failure(let error): case .failure(let error):
if let toastable = self.toastableViewController { self?.handleError(error, title: "Error Unmuting")
let config = ToastConfiguration(from: error, with: "Error Unmuting", in: toastable, retryAction: nil)
DispatchQueue.main.async {
toastable.showToast(configuration: config, animated: true)
}
}
case .success(let relationship, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship) mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
} }
} }
} }
} else { } else {
return UIAction(title: "Mute", image: UIImage(systemName: "speaker.slash")) { [unowned self] _ in return UIAction(title: "Mute", image: UIImage(systemName: "speaker.slash")) { [weak self] _ in
let view = MuteAccountView(account: relationship.account!, mastodonController: mastodonController) let view = MuteAccountView(account: relationship.account!, mastodonController: mastodonController)
let host = UIHostingController(rootView: view) let host = UIHostingController(rootView: view)
self.navigationDelegate?.present(host, animated: true) self?.navigationDelegate?.present(host, animated: true)
} }
} }
} }