|
| 1 | +// |
| 2 | +// EventsTableViewCell.swift |
| 3 | +// PennMobile |
| 4 | +// |
| 5 | +// Created by Samantha Su on 10/1/21. |
| 6 | +// Copyright © 2021 PennLabs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import Kingfisher |
| 11 | +import SwiftSoup |
| 12 | + |
| 13 | +class PennEventsTableViewCell: UITableViewCell { |
| 14 | + |
| 15 | + lazy var imageExistsConstraint = eventImageView.widthAnchor.constraint(equalToConstant:120) |
| 16 | + lazy var imageMissingConstraint = eventImageView.widthAnchor.constraint(equalToConstant:0) |
| 17 | + var isExpanded = false |
| 18 | + |
| 19 | + var pennEvent: PennEvents? { |
| 20 | + didSet { |
| 21 | + guard let event = pennEvent else {return} |
| 22 | + if event.media_image == "" { |
| 23 | + imageExistsConstraint.isActive = false |
| 24 | + imageMissingConstraint.isActive = true |
| 25 | + } else { |
| 26 | + let imageString = "https://penntoday.upenn.edu" + (event.media_image.slice(from: "<img src=\"", to: "\n") ?? "").trimmingCharacters(in: .whitespaces) |
| 27 | + eventImageView.kf.setImage(with: URL(string: imageString)) |
| 28 | + imageExistsConstraint.isActive = true |
| 29 | + imageMissingConstraint.isActive = false |
| 30 | + } |
| 31 | + titleLabel.text = event.shortdate + ": " + event.title |
| 32 | + if let doc: Document = try? SwiftSoup.parse(event.body), let text = try? doc.text() { |
| 33 | + bodyLabel.text = text |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: - Views |
| 39 | + |
| 40 | + let containerView:UIView = { |
| 41 | + let view = UIView() |
| 42 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 43 | + view.clipsToBounds = true // this will make sure its children do not go out of the boundary |
| 44 | + return view |
| 45 | + }() |
| 46 | + |
| 47 | + let eventImageView:UIImageView = { |
| 48 | + let img = UIImageView() |
| 49 | + img.contentMode = .scaleAspectFill // image will never be strecthed vertially or horizontally |
| 50 | + img.translatesAutoresizingMaskIntoConstraints = false // enable autolayout |
| 51 | + img.layer.cornerRadius = 7 |
| 52 | + img.clipsToBounds = true |
| 53 | + return img |
| 54 | + }() |
| 55 | + |
| 56 | + let titleLabel:UILabel = { |
| 57 | + var view = UILabel() |
| 58 | + view.backgroundColor = .clear |
| 59 | + view.numberOfLines = 0 |
| 60 | + |
| 61 | + view.textColor = UIColor(red: 0.122, green: 0.122, blue: 0.122, alpha: 1) |
| 62 | + view.font = UIFont(name: "SFProText-Regular", size: 30) |
| 63 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 64 | + return view |
| 65 | + }() |
| 66 | + |
| 67 | + let bodyLabel:UILabel = { |
| 68 | + var view = UILabel() |
| 69 | + view.backgroundColor = .clear |
| 70 | + view.font = UIFont(name: "Helvetica", size: 12) |
| 71 | + view.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1) |
| 72 | + view.numberOfLines = 3 |
| 73 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 74 | + return view |
| 75 | + }() |
| 76 | + |
| 77 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 78 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 79 | + |
| 80 | + containerView.addSubview(titleLabel) |
| 81 | + containerView.addSubview(bodyLabel) |
| 82 | + self.contentView.addSubview(containerView) |
| 83 | + self.contentView.addSubview(eventImageView) |
| 84 | + |
| 85 | + containerView.leadingAnchor.constraint(equalTo:self.contentView.leadingAnchor, constant:10).isActive = true |
| 86 | + containerView.centerYAnchor.constraint(equalTo:self.contentView.centerYAnchor).isActive = true |
| 87 | + containerView.bottomAnchor.constraint(equalTo:self.contentView.bottomAnchor, constant:-10).isActive = true |
| 88 | + |
| 89 | + eventImageView.centerYAnchor.constraint(equalTo:self.contentView.centerYAnchor).isActive = true |
| 90 | + eventImageView.leadingAnchor.constraint(equalTo:containerView.trailingAnchor, constant:10).isActive = true |
| 91 | + eventImageView.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor, constant:-10).isActive = true |
| 92 | + eventImageView.heightAnchor.constraint(equalToConstant:80).isActive = true |
| 93 | + |
| 94 | + titleLabel.topAnchor.constraint(equalTo:containerView.topAnchor, constant:5).isActive = true |
| 95 | + titleLabel.trailingAnchor.constraint(equalTo:containerView.trailingAnchor, constant:-10).isActive = true |
| 96 | + titleLabel.leadingAnchor.constraint(equalTo:containerView.leadingAnchor, constant:10).isActive = true |
| 97 | + |
| 98 | + bodyLabel.topAnchor.constraint(equalTo:titleLabel.bottomAnchor, constant:0).isActive = true |
| 99 | + bodyLabel.bottomAnchor.constraint(equalTo:containerView.bottomAnchor, constant:-5).isActive = true |
| 100 | + bodyLabel.trailingAnchor.constraint(equalTo:containerView.trailingAnchor, constant:-10).isActive = true |
| 101 | + bodyLabel.leadingAnchor.constraint(equalTo:containerView.leadingAnchor, constant:10).isActive = true |
| 102 | + } |
| 103 | + |
| 104 | + required init?(coder aDecoder: NSCoder) { |
| 105 | + super.init(coder: aDecoder) |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments