-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsersVC.swift
executable file
·201 lines (153 loc) · 7.31 KB
/
UsersVC.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// UsersVC.swift
// Glamly
//
// Created by Kevin Grozav on 5/3/16.
// Copyright © 2016 UCSD. All rights reserved.
//
import ParseUI
import UIKit
import Parse
class UsersVC: UITableViewController, UISearchBarDelegate, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout { //, UICollectionViewDataSource {
var searchBar = UISearchBar()
//table view information we retrieve from servers
var usernameArray = [String]()
var avaArray = [PFFile]()
//collection view information we retrieve from servers
var collectionView : UICollectionView!
var picArray = [PFFile]()
var uuidArray = [String]()
var page : Int = 24
override func viewDidLoad() {
super.viewDidLoad()
//search bar
searchBar.delegate = self
searchBar.sizeToFit()
searchBar.tintColor = UIColor.groupTableViewBackgroundColor()
searchBar.frame.size.width = self.view.frame.size.width - 34
let searchItem = UIBarButtonItem(customView: searchBar)
self.navigationItem.leftBarButtonItem = searchItem
loadUsers()
}
func loadUsers() {
let query = PFQuery(className: "_User")
query.addDescendingOrder("createdAt")
query.limit = 20
query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error: NSError?) in
if error == nil {
//clean up
self.usernameArray.removeAll(keepCapacity: false)
self.avaArray.removeAll(keepCapacity: false)
for object in objects! {
self.usernameArray.append(object.objectForKey("username") as! String)
self.avaArray.append(object.objectForKey("image") as! PFFile)
}
self.tableView.reloadData()
} else {
print(error?.localizedDescription)
}
}
}
func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let query = PFQuery(className: "_User")
query.whereKey("username", matchesRegex: "(?i)" + searchBar.text!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if error == nil {
//find by fullname
if objects!.isEmpty {
let nameQuery = PFQuery()
nameQuery.whereKey("fullname", matchesRegex:"(?i)" + self.searchBar.text!)
nameQuery.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) in
if error == nil {
//clean up
self.usernameArray.removeAll(keepCapacity: false)
self.avaArray.removeAll(keepCapacity: false)
//found related objects
for object in objects! {
self.usernameArray.append(object.objectForKey("username") as! String)
self.avaArray.append(object.objectForKey("ava") as! PFFile)
}
self.tableView.reloadData()
}
})
}
//clean up
self.usernameArray.removeAll(keepCapacity: false)
self.avaArray.removeAll(keepCapacity: false)
//found related objects
for object in objects! {
self.usernameArray.append(object.objectForKey("username") as! String)
self.avaArray.append(object.objectForKey("ava") as! PFFile)
}
self.tableView.reloadData()
}
}
return true
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchBar.resignFirstResponder()
searchBar.text = ""
searchBar.showsCancelButton = false
//show users again after clicking cancel
loadUsers()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return usernameArray.count
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.view.frame.size.width / 4
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! followersCell
cell.followBtn.hidden = true
cell.usernameLbl.text = usernameArray[indexPath.row]
avaArray[indexPath.row].getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) in
if error == nil {
cell.avaImg.image = UIImage(data:data!)
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// let cell = tableView.cellForRowAtIndexPath(indexPath) as! followersCell
// until we implement home vc and guest vc
// if cell.usernameLbl.text! == PFUser.currentUser()?.username {
// let home = self.storyboard?.instantiateInitialViewControllerWithIdentifier("homeVC") as! homeVC
// self.navigationController?.pushViewController(home, animated: true)
// } else {
//
// guestname.append(cell.usernameLbl.text!)
// let guest = storyboard?.instantiateViewControllerWithIdentifier("guestVC")
// self.navigationController?.pushViewController(guest, animated: true)
// }
}
//Collection View Implementation
func collectionViewLaunch() {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSizeMake( self.view.frame.size.width / 3, self.view.frame.size.width / 3)
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
let frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 20)
//instantiate the collection view
collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
collectionView.delegate = self
// collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
}
}