metatext/View Controllers/ProfileViewController.swift

189 lines
7.3 KiB
Swift
Raw Normal View History

2020-09-27 05:54:06 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-11-17 06:46:48 +00:00
import Mastodon
2020-11-30 02:54:11 +00:00
import SwiftUI
2020-09-27 05:54:06 +00:00
import ViewModels
final class ProfileViewController: TableViewController {
private let viewModel: ProfileViewModel
private var cancellables = Set<AnyCancellable>()
2021-01-23 03:48:33 +00:00
required init(
viewModel: ProfileViewModel,
rootViewModel: RootViewModel,
2021-01-26 00:06:35 +00:00
identityContext: IdentityContext,
2021-01-23 03:48:33 +00:00
parentNavigationController: UINavigationController?) {
2020-09-27 05:54:06 +00:00
self.viewModel = viewModel
2021-01-23 03:48:33 +00:00
super.init(
viewModel: viewModel,
rootViewModel: rootViewModel,
parentNavigationController: parentNavigationController)
2020-09-27 05:54:06 +00:00
}
override func viewDidLoad() {
super.viewDidLoad()
2021-01-31 01:43:48 +00:00
let accountHeaderView = AccountHeaderView(viewModel: viewModel)
2020-09-27 05:54:06 +00:00
viewModel.$accountViewModel
.receive(on: DispatchQueue.main)
2020-11-17 06:46:48 +00:00
.sink { [weak self] in
guard let self = self else { return }
accountHeaderView.viewModel = self.viewModel
self.sizeTableHeaderFooterViews()
if let accountViewModel = $0,
let relationship = accountViewModel.relationship {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "ellipsis.circle"),
menu: self.menu(accountViewModel: accountViewModel, relationship: relationship))
}
2020-09-27 05:54:06 +00:00
}
.store(in: &cancellables)
2020-10-22 22:16:06 +00:00
viewModel.imagePresentations.sink { [weak self] in
guard let self = self else { return }
let imagePageViewController = ImagePageViewController(imageURL: $0)
let imageNavigationController = ImageNavigationController(imagePageViewController: imagePageViewController)
imageNavigationController.transitionController.fromDelegate = self
self.transitionViewTag = $0.hashValue
self.present(imageNavigationController, animated: true)
}
.store(in: &cancellables)
2020-09-27 05:54:06 +00:00
tableView.tableHeaderView = accountHeaderView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel.fetchProfile()
.sink { _ in }
.store(in: &cancellables)
}
2020-09-27 05:54:06 +00:00
}
2020-11-17 06:46:48 +00:00
private extension ProfileViewController {
2020-11-30 02:54:11 +00:00
// swiftlint:disable:next function_body_length
2020-11-17 06:46:48 +00:00
func menu(accountViewModel: AccountViewModel, relationship: Relationship) -> UIMenu {
var actions = [UIAction]()
if relationship.following {
if relationship.showingReblogs {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.hide-reblogs", comment: ""),
2020-11-17 06:46:48 +00:00
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
accountViewModel.hideReblogs()
})
} else {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.show-reblogs", comment: ""),
2020-11-17 06:46:48 +00:00
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
accountViewModel.showReblogs()
})
}
}
if relationship.muting {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.unmute", comment: ""),
2020-11-17 06:46:48 +00:00
image: UIImage(systemName: "speaker")) { _ in
accountViewModel.unmute()
})
} else {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.mute", comment: ""),
2020-11-17 06:46:48 +00:00
image: UIImage(systemName: "speaker.slash")) { _ in
accountViewModel.mute()
})
}
2020-11-30 02:54:11 +00:00
actions.append(UIAction(
title: NSLocalizedString("report", comment: ""),
image: UIImage(systemName: "flag"),
attributes: .destructive) { [weak self] _ in
guard let self = self,
let reportViewModel = self.viewModel.accountViewModel?.reportViewModel()
else { return }
2021-01-10 05:56:15 +00:00
self.report(reportViewModel: reportViewModel)
2020-11-30 02:54:11 +00:00
})
2020-11-17 06:46:48 +00:00
if relationship.blocking {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.unblock", comment: ""),
2020-12-04 03:13:18 +00:00
image: UIImage(systemName: "slash.circle"),
attributes: .destructive) { [weak self] _ in
self?.confirm(message: String.localizedStringWithFormat(
NSLocalizedString("account.unblock.confirm-%@", comment: ""),
accountViewModel.accountName)) {
accountViewModel.unblock()
}
})
2020-11-17 06:46:48 +00:00
} else {
actions.append(UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.block", comment: ""),
image: UIImage(systemName: "slash.circle"),
2020-12-04 03:13:18 +00:00
attributes: .destructive) { [weak self] _ in
self?.confirm(message: String.localizedStringWithFormat(
NSLocalizedString("account.block.confirm-%@", comment: ""),
accountViewModel.accountName)) {
accountViewModel.block()
}
})
}
if !accountViewModel.isLocal, let domain = accountViewModel.domain {
if relationship.domainBlocking {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.domain-unblock-%@", comment: ""),
domain),
image: UIImage(systemName: "slash.circle"),
attributes: .destructive) { [weak self] _ in
self?.confirm(message: String.localizedStringWithFormat(
NSLocalizedString("account.domain-unblock.confirm-%@", comment: ""),
domain)) {
accountViewModel.domainUnblock()
}
})
} else {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.domain-block-%@", comment: ""),
domain),
image: UIImage(systemName: "slash.circle"),
attributes: .destructive) { [weak self] _ in
self?.confirm(message: String.localizedStringWithFormat(
NSLocalizedString("account.domain-block.confirm-%@", comment: ""),
domain)) {
accountViewModel.domainBlock()
}
})
}
2020-11-17 06:46:48 +00:00
}
return UIMenu(children: actions)
}
2020-12-04 03:13:18 +00:00
func confirm(message: String, action: @escaping () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel, handler: nil)
let okAction = UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .destructive) { _ in
action()
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
present(alertController, animated: true)
}
2020-11-17 06:46:48 +00:00
}