Advertisement
greedydev

Untitled

Feb 13th, 2024
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.58 KB | None | 0 0
  1. //
  2. //  PostView.swift
  3. //  RedditClient
  4. //
  5. //  Created by Denis on 3/14/23.
  6. //
  7.  
  8. import SDWebImage
  9. import UIKit
  10.  
  11. class RedditPostView: UIView {
  12.     private let postDetailsView = PostDetailsView()
  13.    
  14.     private let postTitleLabel = PostTitleLabel()
  15.     private let postImageView = UIImageView()
  16.     private let postActionsView = PostActionsView()
  17.    
  18.     private var bookmarkIcon: BookmarkIconView!
  19.    
  20.     private var postActionsViewTopToTitleConstraint: NSLayoutConstraint?
  21.     private var postActionsViewTopToImageConstraint: NSLayoutConstraint?
  22.    
  23.     private var postImageViewHeightConstraint: NSLayoutConstraint?
  24.    
  25.     weak var delegate: RedditPostDelegate?
  26.    
  27.     private var post: Post?
  28.  
  29.     override init(frame: CGRect) {
  30.         super.init(frame: frame)
  31.         setupViews()
  32.     }
  33.    
  34.     required init?(coder: NSCoder) {
  35.         fatalError("init(coder:) has not been implemented")
  36.     }
  37.  
  38.     private func setupViews() {
  39.         postImageView.isHidden = true
  40.        
  41.         postImageView.contentMode = .scaleAspectFit
  42.         postImageView.backgroundColor = .secondarySystemBackground
  43.        
  44.         bookmarkIcon = BookmarkIconView(
  45.             frame: CGRect(
  46.                 x: UIScreen.main.bounds.width - 18,
  47.                 y: 4,
  48.                 width: 12,
  49.                 height: 20
  50.             )
  51.         )
  52.        
  53.         addSubview(postDetailsView)
  54.         addSubview(bookmarkIcon)
  55.         addSubview(postTitleLabel)
  56.         addSubview(postImageView)
  57.         addSubview(postActionsView)
  58.        
  59.         // Add the following lines to create new top constraints
  60.         postActionsViewTopToTitleConstraint = postActionsView.topAnchor.constraint(equalTo: postTitleLabel.bottomAnchor, constant: 8)
  61.         postActionsViewTopToImageConstraint = postActionsView.topAnchor.constraint(equalTo: postImageView.bottomAnchor, constant: 8)
  62.  
  63.         // Set the initial state (assuming there is no image initially)
  64.         postActionsViewTopToImageConstraint?.isActive = false
  65.         postActionsViewTopToTitleConstraint?.isActive = true
  66.  
  67.         postImageView.translatesAutoresizingMaskIntoConstraints = false
  68.  
  69.         translatesAutoresizingMaskIntoConstraints = false
  70.        
  71.         NSLayoutConstraint.activate([
  72.             postDetailsView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
  73.             postDetailsView.centerYAnchor.constraint(equalTo: bookmarkIcon.centerYAnchor),
  74.            
  75.             postTitleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8),
  76.             postTitleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8),
  77.             postTitleLabel.topAnchor.constraint(equalTo: bookmarkIcon.bottomAnchor, constant: 8),
  78.            
  79.             postImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
  80.             postImageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
  81.             postImageView.topAnchor.constraint(equalTo: postTitleLabel.bottomAnchor, constant: 8),
  82.             postImageView.heightAnchor.constraint(lessThanOrEqualToConstant: 300),
  83.            
  84.             postActionsView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
  85.             postActionsView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
  86.            
  87.             self.bottomAnchor.constraint(equalTo: postActionsView.bottomAnchor, constant: 8)
  88.         ])
  89.     }
  90.  
  91.     func toggleFavorite() {
  92.         PersistenceManager.updateWith(favorite: post!, actionType: post!.isSaved ? .remove : .add) { [weak self] error in
  93.             guard let self else { return }
  94.            
  95.             guard error != nil else {
  96.                 self.toggleBookmarkIconState()
  97.                 return
  98.             }
  99.         }
  100.     }
  101.    
  102.     private func toggleBookmarkIconState() {
  103.         DispatchQueue.main.async { [weak self] in
  104.             guard let self else { return }
  105.             UIView.transition(
  106.                 with: self,
  107.                 duration: 0.15,
  108.                 options: .transitionCrossDissolve) {
  109.                     self.bookmarkIcon.isHidden.toggle()
  110.                 }
  111.         }
  112.     }
  113.    
  114.     func setBlank() {
  115.         self.postImageView.image = nil
  116.         self.postImageView.sd_cancelCurrentImageLoad()
  117.         postActionsViewTopToImageConstraint?.isActive = false
  118.         postActionsViewTopToTitleConstraint?.isActive = true
  119.     }
  120.    
  121.     func configure(post: Post) {
  122.         self.post = post
  123.        
  124.         postActionsView.shareButton.addTarget(self, action: #selector(shareButtonTapped), for: .touchUpInside)
  125.        
  126.         postDetailsView.configure(author: post.data.author, postTime: post.data.createdAt, domain: post.data.subreddit)
  127.        
  128.         bookmarkIcon.isHidden = !post.isSaved
  129.         postTitleLabel.text = post.data.title
  130.  
  131.         if let imageLink = post.data.preview?.images.first?.source.url {
  132.             postImageViewHeightConstraint = postImageView.heightAnchor.constraint(lessThanOrEqualToConstant: 300)
  133.            
  134.             postImageViewHeightConstraint?.isActive = true
  135.            
  136.             postActionsViewTopToTitleConstraint?.isActive = false
  137.             postActionsViewTopToImageConstraint?.isActive = true
  138.             postImageView.isHidden = false
  139.            
  140.             postImageView.sd_setImage(with: imageLink, placeholderImage: UIImage().withTintColor(.secondaryLabel))
  141.         } else {
  142.             postImageView.isHidden = true
  143.             postActionsViewTopToImageConstraint?.isActive = false
  144.             postActionsViewTopToTitleConstraint?.isActive = true
  145.         }
  146.        
  147.         postActionsView.configure(post: post)
  148.     }
  149.    
  150.     @objc private func shareButtonTapped() {
  151.         delegate?.didTapShareButton(for: post!)
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement