diff --git a/iOS/AppCoordinator.swift b/iOS/AppCoordinator.swift index 37f33b10f..7893e64d4 100644 --- a/iOS/AppCoordinator.swift +++ b/iOS/AppCoordinator.swift @@ -48,6 +48,7 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { private var animatingChanges = false private var expandedNodes = [Node]() private var shadowTable = [[Node]]() + private var lastSearchString = "" private(set) var sortDirection = AppDefaults.timelineSortDirection { didSet { @@ -503,6 +504,21 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } } + func searchArticles(_ searchString: String) { + guard !searchString.isEmpty else { + if let ip = currentMasterIndexPath, let node = nodeFor(ip), let fetcher = node.representedObject as? ArticleFetcher { + timelineFetcher = fetcher + } + return + } + + if searchString != lastSearchString { + timelineFetcher = SmartFeed(delegate: SearchFeedDelegate(searchString: searchString)) + lastSearchString = searchString + } + + } + func selectPrevArticle() { if let indexPath = prevArticleIndexPath { selectArticle(indexPath) diff --git a/iOS/MasterTimeline/MasterTimelineViewController.swift b/iOS/MasterTimeline/MasterTimelineViewController.swift index 47ffe820c..55d6161c5 100644 --- a/iOS/MasterTimeline/MasterTimelineViewController.swift +++ b/iOS/MasterTimeline/MasterTimelineViewController.swift @@ -19,9 +19,11 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner @IBOutlet weak var firstUnreadButton: UIBarButtonItem! private lazy var dataSource = makeDataSource() + private let searchController = UISearchController(searchResultsController: nil) + weak var coordinator: AppCoordinator! var undoableCommands = [UndoableCommand]() - + override var canBecomeFirstResponder: Bool { return true } @@ -29,7 +31,6 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner override func viewDidLoad() { super.viewDidLoad() - tableView.dataSource = dataSource NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil) @@ -40,9 +41,19 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil) + // Setup the Search Controller + searchController.searchResultsUpdater = self + searchController.obscuresBackgroundDuringPresentation = false + searchController.searchBar.placeholder = NSLocalizedString("Search Articles", comment: "Search Articles") + navigationItem.searchController = searchController + definesPresentationContext = true + + // Setup the Refresh Control refreshControl = UIRefreshControl() refreshControl!.addTarget(self, action: #selector(refreshAccounts(_:)), for: .valueChanged) + // Configure the table + tableView.dataSource = dataSource numberOfTextLines = AppDefaults.timelineNumberOfLines resetEstimatedRowHeight() @@ -392,6 +403,14 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner } +// MARK: UISearchResultsUpdating + +extension MasterTimelineViewController: UISearchResultsUpdating { + func updateSearchResults(for searchController: UISearchController) { + coordinator.searchArticles(searchController.searchBar.text!) + } +} + // MARK: Private private extension MasterTimelineViewController {