updated some storyboard elements for friend system, reworked friend request system due to trouble with CC in parse, friend system 95% done

This commit is contained in:
Okechi 2016-02-03 09:16:03 -05:00
parent d39b473f21
commit e9faaf69fd
5 changed files with 225 additions and 39 deletions

View File

@ -8,15 +8,22 @@
import UIKit
import CoreData
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate , CLLocationManagerDelegate{
var window: UIWindow?
// var locationStarted = false
// var locationManager: CLLocationManager = CLLocationManager()
// var app = UIApplication.sharedApplication()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//create new CLLocaationManager
/*var locationStarted = false
locationManager.delegate = self*/
Parse.logLevel()
//NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveWillSendURLRequestNotification:", name: PFNetworkWillSendURLRequestNotification, object: nil)
@ -25,8 +32,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Override point for customization after application launch.
Parse.setApplicationId("OUFlGeqCzg03ZbstGcbskj7UNwp53Y0yad9Fi6I0", clientKey: "xaw0kbAFnBi3zLhT1uSnzrzju56m7CnQJ8hJHW1K")
PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions);
/*
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
print(paths[0])
print(paths[0])*/
return true
}
@ -50,8 +59,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// locationManager.startUpdatingLocation()
}
func applicationWillEnterForeground(application: UIApplication) {
@ -69,6 +77,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.saveContext()
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: NSURL = {

View File

@ -17,10 +17,11 @@ class FriendDataSource{
var query = PFUser.query()
var currentUser: String! = PFUser.currentUser()?.username
let predicate: NSPredicate = NSPredicate(format: "(((RequestStatus = %@) OR (RequestStatus = %@)) AND (username = %@))", argumentArray: ["accepted", "pending",currentUser])
var friendsQuery: PFQuery = PFQuery(className: "FriendRequest", predicate: predicate)
//finds pending friend request from main FriendRequest table
let predicate: NSPredicate = NSPredicate(format: "((RequestStatus = %@) AND (username = %@))", argumentArray: ["pending",currentUser])
var friendsPendingQuery: PFQuery = PFQuery(className: "FriendRequest", predicate: predicate)
friendsQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock {
friendsPendingQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
//print(error)
@ -50,6 +51,30 @@ class FriendDataSource{
}
NSLog("%d", self.dataSource.count)
//dispatches to the main queue the task of getting users current friends from relational table in Parse
var friendsRelation: PFRelation! = PFUser.currentUser()?.relationForKey("FriendRequest")
var friendsQuery = friendsRelation.query().whereKey("RequestStatus", equalTo: "accepted")
dispatch_async(dispatch_get_main_queue(), {
friendsQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock({
(objects, error) -> Void in
if (error == nil){
for object in objects! {
let userName = object.objectForKey("OtherUser")!.objectForKey("username") as! String
let requestStatus = object.objectForKey("RequestStatus")! as! String
var newFriend: FriendData = FriendData(display: userName, status: requestStatus)
//print(userName)
self.dataSource.append(newFriend)
}
}
})
})
}
NSNotificationCenter.defaultCenter().postNotificationName("refreshTableView", object: nil)

View File

@ -8,19 +8,102 @@
import UIKit
class FriendRequestViewController: UIViewController {
class FriendRequestViewController: UIViewController{
var searchedUsername: String!
var frnds: [String]!
@IBOutlet var displayImage: UIImageView!
@IBOutlet var foundName: UILabel!
@IBOutlet var sendRequest: UIButton!
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var searchBar: UITextField!
@IBAction func sendButtonClicked(sender: AnyObject){
var query = PFQuery(className: "_User")
query.whereKey("username", equalTo: foundName.text!)
println(foundName.text!)
dispatch_async(dispatch_get_main_queue(),{
query.getFirstObjectInBackgroundWithBlock({
(object:PFObject?, error: NSError?) -> Void in
//make friend request object for current user
var friendRequest:PFObject = PFObject(className: "FriendRequest")
friendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: (object!.objectId)!), forKey: "OtherUser")
friendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: PFUser.currentUser()?.objectId!), forKey: "FromUser")
friendRequest.setObject(PFUser.currentUser()?.username as String!, forKey: "username")
friendRequest.setObject(object!.objectForKey("username")!, forKey: "inRealtionTo")
friendRequest.setObject("Awaiting Response", forKey: "RequestStatus")
friendRequest.save()
PFUser.currentUser()?.relationForKey("FriendRequest").addObject(friendRequest)
//make friend request object for other user
var otherFriendRequest:PFObject = PFObject(className: "FriendRequest")
otherFriendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: (object!.objectId)!), forKey: "FromUser")
otherFriendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: PFUser.currentUser()?.objectId!), forKey: "OtherUser")
otherFriendRequest.setObject(object!.objectForKey("username")!, forKey: "username")
otherFriendRequest.setObject(PFUser.currentUser()?.username as String!, forKey: "inRealtionTo")
otherFriendRequest.setObject("pending", forKey: "RequestStatus")
otherFriendRequest.save()
dispatch_async(dispatch_get_main_queue(), {() -> Void in
PFUser.currentUser()!.saveInBackgroundWithBlock({
(succeeded, error) -> Void in
if(succeeded){
var alert = UIAlertController(title: "Request Sent!", message: "Your friend request was sent successfully!", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)}))
self.presentViewController(alert, animated: true, completion: nil)
println(friendRequest.objectId!)
var params = NSMutableDictionary()
params.setObject(friendRequest.objectId! as String!, forKey: "friendRequest")
//var params = NSMutableDictionary()
//params.s
//var param = ["friendRequest" : friendRequest.objectId!]
/*
PFCloud.callFunctionInBackground("addFriendToFriendRelation", withParameters: params as [NSObject : AnyObject]){
(response, error) -> Void in
if(error == nil){
println(response as! String)
}else{
println(error)
}
}*/
/*PFCloud.callFunctionInBackground("hello", withParameters: nil){
(response, error) -> Void in
println(response as! String)
}*/
}
})
})
})
})
}
@IBAction func buttonClicked(sender: AnyObject){
searchBar.resignFirstResponder();
}
func setData(friends: [String]){
self.frnds = friends
}
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
self.displayImage.layer.cornerRadius = self.displayImage.frame.size.width/2
self.sendRequest.enabled = false
self.sendRequest.tintColor = UIColor.grayColor()
// Do any additional setup after loading the view.
}
@ -41,3 +124,64 @@ class FriendRequestViewController: UIViewController {
*/
}
extension FriendRequestViewController: UITextFieldDelegate{
func textFieldDidBeginEditing(textField: UITextField) {
println("Textfield did begin editing method called")
}
func textFieldDidEndEditing(textField: UITextField) {
self.searchedUsername = searchBar.text
var inFriendsList: Bool = false
/*for name in self.frnds{
if name == self.searchedUsername{
inFriendsList = true
break
}
}*/
if(inFriendsList == false){
var query = PFQuery(className: "_User")
query.whereKey("username", equalTo: searchedUsername)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
query.getFirstObjectInBackgroundWithBlock({
(object: PFObject?, error: NSError?) -> Void in
if(error == nil){
self.foundName.text = object?.objectForKey("username") as? String
self.sendRequest.enabled = true
}else{
self.foundName.text = "No Friend Found :("
self.sendRequest.enabled = false
}
})
})
}else{
let alert = UIAlertController(title: "Already Friends", message: "You are already friends with this user!", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
return true
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
return true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
searchBar.resignFirstResponder()
return true
}
}

View File

@ -141,7 +141,20 @@ class FriendsListViewController: UIViewController, UITableViewDelegate, UITableV
}
if segue.identifier == "FriendRequestSegue" {
/* var frndNames: [String] = []
for i in 0...self.friends.count-1{
var str:String! = self.friends[i].displayName as String!
frndNames.append(str)
}
/*for name in self.friends{
//bad instruction here, fix later
println(name.displayName as String!)
frndNames.append(name.displayName!)
}*/
let vc: FriendRequestViewController = (segue.destinationViewController as? FriendRequestViewController)!
vc.setData(frndNames)*/
}
}

View File

@ -38,18 +38,14 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<searchBar contentMode="redraw" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="h8Q-XM-pW1">
<rect key="frame" x="0.0" y="64" width="320" height="44"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="Iog-DT-pLJ" id="P8f-w3-vgg"/>
</connections>
</searchBar>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Gck-Ni-mM6">
<rect key="frame" x="85" y="329" width="150" height="30"/>
<state key="normal" backgroundImage="Send.png">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="sendButtonClicked:" destination="Iog-DT-pLJ" eventType="touchUpInside" id="AqX-lB-tHv"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dcC-PI-dUM">
<rect key="frame" x="92" y="172" width="137" height="137"/>
@ -61,6 +57,14 @@
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Search for a new Friend" textAlignment="center" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="EAo-0U-cmz">
<rect key="frame" x="0.0" y="64" width="320" height="30"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<textInputTraits key="textInputTraits"/>
<connections>
<action selector="buttonClicked:" destination="Iog-DT-pLJ" eventType="editingDidEnd" id="Xrc-To-w28"/>
</connections>
</textField>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
@ -68,21 +72,11 @@
<connections>
<outlet property="displayImage" destination="dcC-PI-dUM" id="Lxu-7b-iya"/>
<outlet property="foundName" destination="wxx-ws-paN" id="wjW-kZ-8UT"/>
<outlet property="searchBar" destination="h8Q-XM-pW1" id="MKf-3P-5b3"/>
<outlet property="searchDisplayController" destination="NMM-eX-IwK" id="umT-fC-wZJ"/>
<outlet property="searchBar" destination="EAo-0U-cmz" id="GVg-mw-8h0"/>
<outlet property="sendRequest" destination="Gck-Ni-mM6" id="TI6-8O-dns"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ADA-FB-J9A" userLabel="First Responder" sceneMemberID="firstResponder"/>
<searchDisplayController id="NMM-eX-IwK">
<connections>
<outlet property="delegate" destination="Iog-DT-pLJ" id="9AD-Tz-PcE"/>
<outlet property="searchBar" destination="h8Q-XM-pW1" id="dzH-oO-HLs"/>
<outlet property="searchContentsController" destination="Iog-DT-pLJ" id="fVJ-8e-3wm"/>
<outlet property="searchResultsDataSource" destination="Iog-DT-pLJ" id="3ar-To-0xK"/>
<outlet property="searchResultsDelegate" destination="Iog-DT-pLJ" id="BDA-1Y-kVj"/>
</connections>
</searchDisplayController>
</objects>
<point key="canvasLocation" x="471" y="-1207"/>
</scene>
@ -185,32 +179,32 @@
<rect key="frame" x="0.0" y="64" width="320" height="504"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<searchBar key="tableHeaderView" contentMode="redraw" id="pFw-ta-NIX">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<rect key="frame" x="0.0" y="262" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="Wv2-hP-rqT" id="a4A-dx-bsZ"/>
</connections>
</searchBar>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="RequestCell" rowHeight="24" id="3OS-XI-GBP" customClass="FriendRequestCell" customModule="GetHip" customModuleProvider="target">
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="RequestCell" rowHeight="23" id="3OS-XI-GBP" customClass="FriendRequestCell" customModule="GetHip" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="3OS-XI-GBP" id="02t-J2-u14">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Friend Requests" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MOq-7s-3br">
<rect key="frame" x="21" y="2" width="139" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="0" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="TSY-RW-sS0">
<rect key="frame" x="237" y="1" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.53841145833333326" green="0.53841145833333326" blue="0.53841145833333326" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Friend Requests" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="MOq-7s-3br">
<rect key="frame" x="21" y="1" width="139" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>