LNFeedLikeView.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // LNFeedLikeView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/4.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNFeedLikeView: UIView, LNFeedManagerNotify {
  11. let likeIc = UIImageView()
  12. let likeLabel = UILabel()
  13. var uiColor: UIColor = .text_4 {
  14. didSet {
  15. likeLabel.textColor = uiColor
  16. if !isLiked {
  17. likeIc.image = .icLikeEmpty.withTintColor(uiColor, renderingMode: .alwaysOriginal)
  18. }
  19. }
  20. }
  21. private var curId: String?
  22. private var isLiked = false
  23. private var likeCount: Int = 0
  24. override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. likeIc.isUserInteractionEnabled = false
  27. likeIc.image = .icLikeEmpty.withTintColor(.text_4, renderingMode: .alwaysOriginal)
  28. addSubview(likeIc)
  29. likeIc.snp.makeConstraints { make in
  30. make.leading.equalToSuperview()
  31. make.verticalEdges.equalToSuperview()
  32. }
  33. likeLabel.isUserInteractionEnabled = false
  34. likeLabel.font = .heading_h5
  35. likeLabel.textColor = .text_4
  36. addSubview(likeLabel)
  37. likeLabel.snp.makeConstraints { make in
  38. make.centerY.equalToSuperview()
  39. make.trailing.equalToSuperview()
  40. make.leading.equalTo(likeIc.snp.trailing).offset(2)
  41. }
  42. onTap { [weak self] in
  43. guard let self, let curId else { return }
  44. LNFeedManager.shared.likeFeed(id: curId)
  45. }
  46. LNEventDeliver.addObserver(self)
  47. }
  48. func update(id: String, liked: Bool, count: Int) {
  49. curId = id
  50. isLiked = liked
  51. likeCount = count
  52. _updateUI()
  53. }
  54. func onFeedLikedChanged(id: String, liked: Bool) {
  55. guard id == curId else { return }
  56. isLiked = liked
  57. likeCount += liked ? 1 : -1
  58. _updateUI()
  59. }
  60. private func _updateUI() {
  61. likeIc.image = isLiked ? .icLikeFilled : .icLikeEmpty.withTintColor(uiColor, renderingMode: .alwaysOriginal)
  62. likeLabel.text = likeCount == 0 ? .init(key: "A00301") : "\(likeCount.formattedAsShortNumber())"
  63. }
  64. required init?(coder: NSCoder) {
  65. fatalError("init(coder:) has not been implemented")
  66. }
  67. }