Skip to content

Instantly share code, notes, and snippets.

@lfarah
Created July 21, 2014 16:34
Show Gist options
  • Save lfarah/b70d1cf861801d39ca2c to your computer and use it in GitHub Desktop.
Save lfarah/b70d1cf861801d39ca2c to your computer and use it in GitHub Desktop.
//
// MeetingProfileViewController.swift
// InviteMeu
//
// Created by Lucas Farah on 7/15/14.
// Copyright (c) 2014 Hackaton. All rights reserved.
//
import UIKit
class MeetingProfileViewController: UIViewController {
@IBOutlet var imgvFoto: UIImageView
@IBAction func butRequest(sender: AnyObject)
{
let user: PFUser = PFUser.currentUser()
let nomeUser = user.username
//Local Notif
// var localNotification:UILocalNotification = UILocalNotification()
// localNotification.category = "FIRST_CATEGORY"
// localNotification.alertBody = "\(nomeUser) sent you an invitation"
//
// localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
// UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
let defaults = NSUserDefaults.standardUserDefaults()
let latitude = defaults.objectForKey("latitude") as Double
let longitude = defaults.objectForKey("longitude") as Double
//Push Notifif
let data: NSDictionary = ["title":"matata","alert":"potato","sound":"cheering.caf","user":nomeUser,"latitude":latitude,"longitude":longitude,"isRequest":false]
let query: PFQuery = PFUser.query()
query.whereKey("username", equalTo: defaults.objectForKey("meetNome") as String)
var push: PFPush = PFPush()
push.setData(data)
push.setQuery(query)
push.sendPushInBackground()
}
//Resposta push da pessoa
func requestReceived()
{
let defaults = NSUserDefaults.standardUserDefaults()
//Se pessoa aceitou o invite
if defaults.objectForKey("isAccept").isEqual(true)
{
// self.presentViewController(ConnectViewController(), animated: true, completion: nil)
// self.showViewController(ConnectViewController(), sender: self)
self.performSegueWithIdentifier("macumba", sender: self)
}
else
{
self.presentViewController(PeopleNearViewController(), animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
self.title = defaults.objectForKey("meetNome") as String
self.imgvFoto.layer.cornerRadius = self.imgvFoto.frame.size.width / 2
self.imgvFoto.clipsToBounds = true
//Convertendo NSData do NSUserDefaults para UIImage
let data = defaults.objectForKey("meetFoto") as NSData
self.imgvFoto.image = UIImage(data: data)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
//
// PeopleNearViewController.swift
// InviteMeu
//
// Created by Lucas Farah on 7/12/14.
// Copyright (c) 2014 Hackaton. All rights reserved.
//
import UIKit
import CoreLocation
class PeopleNearViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate {
@IBOutlet var tabela: UITableView
@IBOutlet var user: PFUser
var location : CLLocationManager = CLLocationManager();
var foto: NSMutableArray = NSMutableArray()
var lista: NSArray = ["potato","matata"]
var point = PFGeoPoint(latitude:40.0, longitude:-30.0)
var counter = 1
override func viewDidLoad() {
super.viewDidLoad()
self.location.delegate = self;
self.location.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.location.distanceFilter = 10;
self.location.requestAlwaysAuthorization();
self.location.startUpdatingLocation();
if PFUser.currentUser()
{
self.user = PFUser.currentUser()
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint!, error: NSError!) -> Void in
if !error {
self.location.stopUpdatingLocation()
println("Estou no \(geoPoint.latitude),\(geoPoint.longitude)")
self.point = geoPoint
// User's location
let userGeoPoint = geoPoint
// Create a query for places
// var query = PFQuery(className:"User")
var query = PFUser.query()
// Interested in locations near user.
// query.whereKey("location", nearGeoPoint:userGeoPoint)
query.whereKey("location", nearGeoPoint: userGeoPoint, withinKilometers: 1)
// query.whereKey("email", equalTo:"[email protected]")
// Limit what could be a lot of points.
// query.limit = 10
// Final list of objects
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if !error {
// The find succeeded.
NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
self.lista = objects as NSArray
for object : PFObject! in objects {
let score: NSString = object["username"] as NSString
println(score)
// self.lista.append(score)
self.counter=2
self.tabela.reloadData()
}
println(self.lista)
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo)
}
}
}
}}
else
{
//Tela de Login
self.performSegueWithIdentifier("loginSemFazer", sender: self)
}
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell : TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as TableViewCell;
if (cell == nil){
cell = TableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell");
}
if self.counter==2
{
let score: PFObject = self.lista[indexPath.row] as PFObject
// for object : PFObject! in self.lista{
// let username: NSString = object["username"] as NSString
let username: NSString = score["username"] as NSString
println(username)
cell.lblNome.text = username
let geopoint: PFGeoPoint = score["location"] as PFGeoPoint
var distance:Double? = geopoint.distanceInKilometersTo(self.point)
var stringMedida = "km"
//Se for menor que 1, converter para km
if distance < 1
{
distance = distance!*1000
stringMedida = "m"
}
// cell.lblDistancia.text = "\(distance)\(stringMedida)"
cell.lblDistancia.text = NSString(format: "%.2f %@",distance!,stringMedida)
if stringMedida == "m"
{
cell.lblDistancia.text = NSString(format: "%.f %@",distance!,stringMedida)
}
let userImageFile = score["photo"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
cell.imgvFoto.image = image
self.foto.addObject(image)
cell.imgvFoto.layer.cornerRadius = cell.imgvFoto.frame.size.width / 2
cell.imgvFoto.clipsToBounds = true
} }
}
return cell;
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.lista.count
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
let defaults = NSUserDefaults.standardUserDefaults()
let score: PFObject = self.lista[indexPath.row] as PFObject
defaults.setObject(score["username"], forKey: "meetNome")
//Latitude&longitude
println(score["location"])
let geoPoint: PFGeoPoint = score["location"] as PFGeoPoint
let latitude = geoPoint.latitude
let longitude = geoPoint.longitude
defaults.setObject(latitude, forKey: "latitude")
defaults.setObject(longitude, forKey: "longitude")
var data: NSData = UIImageJPEGRepresentation(self.foto[indexPath.row] as UIImage, 1)
defaults.setObject(data, forKey: "meetFoto")
self.performSegueWithIdentifier("meetFeito", sender: self)
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment