Tusker/Tusker/Screens/Timeline/TimelinesPageViewController...

116 lines
4.5 KiB
Swift
Raw Normal View History

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
2019-09-14 19:55:06 +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")
weak var mastodonController: MastodonController!
2022-12-21 04:37:12 +00:00
private var pinnedTimelinesObservation: NSKeyValueObservation?
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
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 {
return false
}
Task {
await vc.checkPresent(jumpImmediately: true)
}
return true
})
]
2022-12-21 04:37:12 +00:00
pinnedTimelinesObservation = mastodonController.accountPreferences.observe(\.pinnedTimelinesData, changeHandler: { [unowned self] _, _ in
let pages = self.mastodonController.accountPreferences.pinnedTimelines.map {
Page(mastodonController: self.mastodonController, timeline: $0)
}
self.setPages(pages, animated: false)
})
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)
}
func stateRestorationActivity() -> NSUserActivity? {
2022-12-21 04:37:12 +00:00
return (currentViewController as! TimelineViewController).stateRestorationActivity()
}
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)
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-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
}