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
  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: "Home", bundle: Bundle.main)
  252. if let messageViewController = storyboard.instantiateViewController(withIdentifier: "message") as? MessageComposeViewController {
  253. messageViewController.supportIndex = 0
  254. self.navigationController!.pushViewController(messageViewController, animated: true)
  255. }
  256. }
  257. liveChat.setValue(UIImage(named:"ic_live_chat")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  258. contactNumber.setValue(UIImage(named:"ic_call")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  259. supportEmail.setValue(UIImage(named:"ic_email")?.withRenderingMode(.alwaysOriginal),forKey:"image")
  260. // TODO: have to enable when zendesk is provided
  261. // alert.addAction(liveChat)
  262. alert.addAction(contactNumber)
  263. alert.addAction(supportEmail)
  264. alert.view.tintColor = UIColor.red
  265. alert.view.subviews.last?.subviews.last?.layer.cornerRadius = 10
  266. alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
  267. present(alert, animated: true, completion: nil)
  268. }
  269. @objc func showNotification(){
  270. performSegue(withIdentifier: "notification", sender: nil)
  271. }
  272. @objc func showSideMenu() {
  273. performSegue(withIdentifier: "showSideMenu", sender: nil)
  274. }
  275. func startCall(contactNumber: String){
  276. if let url = URL(string: "tel://\(contactNumber)"), UIApplication.shared.canOpenURL(url) {
  277. if #available(iOS 10, *) {
  278. UIApplication.shared.open(url)
  279. } else {
  280. UIApplication.shared.openURL(url)
  281. }
  282. }
  283. }
  284. @IBAction func buttonUploadStatus(_ sender: Any) {
  285. statusUploadMode = 0
  286. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  287. }
  288. @IBAction func buttonUploadSocialImage(_ sender: Any) {
  289. statusUploadMode = 1
  290. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  291. }
  292. @IBAction func buttomGmeFamilyTap(_ sender: Any) {
  293. showHomeScreen()
  294. }
  295. // MARK: - TableView
  296. func numberOfSections(in tableView: UITableView) -> Int {
  297. return 3
  298. }
  299. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  300. switch section {
  301. case 0:
  302. return 1
  303. case 1:
  304. if allowFetchRequest && self.socialfeedviewmodel!.getPrivateSocialFeedsCount() < 10 {
  305. showActivityIndicator(activityIndicator: activityIndicator)
  306. socialfeedviewmodel!.fetchSocialFeeds(isPublic: false, loadMore: false)
  307. allowFetchRequest = false
  308. return 0
  309. }
  310. return self.socialfeedviewmodel!.getPrivateSocialFeedsCount()
  311. case 2:
  312. return 1
  313. default:
  314. return 0
  315. }
  316. }
  317. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  318. switch indexPath.section {
  319. case 0:
  320. let cellStatus = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! MyStoryTableViewCell
  321. cellStatus.labelInitial.isHidden = false
  322. cellStatus.labelInitial.backgroundColor = UIColor(hex: 0x2e3192)
  323. cellStatus.labelInitial.layer.cornerRadius = cellStatus.labelInitial.frame.height / 2
  324. cellStatus.labelInitial.text = getFirstName().prefix(1).uppercased()
  325. let userDpString = UserDefaults.standard.object(forKey: "com.gmeremit.dpUrl") as? String
  326. if let userDpUrl = URL(string: userDpString!) {
  327. cellStatus.imageViewUserStatus.isHidden = false
  328. cellStatus.imageViewUserStatus.sd_setImage(with: userDpUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  329. cellStatus.imageViewUserStatus.layer.cornerRadius = cellStatus.imageViewUserStatus.frame.height/2
  330. cellStatus.labelInitial.isHidden = true
  331. }
  332. return cellStatus
  333. case 1:
  334. socialfeedviewmodel!.setAccessType(accessType: "private")
  335. let socialFeed = self.socialfeedviewmodel!.getSocialFeedAt(index:indexPath.row)
  336. if socialFeed.blocked! {
  337. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 0)
  338. } else {
  339. if socialFeed.feedText != nil && socialFeed.feedText != "" {
  340. if socialFeed.feedImageId != nil {
  341. //CASE 3: text and image
  342. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 3)
  343. } else {
  344. //CASE 1: only text
  345. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 1)
  346. }
  347. } else {
  348. //CASE 2: only image
  349. return self.getTableViewCell(indexPath: indexPath, socialFeed: socialFeed, mode: 2)
  350. }
  351. }
  352. case 2:
  353. let cell = tableView.dequeueReusableCell(withIdentifier: "loadMoreCell", for: indexPath) as! MyStoryTableViewCell
  354. if socialfeedviewmodel!.loadMore() {
  355. cell.buttonLoadMore.addTarget(self, action: #selector(loadMore(_:)), for: .touchUpInside)
  356. cell.buttonLoadMore.isHidden = false
  357. } else {
  358. cell.buttonLoadMore.isHidden = true
  359. }
  360. cell.buttonLoadMore.addTarget(self, action: #selector(loadMore(_:)), for: .touchUpInside)
  361. return cell
  362. default:
  363. return cellStory!
  364. }
  365. }
  366. @objc func loadMore(_ sender: TableViewCell) {
  367. showActivityIndicator(activityIndicator: activityIndicator)
  368. newFeedsWillBeAppended = true
  369. myStoryTableView.cellForRow(at: IndexPath(row: 0, section: 2))?.isHidden = true
  370. socialfeedviewmodel!.fetchSocialFeeds(isPublic: false, loadMore: true)
  371. }
  372. //DISPLAY FEEDS ON THE GME WALL
  373. func getTableViewCell(indexPath: IndexPath, socialFeed : SocialFeed, mode: Int) -> UITableViewCell {
  374. let cell: MyStoryTableViewCell!
  375. switch mode {
  376. case 0:
  377. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellText", for: indexPath) as! MyStoryTableViewCell
  378. cell.labelStatus.isHidden = true
  379. cell.labelBlocked.alpha = 1
  380. cell.imageViewBlocked.alpha = 1
  381. cell.viewSocialAction.isHidden = true
  382. return cell
  383. case 1:
  384. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellText", for: indexPath) as! MyStoryTableViewCell
  385. cell.labelStatus.isHidden = false
  386. cell.labelBlocked.alpha = 0
  387. cell.imageViewBlocked.alpha = 0
  388. cell.viewSocialAction.isHidden = false
  389. cell.labelStatus.text = socialFeed.feedText
  390. cell.labelStatus.textColor = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
  391. case 2:
  392. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellImage", for: indexPath) as! MyStoryTableViewCell
  393. cell.imageViewSocial.sd_setImage(with: URL(string: socialFeed.feedImage!)!, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  394. cell.postImageHeight.constant = view.frame.width
  395. case 3:
  396. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "myStoryCellTextImage", for: indexPath) as! MyStoryTableViewCell
  397. cell.labelStatus.text = socialFeed.feedText
  398. cell.labelStatus.textColor = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
  399. cell.imageViewSocial.sd_setImage(with: URL(string: socialFeed.feedImage!)!, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  400. cell.postImageHeight.constant = view.frame.width
  401. default:
  402. cell = myStoryTableView.dequeueReusableCell(withIdentifier: "socialCellTextImage", for: indexPath) as! MyStoryTableViewCell
  403. }
  404. cell.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor
  405. cell.layer.borderWidth = 2
  406. cell.imageViewUserImage.layer.cornerRadius = cell.imageViewUserImage.frame.height / 2
  407. cell.editPostButton?.addTarget(self, action:#selector(showPostOptions(_:)), for:.touchUpInside)
  408. cell.heartButton?.addTarget(self, action:#selector(toggleLike(_:)), for:.touchUpInside)
  409. cell.likeButton?.addTarget(self, action:#selector(showLikes(_:)), for:.touchUpInside)
  410. if socialFeed.accessType == "public" {
  411. cell.commentButton?.addTarget(self, action: #selector(showComments(_:)), for: .touchUpInside)
  412. }
  413. //Display USERNAME who has posted the feed
  414. cell.labelSocialUsername.text = socialfeedviewmodel!.getFullName(feed: socialFeed)
  415. cell.labelUserNameInitial.isHidden = false
  416. cell.labelUserNameInitial.backgroundColor = UIColor(hex: 0x2e3192)
  417. cell.labelUserNameInitial.layer.cornerRadius = cell.labelUserNameInitial.frame.height / 2
  418. cell.labelUserNameInitial.text = socialFeed.firstName.prefix(1).uppercased()
  419. if let userDpString = socialFeed.userDpUrl {
  420. if let userDpUrl = URL(string: userDpString) {
  421. cell.imageViewUserImage.isHidden = false
  422. cell.imageViewUserImage.sd_setImage(with: userDpUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  423. }
  424. cell.labelUserNameInitial.isHidden = true
  425. }
  426. //Display timeStamp (ago date)
  427. cell.labelTimeStamp.text = socialFeed.agoDate
  428. //Display LIKE status
  429. if socialFeed.liked {
  430. cell.heartButton?.setImage(UIImage(named:"ic_heartfull"), for: UIControlState.normal)
  431. } else {
  432. cell.heartButton?.setImage(UIImage(named:"ic_heart"), for: UIControlState.normal)
  433. }
  434. //Display total number of LIKES
  435. if socialFeed.totalLike > 1 {
  436. cell.likeButton.setTitle("\(socialFeed.totalLike) Likes", for:UIControlState.normal)
  437. } else {
  438. cell.likeButton.setTitle("\(socialFeed.totalLike) Like", for:UIControlState.normal)
  439. }
  440. //Display total number of COMMENTS
  441. if socialFeed.totalComment > 1 {
  442. cell.commentButton.setTitle(" \(socialFeed.totalComment) Comments", for: UIControlState.normal)
  443. } else {
  444. cell.commentButton.setTitle(" \(socialFeed.totalComment) Comment", for: UIControlState.normal)
  445. }
  446. return cell
  447. }
  448. func populateTableViewCell(index:Int, privateFeed: SocialFeed){
  449. cellStory?.imageViewUserImage.image = UIImage(named:"profile_img")
  450. cellStory?.imageViewUserImage.layer.cornerRadius = (cellStory?.imageViewUserImage.frame.height)! / 2
  451. cellStory?.editPostButton?.addTarget(self, action:#selector(showPostOptions(_:)), for:.touchUpInside)
  452. cellStory?.heartButton?.addTarget(self, action:#selector(toggleLike(_:)), for:.touchUpInside)
  453. cellStory?.commentButton?.addTarget(self, action: #selector(showComments(_:)), for: .touchUpInside)
  454. cellStory?.labelStatus.text = privateFeed.feedText
  455. //Display User Name
  456. cellStory?.labelSocialUsername.text = socialfeedviewmodel?.getFullName(feed: privateFeed)
  457. //Display User Image
  458. if let userDpString = privateFeed.userDpUrl {
  459. if let userDpUrl = URL(string: userDpString) {
  460. cellStory?.imageViewUserImage.sd_setImage(with: userDpUrl, placeholderImage: #imageLiteral(resourceName: "gme-logo"), options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  461. }
  462. }
  463. //Display TIMES STAMP
  464. cellStory?.labelTimeStamp.text = privateFeed.agoDate
  465. if privateFeed.blocked! {
  466. cellStory?.imageViewBlocked.isHidden = false
  467. cellStory?.labelBlocked.isHidden = false
  468. cellStory?.labelStatus.isHidden = true
  469. cellStory?.imageViewSocial.isHidden = true
  470. cellStory?.viewSocialAction.isHidden = true
  471. cellStory?.constraintHeightViewSocial.constant = 0
  472. } else {
  473. cellStory?.imageViewBlocked.isHidden = true
  474. cellStory?.labelBlocked.isHidden = true
  475. //Display feed
  476. cellStory?.labelStatus.text = privateFeed.feedText
  477. //change like button color
  478. if privateFeed.liked {
  479. cellStory?.heartButton?.setImage(UIImage(named:"ic_heartfull"), for: UIControlState.normal)
  480. }
  481. else {
  482. cellStory?.heartButton?.setImage(UIImage(named:"ic_heart"), for: UIControlState.normal)
  483. }
  484. //Display total number of likes
  485. if privateFeed.totalLike > 1 {
  486. cellStory?.likeButton.setTitle("\(privateFeed.totalLike) Likes", for:UIControlState.normal)
  487. }
  488. else {
  489. cellStory?.likeButton.setTitle("\(privateFeed.totalLike) Like", for:UIControlState.normal)
  490. }
  491. //Display total number of comments
  492. if privateFeed.totalComment > 1{
  493. cellStory?.commentButton.setTitle(" \(privateFeed.totalComment) Comments", for: UIControlState.normal)
  494. }
  495. else{
  496. cellStory?.commentButton.setTitle(" \(privateFeed.totalComment) Comment", for: UIControlState.normal)
  497. }
  498. //Display feed Image
  499. if privateFeed.feedImage != nil {
  500. let feedImageUrl = URL(string: (privateFeed.feedImage!))
  501. // cellStory?.postImageHeight.constant = view.frame.width
  502. cellStory?.imageViewSocial.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.width)
  503. cellStory?.imageViewSocial.sd_setImage(with: feedImageUrl, placeholderImage: #imageLiteral(resourceName: "gme-logo"), options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  504. } else {
  505. // cellStory?.postImageHeight.constant = 0
  506. cellStory?.imageViewSocial.image = nil
  507. }
  508. }
  509. }
  510. @objc func toggleLike(_ sender: MyStoryTableViewCell) {
  511. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  512. return
  513. }
  514. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  515. tappedIndex = clickedCellIndexPath.row
  516. let tappedFeed = socialfeedviewmodel!.getSocialFeedAt(index: tappedIndex!)
  517. socialfeedviewmodel!.likeDislikePost(feedId: tappedFeed.id, liked: tappedFeed.liked)
  518. }
  519. }
  520. @objc func showComments(_ sender: MyStoryTableViewCell) {
  521. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  522. return
  523. }
  524. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  525. tappedIndex = clickedCellIndexPath.row
  526. guard Reachability.isConnectedToNetwork() else {
  527. self.popUpMessage(value: 15)
  528. return
  529. }
  530. self.performSegue(withIdentifier: "showCommentsBy2", sender: nil)
  531. }
  532. }
  533. @objc func showLikes(_ sender: MyStoryTableViewCell) {
  534. guard let clickedCell = sender.superview?.superview?.superview?.superview as? MyStoryTableViewCell else {
  535. return
  536. }
  537. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  538. tappedIndex = clickedCellIndexPath.row
  539. let tappedFeed = socialfeedviewmodel!.getSocialFeedAt(index: clickedCellIndexPath.row)
  540. if tappedFeed.totalLike > 0 {
  541. guard Reachability.isConnectedToNetwork() else {
  542. self.popUpMessage(value: 15)
  543. return
  544. }
  545. //socialfeedviewmodel.fetchAllLikesFor(feedId: tappedFeed.id)
  546. self.performSegue(withIdentifier: "showLikedBy2", sender: nil)
  547. }
  548. }
  549. }
  550. @objc func showPostOptions(_ sender: MyStoryTableViewCell) {
  551. guard let clickedCell = sender.superview?.superview?.superview as? MyStoryTableViewCell else {
  552. return
  553. }
  554. if let clickedCellIndexPath = myStoryTableView.indexPath(for: clickedCell) {
  555. enumeratePostOptions(index: clickedCellIndexPath.row)
  556. }
  557. }
  558. func enumeratePostOptions(index: Int) {
  559. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  560. let switchCategoryTitle: String!
  561. if (socialfeedviewmodel?.isPostPrivate(postIndex: index))! {
  562. switchCategoryTitle = "Show this post to GME Community"
  563. } else {
  564. switchCategoryTitle = "Hide this post from GME Community"
  565. }
  566. let show = UIAlertAction(title: switchCategoryTitle, style: .default) {
  567. UIAlertAction in
  568. let feedToEdit = self.socialfeedviewmodel!.getSocialFeedAt(index: index)
  569. if let feedText = feedToEdit.feedText {
  570. self.socialfeedviewmodel!.updateFeedText(text: feedText)
  571. }
  572. if let feedImageId = feedToEdit.feedImageId {
  573. self.socialfeedviewmodel!.setFeedImageId(imageId: feedImageId)
  574. }
  575. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  576. self.socialfeedviewmodel!.updateSocialFeed(feed: feedToEdit, index: index, toggle: true)
  577. }
  578. let edit = UIAlertAction(title: "Edit this post", style: .default) {
  579. UIAlertAction in
  580. self.statusUploadMode = 2
  581. self.editablePostIndex = index
  582. self.performSegue(withIdentifier: "statusUpdate2", sender: nil)
  583. }
  584. let delete = UIAlertAction(title: "Delete this post", style: .default) {
  585. UIAlertAction in
  586. self.deleteConfirmation(postIndex: index)
  587. }
  588. let cancel = UIAlertAction(title: "Cancel", style: .destructive) {
  589. UIAlertAction in
  590. }
  591. alert.addAction(show)
  592. alert.addAction(edit)
  593. alert.addAction(delete)
  594. alert.addAction(cancel)
  595. present(alert, animated: true, completion: nil)
  596. }
  597. func deleteConfirmation(postIndex: Int) {
  598. let alert = UIAlertController(title:nil, message:"Are you sure you want to delete?", preferredStyle: .actionSheet)
  599. let cancel = UIAlertAction(title: "No", style: .destructive) {
  600. UIAlertAction in
  601. }
  602. let delete = UIAlertAction(title: "Yes", style: .destructive) {
  603. UIAlertAction in
  604. self.tappedIndex = postIndex
  605. // self.feedDataIndex = 4
  606. self.socialfeedviewmodel!.deleteSocialFeed(feedId: self.socialfeedviewmodel!.getSocialFeedAt(index: postIndex).id)
  607. }
  608. alert.addAction(cancel)
  609. alert.addAction(delete)
  610. present(alert, animated: true, completion: nil)
  611. }
  612. override func didReceiveMemoryWarning() {
  613. super.didReceiveMemoryWarning()
  614. // Dispose of any resources that can be recreated.
  615. }
  616. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  617. switch segue.identifier {
  618. case "statusUpdate2"?:
  619. let statusUploadViewController = segue.destination as! StatusUploadViewController
  620. statusUploadViewController.statusUploadMode = statusUploadMode
  621. statusUploadViewController.editablePostIndex = editablePostIndex
  622. statusUploadViewController.socialfeedviewmodel = socialfeedviewmodel
  623. statusUploadViewController.showNotificationParameter = showNotificationParameter!
  624. case "statusUpdate"?:
  625. let vc = segue.destination as! StatusUploadViewController
  626. if statusUploadMode == 0 {
  627. vc.statusUploadMode = statusUploadMode
  628. } else if statusUploadMode == 1 {
  629. vc.statusUploadMode = statusUploadMode
  630. } else if statusUploadMode == 2 {
  631. vc.statusUploadMode = statusUploadMode
  632. }
  633. vc.showNotificationParameter = showNotificationParameter!
  634. case "showLikedBy2"?:
  635. let likesViewController = segue.destination as! LikesViewController
  636. likesViewController.socialfeedviewmodel = self.socialfeedviewmodel
  637. likesViewController.tappedIndex = self.tappedIndex
  638. case "showCommentsBy2"?:
  639. let commentsViewController = segue.destination as! CommentsViewController
  640. commentsViewController.socialfeedviewmodel = socialfeedviewmodel
  641. commentsViewController.tappedIndex = tappedIndex
  642. case "showMoreOptions2"?:
  643. let homeViewController = segue.destination as! HomeViewController
  644. homeViewController.numberCellsInCollectionView = 6
  645. homeViewController.showAllCollectionViewCells = true
  646. default:
  647. return
  648. }
  649. }
  650. }