From fe85e3c1d65c1cddd3d52aa279487619a06584f1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 5 Jan 2019 12:59:55 -0500 Subject: [PATCH] Redo app navigation/tab bar --- Tusker.xcodeproj/project.pbxproj | 8 +- .../Compose/ComposeViewController.swift | 2 + .../Main/MainTabBarViewController.swift | 55 ++++------- .../Preferences/Preferences.storyboard | 57 ++---------- .../PreferencesTableViewController.swift | 4 + .../Preferences/TabsTableViewController.swift | 92 ------------------- .../MyProfileTableViewController.swift | 47 ++++++++++ .../TimelineTableViewController.swift | 1 - 8 files changed, 86 insertions(+), 180 deletions(-) delete mode 100644 Tusker/Screens/Preferences/TabsTableViewController.swift create mode 100644 Tusker/Screens/Profile/MyProfileTableViewController.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 495c66f6..3ca328ef 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -57,7 +57,7 @@ D6109A0D214599E100432DC2 /* RequestRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A0C214599E100432DC2 /* RequestRange.swift */; }; D6109A0F21459B6900432DC2 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A0E21459B6900432DC2 /* Pagination.swift */; }; D6109A11214607D500432DC2 /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A10214607D500432DC2 /* Timeline.swift */; }; - D621544821682A9D0003D87D /* TabsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D621544721682A9D0003D87D /* TabsTableViewController.swift */; }; + D6163F2C21AA0AF1008DAC41 /* MyProfileTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6163F2B21AA0AF1008DAC41 /* MyProfileTableViewController.swift */; }; D621544B21682AD30003D87D /* TabTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D621544A21682AD30003D87D /* TabTableViewCell.swift */; }; D621544D21682AD90003D87D /* TabTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D621544C21682AD90003D87D /* TabTableViewCell.xib */; }; D627FF74217BBC9700CC0648 /* AppRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627FF73217BBC9700CC0648 /* AppRouter.swift */; }; @@ -256,7 +256,7 @@ D6109A0C214599E100432DC2 /* RequestRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestRange.swift; sourceTree = ""; }; D6109A0E21459B6900432DC2 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; D6109A10214607D500432DC2 /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = ""; }; - D621544721682A9D0003D87D /* TabsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabsTableViewController.swift; sourceTree = ""; }; + D6163F2B21AA0AF1008DAC41 /* MyProfileTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyProfileTableViewController.swift; sourceTree = ""; }; D621544A21682AD30003D87D /* TabTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabTableViewCell.swift; sourceTree = ""; }; D621544C21682AD90003D87D /* TabTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TabTableViewCell.xib; sourceTree = ""; }; D627FF73217BBC9700CC0648 /* AppRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppRouter.swift; sourceTree = ""; }; @@ -569,6 +569,7 @@ isa = PBXGroup; children = ( D667E5E621349D4C0057A976 /* ProfileTableViewController.swift */, + D6163F2B21AA0AF1008DAC41 /* MyProfileTableViewController.swift */, ); path = Profile; sourceTree = ""; @@ -615,7 +616,6 @@ D663626521360DD700C9CBA2 /* Preferences.storyboard */, D663626721360E2C00C9CBA2 /* PreferencesTableViewController.swift */, D67E0512216438A7000E0927 /* AppearanceTableViewController.swift */, - D621544721682A9D0003D87D /* TabsTableViewController.swift */, D627FF80217FE8F400CC0648 /* BehaviorTableViewController.swift */, D6C693C92161253F007D6A6D /* SilentActionPermissionsTableViewController.swift */, ); @@ -1183,7 +1183,6 @@ D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */, D663626221360B1900C9CBA2 /* Preferences.swift in Sources */, D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, - D621544821682A9D0003D87D /* TabsTableViewController.swift in Sources */, D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */, 04496BD721625361001F1B23 /* ContentLabel.swift in Sources */, D663626C21361C6700C9CBA2 /* Account+Preferences.swift in Sources */, @@ -1215,6 +1214,7 @@ D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */, D667E5E721349D4C0057A976 /* ProfileTableViewController.swift in Sources */, D6D4DDD0212518A000E1C4BB /* AppDelegate.swift in Sources */, + D6163F2C21AA0AF1008DAC41 /* MyProfileTableViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index a11f9ae5..71593aa5 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -66,6 +66,8 @@ class ComposeViewController: UIViewController { super.init(nibName: "ComposeViewController", bundle: nil) + title = "Compose" + navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelPressed)) navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Drafts", style: .plain, target: self, action: #selector(draftsPressed)) } diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index 50f40fa6..b89065c2 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -8,48 +8,22 @@ import UIKit -class MainTabBarViewController: UITabBarController { +class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate { lazy var router = AppRouter(root: self) override func viewDidLoad() { super.viewDidLoad() - updateTabs(animated: false) - } - - func updateTabs(animated: Bool) { - let currentTabs = Preferences.shared.tabs.filter { $1 >= 0 }.sorted { $1.1 > $0.1 }.map { $0.key } - let viewControllers: [UIViewController] = currentTabs.map { (tab) in - if tab == .preferences, let preferences = selectedViewController { - return preferences - } else { - return embedInNavigationController(createVC(for: tab)) - } - } - setViewControllers(viewControllers, animated: animated) - } - - func createVC(for tab: Tab) -> UIViewController { - switch tab { - case .home: - return TimelineTableViewController(for: .home, router: router) - case .federated: - return TimelineTableViewController(for: .public(local: false), router: router) - case .local: - return TimelineTableViewController(for: .public(local: true), router: router) - case .myProfile: - let myProfile = ProfileTableViewController(accountID: nil, router: router) - myProfile.title = "My Profile" - MastodonController.getOwnAccount { (account) in - myProfile.accountID = account.id - } - return myProfile - case .notifications: - return NotificationsTableViewController(router: router) - case .preferences: - return PreferencesTableViewController.create() - } + self.delegate = self + + viewControllers = [ + embedInNavigationController(TimelineTableViewController(for: .home, router: router)), + embedInNavigationController(NotificationsTableViewController(router: router)), + ComposeViewController(router: router), + embedInNavigationController(TimelineTableViewController(for: .public(local: false), router: router)), + embedInNavigationController(MyProfileTableViewController(router: router)), + ] } func embedInNavigationController(_ vc: UIViewController) -> UINavigationController { @@ -60,4 +34,13 @@ class MainTabBarViewController: UITabBarController { } } + func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { + if viewController is ComposeViewController { + let compose = embedInNavigationController(ComposeViewController(router: router)) + tabBarController.present(compose, animated: true) + return false + } + return true + } + } diff --git a/Tusker/Screens/Preferences/Preferences.storyboard b/Tusker/Screens/Preferences/Preferences.storyboard index 1f3a33ba..56e37789 100644 --- a/Tusker/Screens/Preferences/Preferences.storyboard +++ b/Tusker/Screens/Preferences/Preferences.storyboard @@ -1,10 +1,10 @@ - + - + @@ -93,7 +93,13 @@ - + + + + + + + @@ -198,31 +204,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -326,25 +308,6 @@ - - - - - - - - - - - - - - - - - - - diff --git a/Tusker/Screens/Preferences/PreferencesTableViewController.swift b/Tusker/Screens/Preferences/PreferencesTableViewController.swift index 319730a9..26607b9e 100644 --- a/Tusker/Screens/Preferences/PreferencesTableViewController.swift +++ b/Tusker/Screens/Preferences/PreferencesTableViewController.swift @@ -15,4 +15,8 @@ class PreferencesTableViewController: UITableViewController { return navigationController } + @IBAction func donePressed(_ sender: Any) { + dismiss(animated: true) + } + } diff --git a/Tusker/Screens/Preferences/TabsTableViewController.swift b/Tusker/Screens/Preferences/TabsTableViewController.swift deleted file mode 100644 index e9c03620..00000000 --- a/Tusker/Screens/Preferences/TabsTableViewController.swift +++ /dev/null @@ -1,92 +0,0 @@ -// -// TabsTableViewController.swift -// Tusker -// -// Created by Shadowfacts on 10/5/18. -// Copyright © 2018 Shadowfacts. All rights reserved. -// - -import UIKit - -class TabsTableViewController: UITableViewController { - - var tabs: [(Tab, Bool)] = [] - - override func viewDidLoad() { - super.viewDidLoad() - - tableView.register(UINib(nibName: "TabTableViewCell", bundle: nil), forCellReuseIdentifier: "tabCell") - - tabs = Preferences.shared.tabs.filter { $0.value >= 0 }.sorted(by: { $0.value < $1.value }).map { ($0.key, true) } - tabs.append(contentsOf: Preferences.shared.tabs.filter { $0.value == -1 }.map { ($0.key, false) }) - - tableView.setEditing(true, animated: false) - } - - func updatePreferences() { - var dict = [Tab: Int]() - var maxIndex = 0 - for (tab, enabled) in tabs { - if enabled { - dict[tab] = maxIndex - maxIndex += 1 - } else { - dict[tab] = -1 - } - } - Preferences.shared.tabs = dict - - guard let tabBarController = tabBarController as? MainTabBarViewController else { fatalError("Tab bar controller should be of type MainTabBarViewController") } - tabBarController.updateTabs(animated: true) - } - - // MARK: - Table view data source - - override func numberOfSections(in tableView: UITableView) -> Int { - return 1 - } - - override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return tabs.count - } - - override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - guard let cell = tableView.dequeueReusableCell(withIdentifier: "tabCell", for: indexPath) as? TabTableViewCell else { fatalError() } - - let (tab, _) = tabs[indexPath.row] - cell.updateUI(for: tab) - cell.delegate = self - - return cell - } - - override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { - return .none - } - - override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { - let tab = tabs.remove(at: sourceIndexPath.row) - tabs.insert(tab, at: destinationIndexPath.row) - updatePreferences() - } - - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destination. - // Pass the selected object to the new view controller. - } - */ - -} - -extension TabsTableViewController: TabTableViewCellDelegate { - func setEnabled(tab: Tab, enabled: Bool) { - let index = tabs.firstIndex(where: { $0.0 == tab })! - tabs[index] = (tab, enabled) - updatePreferences() - } -} - diff --git a/Tusker/Screens/Profile/MyProfileTableViewController.swift b/Tusker/Screens/Profile/MyProfileTableViewController.swift new file mode 100644 index 00000000..e9a5cb47 --- /dev/null +++ b/Tusker/Screens/Profile/MyProfileTableViewController.swift @@ -0,0 +1,47 @@ +// +// MyProfileTableViewController.swift +// Tusker +// +// Created by Shadowfacts on 11/24/18. +// Copyright © 2018 Shadowfacts. All rights reserved. +// + +import UIKit + +class MyProfileTableViewController: ProfileTableViewController { + + init(router: AppRouter) { + super.init(accountID: nil, router: router) + + title = "My Profile" + + MastodonController.getOwnAccount { (account) in + self.accountID = account.id + + ImageCache.avatars.get(account.avatar, completion: { (data) in + guard let data = data else { return } + var image = UIImage(data: data)! + image = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image(actions: { (_) in + image.draw(in: CGRect(x: 0, y: 0, width: 30, height: 30)) + }) + image = image.withRenderingMode(.alwaysOriginal) + self.tabBarItem.image = image + }) + } + + navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Preferences", style: .plain, target: self, action: #selector(preferencesPressed)) + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func viewDidLoad() { + super.viewDidLoad() + } + + @objc func preferencesPressed() { + router.present(PreferencesTableViewController.create(), animated: true) + } + +} diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index a9d2eb10..c0947714 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -60,7 +60,6 @@ class TimelineTableViewController: EnhancedTableViewController { self.refreshControl = UIRefreshControl() refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged) - self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(composePressed(_:))) } required init?(coder aDecoder: NSCoder) {