@@ -25,21 +25,15 @@ import SparrowKit
2525
2626open class ProfileTableViewCell : SPTableViewCell {
2727
28- public let titleLabel = SPLabel ( ) . do {
29- $0. numberOfLines = 1
30- $0. font = UIFont . preferredFont ( forTextStyle: . title2, weight: . semibold)
31- $0. textColor = . label
32- }
28+ // MARK: - Views
3329
34- public let descriptionLabel = SPLabel ( ) . do {
35- $0. numberOfLines = 1
36- $0. font = UIFont . preferredFont ( forTextStyle: . footnote, weight: . regular)
37- $0. textColor = . secondaryLabel
38- }
30+ public let profileLabelsView = ProfileLabelsView ( )
31+
32+ public let authLabelsView = AuthLabelsView ( )
3933
4034 public let avatarView = NativeAvatarView ( ) . do {
4135 $0. isEditable = false
42- $0. placeholderImage = UIImage . system ( " person.crop.circle.fill " , font : . systemFont ( ofSize : 52 , weight : . medium) )
36+ $0. placeholderImage = NativeAvatarView . generatePlaceholderImage ( fontSize : 46 , fontWeight : . medium)
4337 $0. avatarAppearance = . placeholder
4438 }
4539
@@ -48,9 +42,10 @@ open class ProfileTableViewCell: SPTableViewCell {
4842 open override func commonInit( ) {
4943 super. commonInit ( )
5044 higlightStyle = . content
51- contentView. addSubviews ( avatarView, titleLabel , descriptionLabel )
45+ contentView. addSubviews ( avatarView, profileLabelsView , authLabelsView )
5246 accessoryType = . disclosureIndicator
5347 updateAppearance ( )
48+ configureObservers ( )
5449 }
5550
5651 open override func prepareForReuse( ) {
@@ -65,11 +60,12 @@ open class ProfileTableViewCell: SPTableViewCell {
6560
6661 // MARK: - Ovveride
6762
63+ #warning("change to subbiews")
6864 open override func setHighlighted( _ highlighted: Bool , animated: Bool ) {
6965 super. setHighlighted ( highlighted, animated: animated)
7066 let higlightContent = ( higlightStyle == . content)
7167 if higlightContent {
72- [ avatarView, titleLabel , descriptionLabel ] . forEach ( { $0? . alpha = highlighted ? 0.6 : 1 } )
68+ [ avatarView, profileLabelsView , authLabelsView ] . forEach ( { $0? . alpha = highlighted ? 0.6 : 1 } )
7369 }
7470 }
7571
@@ -81,28 +77,24 @@ open class ProfileTableViewCell: SPTableViewCell {
8177 avatarView. setXToSuperviewLeftMargin ( )
8278 avatarView. frame. origin. y = contentView. layoutMargins. top
8379
80+ let visibleLabelsView = profileLabelsView. isHidden ? authLabelsView : profileLabelsView
8481 let avatarRightSpace : CGFloat = NativeLayout . Spaces. default
85- let labelVerticalSpace : CGFloat = NativeLayout . Spaces. step / 2
86- let labelWidth = contentView. layoutWidth - avatarView. frame. width - avatarRightSpace
87- titleLabel. layoutDynamicHeight ( width: labelWidth)
88- descriptionLabel. layoutDynamicHeight ( width: labelWidth)
89-
90- titleLabel. frame. origin. x = avatarView. frame. maxX + avatarRightSpace
91- descriptionLabel. frame. origin. x = titleLabel. frame. origin. x
82+ let labelsWidth = contentView. layoutWidth - avatarView. frame. width - avatarRightSpace
83+ visibleLabelsView. frame. setWidth ( labelsWidth)
84+ visibleLabelsView. sizeToFit ( )
85+ visibleLabelsView. frame. origin. x = avatarView. frame. maxX + avatarRightSpace
9286
93- let labelHeight = titleLabel. frame. height + labelVerticalSpace + descriptionLabel. frame. height
94- if ( avatarView. frame. origin. y + labelHeight) > avatarView. frame. maxY {
95- titleLabel. frame. origin. y = contentView. layoutMargins. top
87+ if ( avatarView. frame. origin. y + visibleLabelsView. frame. height) > avatarView. frame. maxY {
88+ visibleLabelsView. frame. origin. y = contentView. layoutMargins. top
9689 } else {
97- titleLabel . frame. origin. y = contentView. layoutMargins. top + ( contentView. layoutHeight - labelHeight ) / 2
90+ visibleLabelsView . frame. origin. y = contentView. layoutMargins. top + ( contentView. layoutHeight - visibleLabelsView . frame . height ) / 2
9891 }
99-
100- descriptionLabel. frame. origin. y = titleLabel. frame. maxY + labelVerticalSpace
10192 }
10293
10394 open override func sizeThatFits( _ size: CGSize ) -> CGSize {
10495 layoutSubviews ( )
105- return . init( width: size. width, height: max ( avatarView. frame. maxY, descriptionLabel. frame. maxY) + contentView. layoutMargins. bottom)
96+ let visibleLabelsView = profileLabelsView. isHidden ? authLabelsView : profileLabelsView
97+ return . init( width: size. width, height: max ( avatarView. frame. maxY, visibleLabelsView. frame. maxY) + contentView. layoutMargins. bottom)
10698 }
10799
108100 // MARK: - Internal
@@ -124,23 +116,64 @@ open class ProfileTableViewCell: SPTableViewCell {
124116 }
125117
126118 internal func updateAppearance( ) {
127- if ProfileModel . isAuthed, let profileModel = ProfileModel . currentProfile {
128- setProfile ( profileModel, completion: nil )
119+ let profileModel = ProfileModel . currentProfile
120+ authLabelsView. titleLabel. text = Texts . Auth. sign_in
121+ profileLabelsView. titleLabel. text = profileModel? . name ?? profileModel? . email ?? Texts . Profile. placeholder_name
122+
123+ if ProfileModel . isAnonymous ?? true {
124+ avatarView. avatarAppearance = . placeholder
125+ authLabelsView. isHidden = false
126+ profileLabelsView. isHidden = true
129127 } else {
130- setAuthAppearance ( )
128+ guard let profileModel = ProfileModel . currentProfile else { return }
129+ avatarView. setAvatar ( of: profileModel)
130+ authLabelsView. isHidden = true
131+ profileLabelsView. isHidden = false
131132 }
133+
134+ layoutSubviews ( )
132135 }
133136
134- internal func setAuthAppearance( ) {
135- avatarView. avatarAppearance = . placeholder
136- titleLabel. text = Texts . Auth. sign_in
137- titleLabel. textColor = . tint
137+ // MARK: - Views
138+
139+ public class ProfileLabelsView : SPView {
140+
141+ public let titleLabel = SPLabel ( ) . do {
142+ $0. numberOfLines = 1
143+ $0. font = UIFont . preferredFont ( forTextStyle: . title2, weight: . semibold)
144+ $0. textColor = . label
145+ }
146+
147+ public let descriptionLabel = SPLabel ( ) . do {
148+ $0. numberOfLines = 1
149+ $0. font = UIFont . preferredFont ( forTextStyle: . footnote, weight: . regular)
150+ $0. textColor = . secondaryLabel
151+ }
152+
153+ public override func commonInit( ) {
154+ super. commonInit ( )
155+ layoutMargins = . zero
156+ addSubview ( titleLabel)
157+ addSubview ( descriptionLabel)
158+ }
159+
160+ public override func layoutSubviews( ) {
161+ super. layoutSubviews ( )
162+ titleLabel. layoutDynamicHeight ( x: . zero, y: . zero, width: frame. width)
163+ descriptionLabel. layoutDynamicHeight ( x: . zero, y: titleLabel. frame. maxY + 2 , width: frame. width)
164+ }
165+
166+ public override func sizeThatFits( _ size: CGSize ) -> CGSize {
167+ layoutSubviews ( )
168+ return . init( width: size. width, height: descriptionLabel. frame. maxY)
169+ }
138170 }
139171
140- internal func setProfile( _ profileModel: ProfileModel , completion: ( ( ) -> ( ) ) ? = nil ) {
141- titleLabel. text = profileModel. name ?? profileModel. email ?? Texts . Profile. placeholder_name
142- avatarView. setAvatar ( of: profileModel) {
143- completion ? ( )
172+ public class AuthLabelsView : ProfileLabelsView {
173+
174+ public override func commonInit( ) {
175+ super. commonInit ( )
176+ titleLabel. textColor = . tint
144177 }
145178 }
146179}
0 commit comments