Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PostView.swift
- // RedditClient
- //
- // Created by Denis on 3/14/23.
- //
- import SDWebImage
- import UIKit
- class RedditPostView: UIView {
- private let postDetailsView = PostDetailsView()
- private let postTitleLabel = PostTitleLabel()
- private let postImageView = UIImageView()
- private let postActionsView = PostActionsView()
- private var bookmarkIcon: BookmarkIconView!
- private var postActionsViewTopToTitleConstraint: NSLayoutConstraint?
- private var postActionsViewTopToImageConstraint: NSLayoutConstraint?
- private var postImageViewHeightConstraint: NSLayoutConstraint?
- weak var delegate: RedditPostDelegate?
- private var post: Post?
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- }
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- private func setupViews() {
- postImageView.isHidden = true
- postImageView.contentMode = .scaleAspectFit
- postImageView.backgroundColor = .secondarySystemBackground
- bookmarkIcon = BookmarkIconView(
- frame: CGRect(
- x: UIScreen.main.bounds.width - 18,
- y: 4,
- width: 12,
- height: 20
- )
- )
- addSubview(postDetailsView)
- addSubview(bookmarkIcon)
- addSubview(postTitleLabel)
- addSubview(postImageView)
- addSubview(postActionsView)
- // Add the following lines to create new top constraints
- postActionsViewTopToTitleConstraint = postActionsView.topAnchor.constraint(equalTo: postTitleLabel.bottomAnchor, constant: 8)
- postActionsViewTopToImageConstraint = postActionsView.topAnchor.constraint(equalTo: postImageView.bottomAnchor, constant: 8)
- // Set the initial state (assuming there is no image initially)
- postActionsViewTopToImageConstraint?.isActive = false
- postActionsViewTopToTitleConstraint?.isActive = true
- postImageView.translatesAutoresizingMaskIntoConstraints = false
- translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- postDetailsView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
- postDetailsView.centerYAnchor.constraint(equalTo: bookmarkIcon.centerYAnchor),
- postTitleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
- postTitleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8),
- postTitleLabel.topAnchor.constraint(equalTo: bookmarkIcon.bottomAnchor, constant: 8),
- postImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
- postImageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
- postImageView.topAnchor.constraint(equalTo: postTitleLabel.bottomAnchor, constant: 8),
- postImageView.heightAnchor.constraint(lessThanOrEqualToConstant: 300),
- postActionsView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
- postActionsView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
- self.bottomAnchor.constraint(equalTo: postActionsView.bottomAnchor, constant: 8)
- ])
- }
- func toggleFavorite() {
- PersistenceManager.updateWith(favorite: post!, actionType: post!.isSaved ? .remove : .add) { [weak self] error in
- guard let self else { return }
- guard error != nil else {
- self.toggleBookmarkIconState()
- return
- }
- }
- }
- private func toggleBookmarkIconState() {
- DispatchQueue.main.async { [weak self] in
- guard let self else { return }
- UIView.transition(
- with: self,
- duration: 0.15,
- options: .transitionCrossDissolve) {
- self.bookmarkIcon.isHidden.toggle()
- }
- }
- }
- func setBlank() {
- self.postImageView.image = nil
- self.postImageView.sd_cancelCurrentImageLoad()
- postActionsViewTopToImageConstraint?.isActive = false
- postActionsViewTopToTitleConstraint?.isActive = true
- }
- func configure(post: Post) {
- self.post = post
- postActionsView.shareButton.addTarget(self, action: #selector(shareButtonTapped), for: .touchUpInside)
- postDetailsView.configure(author: post.data.author, postTime: post.data.createdAt, domain: post.data.subreddit)
- bookmarkIcon.isHidden = !post.isSaved
- postTitleLabel.text = post.data.title
- if let imageLink = post.data.preview?.images.first?.source.url {
- postImageViewHeightConstraint = postImageView.heightAnchor.constraint(lessThanOrEqualToConstant: 300)
- postImageViewHeightConstraint?.isActive = true
- postActionsViewTopToTitleConstraint?.isActive = false
- postActionsViewTopToImageConstraint?.isActive = true
- postImageView.isHidden = false
- postImageView.sd_setImage(with: imageLink, placeholderImage: UIImage().withTintColor(.secondaryLabel))
- } else {
- postImageView.isHidden = true
- postActionsViewTopToImageConstraint?.isActive = false
- postActionsViewTopToTitleConstraint?.isActive = true
- }
- postActionsView.configure(post: post)
- }
- @objc private func shareButtonTapped() {
- delegate?.didTapShareButton(for: post!)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement