Skip to content

Instantly share code, notes, and snippets.

@DDavis1025
Created June 19, 2020 19:11
Show Gist options
  • Save DDavis1025/2b5fe54c0518b662ab24bae830bfd15f to your computer and use it in GitHub Desktop.
Save DDavis1025/2b5fe54c0518b662ab24bae830bfd15f to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import Combine
class CommentView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
sizeToFit()
addSubview(textView)
addSubview(user_image)
addSubview(username)
setImageConstraints()
setUsernameConstraints()
textViewContstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var textView:UITextView = {
let tv = UITextView()
tv.isScrollEnabled = false
tv.isEditable = false
tv.sizeToFit()
tv.backgroundColor = UIColor.lightGray
tv.textContainer.maximumNumberOfLines = 0
tv.textContainer.lineBreakMode = .byCharWrapping
tv.font = UIFont(name: "GillSans", size: 18)
return tv
}()
lazy var user_image: UIImageView = {
let image = UIImageView()
return image
}()
lazy var username:UILabel = {
let label = UILabel()
label.textColor = UIColor.gray
label.font = UIFont.systemFont(ofSize: 14)
return label
}()
func setImageConstraints() {
user_image.translatesAutoresizingMaskIntoConstraints = false
user_image.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
user_image.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8).isActive = true
user_image.heightAnchor.constraint(equalToConstant: 40).isActive = true
user_image.widthAnchor.constraint(equalToConstant: 40).isActive = true
}
func setUsernameConstraints() {
username.translatesAutoresizingMaskIntoConstraints = false
username.topAnchor.constraint(equalTo: user_image.topAnchor, constant: 5).isActive = true
username.leadingAnchor.constraint(equalTo: user_image.trailingAnchor, constant: 8).isActive = true
username.heightAnchor.constraint(equalToConstant: 10).isActive = true
username.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
}
func textViewContstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: username.bottomAnchor, constant: 5).isActive = true
textView.leadingAnchor.constraint(equalTo: user_image.trailingAnchor, constant: 8).isActive = true
textView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
}
}
class CommentCell:UITableViewCell {
static var shared = CommentCell()
var user_image = UIImageView()
var username = UILabel()
var imageLoader:DownloadImage?
var stackView = UIStackView()
var sub_comments:[Comments] = []
var bottomConstraint:NSLayoutConstraint?
var parent_id:String?
var components:URLComponents = {
var component = URLComponents()
component.scheme = "http"
component.host = "localhost"
component.port = 8000
return component
}()
lazy var textView:UITextView = {
let tv = UITextView()
tv.isScrollEnabled = false
tv.isEditable = false
tv.sizeToFit()
tv.backgroundColor = UIColor.lightGray
tv.textContainer.maximumNumberOfLines = 0
tv.textContainer.lineBreakMode = .byCharWrapping
tv.font = UIFont(name: "GillSans", size: 18)
return tv
}()
lazy var repliesBtn:UIButton = {
let btn = UIButton()
btn.setTitle("View Replies ⌄", for: .normal)
btn.setTitleColor(UIColor.gray, for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 12)
btn.contentHorizontalAlignment = .left
btn.addTarget(self, action: #selector(repliesBtnPressed), for: .touchUpInside)
return btn
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(user_image)
setImageConstraints()
setUsername()
addSubview(username)
setUsernameConstraints()
addSubview(textView)
addSubview(repliesBtn)
textViewContstraints()
repliesBtnConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func repliesBtnPressed() {
repliesBtn.removeFromSuperview()
configureStackView()
setSuBComments()
}
func configureStackView() {
addSubview(stackView)
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.spacing = 10
setStackViewContstraints()
}
func setSuBComments() {
print("setSubComments")
let getComments = GETComments(id: parent_id, path: "subComments")
getComments.getAllById {
print("$0 \($0)")
self.sub_comments = $0
self.addCommentViewsToStackView(subComments: $0)
}
}
func addCommentViewsToStackView(subComments: [Comments]) {
for i in sub_comments {
let comment_view = CommentView()
comment_view.textView.text = i.text
self.imageLoader = DownloadImage()
imageLoader?.imageDidSet = { [weak self] image in
comment_view.user_image.image = image
}
if let picture = i.user_picture {
imageLoader?.downloadImage(urlString: picture)
}
comment_view.username.text = i.username
stackView.addArrangedSubview(comment_view)
}
}
func setStackViewContstraints() {
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: -6).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: textView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: textView.trailingAnchor).isActive = true
}
func set(comment: Comments) {
self.imageLoader = DownloadImage()
imageLoader?.imageDidSet = { [weak self] image in
self?.user_image.image = image
}
if (comment.user_picture?.contains("http"))! {
if let comment_picture = comment.user_picture {
imageLoader?.downloadImage(urlString: comment_picture)
}
} else {
components.path = "/\(comment.user_picture)"
if let comment_picture = components.url?.absoluteString {
imageLoader?.downloadImage(urlString: comment_picture)
}
}
username.text = comment.username
if let comment_text = comment.text {
textView.text = comment_text
}
}
func setImageConstraints() {
user_image.translatesAutoresizingMaskIntoConstraints = false
user_image.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
user_image.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8).isActive = true
user_image.heightAnchor.constraint(equalToConstant: 40).isActive = true
user_image.widthAnchor.constraint(equalToConstant: 40).isActive = true
}
func setUsername() {
username.textColor = UIColor.gray
username.font = UIFont.systemFont(ofSize: 14)
}
func setUsernameConstraints() {
username.translatesAutoresizingMaskIntoConstraints = false
username.topAnchor.constraint(equalTo: user_image.topAnchor, constant: 5).isActive = true
username.leadingAnchor.constraint(equalTo: user_image.trailingAnchor, constant: 8).isActive = true
username.heightAnchor.constraint(equalToConstant: 10).isActive = true
username.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
}
func textViewContstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: username.bottomAnchor, constant: 5).isActive = true
textView.leadingAnchor.constraint(equalTo: user_image.trailingAnchor, constant: 8).isActive = true
textView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -60).isActive = true
}
func repliesBtnConstraints() {
repliesBtn.translatesAutoresizingMaskIntoConstraints = false
bottomConstraint =
repliesBtn.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -6)
bottomConstraint?.isActive = true
repliesBtn.widthAnchor.constraint(equalTo: textView.widthAnchor).isActive = true
repliesBtn.heightAnchor.constraint(equalToConstant: 30).isActive = true
repliesBtn.leadingAnchor.constraint(equalTo: textView.leadingAnchor).isActive = true
repliesBtn.topAnchor.constraint(equalTo: textView.bottomAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment