ThumbnailCollectionViewCell.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. class ThumbnailCollectionViewCell: UICollectionViewCell {
  22. @IBOutlet weak var imageView: UIImageView!
  23. @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  24. var identifier : IndexPath?
  25. override var isSelected: Bool {
  26. didSet {
  27. self.updateSelectionState()
  28. }
  29. }
  30. func updateSelectionState() {
  31. self.layer.borderColor = isSelected ? Colors.main()?.cgColor : Colors.backgroundInverted()?.cgColor
  32. }
  33. override func prepareForReuse() {
  34. self.imageView.image = nil
  35. self.startLoading()
  36. self.isSelected = false
  37. self.updateSelectionState()
  38. }
  39. func setColors() {
  40. self.backgroundColor = .clear
  41. self.layer.borderColor = self.isSelected ? Colors.main()?.cgColor : Colors.backgroundInverted()?.cgColor
  42. self.layer.borderWidth = 2
  43. self.layer.cornerRadius = 5
  44. self.activityIndicator.color = Colors.fontNormal()
  45. }
  46. func startLoading() {
  47. self.setColors()
  48. self.activityIndicator.startAnimating()
  49. self.imageView.isHidden = true
  50. self.activityIndicator.isHidden = false
  51. }
  52. func loadImage(image : UIImage) {
  53. self.imageView.translatesAutoresizingMaskIntoConstraints = false
  54. self.imageView.clipsToBounds = true
  55. self.imageView.contentMode = .scaleAspectFill
  56. self.imageView.image = image
  57. self.activityIndicator.stopAnimating()
  58. self.imageView.isHidden = false
  59. self.activityIndicator.isHidden = true
  60. }
  61. }