Skip to content

Commit a885461

Browse files
committed
- converted ProfileImagePicker ViewController
- bug fixes
1 parent 3563eee commit a885461

File tree

6 files changed

+210
-20
lines changed

6 files changed

+210
-20
lines changed

SampleAppSwift/SampleAppSwift/Base.lproj/Main.storyboard

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,25 @@
326326
<rect key="frame" x="0.0" y="179" width="329" height="389"/>
327327
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
328328
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
329+
<prototypes>
330+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="profileImageTableViewCell" textLabel="UNz-Q5-1tl" style="IBUITableViewCellStyleDefault" id="Jci-WB-VTM">
331+
<rect key="frame" x="0.0" y="22" width="329" height="44"/>
332+
<autoresizingMask key="autoresizingMask"/>
333+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Jci-WB-VTM" id="mBB-0N-nQQ">
334+
<rect key="frame" x="0.0" y="0.0" width="329" height="43"/>
335+
<autoresizingMask key="autoresizingMask"/>
336+
<subviews>
337+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="UNz-Q5-1tl">
338+
<rect key="frame" x="15" y="0.0" width="299" height="43"/>
339+
<autoresizingMask key="autoresizingMask"/>
340+
<fontDescription key="fontDescription" type="system" pointSize="16"/>
341+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
342+
<nil key="highlightedColor"/>
343+
</label>
344+
</subviews>
345+
</tableViewCellContentView>
346+
</tableViewCell>
347+
</prototypes>
329348
<connections>
330349
<outlet property="dataSource" destination="AVr-4i-Laq" id="Ut5-2B-XWQ"/>
331350
<outlet property="delegate" destination="AVr-4i-Laq" id="46A-Qh-oAY"/>

SampleAppSwift/SampleAppSwift/Controller/ContactEditViewController.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class ContactEditViewController: UIViewController {
11+
class ContactEditViewController: UIViewController, ProfileImagePickerDelegate {
1212
@IBOutlet weak var contactEditScrollView: UIScrollView!
1313

1414
weak var contactViewController: ContactViewController?
@@ -136,7 +136,26 @@ class ContactEditViewController: UIViewController {
136136
}
137137

138138
func onChangeImageClick() {
139+
let profileImagePickerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileImagePickerViewController") as! ProfileImagePickerViewController
140+
profileImagePickerViewController.delegate = self
141+
profileImagePickerViewController.record = contactRecord!
139142

143+
self.navigationController?.pushViewController(profileImagePickerViewController, animated: true)
144+
}
145+
146+
// Profile image picker delegate
147+
148+
func didSelectItem(item: String) {
149+
self.navigationController?.popViewControllerAnimated(true)
150+
// gets info passed back up from the image picker
151+
self.contactRecord!.imageURL = item
152+
}
153+
154+
func didSelectItem(item: String, withImage image: UIImage) {
155+
self.navigationController?.popViewControllerAnimated(true)
156+
// gets info passed back up from the image picker
157+
self.imageURL = item
158+
self.profileImage = image
140159
}
141160

142161
// MARK: - Private methods
@@ -160,6 +179,17 @@ class ContactEditViewController: UIViewController {
160179
putValueIn(contactRecord.notes, forKey: "Notes")
161180
}
162181

182+
let changeImageButton = UIButton(type: .System)
183+
var y = CGRectGetMaxY(contactEditScrollView.subviews.last!.frame)
184+
changeImageButton.frame = CGRectMake(0, y + 10, view.frame.size.width, 40)
185+
changeImageButton.titleLabel?.textAlignment = .Center
186+
changeImageButton.setTitle("Change image", forState: .Normal)
187+
changeImageButton.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 20.0)
188+
changeImageButton.setTitleColor(UIColor(red: 107/255.0, green: 170/255.0, blue: 178/255.0, alpha: 1.0), forState: .Normal)
189+
190+
changeImageButton.addTarget(self, action: "onChangeImageClick", forControlEvents: .TouchUpInside)
191+
//contactEditScrollView.addSubview(changeImageButton)
192+
163193
// add all the contact info views
164194
if let contactDetails = contactDetails {
165195

@@ -174,7 +204,7 @@ class ContactEditViewController: UIViewController {
174204
}
175205

176206
// create button to add a new address
177-
let y = CGRectGetMaxY(contactEditScrollView.subviews.last!.frame)
207+
y = CGRectGetMaxY(contactEditScrollView.subviews.last!.frame)
178208
let addButton = UIButton(type: .System)
179209
addButton.frame = CGRectMake(0, y + 10, view.frame.size.width, 40)
180210
addButton.backgroundColor = UIColor(red: 107/255.0, green: 170/255.0, blue: 178/255.0, alpha: 1.0)

SampleAppSwift/SampleAppSwift/Controller/ContactViewController.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ class ContactViewController: UIViewController {
139139

140140
var next_y: CGFloat = 40 // track where the lowest item is
141141
if !record.email.isEmpty {
142-
let label = UILabel(frame: CGRectMake(75, 40, subView.frame.size.width - 75, 30))
142+
let label = UILabel(frame: CGRectMake(50, 40, subView.frame.size.width - 50, 30))
143143
label.text = record.email
144144
label.font = UIFont(name: "HelveticaNeue-Light", size: 20.0)
145145
label.textColor = UIColor(red: 249/255.0, green: 249/255.0, blue: 249/255.0, alpha: 1.0)
146146
subView.addSubview(label)
147147

148-
let imageView = UIImageView(frame: CGRectMake(50, 45, 20, 20))
148+
let imageView = UIImageView(frame: CGRectMake(25, 45, 20, 20))
149149
imageView.image = UIImage(named: "mail")
150150
imageView.contentMode = .ScaleAspectFit
151151
subView.addSubview(imageView)
@@ -154,13 +154,13 @@ class ContactViewController: UIViewController {
154154
}
155155

156156
if !record.phone.isEmpty {
157-
let label = UILabel(frame: CGRectMake(75, next_y, subView.frame.size.width - 75, 20))
157+
let label = UILabel(frame: CGRectMake(50, next_y, subView.frame.size.width - 50, 20))
158158
label.text = record.phone
159159
label.font = UIFont(name: "HelveticaNeue-Light", size: 20.0)
160160
label.textColor = UIColor(red: 253/255.0, green: 253/255.0, blue: 250/255.0, alpha: 1.0)
161161
subView.addSubview(label)
162162

163-
let imageView = UIImageView(frame: CGRectMake(50, next_y, 20, 20))
163+
let imageView = UIImageView(frame: CGRectMake(25, next_y, 20, 20))
164164
imageView.image = UIImage(named: "phone1")
165165
imageView.contentMode = .ScaleAspectFit
166166
subView.addSubview(imageView)
@@ -169,7 +169,7 @@ class ContactViewController: UIViewController {
169169
}
170170

171171
if !record.address.isEmpty && !record.city.isEmpty && !record.state.isEmpty && !record.zipCode.isEmpty {
172-
let label = UILabel(frame: CGRectMake(75, next_y, subView.frame.size.width - 75, 20))
172+
let label = UILabel(frame: CGRectMake(50, next_y, subView.frame.size.width - 50, 20))
173173
label.font = UIFont(name: "HelveticaNeue-Light", size: 19.0)
174174
label.numberOfLines = 0
175175
label.textColor = UIColor(red: 250/255.0, green: 250/255.0, blue: 250/255.0, alpha: 1.0)
@@ -182,7 +182,7 @@ class ContactViewController: UIViewController {
182182
label.sizeToFit()
183183
subView.addSubview(label)
184184

185-
let imageView = UIImageView(frame: CGRectMake(50, next_y, 20, 20))
185+
let imageView = UIImageView(frame: CGRectMake(25, next_y, 20, 20))
186186
imageView.image = UIImage(named: "home")
187187
imageView.contentMode = .ScaleAspectFit
188188
subView.addSubview(imageView)
@@ -193,8 +193,8 @@ class ContactViewController: UIViewController {
193193
// resize the subview
194194
var viewFrame = subView.frame
195195
viewFrame.size.height = next_y + 20
196-
view.frame.origin.x = view.frame.size.width * 0.06
197-
view.frame.size.width = view.frame.size.width * 0.88
196+
viewFrame.origin.x = view.frame.size.width * 0.06
197+
viewFrame.size.width = view.frame.size.width * 0.88
198198
subView.frame = viewFrame
199199

200200
return subView
@@ -232,7 +232,6 @@ class ContactViewController: UIViewController {
232232
private func buildContactView() {
233233
viewLock.lock()
234234
while !viewReady {
235-
NSThread.sleepForTimeInterval(0.0001)
236235
viewLock.wait()
237236
}
238237
viewLock.unlock()
@@ -242,7 +241,7 @@ class ContactViewController: UIViewController {
242241
}
243242

244243
// clear out the view
245-
dispatch_sync(dispatch_get_main_queue()) {
244+
dispatch_async(dispatch_get_main_queue()) {
246245
for view in self.contactDetailScrollView.subviews {
247246
view.removeFromSuperview()
248247
}
@@ -261,6 +260,9 @@ class ContactViewController: UIViewController {
261260
// add the name label
262261
let nameLabel = UILabel(frame: CGRectMake(0, y, self.view.frame.size.width, 35))
263262
nameLabel.text = self.contactRecord.fullName
263+
nameLabel.font = UIFont(name: "HelveticaNeue-Light", size: 25.0)
264+
nameLabel.textAlignment = .Center
265+
self.contactDetailScrollView.addSubview(nameLabel)
264266
y += 40
265267

266268
if !self.contactRecord.twitter.isEmpty {
@@ -320,10 +322,6 @@ class ContactViewController: UIViewController {
320322
}
321323
}
322324

323-
dispatch_sync(dispatch_get_main_queue()) {
324-
self.contactDetailScrollView.reloadInputViews()
325-
}
326-
327325
// wait until the group is ready to build group list subviews
328326
groupLock.lock()
329327
while !groupReady {

SampleAppSwift/SampleAppSwift/Controller/ProfileImagePickerViewController.swift

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,159 @@
99
import UIKit
1010

1111
protocol ProfileImagePickerDelegate: class {
12-
12+
func didSelectItem(item: String)
13+
func didSelectItem(item: String, withImage image: UIImage)
1314
}
1415

15-
class ProfileImagePickerViewController: UIViewController {
16+
class ProfileImagePickerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
1617
@IBOutlet var tableView: UITableView!
1718
@IBOutlet var imageNameTextField: UITextField!
1819

1920
// set only when editing an existing contact
2021
// the contact we are choosing a profile image for
2122
var record: ContactRecord!
2223

24+
// holds image picked from the camera roll
25+
var imageToUpload: UIImage?
26+
27+
// list of available profile images
28+
var imageListContentArray: [String] = []
29+
2330
weak var delegate: ProfileImagePickerDelegate?
2431

32+
private lazy var baseUrl: String = {
33+
return NSUserDefaults.standardUserDefaults().valueForKey(kBaseInstanceUrl) as! String
34+
}()
35+
2536
override func viewDidLoad() {
2637
super.viewDidLoad()
27-
38+
getImageListFromServer()
39+
40+
imageNameTextField.setValue(UIColor(red: 180/255.0, green: 180/255.0, blue: 180/255.0, alpha: 1.0), forKeyPath: "_placeholderLabel.textColor")
41+
tableView.contentInset = UIEdgeInsetsMake(-70, 0, -20, 0)
42+
}
43+
44+
override func viewWillAppear(animated: Bool) {
45+
super.viewWillAppear(animated)
46+
47+
let navBar = self.navBar
48+
navBar.showDone()
49+
self.navBar.doneButton.addTarget(self, action: "onDoneButtonClick", forControlEvents: .TouchDown)
50+
}
51+
52+
override func viewWillDisappear(animated: Bool) {
53+
super.viewWillDisappear(animated)
54+
55+
self.navBar.doneButton.removeTarget(self, action: "onDoneButtonClick", forControlEvents: .TouchDown)
2856
}
2957

3058
@IBAction func onChooseImageClick() {
59+
let imagePicker = UIImagePickerController()
60+
imagePicker.sourceType = .PhotoLibrary
61+
imagePicker.delegate = self
62+
presentViewController(imagePicker, animated: true, completion: nil)
63+
}
64+
65+
func onDoneButtonClick() {
66+
// actually put image up on the server when the contact gets created
67+
if let imageToUpload = imageToUpload {
68+
// if we chose an image to upload
69+
var imageName: String!
70+
if imageNameTextField.text!.isEmpty {
71+
imageName = "profileImage"
72+
} else {
73+
imageName = imageNameTextField.text!
74+
}
75+
delegate?.didSelectItem(imageName, withImage: imageToUpload)
76+
} else {
77+
self.navigationController?.popViewControllerAnimated(true)
78+
}
79+
}
80+
81+
//MARK: - Image picker delegate
82+
83+
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
84+
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
85+
imageToUpload = image
86+
87+
dismissViewControllerAnimated(true, completion: nil)
88+
}
89+
90+
//MARK: - Tableview data source
91+
92+
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
93+
return imageListContentArray.count
94+
}
95+
96+
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
97+
let cell = tableView.dequeueReusableCellWithIdentifier("profileImageTableViewCell", forIndexPath: indexPath)
98+
cell.textLabel?.text = imageListContentArray[indexPath.row]
3199

100+
return cell
101+
}
102+
103+
//MARK: - Tableview delegate
104+
105+
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
106+
let toPass = imageListContentArray[indexPath.row]
107+
delegate?.didSelectItem(toPass)
108+
}
109+
110+
//MARK: - Private methods
111+
112+
private func getImageListFromServer() {
113+
let swgSessionToken = NSUserDefaults.standardUserDefaults().valueForKey(kSessionTokenKey) as? String
114+
if swgSessionToken?.characters.count > 0 {
115+
116+
let api = NIKApiInvoker.sharedInstance
117+
118+
// build rest path for request, form is <base instance url>/api/v2/files/container/<folder path>/
119+
// here the folder path is contactId/
120+
let containerName = kContainerName
121+
let folderPath = "/\(record.id)"
122+
123+
// note that you need the extra '/' here at the end of the api path because
124+
// the url is pointing to a folder
125+
let restApiPath = "\(baseUrl)/files/\(containerName)/\(folderPath)/"
126+
NSLog("\nAPI path: \(restApiPath)\n")
127+
128+
// only want to get files, not any sub folders
129+
let queryParams: [String: AnyObject] = ["include_folders": "0",
130+
"include_files": "1"]
131+
132+
let headerParams = ["X-DreamFactory-Api-Key": kApiKey,
133+
"X-DreamFactory-Session-Token": swgSessionToken!]
134+
let contentType = "application/json"
135+
136+
api.restPath(restApiPath, method: "GET", queryParams: queryParams, body: nil, headerParams: headerParams, contentType: contentType, completionBlock: { (response, error) -> Void in
137+
if let error = error {
138+
// check if the error is file not found
139+
if error.code == 404 {
140+
let decode = error.userInfo["error"]?.firstItem as? JSON
141+
let message = decode?["message"] as? String
142+
if message != nil && message!.containsString("does not exist in storage") {
143+
NSLog("Warning: Error getting profile image list data from server: \(message)")
144+
return
145+
}
146+
}
147+
// else report normally
148+
NSLog("Error getting profile image list data from server: \(error)")
149+
dispatch_async(dispatch_get_main_queue()) {
150+
self.navigationController?.popToRootViewControllerAnimated(true)
151+
}
152+
} else {
153+
self.imageListContentArray.removeAll()
154+
let records = response!["file"] as! JSONArray
155+
for record in records {
156+
if let record = record["name"] as? String {
157+
self.imageListContentArray.append(record)
158+
}
159+
}
160+
dispatch_async(dispatch_get_main_queue()) {
161+
self.tableView.reloadData()
162+
}
163+
}
164+
})
165+
}
32166
}
33167
}

SampleAppSwift/SampleAppSwift/View/ContactInfoView.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ class ContactInfoView: UIView {
4747
}
4848

4949
func buildToDiciontary() -> [String: AnyObject] {
50-
return [:]
50+
return ["id": record.id,
51+
"contact_id": record.contactId,
52+
"info_type": record.type,
53+
"phone": record.phone,
54+
"email": record.email,
55+
"address": record.address,
56+
"city": record.city,
57+
"state": record.state,
58+
"zip": record.zipCode,
59+
"country": record.country]
5160
}
5261

5362
private func updateFields() {

0 commit comments

Comments
 (0)