Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CianElement.swift « Cian « Content « PlacePageLayout « PlacePage « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a191932ea11fbdb89e9d5d0684b51a8aa0e9bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
final class CianElement: UICollectionViewCell {
  enum State {
    case pending(onButtonAction: () -> Void)
    case offer(model: CianItemModel?, onButtonAction: (CianItemModel?) -> Void)
    case error(onButtonAction: () -> Void)
  }

  @IBOutlet private var contentViews: [UIView]!

  @IBOutlet private weak var pendingView: UIView!
  @IBOutlet private weak var offerView: UIView!
  @IBOutlet private weak var more: UIButton!
  @IBOutlet private weak var price: UILabel! {
    didSet {
      price.font = UIFont.medium14()
      price.textColor = UIColor.linkBlue()
    }
  }

  @IBOutlet private weak var descr: UILabel! {
    didSet {
      descr.font = UIFont.medium14()
      descr.textColor = UIColor.blackPrimaryText()
    }
  }

  @IBOutlet private weak var address: UILabel! {
    didSet {
      address.font = UIFont.regular12()
      address.textColor = UIColor.blackSecondaryText()
    }
  }

  @IBOutlet private weak var details: UIButton! {
    didSet {
      details.setTitleColor(UIColor.linkBlue(), for: .normal)
      details.setBackgroundColor(UIColor.blackOpaque(), for: .normal)
    }
  }

  private var isLastCell = false {
    didSet {
      more.isHidden = !isLastCell
      price.isHidden = isLastCell
      descr.isHidden = isLastCell
      address.isHidden = isLastCell
      details.isHidden = isLastCell
    }
  }

  @IBOutlet private weak var pendingSpinnerView: UIImageView! {
    didSet {
      pendingSpinnerView.tintColor = UIColor.linkBlue()
    }
  }

  @IBOutlet private weak var pendingTitleTopOffset: NSLayoutConstraint!
  @IBOutlet private weak var pendingTitle: UILabel! {
    didSet {
      pendingTitle.font = UIFont.medium14()
      pendingTitle.textColor = UIColor.blackPrimaryText()
      pendingTitle.text = L("preloader_cian_title")
    }
  }

  @IBOutlet private weak var pendingDescription: UILabel! {
    didSet {
      pendingDescription.font = UIFont.regular12()
      pendingDescription.textColor = UIColor.blackSecondaryText()
      pendingDescription.text = L("preloader_cian_message")
    }
  }

  @IBAction func onButtonAction() {
    switch state! {
    case let .pending(action): action()
    case let .offer(model, action): action(model)
    case let .error(action): action()
    }
  }

  var state: State! {
    didSet {
      setupAppearance()
      setNeedsLayout()
      contentViews.forEach { $0.isHidden = false }
      let visibleView: UIView
      var pendingSpinnerViewAlpha: CGFloat = 1
      switch state! {
      case .pending:
        visibleView = pendingView
        configPending()
      case let .offer(model, _):
        visibleView = offerView
        configOffer(model: model)
      case .error:
        pendingSpinnerViewAlpha = 0
        visibleView = pendingView
        configError()
      }
      UIView.animate(withDuration: kDefaultAnimationDuration,
                     animations: {
                       self.pendingSpinnerView.alpha = pendingSpinnerViewAlpha
                       self.contentViews.forEach { $0.alpha = 0 }
                       visibleView.alpha = 1
                       self.layoutIfNeeded()
                     },
                     completion: { _ in
                       self.contentViews.forEach { $0.isHidden = true }
                       visibleView.isHidden = false
      })
    }
  }

  private var isSpinning = false {
    didSet {
      let animationKey = "SpinnerAnimation"
      if isSpinning {
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.fromValue = NSNumber(value: 0)
        animation.toValue = NSNumber(value: 2 * Double.pi)
        animation.duration = 0.8
        animation.repeatCount = Float.infinity
        pendingSpinnerView.layer.add(animation, forKey: animationKey)
      } else {
        pendingSpinnerView.layer.removeAnimation(forKey: animationKey)
      }
    }
  }

  private func setupAppearance() {
    backgroundColor = UIColor.white()
    layer.cornerRadius = 6
    layer.borderWidth = 1
    layer.borderColor = UIColor.blackDividers().cgColor
  }

  private func configPending() {
    isSpinning = true
    details.setTitle(L("preloader_cian_button"), for: .normal)
    pendingTitleTopOffset.priority = UILayoutPriority.defaultLow
  }

  private func configError() {
    isSpinning = false
    details.setTitle(L("preloader_cian_button"), for: .normal)
    pendingTitleTopOffset.priority = UILayoutPriority.defaultHigh
  }

  private func configOffer(model: CianItemModel?) {
    isSpinning = false
    if let model = model {
      isLastCell = false

      let priceFormatter = NumberFormatter()
      priceFormatter.usesGroupingSeparator = true
      if let priceString = priceFormatter.string(from: NSNumber(value: model.priceRur)) {
        price.text = "\(priceString) \(L("rub_month"))"
      } else {
        price.isHidden = true
      }

      let descrFormat = L("room").replacingOccurrences(of: "%s", with: "%@")
      descr.text = String(format: descrFormat, arguments: ["\(model.roomsCount)"])

      address.text = model.address

      details.setTitle(L("details"), for: .normal)
    } else {
      isLastCell = true

      more.setBackgroundImage(UIColor.isNightMode() ? #imageLiteral(resourceName: "btn_float_more_dark") : #imageLiteral(resourceName: "btn_float_more_light"), for: .normal)

      backgroundColor = UIColor.clear
      layer.borderColor = UIColor.clear.cgColor
    }
  }
}