From d9c2956d0080a75379c6bb910df3c98764eb0d84 Mon Sep 17 00:00:00 2001 From: Dwayne Harris Date: Tue, 27 Nov 2018 01:25:11 -0800 Subject: [PATCH] Fix things and clean up code --- .../AbstractStatusTableViewController.swift | 22 +-- elpha-ios/AccountTableViewController.swift | 35 +++-- elpha-ios/AlertManager.swift | 4 +- elpha-ios/AlertView.swift | 2 +- elpha-ios/AppDelegate.swift | 6 +- elpha-ios/AttachmentManager.swift | 8 +- elpha-ios/AttachmentPageViewController.swift | 2 +- elpha-ios/AttachmentViewController.swift | 2 +- elpha-ios/AuthenticateViewController.swift | 18 +-- elpha-ios/AuthenticationManager.swift | 34 ++--- elpha-ios/Base.lproj/Main.storyboard | 126 +++++++++++------- elpha-ios/ComposeAccessoryView.swift | 16 +-- elpha-ios/ComposeViewController.swift | 20 +-- .../Elpha.xcdatamodel/contents | 5 +- elpha-ios/InstanceViewController.swift | 2 +- elpha-ios/InstancesDataManager.swift | 2 +- elpha-ios/InstancesTableViewController.swift | 2 +- elpha-ios/MastodonAPI.swift | 6 +- elpha-ios/SettingsTableViewController.swift | 10 +- elpha-ios/StatusTableViewController.swift | 21 ++- elpha-ios/StatusView.swift | 67 +++++----- elpha-ios/TimelineTableViewController.swift | 27 ++-- elpha-ios/TimelinesViewController.swift | 10 +- elpha-ios/VisibilityInputView.swift | 8 +- 24 files changed, 252 insertions(+), 203 deletions(-) diff --git a/elpha-ios/AbstractStatusTableViewController.swift b/elpha-ios/AbstractStatusTableViewController.swift index 2f00ca7..bc396be 100644 --- a/elpha-ios/AbstractStatusTableViewController.swift +++ b/elpha-ios/AbstractStatusTableViewController.swift @@ -16,7 +16,7 @@ protocol AbstractStatusTableViewCell { class AbstractStatusTableViewController: UITableViewController, StatusViewDelegate { let fetchLimit = 20 - var fetchedResultsController: NSFetchedResultsController? = nil + var fetchedResultsController: NSFetchedResultsController? var loading: Bool = false { didSet { @@ -40,32 +40,32 @@ class AbstractStatusTableViewController: UITableViewController, StatusViewDelega return UIStoryboard(name: "Main", bundle: nil) } - func accountTapped(account: AccountMO) { + func accountTapped(_ sender: Any, account: AccountMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController { controller.account = account self.navigationController?.pushViewController(controller, animated: true) } } - func statusTapped(status: StatusMO) { + func statusTapped(_ sender: Any, status: StatusMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "StatusTableViewController") as? StatusTableViewController { controller.status = status self.navigationController?.pushViewController(controller, animated: true) } } - func loadMoreTapped(status: StatusMO, direction: PaginationDirection) { + func loadMoreTapped(_ sender: Any, status: StatusMO, direction: PaginationDirection) { } - func replyTapped(status: StatusMO) { + func replyTapped(_ sender: Any, status: StatusMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "ComposeViewController") as? ComposeViewController { controller.replyToStatus = status present(controller, animated: true) } } - func attachmentTapped(status: StatusMO, index: Int) { + func attachmentTapped(_ sender: Any, status: StatusMO, index: Int) { if let controller = storyboard().instantiateViewController(withIdentifier: "AttachmentPageViewController") as? AttachmentPageViewController { controller.status = status controller.attachmentIndex = index @@ -74,7 +74,7 @@ class AbstractStatusTableViewController: UITableViewController, StatusViewDelega } } - func urlTapped(url: URL) { + func urlTapped(_ sender: Any, url: URL) { let controller = SFSafariViewController(url: url) controller.delegate = self controller.dismissButtonStyle = .done @@ -83,10 +83,10 @@ class AbstractStatusTableViewController: UITableViewController, StatusViewDelega present(controller, animated: true) } - func revealTapped() {} - func hideTapped() {} - func boostTapped() {} - func favoriteTapped() {} + func revealTapped(_ sender: Any) {} + func hideTapped(_ sender: Any) {} + func boostTapped(_ sender: Any) {} + func favoriteTapped(_ sender: Any) {} func updateCell(_ cell: AbstractStatusTableViewCell, withStatusAt indexPath: IndexPath) { guard let status = fetchedResultsController?.object(at: indexPath) else { diff --git a/elpha-ios/AccountTableViewController.swift b/elpha-ios/AccountTableViewController.swift index 7ef5871..04663ab 100644 --- a/elpha-ios/AccountTableViewController.swift +++ b/elpha-ios/AccountTableViewController.swift @@ -12,7 +12,7 @@ import UIKit import SafariServices class FieldTableViewController: NSObject, UITableViewDelegate, UITableViewDataSource { - var fields: [AccountField]? = nil + var fields: [AccountField]? func numberOfSections(in tableView: UITableView) -> Int { return 1 @@ -75,8 +75,8 @@ class AccountTableViewController: AbstractStatusTableViewController { @IBOutlet var contentTextView: UITextViewFixed! @IBOutlet var fieldTableView: UITableView! - var account: AccountMO? = nil - var fieldTableViewController: FieldTableViewController? = nil + var account: AccountMO? + var fieldTableViewController: FieldTableViewController? override var currentPaginationContext: String { get { @@ -90,6 +90,8 @@ class AccountTableViewController: AbstractStatusTableViewController { @IBAction func statusTypeChanged(_ sender: UISegmentedControl) { initializeFetchedResultsController() + tableView.reloadData() + fetch() } @@ -110,7 +112,7 @@ class AccountTableViewController: AbstractStatusTableViewController { if let account = account { updateHeader(withAccount: account) - sizeHeaderToFit() + // sizeHeaderToFit() initializeFetchedResultsController() } @@ -137,18 +139,25 @@ class AccountTableViewController: AbstractStatusTableViewController { } } - override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - sizeHeaderToFit() - } - private func sizeHeaderToFit() { + headerView.translatesAutoresizingMaskIntoConstraints = false + + let headerWidth = headerView.bounds.size.width + let headerWidthConstraints = NSLayoutConstraint.constraints(withVisualFormat: "[headerView(width)]", options: NSLayoutConstraint.FormatOptions(rawValue: UInt(0)), metrics: ["width": headerWidth], views: ["headerView": headerView]) + + headerView.addConstraints(headerWidthConstraints) headerView.setNeedsLayout() headerView.layoutIfNeeded() -// var frame = headerView.frame -// frame.size.height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height -// headerView.frame = frame + let headerSize = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) + let height = headerSize.height + var frame = headerView.frame + + frame.size.height = height + headerView.frame = frame + + headerView.removeConstraints(headerWidthConstraints) + headerView.translatesAutoresizingMaskIntoConstraints = true } private func updateHeader(withAccount account: AccountMO) { @@ -186,7 +195,7 @@ class AccountTableViewController: AbstractStatusTableViewController { } } - override func loadMoreTapped(status: StatusMO, direction: PaginationDirection) { + override func loadMoreTapped(_ sender: Any, status: StatusMO, direction: PaginationDirection) { func removeMarker(at: Int) { status.markers?.remove(at: at) CoreDataManager.shared.saveContext() diff --git a/elpha-ios/AlertManager.swift b/elpha-ios/AlertManager.swift index a7a246e..b7ec937 100644 --- a/elpha-ios/AlertManager.swift +++ b/elpha-ios/AlertManager.swift @@ -28,8 +28,8 @@ class AlertManager { static let alertEndPosition: CGFloat = -80.0 var done: Bool = true - var alertView: AlertView? = nil - var bottomLayoutConstraint: NSLayoutConstraint? = nil + var alertView: AlertView? + var bottomLayoutConstraint: NSLayoutConstraint? var alerts: [Alert] = [] func createAlertView() { diff --git a/elpha-ios/AlertView.swift b/elpha-ios/AlertView.swift index eb23894..5bd7ade 100644 --- a/elpha-ios/AlertView.swift +++ b/elpha-ios/AlertView.swift @@ -13,7 +13,7 @@ import UIKit @IBOutlet var alertImageView: UIImageView! @IBOutlet var alertLabel: UILabel! - var alert: Alert? = nil + var alert: Alert? override init(frame: CGRect) { super.init(frame: frame) diff --git a/elpha-ios/AppDelegate.swift b/elpha-ios/AppDelegate.swift index 50f58e5..416129b 100644 --- a/elpha-ios/AppDelegate.swift +++ b/elpha-ios/AppDelegate.swift @@ -6,9 +6,13 @@ // Copyright © 2018 Elpha. All rights reserved. // -import CoreData import UIKit +extension Notification.Name { + static let didAuthenticate = Notification.Name("didAuthenticate") + static let didUnauthenticate = Notification.Name("didUnauthenticate") +} + @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? diff --git a/elpha-ios/AttachmentManager.swift b/elpha-ios/AttachmentManager.swift index a83bb07..fabe2f2 100644 --- a/elpha-ios/AttachmentManager.swift +++ b/elpha-ios/AttachmentManager.swift @@ -9,16 +9,16 @@ import Kingfisher import UIKit -protocol AttachmentManagerDelegate { - func attachmentTapped(index: Int) +protocol AttachmentManagerDelegate: class { + func attachmentTapped(_ sender: Any, index: Int) } class AttachmentManager: NSObject { - var delegate: AttachmentManagerDelegate? = nil + weak var delegate: AttachmentManagerDelegate? @objc func attachmentTapped(_ gestureRecognizer: UITapGestureRecognizer) { if let delegate = delegate, let name = gestureRecognizer.name { - delegate.attachmentTapped(index: Int(name)!) + delegate.attachmentTapped(self, index: Int(name)!) } } diff --git a/elpha-ios/AttachmentPageViewController.swift b/elpha-ios/AttachmentPageViewController.swift index caed3d2..f92f088 100644 --- a/elpha-ios/AttachmentPageViewController.swift +++ b/elpha-ios/AttachmentPageViewController.swift @@ -9,7 +9,7 @@ import UIKit class AttachmentPageViewController: UIPageViewController { - var status: StatusMO? = nil + var status: StatusMO? var controllers: [UIViewController] = [] var attachmentIndex = 0 diff --git a/elpha-ios/AttachmentViewController.swift b/elpha-ios/AttachmentViewController.swift index bc8ad24..3406dcb 100644 --- a/elpha-ios/AttachmentViewController.swift +++ b/elpha-ios/AttachmentViewController.swift @@ -14,7 +14,7 @@ class AttachmentViewController: UIViewController { @IBOutlet var attachmentImageView: UIImageView! @IBOutlet var statusTextView: UITextView! - var attachment: AttachmentMO? = nil + var attachment: AttachmentMO? @IBAction func closeTapped(_ sender: Any) { if let controller = self.parent as? AttachmentPageViewController { diff --git a/elpha-ios/AuthenticateViewController.swift b/elpha-ios/AuthenticateViewController.swift index 230d707..f3efb0d 100644 --- a/elpha-ios/AuthenticateViewController.swift +++ b/elpha-ios/AuthenticateViewController.swift @@ -24,14 +24,6 @@ class AuthenticateViewController: UIViewController { instanceTextField.attributedPlaceholder = NSAttributedString(string: "mastodon.social", attributes: [NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)]) } - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - if AuthenticationManager.session != nil { - self.dismiss(animated: true) - } - } - override open var shouldAutorotate: Bool { return false } @@ -122,11 +114,15 @@ extension AuthenticateViewController: SFSafariViewControllerDelegate { } extension AuthenticateViewController: AuthenticationDelegate { - func authenticationSuccess() { - self.dismiss(animated: true) + func authenticationSuccess(_ sender: Any) { + NotificationCenter.default.post(name: .didAuthenticate, object: nil) + + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + self.dismiss(animated: true) + } } - func authenticationFailure(error: Error?) { + func authenticationFailure(_ sender: Any, error: Error?) { AlertManager.shared.show(message: error?.localizedDescription ?? "Authentication error", category: .error) } } diff --git a/elpha-ios/AuthenticationManager.swift b/elpha-ios/AuthenticationManager.swift index 97a50e8..4f24f97 100644 --- a/elpha-ios/AuthenticationManager.swift +++ b/elpha-ios/AuthenticationManager.swift @@ -10,15 +10,15 @@ import Alamofire import CoreData import UIKit -protocol AuthenticationDelegate { - func authenticationSuccess() - func authenticationFailure(error: Error?) +protocol AuthenticationDelegate: class { + func authenticationSuccess(_ sender: Any) + func authenticationFailure(_ sender: Any, error: Error?) } class AuthenticationManager { - static var authenticationState: String? = nil - static var authenticationClient: ClientMO? = nil - static var authenticationDelegate: AuthenticationDelegate? = nil + static var authenticationState: String? + static var authenticationClient: ClientMO? + static weak var authenticationDelegate: AuthenticationDelegate? static var sessions: [SessionMO] { get { @@ -75,6 +75,15 @@ class AuthenticationManager { return session } + static func deleteSelectedSession() { + guard let session = self.session else { + return + } + + KeychainWrapper.standard.removeObject(forKey: "token:\(session.account!.username!)@\(session.client!.host!)") + CoreDataManager.shared.context.delete(session) + } + static func handle(url: URL) { guard let state = authenticationState, let client = authenticationClient else { return @@ -89,10 +98,7 @@ class AuthenticationManager { code = item.value case "state": if item.value != state { - if let delegate = self.authenticationDelegate { - delegate.authenticationFailure(error: nil) - } - + self.authenticationDelegate?.authenticationFailure(self, error: nil) return } default: @@ -127,15 +133,11 @@ class AuthenticationManager { let account = MastodonDataManager.upsertAccount(data) _ = AuthenticationManager.saveSession(client: client, account: account!, token: token) - if let delegate = self.authenticationDelegate { - delegate.authenticationSuccess() - } + self.authenticationDelegate?.authenticationSuccess(self) } } case .failure(let error): - if let delegate = self.authenticationDelegate { - delegate.authenticationFailure(error: error) - } + self.authenticationDelegate?.authenticationFailure(self, error: error) } } } diff --git a/elpha-ios/Base.lproj/Main.storyboard b/elpha-ios/Base.lproj/Main.storyboard index 54bd984..cf05383 100644 --- a/elpha-ios/Base.lproj/Main.storyboard +++ b/elpha-ios/Base.lproj/Main.storyboard @@ -608,61 +608,85 @@ - + + + + + + + + + + + + - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -961,7 +985,7 @@ -