2020-09-27 07:54:06 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
2020-11-17 07:46:48 +01:00
|
|
|
import Mastodon
|
2020-11-30 03:54:11 +01:00
|
|
|
import SwiftUI
|
2020-09-27 07:54:06 +02:00
|
|
|
import ViewModels
|
|
|
|
|
|
|
|
final class ProfileViewController: TableViewController {
|
|
|
|
private let viewModel: ProfileViewModel
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-01-23 04:48:33 +01:00
|
|
|
required init(
|
|
|
|
viewModel: ProfileViewModel,
|
2021-02-15 09:47:30 +01:00
|
|
|
rootViewModel: RootViewModel?,
|
2021-01-26 01:06:35 +01:00
|
|
|
identityContext: IdentityContext,
|
2021-01-23 04:48:33 +01:00
|
|
|
parentNavigationController: UINavigationController?) {
|
2020-09-27 07:54:06 +02:00
|
|
|
self.viewModel = viewModel
|
|
|
|
|
2021-01-23 04:48:33 +01:00
|
|
|
super.init(
|
|
|
|
viewModel: viewModel,
|
|
|
|
rootViewModel: rootViewModel,
|
|
|
|
parentNavigationController: parentNavigationController)
|
2020-09-27 07:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-01-31 02:43:48 +01:00
|
|
|
let accountHeaderView = AccountHeaderView(viewModel: viewModel)
|
2020-09-27 07:54:06 +02:00
|
|
|
|
|
|
|
viewModel.$accountViewModel
|
|
|
|
.receive(on: DispatchQueue.main)
|
2020-11-17 07:46:48 +01:00
|
|
|
.sink { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
accountHeaderView.viewModel = self.viewModel
|
|
|
|
self.sizeTableHeaderFooterViews()
|
|
|
|
|
|
|
|
if let accountViewModel = $0,
|
2021-02-06 23:08:49 +01:00
|
|
|
accountViewModel.id != self.viewModel.identityContext.identity.account?.id,
|
2020-11-17 07:46:48 +01:00
|
|
|
let relationship = accountViewModel.relationship {
|
|
|
|
self.navigationItem.rightBarButtonItem = UIBarButtonItem(
|
|
|
|
image: UIImage(systemName: "ellipsis.circle"),
|
|
|
|
menu: self.menu(accountViewModel: accountViewModel, relationship: relationship))
|
|
|
|
}
|
2020-09-27 07:54:06 +02:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2020-10-23 00:16:06 +02: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 07:54:06 +02:00
|
|
|
tableView.tableHeaderView = accountHeaderView
|
|
|
|
}
|
2020-11-11 08:31:56 +01:00
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
viewModel.fetchProfile()
|
|
|
|
.sink { _ in }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
2020-09-27 07:54:06 +02:00
|
|
|
}
|
2020-11-17 07:46:48 +01:00
|
|
|
|
|
|
|
private extension ProfileViewController {
|
2020-11-30 03:54:11 +01:00
|
|
|
// swiftlint:disable:next function_body_length
|
2020-11-17 07:46:48 +01:00
|
|
|
func menu(accountViewModel: AccountViewModel, relationship: Relationship) -> UIMenu {
|
2021-02-11 04:52:21 +01:00
|
|
|
var actions = [UIAction(
|
|
|
|
title: NSLocalizedString("share", comment: ""),
|
|
|
|
image: UIImage(systemName: "square.and.arrow.up")) { _ in
|
|
|
|
accountViewModel.share()
|
|
|
|
}]
|
2020-11-17 07:46:48 +01:00
|
|
|
|
|
|
|
if relationship.following {
|
2021-03-18 01:59:10 +01:00
|
|
|
actions.append(UIAction(
|
|
|
|
title: NSLocalizedString("account.add-remove-lists", comment: ""),
|
|
|
|
image: UIImage(systemName: "scroll")) { [weak self] _ in
|
|
|
|
self?.addRemoveFromLists(accountViewModel: accountViewModel)
|
|
|
|
})
|
|
|
|
|
2020-11-17 07:46:48 +01:00
|
|
|
if relationship.showingReblogs {
|
|
|
|
actions.append(UIAction(
|
2020-11-22 21:20:36 +01:00
|
|
|
title: NSLocalizedString("account.hide-reblogs", comment: ""),
|
2020-11-17 07:46:48 +01:00
|
|
|
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
|
2021-02-11 04:52:21 +01:00
|
|
|
accountViewModel.confirmHideReblogs()
|
2020-11-17 07:46:48 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
actions.append(UIAction(
|
2020-11-22 21:20:36 +01:00
|
|
|
title: NSLocalizedString("account.show-reblogs", comment: ""),
|
2020-11-17 07:46:48 +01:00
|
|
|
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
|
2021-02-11 04:52:21 +01:00
|
|
|
accountViewModel.confirmShowReblogs()
|
2020-11-17 07:46:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if relationship.muting {
|
|
|
|
actions.append(UIAction(
|
2020-11-22 21:20:36 +01:00
|
|
|
title: NSLocalizedString("account.unmute", comment: ""),
|
2020-11-17 07:46:48 +01:00
|
|
|
image: UIImage(systemName: "speaker")) { _ in
|
2021-02-11 00:41:41 +01:00
|
|
|
accountViewModel.confirmUnmute()
|
2020-11-17 07:46:48 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
actions.append(UIAction(
|
2020-11-22 21:20:36 +01:00
|
|
|
title: NSLocalizedString("account.mute", comment: ""),
|
2020-11-17 07:46:48 +01:00
|
|
|
image: UIImage(systemName: "speaker.slash")) { _ in
|
2021-02-11 00:41:41 +01:00
|
|
|
accountViewModel.confirmMute()
|
2020-11-17 07:46:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:46:00 +01: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 03:54:11 +01: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 06:56:15 +01:00
|
|
|
self.report(reportViewModel: reportViewModel)
|
2020-11-30 03:54:11 +01:00
|
|
|
})
|
|
|
|
|
2020-12-04 04:13:18 +01: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 08:46:00 +01:00
|
|
|
attributes: .destructive) { _ in
|
|
|
|
accountViewModel.confirmDomainUnblock(domain: domain)
|
2020-12-04 04:13:18 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
actions.append(UIAction(
|
|
|
|
title: String.localizedStringWithFormat(
|
|
|
|
NSLocalizedString("account.domain-block-%@", comment: ""),
|
|
|
|
domain),
|
|
|
|
image: UIImage(systemName: "slash.circle"),
|
2021-02-10 08:46:00 +01:00
|
|
|
attributes: .destructive) { _ in
|
|
|
|
accountViewModel.confirmDomainBlock(domain: domain)
|
|
|
|
})
|
2020-12-04 04:13:18 +01:00
|
|
|
}
|
2020-11-17 07:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return UIMenu(children: actions)
|
|
|
|
}
|
|
|
|
}
|