Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. class func showFeed(completion: @escaping ([Feed]) -> Swift.Void){
  2. let token = UserDefaults.standard.string(forKey: "token")
  3. let params: [String: String] = ["user_token": token!]
  4. var feeds = [Feed]()
  5. Alamofire.request(Api.GetFeeds, method: .post, parameters: params).responseJSON { (response) in
  6. switch response.result{
  7. case .success(let data):
  8. let json = JSON(data)
  9. for index in 0...json.count-1 {
  10. let aObject = json[index]
  11. var type = FeedType.text
  12.  
  13. if(aObject["product_preview"]).exists(){
  14. type = .product
  15. }
  16. var mood = ""
  17. if aObject["mood"].exists(){
  18. mood = aObject["mood"].string!
  19. }
  20.  
  21. var images=[String]()
  22. if aObject["file:wallphoto"].exists(){
  23. type = .picture
  24. }
  25. let guid = aObject["guid"].string!
  26. let owerId = aObject["owner_guid"].string!
  27. let timestamp = aObject["time_created"].string!
  28. let description = aObject["description"].string!
  29. let desJson = JSON(parseJSON: description)
  30.  
  31. let text = desJson["post"].string!
  32. var friendTagId = ""
  33. if desJson["friend"].exists(){
  34. friendTagId = desJson["friend"].string!
  35. }
  36. var location = ""
  37. if desJson["location"].exists(){
  38. location = desJson["location"].string!
  39. }
  40.  
  41. let likes = aObject["likes"].string!
  42. let comments = aObject["comments"].string!
  43. print("[Feed] status", text)
  44. var owner:User?
  45. var friendTag:User?
  46.  
  47. User.info(forUserId: owerId, completion: { (user) in
  48. owner = user
  49. print("[Feed] info: ", owerId)
  50. User.info(forUserId: friendTagId, completion: { (userTag) in
  51. friendTag = userTag
  52. print("[Feed] info tag: ", friendTagId)
  53. let feed = Feed(id: Int(guid)!, owner: owner!, type: type, timestamp: Int(timestamp)!, text: text, tagFriend: friendTag!, location: location, mood: mood, images: images, likes: likes, comments: comments)
  54. feeds.append(feed)
  55. print("[Feed] count: ",feeds.count)
  56. })
  57.  
  58. })
  59.  
  60. }
  61.  
  62. completion(feeds)
  63.  
  64. case .failure(let error):
  65. print("Error: ", error)
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement