Skip to content

Commit

Permalink
Added SearchController
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Jun 26, 2019
1 parent fead0cc commit b0dbe48
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
Binary file not shown.
6 changes: 4 additions & 2 deletions iTorrent/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="zpc-fm-4t4" customClass="ThemedUITableView" customModule="iTorrent" customModuleProvider="target">
<tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" keyboardDismissMode="onDrag" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="zpc-fm-4t4" customClass="ThemedUITableView" customModule="iTorrent" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.7843137255" green="0.7843137255" blue="0.7843137255" alpha="1" colorSpace="calibratedRGB"/>
Expand Down Expand Up @@ -1053,7 +1053,7 @@
<!--Preferences-->
<scene sceneID="WC4-w3-y0S">
<objects>
<tableViewController id="9Ia-KW-DZw" customClass="SettingsController" customModule="iTorrent" customModuleProvider="target" sceneMemberID="viewController">
<tableViewController hidesBottomBarWhenPushed="YES" id="9Ia-KW-DZw" customClass="SettingsController" customModule="iTorrent" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="4oS-XS-G5Y">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -1584,7 +1584,9 @@
</tableViewSection>
</sections>
</tableView>
<extendedEdge key="edgesForExtendedLayout" top="YES"/>
<navigationItem key="navigationItem" title="Preferences" id="MGE-5Q-ZzR"/>
<nil key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="adsSwitch" destination="1bh-su-tD5" id="ewV-Rd-Nhs"/>
<outlet property="backgroundSeedSwitch" destination="6Vf-gU-BT3" id="iXa-2t-hZp"/>
Expand Down
8 changes: 5 additions & 3 deletions iTorrent/Cells/FileCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class FileCell: ThemedUITableViewCell {
func update() {
title.text = file?.name
let progressValue = Float(file.downloaded) / Float(file.size)
let percent = progressValue * 100
size.text = addind ? Utils.getSizeText(size: file.size) : Utils.getSizeText(size: file.size) + " / " + Utils.getSizeText(size: file.downloaded) + " (" + String(format: "%.2f", percent) + "%)"
progress.setProgress(progressValue == 1 ? [1] : file.pieces.map{ CGFloat($0) })
let percent = progressValue * 100
if (progress != nil) {
size.text = addind ? Utils.getSizeText(size: file.size) : Utils.getSizeText(size: file.size) + " / " + Utils.getSizeText(size: file.downloaded) + " (" + String(format: "%.2f", percent) + "%)"
progress.setProgress(progressValue == 1 ? [1] : file.pieces.map{ CGFloat($0) })
}

if (hideUI) {
shareButton.isHidden = true
Expand Down
51 changes: 42 additions & 9 deletions iTorrent/ViewControllers/MainController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class MainController: ThemedUIViewController {
var managers : [[TorrentStatus]] = []
var headers : [String] = []

var filterQuery : String?

var topRightItemsCopy : [UIBarButtonItem]?
var bottomItemsCopy : [UIBarButtonItem]?
lazy var bottomEditItems : [UIBarButtonItem] = {
Expand Down Expand Up @@ -63,6 +65,16 @@ class MainController: ThemedUIViewController {
self.present(dialog, animated: true)
}
}

let searchController = UISearchController(searchResultsController: nil)

searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
}
definesPresentationContext = true
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -71,7 +83,8 @@ class MainController: ThemedUIViewController {
navigationController?.toolbar.tintColor = navigationController?.navigationBar.tintColor

managers.removeAll()
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
updateFilterQuery()
tableView.reloadData()

NotificationCenter.default.addObserver(self, selector: #selector(managerUpdated), name: .torrentsUpdated, object: nil)
Expand Down Expand Up @@ -104,10 +117,11 @@ class MainController: ThemedUIViewController {

@objc func managerUpdated() {
var changed = false
var oldManagers = managers
let oldManagers = managers
managers.removeAll()
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
if (oldManagers.count != managers.count) {
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
updateFilterQuery()
if (oldManagers.count != managers.count) {
changed = true
} else {
for i in 0 ..< managers.count {
Expand All @@ -132,8 +146,9 @@ class MainController: ThemedUIViewController {

@objc func managerStateChanged(notfication: NSNotification) {
managers.removeAll()
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
tableView.reloadData()
managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &headers))
updateFilterQuery()
tableView.reloadData()
}

func removeTorrent(indexPath: IndexPath, isMagnet: Bool = false, removeData: Bool = false, visualOnly: Bool = false) {
Expand Down Expand Up @@ -311,8 +326,9 @@ class MainController: ThemedUIViewController {
@IBAction func sortAction(_ sender: UIBarButtonItem) {
let sortingController = SortingManager.createSortingController(buttonItem: sender, applyChanges: {
self.managers.removeAll()
self.managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &self.headers))
self.tableView.reloadData();
self.managers.append(contentsOf: SortingManager.sortTorrentManagers(managers: Manager.torrentStates, headers: &self.headers))
self.updateFilterQuery()
self.tableView.reloadData();
})
present(sortingController, animated: true)
}
Expand Down Expand Up @@ -554,7 +570,7 @@ extension MainController: UITableViewDataSource {

extension MainController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return headers[section].isEmpty ? 0 : 28
return managers[section].isEmpty ? 0 : 28
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
Expand Down Expand Up @@ -633,3 +649,20 @@ extension MainController: GADBannerViewDelegate {
}
}

extension MainController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterQuery = searchController.searchBar.text
self.updateFilterQuery()
self.tableView.reloadData()
}

func updateFilterQuery() {
if let filterQuery = filterQuery, !filterQuery.isEmpty {
for index in 0 ..< managers.count {
managers[index] = managers[index].filter { manager -> Bool in
return manager.title.lowercased().contains(filterQuery.lowercased())
}
}
}
}
}
4 changes: 3 additions & 1 deletion iTorrent/ViewControllers/SettingsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SettingsController: ThemedUITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

darkThemeSwitch.setOn(UserDefaults.standard.integer(forKey: UserDefaultsKeys.themeNum) == 1, animated: false)

Expand Down Expand Up @@ -98,7 +100,7 @@ class SettingsController: ThemedUITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

navigationController?.isToolbarHidden = true
navigationController?.setToolbarHidden(true, animated: false)
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
Expand Down
2 changes: 2 additions & 0 deletions iTorrent/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"Select All" = "Select All";
"Deselect" = "Deselect";

"Search" = "Search";

"time remains" = "time remains";
"eternity" = "eternity";

Expand Down
2 changes: 2 additions & 0 deletions iTorrent/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"Select All" = "Выбрать все";
"Deselect" = "Отменить";

"Search" = "Поиск";

"time remains" = "время осталось";
"eternity" = "вечность";

Expand Down

0 comments on commit b0dbe48

Please sign in to comment.