metatext/View Controllers/ProfileViewController.swift

132 lines
4.9 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-09-27 05:54:06 +00:00
import UIKit
import ViewModels
final class ProfileViewController: TableViewController {
private let viewModel: ProfileViewModel
private var cancellables = Set<AnyCancellable>()
2020-10-07 00:31:29 +00:00
required init(viewModel: ProfileViewModel, identification: Identification) {
2020-09-27 05:54:06 +00:00
self.viewModel = viewModel
2020-10-07 00:31:29 +00:00
super.init(viewModel: viewModel, identification: identification)
2020-09-27 05:54:06 +00:00
}
override func viewDidLoad() {
super.viewDidLoad()
// Initial size is to avoid unsatisfiable constraint warning
let accountHeaderView = AccountHeaderView(frame: .init(origin: .zero, size: .init(width: 100, height: 100)))
accountHeaderView.viewModel = viewModel
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 {
// swiftlint:disable:next function_body_length
func menu(accountViewModel: AccountViewModel, relationship: Relationship) -> UIMenu {
var actions = [UIAction]()
if relationship.following {
if relationship.showingReblogs {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.hide-reblogs-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
accountViewModel.hideReblogs()
})
} else {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.show-reblogs-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "arrow.2.squarepath")) { _ in
accountViewModel.showReblogs()
})
}
}
if relationship.muting {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.unmute-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "speaker")) { _ in
accountViewModel.unmute()
})
} else {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.mute-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "speaker.slash")) { _ in
accountViewModel.mute()
})
}
if relationship.blocking {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.unblock-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "slash.circle")) { _ in
accountViewModel.unblock()
})
} else {
actions.append(UIAction(
title: String.localizedStringWithFormat(
NSLocalizedString("account.block-account", comment: ""),
accountViewModel.accountName),
image: UIImage(systemName: "slash.circle")) { _ in
accountViewModel.block()
})
}
return UIMenu(children: actions)
}
}