You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

896 lines
35 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // MyStoryViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 1/30/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class MyStoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MyStoryTableViewCellDelegate {
  10. @IBOutlet weak var myStoryTableView: UITableView!
  11. private var refreshControl: UIRefreshControl!
  12. private var editablePostIndex: Int!
  13. var statusUploadMode: Int?
  14. var postStatus: String?
  15. var selectedCell: Int?
  16. var selectedImage: UIImage?
  17. var socialfeedviewmodel: SocialFeedViewModel?
  18. var cellStory: MyStoryTableViewCell?
  19. var showNotificationParameter: Bool!
  20. private var allowFetchRequest: Bool = true
  21. private var arrayIndexPath = [IndexPath] ()
  22. private var newFeedsWillBeAppended: Bool = false
  23. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  24. public static var socialFeedConnectionTimeOutCheck = 0
  25. // /*----------------------------
  26. // -----> 0 = get #N social feeds
  27. // -----> 1 = get all likes
  28. // -----> 2 = like/ dislike feed
  29. // -----> 3 = comment
  30. // -----> 4 =
  31. // ----------------------------*/
  32. // private var feedDataIndex: Int = 0
  33. private var tappedIndex: Int?
  34. @IBOutlet weak var buttonMenuItem0: UIButton!
  35. @IBOutlet weak var buttonMenuItem1: UIButton!
  36. @IBOutlet weak var buttonMenuItem2: UIButton!
  37. @IBOutlet weak var labelMenuItem0: UILabel!
  38. @IBOutlet weak var labelMenuItem1: UILabel!
  39. @IBOutlet weak var labelMenuItem2: UILabel!
  40. private var menuIndexArray: [Int]!
  41. private var collectionViewMenuIconArray: [Int: String] = [0: "send-money", 1: "mobile-recharge", 2: "rate-today", 3: "track-transfer", 4: "transaction-statement", 5: "wallet-transfer", 6: "show-more"]
  42. private var collectionViewMenuNameArray: [Int: String] = [0: "Send Money", 1: "Mobile Recharge", 2: "Today's Rate", 3: "Track Your Transfer", 4: "Transaction Statement", 5: "Wallet to Wallet Transfer", 6: "More"]
  43. @IBAction func showHomeScreen(_ sender: Any) {
  44. showHomeScreen2()
  45. }
  46. override func viewWillAppear(_ animated: Bool) {
  47. self.navigationController?.navigationBar.barTintColor = UIColor(hex:0xec1c24)
  48. setNavBar()
  49. setActionBarIcons()
  50. bindFeedsDataToUI()
  51. setUpRefreshControl()
  52. guard socialfeedviewmodel != nil else {
  53. return
  54. }
  55. socialfeedviewmodel?.socialFeedConnectionTimeOut.value = nil
  56. /**
  57. connection timeout
  58. */
  59. socialfeedviewmodel?.socialFeedConnectionTimeOut.bind { [unowned self] in
  60. guard $0 != nil else {
  61. return
  62. }
  63. self.enableUserInteractions()
  64. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  65. if MyStoryViewController.socialFeedConnectionTimeOutCheck == 0{
  66. MyStoryViewController.socialFeedConnectionTimeOutCheck = MyStoryViewController.socialFeedConnectionTimeOutCheck+1
  67. self.popUpMessage(value: 28)
  68. }
  69. }
  70. setUpNetworkListener()
  71. setUpAnotherLoginListener(genericviewmodel: socialfeedviewmodel!)
  72. socialfeedviewmodel!.resetFeedCount()
  73. socialfeedviewmodel!.setAccessType(accessType: "private")
  74. socialfeedviewmodel!.resetBeforeAfterFeedId()
  75. if let menuArray = UserDefaults.standard.array(forKey: "com.gmeremit.collectionViewMenuIndexArray") as? [Int] {
  76. menuIndexArray = menuArray
  77. } else {
  78. menuIndexArray = [0, 1, 2, 6, 4, 5, 3]
  79. }
  80. setUpMenuIcon()
  81. socialfeedviewmodel!.setAccessType(accessType: "private")
  82. myStoryTableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
  83. if tappedIndex != nil {
  84. self.myStoryTableView.scrollToRow(at: IndexPath(row: tappedIndex!, section: 1), at: UITableViewScrollPosition.bottom, animated: true)
  85. }
  86. }
  87. override func viewDidLoad() {
  88. super.viewDidLoad()
  89. }
  90. func setUpNetworkListener() {
  91. socialfeedviewmodel!.internetConnection.value = nil
  92. socialfeedviewmodel!.internetConnection.bind { [unowned self] in
  93. guard $0 != nil else {
  94. return
  95. }
  96. if self.activityIndicator.isAnimating {
  97. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  98. }
  99. self.enableUserInteractions()
  100. self.popUpMessage(value: 15)
  101. }
  102. }
  103. func setUpRefreshControl() {
  104. refreshControl = UIRefreshControl()
  105. refreshControl.backgroundColor = UIColor(white: 0.98, alpha: 1.0)
  106. refreshControl.tintColor = UIColor.red
  107. refreshControl.addTarget(self, action: #selector(refresh(_:)), for: .valueChanged)
  108. myStoryTableView.addSubview(refreshControl)
  109. }
  110. @objc func refresh(_ sender: AnyObject) {
  111. newFeedsWillBeAppended = false
  112. socialfeedviewmodel!.fetchSocialFeeds(isPublic: false, loadMore: false)
  113. }
  114. func setUpMenuIcon() {
  115. buttonMenuItem0.setImage(UIImage(named: collectionViewMenuIconArray[menuIndexArray[0]]!), for: .normal)
  116. buttonMenuItem0.tag = menuIndexArray[0]
  117. buttonMenuItem1.setImage(UIImage(named: collectionViewMenuIconArray[menuIndexArray[1]]!), for: .normal)
  118. buttonMenuItem1.tag = menuIndexArray[1]
  119. buttonMenuItem2.setImage(UIImage(named: collectionViewMenuIconArray[menuIndexArray[2]]!), for: .normal)
  120. buttonMenuItem2.tag = menuIndexArray[2]
  121. labelMenuItem0.text = collectionViewMenuNameArray[menuIndexArray[0]]
  122. labelMenuItem1.text = collectionViewMenuNameArray[menuIndexArray[1]]
  123. labelMenuItem2.text = collectionViewMenuNameArray[menuIndexArray[2]]
  124. buttonMenuItem0.addTarget(self, action: #selector(performRedirection(_:)), for: .touchUpInside)
  125. buttonMenuItem1.addTarget(self, action: #selector(performRedirection(_:)), for: .touchUpInside)
  126. buttonMenuItem2.addTarget(self, action: #selector(performRedirection(_:)), for: .touchUpInside)
  127. }
  128. @objc func performRedirection(_ sender: UIButton) {
  129. switch sender.tag {
  130. case 0:
  131. if (UserDefaults.standard.object(forKey: "com.gmeremit.isVerified") as! Bool) {
  132. self.performSegue(withIdentifier: "recipientList2", sender: nil)
  133. } else {
  134. self.popUpMessage(value: 13)
  135. }
  136. case 1:
  137. if (UserDefaults.standard.object(forKey: "com.gmeremit.isVerified") as! Bool) {
  138. self.performSegue(withIdentifier: "mobileRecharge2", sender: nil)
  139. } else {
  140. self.popUpMessage(value: 13)
  141. }
  142. case 2:
  143. self.performSegue(withIdentifier: "todayRate2", sender: nil)
  144. case 3:
  145. self.performSegue(withIdentifier: "trackTransfer2", sender: nil)
  146. case 4:
  147. self.performSegue(withIdentifier: "transactionStatement2", sender: nil)
  148. case 5:
  149. if (UserDefaults.standard.object(forKey: "com.gmeremit.isVerified") as! Bool) {
  150. self.performSegue(withIdentifier: "walletTransfer2", sender: nil)
  151. } else {
  152. self.popUpMessage(value: 13)
  153. }
  154. default:
  155. return
  156. }
  157. }
  158. //DATA BINDING
  159. func bindFeedsDataToUI() {
  160. socialfeedviewmodel!.socialFeedsRxValue.bind { [unowned self] in
  161. guard $0 != nil else {
  162. return
  163. }
  164. self.dismissActivityIndicator(activityIndicator: (self.activityIndicator))
  165. guard $0 != 0 else {
  166. self.socialfeedviewmodel!.socialFeedsRxValue.value = nil
  167. return
  168. }
  169. switch $0 {
  170. case socialWallCode.fetchPostsPrivate.rawValue?:
  171. let count = self.socialfeedviewmodel!.getPrivateSocialFeedsCount()
  172. if count > 0 {
  173. if (self.newFeedsWillBeAppended) {
  174. self.arrayIndexPath.removeAll()
  175. self.myStoryTableView.beginUpdates()
  176. let currentCount = self.socialfeedviewmodel!.getCurrentlyFetchedFeedCount()
  177. for i in 0...currentCount-1 {
  178. self.arrayIndexPath.append(IndexPath.init(row: count - currentCount + i, section: 1))
  179. }
  180. self.myStoryTableView.insertRows(at: (self.arrayIndexPath), with: .automatic)
  181. self.myStoryTableView.endUpdates()
  182. self.myStoryTableView.cellForRow(at: IndexPath(row: 0, section: 2))?.isHidden = false
  183. } else {
  184. if (self.refreshControl.isRefreshing) {
  185. self.refreshControl.endRefreshing()
  186. }
  187. self.myStoryTableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
  188. }
  189. self.myStoryTableView.reloadSections(NSIndexSet(index: 2) as IndexSet, with: .none)
  190. }
  191. case socialWallCode.deletePost.rawValue?:
  192. self.socialfeedviewmodel!.removeSocialFeed(atIndex: (self.tappedIndex)!)
  193. self.myStoryTableView.deleteRows(at: [IndexPath(item: (self.tappedIndex)!, section: 1)], with: .automatic)
  194. case socialWallCode.likeUnlikePost.rawValue?:
  195. self.socialfeedviewmodel!.updateSocialFeedArrayLikeNumber(atIndex: (self.tappedIndex)!)
  196. self.myStoryTableView.reloadRows(at: [IndexPath(item: (self.tappedIndex)!, section: 1)], with: UITableViewRowAnimation.automatic)
  197. case socialWallCode.createUpdatePost.rawValue?:
  198. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  199. let alert = UIAlertController(title:"", message:"This post is made public." , preferredStyle: .actionSheet)
  200. self.present(alert, animated: true, completion: nil)
  201. let when = DispatchTime.now() + 2
  202. DispatchQueue.main.asyncAfter(deadline: when){
  203. alert.dismiss(animated: true, completion: nil)
  204. }
  205. default:
  206. return
  207. }
  208. self.socialfeedviewmodel!.socialFeedsRxValue.value = nil
  209. }
  210. }
  211. func setNavBar() {
  212. navigationItem.hidesBackButton = true
  213. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 125, height: 30))
  214. imageView.contentMode = .scaleAspectFit
  215. let image = UIImage(named: "ic_gme_new")
  216. imageView.image = image
  217. navigationItem.titleView = imageView
  218. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_hamburger"), style: .plain, target: self, action: #selector(showSideMenu))
  219. self.navigationItem.leftBarButtonItem?.tintColor = UIColor.white
  220. }
  221. func setActionBarIcons() {
  222. let supportImage = UIImage(named: "ic_support")?.withRenderingMode(.alwaysOriginal)
  223. let supportButton = UIBarButtonItem(image:supportImage, style: .plain, target: self, action: #selector(showSupport))
  224. let notificationImage: UIImage!
  225. if showNotificationParameter {
  226. notificationImage = UIImage(named: "ic_notificationDot")?.withRenderingMode(.alwaysOriginal)
  227. } else {
  228. notificationImage = UIImage(named: "ic_notification")?.withRenderingMode(.alwaysOriginal)
  229. }
  230. let notificationButton = UIBarButtonItem(image: notificationImage, style: .plain, target: self, action: #selector(showNotification))
  231. //self.tabBarController?.navigationItem.rightBarButtonItem.badge
  232. self.navigationItem.rightBarButtonItems=[notificationButton,supportButton]
  233. }
  234. /**
  235. @developer : Suraj Shrestha
  236. @date : 2018/02/12
  237. @des : Show support actions
  238. */
  239. @objc func showSupport() {
  240. let alert = UIAlertController(title:nil, message:"We are there to help you \n (Available time 10:00AM - 6:00PM)", preferredStyle: .actionSheet)
  241. alert.view.subviews.last?.subviews.last?.backgroundColor = UIColor.darkGray
  242. let liveChat = UIAlertAction(title: "Live Chat", style: .default) {
  243. UIAlertAction in
  244. }
  245. let contactNumber = UIAlertAction(title: "1588 6864", style: .default) {
  246. UIAlertAction in
  247. self.startCall(contactNumber: "1588 6864")
  248. }
  249. let supportEmail = UIAlertAction(title: "support@gmeremit.com", style: .default) {
  250. UIAlertAction in
  251. let storyboard = UIStoryboard.init(name: "MessageCompose", bundle: Bundle.main)
  252. if let messageViewController = storyboard.instantiateViewController(withIdentifier: "message") as? MessageComposeViewController {
  253. self.navigationController!.pushViewController(messageViewController, animated: true)
  254. }
  255. }
  256. liveChat.setValue(UIImage(named:"ic_live_chat")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  257. contactNumber.setValue(UIImage(named:"ic_call")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  258. supportEmail.setValue(UIImage(named:"ic_email")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  259. // TODO: have to enable when zendesk is provided
  260. // alert.addAction(liveChat)
  261. alert.addAction(contactNumber)
  262. alert.addAction(supportEmail)
  263. alert.view.tintColor = UIColor.red
  264. alert.view.subviews.last?.subviews.last?.layer.cornerRadius = 10
  265. alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
  266. present(alert, animated: true, completion: nil)
  267. }
  268. @objc func showNotification(){
  269. performSegue(withIdentifier: "notification", sender: nil)
  270. }
  271. @objc func showSideMenu() {
  272. performSegue(withIdentifier: "showSideMenu", sender: nil)
  273. }
  274. func startCall(contactNumber: String){
  275. if let url = URL(string: "tel://\(contactNumber)"), UIApplication.shared.canOpenURL(url) {
  276. if #available(iOS 10, *) {
  277. UIApplication.shared.open(url)
  278. } else {
  279. UIApplication.shared.openURL(url)
  280. }
  281. }
  282. }
  283. @IBAction func buttonUploadStatus(_ sender: Any) {
  284. statusUploadMode = 0
  285. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  286. }
  287. @IBAction func buttonUploadSocialImage(_ sender: Any) {
  288. statusUploadMode = 1
  289. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  290. }
  291. @IBAction func buttomGmeFamilyTap(_ sender: Any) {
  292. showHomeScreen()
  293. }
  294. // MARK: - TableView
  295. func numberOfSections(in tableView: UITableView) -> Int {
  296. return 3
  297. }
  298. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  299. switch section {
  300. case 0:
  301. return 1
  302. case 1:
  303. if allowFetchRequest && self.socialfeedviewmodel!.getPrivateSocialFeedsCount() < 10 {
  304. showActivityIndicator(activityIndicator: activityIndicator)
  305. socialfeedviewmodel!.fetchSocialFeeds(isPublic: false, loadMore: false)
  306. allowFetchRequest = false
  307. return 0
  308. }
  309. return self.socialfeedviewmodel!.getPrivateSocialFeedsCount()
  310. case 2:
  311. return 1
  312. default:
  313. return 0
  314. }
  315. }
  316. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  317. switch indexPath.section {
  318. case 0:
  319. let cellStatus = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! MyStoryTableViewCell
  320. cellStatus.labelInitial.isHidden = false
  321. cellStatus.labelInitial.backgroundColor = UIColor(hex: 0x2e3192)
  322. cellStatus.labelInitial.layer.cornerRadius = cellStatus.labelInitial.frame.height / 2
  323. cellStatus.labelInitial.text = getFirstName().prefix(1).uppercased()
  324. let userDpString = UserDefaults.standard.object(forKey: "com.gmeremit.dpUrl") as? String
  325. if let userDpUrl = URL(string: userDpString!) {
  326. cellStatus.imageViewUserStatus.isHidden = false
  327. cellStatus.imageViewUserStatus.sd_setImage(with: userDpUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  328. cellStatus.imageViewUserStatus.layer.cornerRadius = cellStatus.imageViewUserStatus.frame.height/2
  329. cellStatus.labelInitial.isHidden = true
  330. }
  331. return cellStatus
  332. case 1:
  333. socialfeedviewmodel!.setAccessType(accessType: "private")
  334. let socialFeed = self.socialfeedviewmodel!.getSocialFeedAt(index:indexPath.row)
  335. if socialFeed.blocked! {
  336. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 0)
  337. } else {
  338. if socialFeed.feedText != nil && socialFeed.feedText != "" {
  339. if socialFeed.feedImageId != nil {
  340. //CASE 3: text and image
  341. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 3)
  342. } else {
  343. //CASE 1: only text
  344. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 1)
  345. }
  346. } else {
  347. //CASE 2: only image
  348. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 2)
  349. }
  350. }
  351. case 2:
  352. let cell = tableView.dequeueReusableCell(withIdentifier: "loadMoreCell", for: indexPath) as! MyStoryTableViewCell
  353. if socialfeedviewmodel!.loadMore() {
  354. cell.buttonLoadMore.addTarget(self, action: #selector(loadMore(_:)), for: .touchUpInside)
  355. cell.buttonLoadMore.isHidden = false
  356. } else {
  357. cell.buttonLoadMore.isHidden = true
  358. }
  359. cell.buttonLoadMore.addTarget(self, action: #selector(loadMore(_:)), for: .touchUpInside)
  360. return cell
  361. default:
  362. return cellStory!
  363. }
  364. }
  365. @objc func loadMore(_ sender: TableViewCell) {
  366. showActivityIndicator(activityIndicator: activityIndicator)
  367. newFeedsWillBeAppended = true
  368. myStoryTableView.cellForRow(at: IndexPath(row: 0, section: 2))?.isHidden = true
  369. socialfeedviewmodel!.fetchSocialFeeds(isPublic: false, loadMore: true)
  370. }
  371. //DISPLAY FEEDS ON THE GME WALL
  372. func getTableViewCell(indexPath: IndexPath, socialFeed : SocialFeed, mode: Int) -> UITableViewCell {
  373. let cell: MyStoryTableViewCell!
  374. switch mode {
  375. case 0:
  376. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellText", for: indexPath) as! MyStoryTableViewCell
  377. cell.labelStatus.isHidden = true
  378. cell.labelBlocked.alpha = 1
  379. cell.imageViewBlocked.alpha = 1
  380. cell.viewSocialAction.isHidden = true
  381. return cell
  382. case 1:
  383. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellText", for: indexPath) as! MyStoryTableViewCell
  384. cell.labelStatus.isHidden = false
  385. cell.labelBlocked.alpha = 0
  386. cell.imageViewBlocked.alpha = 0
  387. cell.viewSocialAction.isHidden = false
  388. cell.labelStatus.text = socialFeed.feedText
  389. cell.labelStatus.textColor = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
  390. case 2:
  391. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellImage", for: indexPath) as! MyStoryTableViewCell
  392. cell.imageViewSocial.sd_setImage(with: URL(string: socialFeed.feedImage!)!, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  393. cell.postImageHeight.constant = view.frame.width
  394. case 3:
  395. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellTextImage", for: indexPath) as! MyStoryTableViewCell
  396. cell.labelStatus.text = socialFeed.feedText
  397. cell.labelStatus.textColor = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
  398. cell.imageViewSocial.sd_setImage(with: URL(string: socialFeed.feedImage!)!, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  399. cell.postImageHeight.constant = view.frame.width
  400. default:
  401. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "socialCellTextImage", for: indexPath) as! MyStoryTableViewCell
  402. }
  403. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  404. cell.layer.borderWidth = 2
  405. cell.imageViewUserImage.layer.cornerRadius = cell.imageViewUserImage.frame.height / 2
  406. cell.editPostButton?.addTarget(self, action:#selector(showPostOptions(_:)), for:.touchUpInside)
  407. cell.heartButton?.addTarget(self, action:#selector(toggleLike(_:)), for:.touchUpInside)
  408. cell.likeButton?.addTarget(self, action:#selector(showLikes(_:)), for:.touchUpInside)
  409. if socialFeed.accessType == "public" {
  410. cell.commentButton?.addTarget(self, action: #selector(showComments(_:)), for: .touchUpInside)
  411. }
  412. //Display USERNAME who has posted the feed
  413. cell.labelSocialUsername.text = socialfeedviewmodel!.getFullName(feed: socialFeed)
  414. cell.labelUserNameInitial.isHidden = false
  415. cell.labelUserNameInitial.backgroundColor = UIColor(hex: 0x2e3192)
  416. cell.labelUserNameInitial.layer.cornerRadius = cell.labelUserNameInitial.frame.height / 2
  417. cell.labelUserNameInitial.text = socialFeed.firstName.prefix(1).uppercased()
  418. if let userDpString = socialFeed.userDpUrl {
  419. if let userDpUrl = URL(string: userDpString) {
  420. cell.imageViewUserImage.isHidden = false
  421. cell.imageViewUserImage.sd_setImage(with: userDpUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  422. }
  423. cell.labelUserNameInitial.isHidden = true
  424. }
  425. //Display timeStamp (ago date)
  426. cell.labelTimeStamp.text = socialFeed.agoDate
  427. //Display LIKE status
  428. if socialFeed.liked {
  429. cell.heartButton?.setImage(UIImage(named:"ic_heartfull"), for: UIControlState.normal)
  430. } else {
  431. cell.heartButton?.setImage(UIImage(named:"ic_heart"), for: UIControlState.normal)
  432. }
  433. //Display total number of LIKES
  434. if socialFeed.totalLike > 1 {
  435. cell.likeButton.setTitle("\(socialFeed.totalLike) Likes", for:UIControlState.normal)
  436. } else {
  437. cell.likeButton.setTitle("\(socialFeed.totalLike) Like", for:UIControlState.normal)
  438. }
  439. //Display total number of COMMENTS
  440. if socialFeed.totalComment > 1 {
  441. cell.commentButton.setTitle(" \(socialFeed.totalComment) Comments", for: UIControlState.normal)
  442. } else {
  443. cell.commentButton.setTitle(" \(socialFeed.totalComment) Comment", for: UIControlState.normal)
  444. }
  445. return cell
  446. }
  447. func populateTableViewCell(index:Int, privateFeed: SocialFeed){
  448. cellStory?.imageViewUserImage.image = UIImage(named:"profile_img")
  449. cellStory?.imageViewUserImage.layer.cornerRadius = (cellStory?.imageViewUserImage.frame.height)! / 2
  450. cellStory?.editPostButton?.addTarget(self, action:#selector(showPostOptions(_:)), for:.touchUpInside)
  451. cellStory?.heartButton?.addTarget(self, action:#selector(toggleLike(_:)), for:.touchUpInside)
  452. cellStory?.commentButton?.addTarget(self, action: #selector(showComments(_:)), for: .touchUpInside)
  453. cellStory?.labelStatus.text = privateFeed.feedText
  454. //Display User Name
  455. cellStory?.labelSocialUsername.text = socialfeedviewmodel?.getFullName(feed: privateFeed)
  456. //Display User Image
  457. if let userDpString = privateFeed.userDpUrl {
  458. if let userDpUrl = URL(string: userDpString) {
  459. cellStory?.imageViewUserImage.sd_setImage(with: userDpUrl, placeholderImage: #imageLiteral(resourceName: "gme-logo"), options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  460. }
  461. }
  462. //Display TIMES STAMP
  463. cellStory?.labelTimeStamp.text = privateFeed.agoDate
  464. if privateFeed.blocked! {
  465. cellStory?.imageViewBlocked.isHidden = false
  466. cellStory?.labelBlocked.isHidden = false
  467. cellStory?.labelStatus.isHidden = true
  468. cellStory?.imageViewSocial.isHidden = true
  469. cellStory?.viewSocialAction.isHidden = true
  470. cellStory?.constraintHeightViewSocial.constant = 0
  471. } else {
  472. cellStory?.imageViewBlocked.isHidden = true
  473. cellStory?.labelBlocked.isHidden = true
  474. //Display feed
  475. cellStory?.labelStatus.text = privateFeed.feedText
  476. //change like button color
  477. if privateFeed.liked {
  478. cellStory?.heartButton?.setImage(UIImage(named:"ic_heartfull"), for: UIControlState.normal)
  479. }
  480. else {
  481. cellStory?.heartButton?.setImage(UIImage(named:"ic_heart"), for: UIControlState.normal)
  482. }
  483. //Display total number of likes
  484. if privateFeed.totalLike > 1 {
  485. cellStory?.likeButton.setTitle("\(privateFeed.totalLike) Likes", for:UIControlState.normal)
  486. }
  487. else {
  488. cellStory?.likeButton.setTitle("\(privateFeed.totalLike) Like", for:UIControlState.normal)
  489. }
  490. //Display total number of comments
  491. if privateFeed.totalComment > 1{
  492. cellStory?.commentButton.setTitle(" \(privateFeed.totalComment) Comments", for: UIControlState.normal)
  493. }
  494. else{
  495. cellStory?.commentButton.setTitle(" \(privateFeed.totalComment) Comment", for: UIControlState.normal)
  496. }
  497. //Display feed Image
  498. if privateFeed.feedImage != nil {
  499. let feedImageUrl = URL(string: (privateFeed.feedImage!))
  500. // cellStory?.postImageHeight.constant = view.frame.width
  501. cellStory?.imageViewSocial.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.width)
  502. cellStory?.imageViewSocial.sd_setImage(with: feedImageUrl, placeholderImage: #imageLiteral(resourceName: "gme-logo"), options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  503. } else {
  504. // cellStory?.postImageHeight.constant = 0
  505. cellStory?.imageViewSocial.image = nil
  506. }
  507. }
  508. }
  509. @objc func toggleLike(_ sender: MyStoryTableViewCell) {
  510. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  511. return
  512. }
  513. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  514. tappedIndex = clickedCellIndexPath.row
  515. let tappedFeed = socialfeedviewmodel!.getSocialFeedAt(index: tappedIndex!)
  516. socialfeedviewmodel!.likeDislikePost(feedId: tappedFeed.id, liked: tappedFeed.liked)
  517. }
  518. }
  519. @objc func showComments(_ sender: MyStoryTableViewCell) {
  520. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  521. return
  522. }
  523. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  524. tappedIndex = clickedCellIndexPath.row
  525. guard Reachability.isConnectedToNetwork() else {
  526. self.popUpMessage(value: 15)
  527. return
  528. }
  529. self.performSegue(withIdentifier: "showCommentsBy2", sender: nil)
  530. }
  531. }
  532. @objc func showLikes(_ sender: MyStoryTableViewCell) {
  533. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  534. return
  535. }
  536. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  537. tappedIndex = clickedCellIndexPath.row
  538. let tappedFeed = socialfeedviewmodel!.getSocialFeedAt(index: clickedCellIndexPath.row)
  539. if tappedFeed.totalLike > 0 {
  540. guard Reachability.isConnectedToNetwork() else {
  541. self.popUpMessage(value: 15)
  542. return
  543. }
  544. //socialfeedviewmodel.fetchAllLikesFor(feedId: tappedFeed.id)
  545. self.performSegue(withIdentifier: "showLikedBy2", sender: nil)
  546. }
  547. }
  548. }
  549. @objc func showPostOptions(_ sender: MyStoryTableViewCell) {
  550. guard let clickedCell = sender.superview?.superview?.superview as? MyStoryTableViewCell else {
  551. return
  552. }
  553. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  554. enumeratePostOptions(index: clickedCellIndexPath.row)
  555. }
  556. }
  557. func enumeratePostOptions(index: Int) {
  558. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  559. let switchCategoryTitle: String!
  560. if (socialfeedviewmodel?.isPostPrivate(postIndex: index))! {
  561. switchCategoryTitle = "Show this post to GME Community"
  562. } else {
  563. switchCategoryTitle = "Hide this post from GME Community"
  564. }
  565. let show = UIAlertAction(title: switchCategoryTitle, style: .default) {
  566. UIAlertAction in
  567. let feedToEdit = self.socialfeedviewmodel!.getSocialFeedAt(index: index)
  568. if let feedText = feedToEdit.feedText {
  569. self.socialfeedviewmodel!.updateFeedText(text: feedText)
  570. }
  571. if let feedImageId = feedToEdit.feedImageId {
  572. self.socialfeedviewmodel!.setFeedImageId(imageId: feedImageId)
  573. }
  574. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  575. self.socialfeedviewmodel!.updateSocialFeed(feed: feedToEdit, index: index, toggle: true)
  576. }
  577. let edit = UIAlertAction(title: "Edit this post", style: .default) {
  578. UIAlertAction in
  579. self.statusUploadMode = 2
  580. self.editablePostIndex = index
  581. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  582. }
  583. let delete = UIAlertAction(title: "Delete this post", style: .default) {
  584. UIAlertAction in
  585. self.deleteConfirmation(postIndex: index)
  586. }
  587. let cancel = UIAlertAction(title: "Cancel", style: .destructive) {
  588. UIAlertAction in
  589. }
  590. alert.addAction(show)
  591. alert.addAction(edit)
  592. alert.addAction(delete)
  593. alert.addAction(cancel)
  594. present(alert, animated: true, completion: nil)
  595. }
  596. func deleteConfirmation(postIndex: Int) {
  597. let alert = UIAlertController(title:nil, message:"Are you sure you want to delete?", preferredStyle: .actionSheet)
  598. let cancel = UIAlertAction(title: "No", style: .destructive) {
  599. UIAlertAction in
  600. }
  601. let delete = UIAlertAction(title: "Yes", style: .destructive) {
  602. UIAlertAction in
  603. self.tappedIndex = postIndex
  604. // self.feedDataIndex = 4
  605. self.socialfeedviewmodel!.deleteSocialFeed(feedId: self.socialfeedviewmodel!.getSocialFeedAt(index: postIndex).id)
  606. }
  607. alert.addAction(cancel)
  608. alert.addAction(delete)
  609. present(alert, animated: true, completion: nil)
  610. }
  611. override func didReceiveMemoryWarning() {
  612. super.didReceiveMemoryWarning()
  613. // Dispose of any resources that can be recreated.
  614. }
  615. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  616. switch segue.identifier {
  617. case "statusUpdate2"?:
  618. let statusUploadViewController = segue.destination as! StatusUploadViewController
  619. statusUploadViewController.statusUploadMode = statusUploadMode
  620. statusUploadViewController.editablePostIndex = editablePostIndex
  621. statusUploadViewController.socialfeedviewmodel = socialfeedviewmodel
  622. statusUploadViewController.showNotificationParameter = showNotificationParameter!
  623. case "statusUpdate"?:
  624. let vc = segue.destination as! StatusUploadViewController
  625. if statusUploadMode == 0 {
  626. vc.statusUploadMode = statusUploadMode
  627. } else if statusUploadMode == 1 {
  628. vc.statusUploadMode = statusUploadMode
  629. } else if statusUploadMode == 2 {
  630. vc.statusUploadMode = statusUploadMode
  631. }
  632. vc.showNotificationParameter = showNotificationParameter!
  633. case "showLikedBy2"?:
  634. let likesViewController = segue.destination as! LikesViewController
  635. likesViewController.socialfeedviewmodel = self.socialfeedviewmodel
  636. likesViewController.tappedIndex = self.tappedIndex
  637. case "showCommentsBy2"?:
  638. let commentsViewController = segue.destination as! CommentsViewController
  639. commentsViewController.socialfeedviewmodel = socialfeedviewmodel
  640. commentsViewController.tappedIndex = tappedIndex
  641. case "showMoreOptions2"?:
  642. let homeViewController = segue.destination as! _HomeViewController
  643. // homeViewController.numberCellsInCollectionView = 6
  644. // homeViewController.showAllCollectionViewCells = true
  645. default:
  646. return
  647. }
  648. }
  649. }