2019-09-14 19:55:06 +00:00
|
|
|
|
//
|
|
|
|
|
// TimelinesPageViewController.swift
|
|
|
|
|
// Tusker
|
|
|
|
|
//
|
|
|
|
|
// Created by Shadowfacts on 9/14/19.
|
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2022-12-03 02:40:14 +00:00
|
|
|
|
import SwiftUI
|
2022-12-21 04:37:12 +00:00
|
|
|
|
import Pachyderm
|
2022-12-24 17:20:13 +00:00
|
|
|
|
import Combine
|
2019-09-14 19:55:06 +00:00
|
|
|
|
|
2022-12-13 01:57:38 +00:00
|
|
|
|
class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageViewController.Page> {
|
2019-09-14 19:55:06 +00:00
|
|
|
|
|
|
|
|
|
private let homeTitle = NSLocalizedString("Home", comment: "home timeline tab title")
|
|
|
|
|
private let federatedTitle = NSLocalizedString("Federated", comment: "federated timeline tab title")
|
|
|
|
|
private let localTitle = NSLocalizedString("Local", comment: "local timeline tab title")
|
|
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
|
weak var mastodonController: MastodonController!
|
2020-01-05 20:25:07 +00:00
|
|
|
|
|
2022-12-24 17:20:13 +00:00
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2022-12-21 04:37:12 +00:00
|
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
let pages = mastodonController.accountPreferences.pinnedTimelines.map {
|
|
|
|
|
Page(mastodonController: mastodonController, timeline: $0)
|
|
|
|
|
}
|
|
|
|
|
super.init(pages: pages) { page in
|
|
|
|
|
let vc = TimelineViewController(for: page.timeline, mastodonController: page.mastodonController)
|
|
|
|
|
vc.title = page.segmentedControlTitle
|
|
|
|
|
vc.persistsState = true
|
|
|
|
|
return vc
|
|
|
|
|
}
|
2019-09-14 19:55:06 +00:00
|
|
|
|
|
|
|
|
|
title = homeTitle
|
|
|
|
|
tabBarItem.image = UIImage(systemName: "house.fill")
|
2022-12-03 02:40:14 +00:00
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
let customizeItem = UIBarButtonItem(image: UIImage(systemName: "slider.horizontal.3"), style: .plain, target: self, action: #selector(customizePressed))
|
|
|
|
|
customizeItem.accessibilityLabel = "Customize Timelines"
|
|
|
|
|
navigationItem.rightBarButtonItem = customizeItem
|
2022-12-05 22:13:45 +00:00
|
|
|
|
|
|
|
|
|
let jumpToPresentName = NSMutableAttributedString("Jump to Present")
|
|
|
|
|
// otherwise it pronounces it as 'pɹizˈənt'
|
|
|
|
|
// its IPA is also bad, this should be an alveolar approximant not a trill
|
|
|
|
|
jumpToPresentName.addAttribute(.accessibilitySpeechIPANotation, value: "ˈprɛ.zənt", range: NSRange(location: "Jump to ".count, length: "Present".count))
|
|
|
|
|
segmentedControl.accessibilityCustomActions = [
|
|
|
|
|
UIAccessibilityCustomAction(attributedName: jumpToPresentName, actionHandler: { [unowned self] _ in
|
2022-12-21 04:37:12 +00:00
|
|
|
|
guard let vc = currentViewController as? TimelineViewController else {
|
2022-12-05 22:13:45 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
Task {
|
|
|
|
|
await vc.checkPresent(jumpImmediately: true)
|
|
|
|
|
}
|
|
|
|
|
return true
|
2022-12-23 21:35:58 +00:00
|
|
|
|
}),
|
|
|
|
|
UIAccessibilityCustomAction(name: "Jump to Sync Position", actionHandler: { [unowned self] _ in
|
|
|
|
|
guard let vc = currentViewController as? TimelineViewController else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-01-01 17:25:44 +00:00
|
|
|
|
Task {
|
|
|
|
|
_ = await vc.restoreState()
|
|
|
|
|
}
|
2022-12-23 21:35:58 +00:00
|
|
|
|
return true
|
|
|
|
|
}),
|
2022-12-05 22:13:45 +00:00
|
|
|
|
]
|
2022-12-21 04:37:12 +00:00
|
|
|
|
|
2022-12-24 17:20:13 +00:00
|
|
|
|
mastodonController.accountPreferences.publisher(for: \.pinnedTimelinesData)
|
|
|
|
|
.map { _ in () }
|
|
|
|
|
.merge(with: NotificationCenter.default.publisher(for: .accountPreferencesChangedRemotely).map { _ in () })
|
|
|
|
|
.sink { _ in
|
|
|
|
|
let pages = self.mastodonController.accountPreferences.pinnedTimelines.map {
|
|
|
|
|
Page(mastodonController: self.mastodonController, timeline: $0)
|
|
|
|
|
}
|
|
|
|
|
self.setPages(pages, animated: false)
|
2022-12-21 04:37:12 +00:00
|
|
|
|
}
|
2022-12-24 17:20:13 +00:00
|
|
|
|
.store(in: &cancellables)
|
2019-09-14 19:55:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
2022-11-23 16:35:25 +00:00
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
func selectTimeline(_ timeline: Timeline, animated: Bool) {
|
|
|
|
|
self.selectPage(Page(mastodonController: mastodonController, timeline: timeline), animated: animated)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 21:33:19 +00:00
|
|
|
|
func stateRestorationActivity() -> NSUserActivity? {
|
2022-12-31 21:46:00 +00:00
|
|
|
|
return (currentViewController as? TimelineViewController)?.stateRestorationActivity()
|
2022-11-28 21:33:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 16:35:25 +00:00
|
|
|
|
func restoreActivity(_ activity: NSUserActivity) {
|
|
|
|
|
guard let timeline = UserActivityManager.getTimeline(from: activity) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-12-21 04:37:12 +00:00
|
|
|
|
let page = Page(mastodonController: mastodonController, timeline: timeline)
|
2022-12-13 01:57:38 +00:00
|
|
|
|
selectPage(page, animated: false)
|
2022-11-23 16:35:25 +00:00
|
|
|
|
}
|
2022-12-03 02:40:14 +00:00
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
@objc private func customizePressed() {
|
|
|
|
|
present(UIHostingController(rootView: CustomizeTimelinesView(mastodonController: mastodonController)), animated: true)
|
2022-12-03 02:40:14 +00:00
|
|
|
|
}
|
2022-12-13 01:57:38 +00:00
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
}
|
2019-09-14 19:55:06 +00:00
|
|
|
|
|
2022-12-21 04:37:12 +00:00
|
|
|
|
extension TimelinesPageViewController {
|
|
|
|
|
struct Page: SegmentedPageViewControllerPage {
|
|
|
|
|
let mastodonController: MastodonController
|
|
|
|
|
let timeline: Timeline
|
|
|
|
|
|
|
|
|
|
static func ==(lhs: Page, rhs: Page) -> Bool {
|
|
|
|
|
return lhs.timeline == rhs.timeline
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(timeline)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var segmentedControlTitle: String {
|
|
|
|
|
if case let .list(id) = timeline,
|
|
|
|
|
let list = try? mastodonController.persistentContainer.viewContext.fetch(ListMO.fetchRequest(id: id)).first {
|
|
|
|
|
return list.title
|
|
|
|
|
} else {
|
|
|
|
|
return timeline.title
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 19:55:06 +00:00
|
|
|
|
}
|