Skip to content

Instantly share code, notes, and snippets.

@bibscy
Last active March 23, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bibscy/dc24db78177110e564783505e36aa83f to your computer and use it in GitHub Desktop.
Save bibscy/dc24db78177110e564783505e36aa83f to your computer and use it in GitHub Desktop.
//
// MediaTableViewCell.swift
// myStreet.com
//
// Created by Bogdan Barbulescu on 12/04/2018.
// Copyright © 2018 vividapartments. All rights reserved.
//
import UIKit
import SAMCache
protocol MediaTableViewCellDelegate: class {
func commentButtonDidTap(media: Media)
func viewAllComments(media: Media)
func moreOptionsTapped(cell: MediaTableViewCell, mediaObject: Media)
func mediaTableViewCell(cell: MediaTableViewCell, didClickProfileImage: Bool)
func mediaTableViewCell(cell: MediaTableViewCell, didClickProfileName: Bool)
}
//protocol MediaTableViewCellHeaderDelegate: class {
// func moreOptionsTapped(cell: MediaHeaderCell)
//}
class MediaTableViewCell: UITableViewCell {
weak var delegate: MediaTableViewCellDelegate?
// @IBOutlet weak var captionLabel: UILabel! // The text that user wrote in the post
@IBOutlet weak var captionLabel: UITextView!
@IBOutlet weak var createdAtLabel: UILabel!
@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var commenButton: UIButton!
@IBOutlet weak var shareButton: UIButton!
@IBOutlet weak var numberOfLikesButton: UIButton!
@IBOutlet weak var viewAllCommentsButton: UIButton!
@IBOutlet weak var moreOptionsButton: UIButton!
//managing header looking top part of the cell
@IBOutlet weak var mediaImageView: UIImageView!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var fullNameButton: UIButton!
// @IBOutlet weak var followButton: UIButton!
//if this is true, it means that the request to populate this cell with data comes from NewsFeedTableViewController
// The reason why we do this check is because we don't want to delete MediaHeaderCell in other ViewControllers, such as MediaDetailTableViewController and ProfileViewController because they already contain MediaHeaderCell
var isNewsFeed: Bool = false
//initialized after the image is downloaded and it is used in NewsfeedVC. When the post needs to be edited, we access this property from NewsfeedVC and pass it to EditPostViewController, thus it will contain the downloaded image too
var mediaObject: Media!
var currentUser: UserModel!
var comments = [Comment]()
var media: Media! {
didSet {
if currentUser != nil {
self.updateUI()
removeAllObserversInCell()
if isNewsFeed == true {
self.updateUIForHeader()
}
}else{
printsNow(message: "currentUser is nil")
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
initialize()
}
private func initialize() {
let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileImageTapped))
profileImageView.addGestureRecognizer(imageTapGesture)
// imageviews by default aren't interactable
profileImageView.isUserInteractionEnabled = true
fullNameButton.addTarget(self, action: #selector(profileButtonTapped), for: .touchUpInside)
}
@objc private func profileImageTapped() {
delegate?.mediaTableViewCell(cell: self, didClickProfileImage: true)
}
@objc private func profileButtonTapped() {
delegate?.mediaTableViewCell(cell: self, didClickProfileName: true)
}
func removeAllObserversInCell() {
// //remove likes observers
// DDatabaseRReference.media.reference().child("\(media.mediaUID)/likes").removeAllObservers()
//
// //remove comments observers
// DDatabaseRReference.media.reference().child("\(media.mediaUID)/comments").removeAllObservers()
}
override func prepareForReuse() {
super.prepareForReuse()
// removeAllObserversInCell()
}
func updateUI() {
captionLabel.text = media.caption
self.mediaImageView.image = nil
let mediaImageKey = "\(media.mediaUID)-mediaImage"
if let image = SAMCache.shared().object(forKey: mediaImageKey) as? UIImage {
self.mediaObject = media
self.mediaObject.mediaImage = image
self.mediaImageView.image = image
} else {
//download image the from 'media' selected in cellForRowAt:
// the reference of picture is ~/StorageReference.images.reference().child(uid) -> uid is uid of media
media.downloadMediaImage { [weak self] (image, error) in
if image != nil {
self?.mediaImageView.image = image
SAMCache.shared().setObject(image, forKey: mediaImageKey)
}
self?.mediaImageView.image = image ?? UIImage(named: "icon-defaultAvatar")
self?.mediaObject = self?.media
self?.mediaObject.mediaImage = image
// self?.profileImageView.layer.cornerRadius = (self?.profileImageView.bounds.width)! / 2.0
// self?.profileImageView.layer.masksToBounds = true
}//end downloadMediaImage
}//end of else
likeButton.setImage(UIImage(named: "icon-like"), for: [])
assign(likesNumber: media.likes.count)
if media.likes.contains(currentUser) {
likeButton.setImage(UIImage(named: "icon-like-filled"), for: [])
}
if media.comments.count == 0 {
viewAllCommentsButton.setTitle("Be the first to share a comment", for: [])
} else {
viewAllCommentsButton.setTitle("View all \(media.comments.count) comments", for: [])
}
let time = getTimeAsString(timeStamp: media.createdTime, timeFormat: "dd MMM yyyy HH:mm")
self.createdAtLabel.text = "shared on \(time)"
// self.captionLabel.numberOfLines = 0
if !self.likeButton.isHidden {
print("likeButton is not hidden in UpdateUI")
}else {
print("it is hidden")
}
}
func assign(likesNumber: Int) {
if likesNumber == 0 {
numberOfLikesButton.setTitle("Be the first to like this!", for: [])
likeButton.setImage(UIImage(named: "icon-like"), for: [])
} else {
numberOfLikesButton.setTitle("♥︎ \(media.likes.count) likes", for: [])
}
}
@IBAction func likeDidTap() {
if media.likes.contains(currentUser) {
likeButton.setImage(UIImage(named: "icon-like"), for: [])
//unlike the post
media.unlikedBy(user: currentUser)
} else {
likeButton.setImage(UIImage(named: "icon-like-filled"), for: [])
//like the post
media.likedBy(user: currentUser)
}
//used when MediaDetail
// media.observeNumberOfLikes { numberOfLikes in
// self.assign(likesNumber: numberOfLikes)
// }
}
@IBAction func commentDidTap() {
delegate?.commentButtonDidTap(media: media)
}
@IBAction func shareDidTap (){
}
@IBAction func numberOfLikesDidTap() {
delegate?.viewAllComments(media: media)
}
@IBAction func viewAllCommentsDidTap() {
delegate?.viewAllComments(media: media)
}
@IBOutlet weak var moreOptionButton: UIButton!
@IBAction func moreOptionsAction(_ sender: UIButton) {
if mediaObject.mediaImage != nil {
delegate?.moreOptionsTapped(cell: self, mediaObject: mediaObject)
}else {
printsNow(message: "no image in here")
}
}
// @IBAction func followDidTap(_ sender: AnyObject) {
//
// currentUser.follow(user: media.createdBy)
// media.createdBy.isFollowedBy(currentUser)
// followButton.isHidden = true
// }
}//end of cell class
extension MediaTableViewCell {
func updateUIForHeader() {
//update the upper part of the cell, we don't have headerForCell in NewsFeed
profileImageView.image = #imageLiteral(resourceName: "icon-defaultAvatar")
if let image = SAMCache.shared().object(forKey: "\(self.media.createdBy.userUID)-headerImage") as? UIImage {
self.profileImageView.image = image
} else {
media.createdBy.downloadProfilePicture { [weak self] (image, error) in
if let image = image {
self?.profileImageView.image = image
SAMCache.shared().setObject(image, forKey: "\(self?.media.createdBy.userUID)-headerImage")
} else if error != nil {
print(error)
}
}
}
profileImageView.layer.cornerRadius = profileImageView.bounds.width / 2.0
profileImageView.layer.masksToBounds = true
let firstName = media.createdBy.firstName
let lastName = media.createdBy.lastName
let fullName = "\(firstName) \(lastName)"
fullNameButton.setTitle(fullName, for: [])
// followButton.layer.borderWidth = 1
// followButton.layer.cornerRadius = 2.0
// followButton.layer.borderColor = followButton.tintColor.cgColor
// followButton.layer.masksToBounds = true
// if currentUser.follows.contains(media.createdBy) || media.createdBy.userUID == currentUser.userUID {
// followButton.isHidden = true
// } else {
// followButton.isHidden = false
// }
// if !self.likeButton.isHidden {
// print("likeButton is not hidden in UpdateUIHEADER")
// }else {
// print("it is hidden")
// }
}
}//end of extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment