7
7
//
8
8
9
9
import UIKit
10
- import AFNetworking
11
10
12
11
let reuseIdentifier = " Cell "
13
12
14
13
class BooksViewController : UIViewController {
15
14
var filteredBooks = [ Book] ( )
16
- let books = [
17
- Book (
18
- title: " Operating Systems " ,
19
- course: " COP4600 " , price: 45 , coverImage: NSURL ( string: " http://ecx.images-amazon.com/images/I/51T73lIemvL._SX328_BO1,204,203,200_.jpg " ) ) ,
20
- Book ( title: " English Composition 1 " , course: " ENC1102 " , price: 60 , coverImage: NSURL ( string: " http://image.slidesharecdn.com/modernworldhistorytextbooksocialtb-140208031911-phpapp01/95/modern-world-history-textbook-social-tb-1-638.jpg?cb=1391830310 " ) )
21
-
22
- ]
15
+ var books = [ Book] ( )
23
16
24
17
var collectionView : UICollectionView !
25
18
let collectionViewInsets = UIEdgeInsets ( top: 4 , left: 4 , bottom: 0 , right: 4 )
19
+
20
+ let refreshControl = UIRefreshControl ( )
26
21
27
22
@IBOutlet weak var searchField : UITextField !
28
23
29
24
override func viewDidLoad( ) {
30
25
super. viewDidLoad ( )
31
26
setupViews ( )
27
+ refresh ( refreshControl)
32
28
}
33
29
34
30
func setupViews( ) {
@@ -54,8 +50,13 @@ class BooksViewController: UIViewController {
54
50
collectionView!. setCollectionViewLayout ( layout, animated: false )
55
51
collectionView!. registerClass ( BookListingCell . self, forCellWithReuseIdentifier: reuseIdentifier)
56
52
collectionView. backgroundColor = . whiteColor( )
57
-
53
+ collectionView. alwaysBounceVertical = true ;
54
+ collectionView. bounces = true
55
+
56
+ refreshControl. addTarget ( self , action: " refresh: " , forControlEvents: UIControlEvents . ValueChanged)
57
+
58
58
view. addSubview ( collectionView)
59
+ collectionView. addSubview ( refreshControl)
59
60
setupConstraints ( )
60
61
}
61
62
@@ -86,6 +87,16 @@ class BooksViewController: UIViewController {
86
87
let array = ( books as NSArray ) . filteredArrayUsingPredicate ( searchPredicate)
87
88
filteredBooks = array as! [ Book ]
88
89
}
90
+
91
+ func refresh( sender: UIRefreshControl ) {
92
+ Database . getBooks ( ) . then { books -> Void in
93
+ self . books = books
94
+ self . collectionView. reloadData ( )
95
+ sender. endRefreshing ( )
96
+ } . error { error -> Void in
97
+ print ( " Getting Books Error: \( error) " )
98
+ }
99
+ }
89
100
}
90
101
91
102
extension BooksViewController : UICollectionViewDelegate , UICollectionViewDataSource {
@@ -112,8 +123,12 @@ extension BooksViewController: UICollectionViewDelegate, UICollectionViewDataSou
112
123
113
124
func collectionView( collectionView: UICollectionView , didSelectItemAtIndexPath indexPath: NSIndexPath ) {
114
125
collectionView. deselectItemAtIndexPath ( indexPath, animated: true )
115
- let bookDetailsViewController = storyboard? . instantiateViewControllerWithIdentifier ( " BookDetailsViewController " )
116
- navigationController? . pushViewController ( bookDetailsViewController!, animated: true )
126
+ if let bookDetailsViewController = storyboard? . instantiateViewControllerWithIdentifier ( " BookDetailsViewController " )
127
+ as? BookDetailsNEWController {
128
+ let book = searchField. text!. isEmpty ? books [ indexPath. row] : filteredBooks [ indexPath. row]
129
+ bookDetailsViewController. book = book
130
+ navigationController? . pushViewController ( bookDetailsViewController, animated: true )
131
+ }
117
132
}
118
133
}
119
134
0 commit comments