forked from HabitRPG/habitica-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassSelectionOptionView.swift
129 lines (114 loc) · 4.05 KB
/
ClassSelectionOptionView.swift
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
//
// ClassSelectionOptionView.swift
// Habitica
//
// Created by Phillip Thelen on 26.04.18.
// Copyright © 2018 HabitRPG Inc. All rights reserved.
//
import UIKit
import Habitica_Models
class ClassSelectionOptionView: UIView, Themeable {
private let avatarView: AvatarView = {
let avatarView = AvatarView()
avatarView.showBackground = false
avatarView.showPet = false
avatarView.showMount = false
avatarView.size = .compact
return avatarView
}()
private let labelWrapper: UIView = {
let labelWrapper = UIView()
labelWrapper.backgroundColor = UIColor.gray700
labelWrapper.layer.cornerRadius = 4
return labelWrapper
}()
private let iconView = UIImageView()
private let label: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
return label
}()
private var onSelected: (() -> Void)?
var isSelected = false {
didSet {
let newWidth = self.isSelected ? 2 : 0
let widthAnimation = CABasicAnimation(keyPath: "borderWidth")
widthAnimation.fromValue = self.labelWrapper.layer.borderWidth
widthAnimation.toValue = newWidth
widthAnimation.duration = 0.2
self.labelWrapper.layer.borderWidth = CGFloat(newWidth)
self.labelWrapper.layer.add(widthAnimation, forKey: "border width")
}
}
override var tintColor: UIColor! {
didSet {
labelWrapper.layer.borderColor = tintColor.cgColor
label.textColor = tintColor
}
}
var userStyle: UserStyleProtocol? {
didSet {
if let userStyle = self.userStyle {
avatarView.avatar = AvatarViewModel(avatar: userStyle)
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
private func setupView() {
addSubview(avatarView)
addSubview(labelWrapper)
labelWrapper.addSubview(iconView)
labelWrapper.addSubview(label)
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTapped)))
isUserInteractionEnabled = true
backgroundColor = .clear
ThemeService.shared.addThemeable(themable: self)
}
func applyTheme(theme: Theme) {
labelWrapper.backgroundColor = theme.windowBackgroundColor
}
override func layoutSubviews() {
super.layoutSubviews()
avatarView.pin.width(76).height(60).top((bounds.size.height-103)/2).hCenter()
labelWrapper.pin.width(116).height(43).hCenter().below(of: avatarView).marginTop(10)
iconView.pin.size(32).vCenter()
label.pin.vertically().sizeToFit(.height)
let labelContentWidth = 32 + 4 + label.bounds.size.width
iconView.pin.left((labelWrapper.bounds.size.width-labelContentWidth)/2)
label.pin.right(of: iconView).marginLeft(4)
}
func configure(habiticaClass: HabiticaClass, onSelected: @escaping (() -> Void)) {
self.onSelected = onSelected
switch habiticaClass {
case .warrior:
iconView.image = HabiticaIcons.imageOfWarriorLightBg
label.text = L10n.Classes.warrior
tintColor = UIColor.red10
case .mage:
iconView.image = HabiticaIcons.imageOfMageLightBg
label.text = L10n.Classes.mage
tintColor = UIColor.blue10
case .healer:
iconView.image = HabiticaIcons.imageOfHealerLightBg
label.text = L10n.Classes.healer
tintColor = UIColor.yellow10
case .rogue:
iconView.image = HabiticaIcons.imageOfRogueLightBg
label.text = L10n.Classes.rogue
tintColor = UIColor.purple300
}
}
@objc
private func onTapped() {
if let action = onSelected {
action()
}
}
}