metatext/View Controllers/ProfileViewController.swift

170 lines
6.3 KiB
Swift
Raw Permalink 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,
2021-02-15 08:47:30 +00:00
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,
2021-02-06 22:08:49 +00:00
accountViewModel.id != self.viewModel.identityContext.identity.account?.id,
2020-11-17 06:46:48 +00:00
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 {
2021-02-11 03:52:21 +00:00
var actions = [UIAction(
title: NSLocalizedString("share", comment: ""),
image: UIImage(systemName: "square.and.arrow.up")) { _ in
accountViewModel.share()
}]
2020-11-17 06:46:48 +00:00
if relationship.following {
actions.append(UIAction(
title: NSLocalizedString("account.add-remove-lists", comment: ""),
image: UIImage(systemName: "scroll")) { [weak self] _ in
self?.addRemoveFromLists(accountViewModel: accountViewModel)
})
2020-11-17 06:46:48 +00:00
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
2021-02-11 03:52:21 +00:00
accountViewModel.confirmHideReblogs()
2020-11-17 06:46:48 +00:00
})
} 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
2021-02-11 03:52:21 +00:00
accountViewModel.confirmShowReblogs()
2020-11-17 06:46:48 +00:00
})
}
}
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
2021-02-10 23:41:41 +00:00
accountViewModel.confirmUnmute()
2020-11-17 06:46:48 +00:00
})
} 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
2021-02-10 23:41:41 +00:00
accountViewModel.confirmMute()
2020-11-17 06:46:48 +00:00
})
}
2021-02-10 07:46:00 +00:00
if relationship.blocking {
actions.append(UIAction(
title: NSLocalizedString("account.unblock", comment: ""),
image: UIImage(systemName: "slash.circle"),
attributes: .destructive) { _ in
accountViewModel.confirmUnblock()
})
} else {
actions.append(UIAction(
title: NSLocalizedString("account.block", comment: ""),
image: UIImage(systemName: "slash.circle"),
attributes: .destructive) { _ in
accountViewModel.confirmBlock()
})
}
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-12-04 03:13:18 +00:00
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"),
2021-02-10 07:46:00 +00:00
attributes: .destructive) { _ in
accountViewModel.confirmDomainUnblock(domain: domain)
2020-12-04 03:13:18 +00:00
})
} else {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.domain-block-%@", comment: ""),
domain),
image: UIImage(systemName: "slash.circle"),
2021-02-10 07:46:00 +00:00
attributes: .destructive) { _ in
accountViewModel.confirmDomainBlock(domain: domain)
})
2020-12-04 03:13:18 +00:00
}
2020-11-17 06:46:48 +00:00
}
return UIMenu(children: actions)
}
}