Last active
November 19, 2016 11:48
-
-
Save ShawnBaek/d5c808290a634bd5a3e71a0f8c0618b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CoreData | |
import Parse | |
import ParseLiveQuery | |
import ParseFacebookUtilsV4 | |
import Fabric | |
import Crashlytics | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func configureParse() { | |
Post.registerSubclass() | |
} | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
//Hidden statusbar | |
UIApplication.shared.isStatusBarHidden = true | |
// Override point for customization after application launch. | |
Parse.enableLocalDatastore() | |
Parse.setLogLevel(PFLogLevel.info); | |
//Initialize Pare | |
let config = ParseClientConfiguration(block: { | |
(ParseMutableClientConfiguration) -> Void in | |
//back4app | |
ParseMutableClientConfiguration.applicationId = "xxxxxxxx"; | |
ParseMutableClientConfiguration.clientKey = "xxxxxxxx"; | |
ParseMutableClientConfiguration.server = "https://xxx.com/"; | |
}); | |
Parse.initialize(with: config); | |
//Initialize Facebook | |
PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions) | |
PFUser.enableAutomaticUser() | |
buildUserInterface() | |
// color of window | |
window?.backgroundColor = UIColor.white | |
//configure Parse | |
configureParse() | |
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FeedVC.swift | |
// craket | |
// | |
// Created by BaekSungwook on 2016. 7. 17.. | |
// Copyright © 2016년 escape. All rights reserved. | |
// | |
import UIKit | |
import Parse | |
import ParseLiveQuery | |
import SDWebImage | |
import ActiveLabel | |
import MXParallaxHeader | |
class FeedVC: UITableViewController{ | |
let liveQueryClient = ParseLiveQuery.Client() | |
var posts = [PFObject]() { | |
didSet { | |
tableView.reloadData() | |
} | |
} | |
var postQuery: PFQuery<PFObject>? { | |
return Post.query()? | |
.order(byDescending: "createdAt") | |
} | |
//Post Subscription | |
private var subscription: Subscription<Post>? | |
//User class | |
var usernameArray = [String]() | |
var profileImgArray = [PFFile]() | |
//Post class | |
var dateArray = [String]() | |
var postImgArray = [PFFile]() | |
var descriptionArray = [String]() | |
var descriptionTestArray = [NSMutableAttributedString]() | |
var postID = [PFObject]() | |
//Comment class | |
var commentsArray = [String]() | |
var commentsByArray = [String]() | |
var idCommentArray = [NSMutableAttributedString]() | |
//Likes Count | |
var likesCountArray = [Int]() | |
var hasLikedArray = [Bool]() | |
var isLoadedView:Bool = false | |
var sellingArray = [Bool]() | |
var followingArray = [PFUser]() | |
//page size | |
var page : Int = 10 | |
//Default func | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView?.backgroundColor = UIColor(red: 0.0 / 255.0, green: 0.0 / 255.0, blue: 0.0 / 255.0, alpha: 1) | |
// Automatic row height | |
tableView.estimatedRowHeight = 450 | |
tableView.rowHeight = UITableViewAutomaticDimension | |
//Calling function to load posts | |
loadPosts() | |
} | |
//LiveQuery test | |
func testPosts() { | |
postQuery?.findObjectsInBackground(block: { (objects, error) in | |
if error == nil { | |
if let objects = objects { | |
self.posts = objects | |
self.subscribeToUpdates() | |
} | |
} else { | |
print("error : \(error?.localizedDescription)") | |
} | |
}) | |
} | |
func subscribeToUpdates() { | |
subscription = liveQueryClient | |
.subscribe(postQuery!) | |
.handle(Event.created) { _, message in | |
// self.printMessage(message) | |
print("created") | |
} | |
} | |
// load posts -> I want change liveQuery | |
func loadPosts() { | |
//STEP 1. Find posts related to people who we are following | |
//Follower equal to me means I'm following some users | |
let friendsListQuery = PFQuery(className: "Friend") | |
friendsListQuery.whereKey("follower", equalTo: PFUser.current()!) | |
//STEP 2. Find Friends posts | |
let friendsPostQuery = PFQuery(className: "Post") | |
friendsPostQuery.whereKey("postBy", matchesKey: "following", in: friendsListQuery) | |
friendsPostQuery.whereKey("sell", equalTo: false) | |
//STEP 3. Find My Posts | |
let myPostQuery = PFQuery(className: "Post") | |
myPostQuery.whereKey("postBy", equalTo: PFUser.current()!) | |
myPostQuery.whereKey("sell", equalTo: false) | |
//STEP 4. Find Selling Post | |
let sellPostQuery = PFQuery(className: "Post") | |
sellPostQuery.whereKey("sell", equalTo: true) | |
//STEP 5. Get All of Post | |
let postQuery = PFQuery.orQuery(withSubqueries: [friendsPostQuery, myPostQuery, sellPostQuery]) | |
postQuery.includeKeys(["postBy", "comment", "comment.commentBy"]) | |
postQuery.limit = self.page | |
postQuery.addDescendingOrder("createdAt") | |
postQuery.findObjectsInBackground { (objects:[PFObject]?, error:Error?) in | |
//Object is found | |
if error == nil { | |
//clean up | |
self.usernameArray.removeAll(keepingCapacity: false) | |
self.profileImgArray.removeAll(keepingCapacity: false) | |
self.dateArray.removeAll(keepingCapacity: false) | |
self.postImgArray.removeAll(keepingCapacity: false) | |
self.descriptionArray.removeAll(keepingCapacity: false) | |
self.commentsArray.removeAll(keepingCapacity: false) | |
self.commentsByArray.removeAll(keepingCapacity: false) | |
self.idCommentArray.removeAll(keepingCapacity: false) | |
self.likesCountArray.removeAll(keepingCapacity: false) | |
self.hasLikedArray.removeAll(keepingCapacity: false) | |
self.postID.removeAll(keepingCapacity: false) | |
for object in objects! { | |
//Set Post Object | |
self.postID.append(object) | |
//Pointer to PFUser Class [username, profileImg] | |
if let commentUser = object["postBy"] as? PFUser { | |
self.usernameArray.append(commentUser["username"] as! String) | |
self.profileImgArray.append(commentUser["profileImg"] as! PFFile) | |
} | |
//Retrieve Post Image | |
self.postImgArray.append(object.object(forKey: "postImg") as! PFFile) | |
//Set Description | |
if let description = object.object(forKey: "description") as? String { | |
self.descriptionArray.append(description) | |
} | |
if let lastComment = object["comment"] as? PFObject { | |
self.commentsArray.append(lastComment["comment"] as! String) | |
if let commentBy = lastComment["commentBy"] as? PFObject{ | |
self.commentsByArray.append(commentBy["username"] as! String) | |
} | |
}else { | |
self.commentsArray.append("") | |
self.commentsByArray.append("") | |
} | |
//Get Likes Count | |
let likesCount: Int = object["likeCount"] as! Int | |
self.likesCountArray.append(likesCount) | |
let isLikedQuery = PFQuery(className: "Likes") | |
isLikedQuery.whereKey("likeBy", equalTo: PFUser.current()!) | |
isLikedQuery.whereKey("to", equalTo: object) | |
var isLikedError: NSError? | |
var isLiked: Int = 0 | |
isLiked = isLikedQuery.countObjects(&isLikedError) | |
if isLikedError == nil { | |
print(isLiked) | |
if isLiked == 0 { | |
self.hasLikedArray.append(false) | |
print("I haven't tapped like button \(self.hasLikedArray.count)") | |
}else { | |
self.hasLikedArray.append(true) | |
print("It has like button \(self.hasLikedArray.count)") | |
} | |
}else { | |
print(isLikedError?.localizedDescription as Any) | |
} | |
} | |
//Reload tableView & end spinning of refresher | |
self.tableView.reloadData() | |
self.refresher.endRefreshing() | |
} | |
} | |
} | |
//scrolled down | |
override func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
if scrollView.contentOffset.y >= scrollView.contentSize.height - self.view.frame.size.height * 2 { | |
loadMore() | |
} | |
} | |
func loadMore(){ | |
} | |
//number of cell | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return postImgArray.count | |
} | |
// cell config | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
//STEP 1. Define ShopDetailCell | |
let cell = tableView.dequeueReusableCell(withIdentifier: "ShopDetailCell", for: indexPath) as! ShopDetailCell | |
//STEP 2. Set data from arrays | |
//set PFObject | |
cell.postID = postID[(indexPath as NSIndexPath).row] | |
//set Username | |
cell.userNameLabel.text = usernameArray[(indexPath as NSIndexPath).row] | |
cell.userNameLabel.sizeToFit() | |
//set descriptionLabel | |
cell.descriptionLabel.text = descriptionArray[indexPath.row] | |
// cell.descriptionLabel.attributedText = descriptionTestArray[indexPath.row] | |
cell.descriptionLabel.sizeToFit() | |
cell.delegate = self | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.parallaxHeader.height = CGFloat(indexPath.row * 10) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Parse | |
class Post: PFObject, PFSubclassing { | |
//NSManaged var is Parse-Server "Post" columns | |
@NSManaged var userObj: PFObject? | |
@NSManaged var postDescription: String? | |
@NSManaged var isSell: NSNumber? | |
@NSManaged var commentObj: PFObject? | |
@NSManaged var likesCount: NSNumber? | |
//var userName : userObj | |
//var userImg : userObj | |
//var commentBy : commentObj | |
//var comment : commentObj | |
static func parseClassName() -> String { | |
return "Post" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func index(ofResult result: Post) -> Array<Post>.Index? { | |
return posts.index(where: { | |
$0.objectId == result.objectId | |
}) | |
} | |
func handleEvent(event: Event<Post>) { | |
// Make sure we're on main thread | |
if Thread.current != Thread.main { | |
return DispatchQueue.main.async { [weak self] _ in | |
self?.handleEvent(event: event) | |
} | |
} | |
navigationController?.navigationBar.backgroundColor = .red | |
UIView.animate(withDuration: 1.0, animations: { | |
self.navigationController?.navigationBar.backgroundColor = .clear | |
}) | |
switch event { | |
case .created(let obj), | |
.entered(let obj): | |
posts.insert(obj, at: 0) | |
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) | |
case .updated(let obj): | |
guard let index = index(ofResult: obj) else { break } | |
posts.remove(at: index) | |
posts.insert(obj, at: 0) | |
tableView.moveRow(at: IndexPath(row: index, section: 0), to: IndexPath(row: 0, section: 0)) | |
tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) | |
case .deleted(let obj), | |
.left(let obj): | |
guard let index = index(ofResult: obj) else { break } | |
posts.remove(at: index) | |
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic) | |
} | |
} | |
func fetchObjects() { | |
// query.whereKey(<#T##key: String##String#>, equalTo: <#T##Any#>) | |
postQuery?.findObjectsInBackground().continue(with: BFExecutor.mainThread(), with: { (task) -> Any? in | |
guard let objects = task.result as? [Post] else { | |
return nil | |
} | |
self.posts = objects | |
self.tableView.reloadData() | |
print("Got it!!!") | |
print(objects) | |
return nil | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment