-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUserTableViewController.swift
More file actions
158 lines (129 loc) · 6.17 KB
/
UserTableViewController.swift
File metadata and controls
158 lines (129 loc) · 6.17 KB
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
//
// UserTableViewController.swift
// Snapchat
//
// Created by Rommel Rico on 3/18/15.
// Copyright (c) 2015 Rommel Rico. All rights reserved.
//
import UIKit
class UserTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var userArray: [String] = []
var activeRecipient = 0
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
//Get list of users.
var query = PFUser.query()
query!.whereKey("username", notEqualTo:PFUser.currentUser()!.username!)
var users = query!.findObjects()
for user in users! {
userArray.append((user.username)!!)
tableView.reloadData()
}
timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("checkMessages"), userInfo: nil, repeats: true)
}
func checkMessages() {
var query = PFQuery(className: "Image")
query.whereKey("recipientUsername", equalTo:PFUser.currentUser()!.username!)
var images = query.findObjects()
var done = false
for image in images! {
if done == false {
var imageView:PFImageView = PFImageView()
imageView.file = image["photo"] as! PFFile
//Download image
imageView.loadInBackground({ (photo, error) -> Void in
if error == nil {
var senderUsername = ""
if image["senderUsername"] != nil {
senderUsername = image["senderUsername"]! as! String
}
var alert = UIAlertController(title: "You have a message!", message: "Message from \(senderUsername)", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
var backgroundImage = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
backgroundImage.backgroundColor = UIColor.blackColor()
backgroundImage.alpha = 0.9
backgroundImage.tag = 3
self.view.addSubview(backgroundImage)
var displayedImage = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
displayedImage.image = photo
displayedImage.contentMode = UIViewContentMode.ScaleAspectFit
displayedImage.tag = 3
self.view.addSubview(displayedImage)
//delete the image
image.delete()
self.timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("hideMessage"), userInfo: nil, repeats: false)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
})
done = true
}
}
}
func hideMessage() {
//UIApplication.sharedApplication().endIgnoringInteractionEvents()
for subview in self.view.subviews {
if subview.tag == 3 {
subview.removeFromSuperview()
}
}
}
func pickImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = true
NSLog("About to present the image.")
self.presentViewController(image, animated: true, completion: nil)
}
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 Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return userArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = userArray[indexPath.row]
return cell
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: nil)
//Upload to Parse Code goes here.
var imageToSend = PFObject(className:"Image")
//imageToSend["image"] = UIImageJPEGRepresentation(image, 0.1111)
imageToSend["photo"] = PFFile(name: "Image", data: UIImageJPEGRepresentation(image, 0.5))
imageToSend["senderUsername"] = PFUser.currentUser()!.username
imageToSend["recipientUsername"] = userArray[activeRecipient]
imageToSend.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The object has been saved.
NSLog("Image sent succesfully!")
} else {
// There was a problem, check error.description
NSLog("ERROR: \(error!.description)")
}
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
activeRecipient = indexPath.row
pickImage(self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "logout" {
timer.invalidate()
PFUser.logOut()
}
}
}