2022-04-12 11:32:38 +02:00
|
|
|
//
|
|
|
|
// DiscoveryViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-12.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import Tabman
|
2022-05-11 12:40:21 +02:00
|
|
|
import Pageboy
|
2022-04-12 11:32:38 +02:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonUI
|
2022-04-12 11:32:38 +02:00
|
|
|
|
|
|
|
public class DiscoveryViewController: TabmanViewController, NeedsDependency {
|
|
|
|
|
|
|
|
public static let containerViewMarginForRegularHorizontalSizeClass: CGFloat = 64
|
|
|
|
public static let containerViewMarginForCompactHorizontalSizeClass: CGFloat = 16
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "DiscoveryViewController", category: "ViewController")
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
2022-10-09 14:07:57 +02:00
|
|
|
|
|
|
|
var viewModel: DiscoveryViewModel!
|
2022-04-12 11:32:38 +02:00
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
private(set) lazy var buttonBar: TMBar.ButtonBar = {
|
2022-04-12 11:32:38 +02:00
|
|
|
let buttonBar = TMBar.ButtonBar()
|
2022-04-15 11:17:39 +02:00
|
|
|
buttonBar.backgroundView.style = .custom(view: buttonBarBackgroundView)
|
|
|
|
buttonBar.layout.interButtonSpacing = 0
|
2022-04-12 11:32:38 +02:00
|
|
|
buttonBar.layout.contentInset = .zero
|
2022-04-15 11:17:39 +02:00
|
|
|
buttonBar.indicator.backgroundColor = Asset.Colors.Label.primary.color
|
|
|
|
buttonBar.indicator.weight = .custom(value: 2)
|
2022-04-12 11:32:38 +02:00
|
|
|
return buttonBar
|
|
|
|
}()
|
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
let buttonBarBackgroundView: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
let barBottomLine = UIView.separatorLine
|
|
|
|
barBottomLine.backgroundColor = Asset.Colors.Label.secondary.color.withAlphaComponent(0.5)
|
|
|
|
barBottomLine.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(barBottomLine)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
barBottomLine.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
barBottomLine.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
barBottomLine.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
barBottomLine.heightAnchor.constraint(equalToConstant: 2).priority(.required - 1),
|
|
|
|
])
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
func customizeButtonBarAppearance() {
|
|
|
|
// The implmention use CATextlayer. Adapt for Dark Mode without dynamic colors
|
|
|
|
// Needs trigger update when `userInterfaceStyle` chagnes
|
|
|
|
let userInterfaceStyle = traitCollection.userInterfaceStyle
|
|
|
|
buttonBar.buttons.customize { button in
|
|
|
|
switch userInterfaceStyle {
|
|
|
|
case .dark:
|
|
|
|
// Asset.Colors.Label.primary.color
|
|
|
|
button.selectedTintColor = UIColor(red: 238.0/255.0, green: 238.0/255.0, blue: 238.0/255.0, alpha: 1.0)
|
|
|
|
// Asset.Colors.Label.secondary.color
|
|
|
|
button.tintColor = UIColor(red: 151.0/255.0, green: 157.0/255.0, blue: 173.0/255.0, alpha: 1.0)
|
|
|
|
default:
|
|
|
|
// Asset.Colors.Label.primary.color
|
|
|
|
button.selectedTintColor = UIColor(red: 40.0/255.0, green: 44.0/255.0, blue: 55.0/255.0, alpha: 1.0)
|
|
|
|
// Asset.Colors.Label.secondary.color
|
|
|
|
button.tintColor = UIColor(red: 60.0/255.0, green: 60.0/255.0, blue: 67.0/255.0, alpha: 0.6)
|
|
|
|
}
|
|
|
|
|
|
|
|
button.backgroundColor = .clear
|
|
|
|
button.contentInset = UIEdgeInsets(top: 12, left: 26, bottom: 12, right: 26)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 11:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryViewController {
|
|
|
|
|
|
|
|
public override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
setupAppearance(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupAppearance(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
dataSource = viewModel
|
|
|
|
addBar(
|
|
|
|
buttonBar,
|
|
|
|
dataSource: viewModel,
|
|
|
|
at: .top
|
|
|
|
)
|
2022-04-15 11:17:39 +02:00
|
|
|
customizeButtonBarAppearance()
|
|
|
|
|
|
|
|
viewModel.$viewControllers
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.reloadData()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-04-12 11:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
customizeButtonBarAppearance()
|
2022-04-12 11:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryViewController {
|
|
|
|
|
|
|
|
private func setupAppearance(theme: Theme) {
|
2022-04-15 11:17:39 +02:00
|
|
|
view.backgroundColor = theme.secondarySystemBackgroundColor
|
|
|
|
buttonBarBackgroundView.backgroundColor = theme.systemBackgroundColor
|
2022-04-12 11:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-05-11 12:40:21 +02:00
|
|
|
|
|
|
|
// MARK: - ScrollViewContainer
|
|
|
|
extension DiscoveryViewController: ScrollViewContainer {
|
2022-05-13 11:23:35 +02:00
|
|
|
var scrollView: UIScrollView {
|
|
|
|
return (currentViewController as? ScrollViewContainer)?.scrollView ?? UIScrollView()
|
2022-05-11 12:40:21 +02:00
|
|
|
}
|
2022-11-03 14:24:30 +01:00
|
|
|
func scrollToTop(animated: Bool) {
|
|
|
|
if scrollView.contentOffset.y <= 0 {
|
|
|
|
scrollToPage(.first, animated: animated)
|
|
|
|
} else {
|
|
|
|
scrollView.scrollToTop(animated: animated)
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 12:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension DiscoveryViewController {
|
|
|
|
|
|
|
|
public override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return pageboyNavigateKeyCommands
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - PageboyNavigateable
|
|
|
|
extension DiscoveryViewController: PageboyNavigateable {
|
|
|
|
|
|
|
|
var navigateablePageViewController: PageboyViewController {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func pageboyNavigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
|
|
|
|
pageboyNavigateKeyCommandHandler(sender)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|