Skip to content

Commit

Permalink
🔎 🔎 Implements a Search Bar for data filtering! 🔎 🔎
Browse files Browse the repository at this point in the history
🔎 🔎  Implements a Search Bar for data filtering! 🔎 🔎  
+ Swift Data Tables will automatically add a search bar to the data grid.
+ The search will find occurrences in every single data item, efficiently. 
+ A row will be returned in the filtering process if even one of it's data items contain the needle.
+ The default filtering algorithm uses a contains matching expression.
  • Loading branch information
pavankataria authored Mar 17, 2017
2 parents 8ebb026 + d6c4703 commit 55b48d9
Show file tree
Hide file tree
Showing 16 changed files with 619 additions and 269 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SwiftDataTables (0.3.0)
- SwiftDataTables (0.3.1)

DEPENDENCIES:
- SwiftDataTables (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SwiftDataTables: bb9323c35a72b0da6acd042721df3c4a07eb1a02
SwiftDataTables: d1cc6d6b606e7cb57146b429c9cc2a2b3765705b

PODFILE CHECKSUM: 117ac710a88fe801ecac2a925baa29b1107fef2e

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SwiftDataTables.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

366 changes: 180 additions & 186 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/SwiftDataTables.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -489,7 +489,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
7 changes: 4 additions & 3 deletions Example/SwiftDataTables/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ViewController: UIViewController {
super.viewDidLoad()

self.navigationController?.navigationBar.isTranslucent = false

// self.navigationController?.navigationBar.isHidden = true
self.view.backgroundColor = UIColor.white

var options = DataTableConfiguration()
Expand All @@ -38,12 +38,13 @@ class ViewController: UIViewController {
headerTitles: self.columnHeaders(),
options: options
)

self.automaticallyAdjustsScrollViewInsets = false

self.dataTable.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)

//25/255, green: 33/255, blue: 39/255, alpha: 1)
self.dataTable.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.dataTable.frame = self.view.bounds
self.dataTable.frame = self.view.frame
self.view.addSubview(self.dataTable);
}
}
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SwiftDataTables

<!--[![CI Status](http://img.shields.io/travis/pavankataria/SwiftDataTables.svg?style=flat)](https://travis-ci.org/pavankataria/SwiftDataTables)-->
[![Platform](https://img.shields.io/cocoapods/p/SwiftDataTables.svg?style=flat)](http://cocoapods.org/pods/SwiftDataTables)
<!--[![CI Status](http://img.shields.io/travis/pavankataria/SwiftDataTables.svg?style=flat)](https://travis-ci.org/pavankataria/SwiftDataTables)
https://img.shields.io/cocoapods/p/SwiftDataTables.svg
-->
[![Platform](https://img.shields.io/badge/%20%20Platform%20%20-iOS-brightgreen.svg?style=flat)](http://cocoapods.org/pods/SwiftDataTables)
[![Swift Version](https://img.shields.io/badge/%20%20Swift%20Version%20%20-3.0-brightgreen.svg?style=flat)](http://cocoapods.org/pods/SwiftDataTables)
[![GitHub issues](https://img.shields.io/github/issues/pavankataria/SwiftDataTables.svg)](https://github.com/pavankataria/SwiftDataTables/issues)
[![Version](https://img.shields.io/cocoapods/v/SwiftDataTables.svg?style=flat)](http://cocoapods.org/pods/SwiftDataTables)
[![License](https://img.shields.io/cocoapods/l/SwiftDataTables.svg?style=flat)](http://cocoapods.org/pods/SwiftDataTables)
Expand All @@ -22,6 +25,9 @@ To run the example project do the following:
If you have any questions or wish to make any suggestions, please open an issue with the appropriate label, and I'll get back to you right away. Thank you

## Requirements
+ iOS 8.0+
+ Xcode 8
+ Swift 3

## Installation

Expand Down
15 changes: 15 additions & 0 deletions SwiftDataTables/Classes/DataCell/DataCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ open class DataCellViewModel: VirtualPositionTrackable, CollectionViewCellRepres
return cell
}
}
extension DataCellViewModel: Equatable {
/// Returns a Boolean value indicating whether two values are equal.
///
/// Equality is the inverse of inequality. For any values `a` and `b`,
/// `a == b` implies that `a != b` is `false`.
///
/// - Parameters:
/// - lhs: A value to compare.
/// - rhs: Another value to compare.
public static func ==(lhs: DataCellViewModel, rhs: DataCellViewModel) -> Bool {
return lhs.data == rhs.data
&& lhs.highlighted == rhs.highlighted
}

}
49 changes: 49 additions & 0 deletions SwiftDataTables/Classes/DataTableSearchTextField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// DataTableSearchTextField.swift
// Pods
//
// Created by Pavan Kataria on 15/03/2017.
//
//

import UIKit


class DataTableSearchTextField: UITextField {

//MARK: - Properties

//MARK: - Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
self.setup()
}

public func setup(){
self.borderStyle = .none
self.backgroundColor = UIColor.white
self.clearButtonMode = .always
}

override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = (self.bounds.height / 2)-1
}

let inset: CGFloat = 10

// placeholder position
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset , dy: inset)
}

// text position
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset , dy: inset)
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: inset, dy: inset)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ extension Collection where Indices.Iterator.Element == Index {
return indices.contains(index) ? self[index] : nil
}
}

extension UIScrollView {
/// Sets content offset to the top.
func resetScrollPositionToTop() {
self.contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
}
}
23 changes: 22 additions & 1 deletion SwiftDataTables/Classes/MenuLengthHeader/MenuLengthHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@
import UIKit

class MenuLengthHeader: UICollectionReusableView {

//MARK: - Properties
// @IBOutlet var searchTextField: DataTableSearchTextField!
@IBOutlet var searchBar: UISearchBar!

//MARK: - Events
var searchTextDidChange: ((String) -> Void)?

//MARK: - Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

func setup(_ viewModel: MenuLengthHeaderViewModel){
// self.searchTextField.addTarget(viewModel, action: #selector(MenuLengthHeaderViewModel.textFieldDidChange), for: .editingChanged)

self.searchTextDidChange = { searchText in
viewModel.searchTextFieldDidChangeEvent?(searchText)
}
self.searchBar.delegate = self
self.searchBar.searchBarStyle = .minimal
self.searchBar.placeholder = "Search"// - \(viewModel.count) names"
}
}

extension MenuLengthHeader: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchTextDidChange?(searchText)
}
}
20 changes: 11 additions & 9 deletions SwiftDataTables/Classes/MenuLengthHeader/MenuLengthHeader.xib
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
<rect key="frame" x="0.0" y="0.0" width="505" height="69"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Menu Length Options" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Xm8-gq-2pj">
<searchBar contentMode="redraw" searchBarStyle="minimal" translatesAutoresizingMaskIntoConstraints="NO" id="AfP-mW-V51">
<rect key="frame" x="0.0" y="0.0" width="505" height="69"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textInputTraits key="textInputTraits"/>
</searchBar>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="Xm8-gq-2pj" firstAttribute="leading" secondItem="U6b-Vx-4bR" secondAttribute="leading" id="Pl0-NG-DuT"/>
<constraint firstAttribute="bottom" secondItem="Xm8-gq-2pj" secondAttribute="bottom" id="REK-V7-g3c"/>
<constraint firstAttribute="trailing" secondItem="Xm8-gq-2pj" secondAttribute="trailing" id="RnX-rb-WEq"/>
<constraint firstItem="Xm8-gq-2pj" firstAttribute="top" secondItem="U6b-Vx-4bR" secondAttribute="top" id="d5d-Es-nrI"/>
<constraint firstItem="AfP-mW-V51" firstAttribute="top" secondItem="U6b-Vx-4bR" secondAttribute="top" id="1qZ-Pp-LzF"/>
<constraint firstItem="AfP-mW-V51" firstAttribute="leading" secondItem="U6b-Vx-4bR" secondAttribute="leading" id="McA-ns-Iev"/>
<constraint firstAttribute="trailing" secondItem="AfP-mW-V51" secondAttribute="trailing" id="oI9-iE-bZE"/>
<constraint firstAttribute="bottom" secondItem="AfP-mW-V51" secondAttribute="bottom" id="vPu-ls-EOT"/>
</constraints>
<connections>
<outlet property="searchBar" destination="AfP-mW-V51" id="DuP-ym-8XO"/>
</connections>
<point key="canvasLocation" x="-57.5" y="63.5"/>
</collectionReusableView>
</objects>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import Foundation
import UIKit

class MenuLengthHeaderViewModel {

class MenuLengthHeaderViewModel: NSObject {
//MARK: - Events
var searchTextFieldDidChangeEvent: ((String) -> Void)? = nil
}

extension MenuLengthHeaderViewModel: CollectionViewSupplementaryElementRepresentable {
Expand Down Expand Up @@ -39,3 +40,16 @@ extension MenuLengthHeaderViewModel: CollectionViewSupplementaryElementRepresent
return headerView
}
}

extension MenuLengthHeaderViewModel {
@objc func textFieldDidChange(textField: UITextField){
guard let text = textField.text else {
return
}
self.searchTextFieldDidChangeEvent?(text)
}
}

extension MenuLengthHeaderViewModel: UISearchBarDelegate {

}
Loading

0 comments on commit 55b48d9

Please sign in to comment.