-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added table cell. Added diffable assets. Clean code.
- Loading branch information
1 parent
767263e
commit a70c627
Showing
10 changed files
with
252 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Sources/SPProfiling/Interface/Profile/Table/CellProvider+Profile.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// The MIT License (MIT) | ||
// Copyright © 2022 Ivan Vorobei ([email protected]) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import UIKit | ||
import SPDiffable | ||
|
||
extension SPDiffableTableDataSource.CellProvider { | ||
|
||
public static var profile: SPDiffableTableDataSource.CellProvider { | ||
return SPDiffableTableDataSource.CellProvider() { (tableView, indexPath, item) -> UITableViewCell? in | ||
guard let _ = item as? DiffableProfileItem else { return nil } | ||
let cell = tableView.dequeueReusableCell(withClass: ProfileTableViewCell.self, for: indexPath) | ||
return cell | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Sources/SPProfiling/Interface/Profile/Table/DiffableProfileItem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// The MIT License (MIT) | ||
// Copyright © 2022 Ivan Vorobei ([email protected]) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import UIKit | ||
import SPDiffable | ||
|
||
open class DiffableProfileItem: SPDiffableActionableItem { | ||
|
||
public static let id: String = "spprofiling-profile-item" | ||
|
||
public init(action: SPDiffableActionableItem.Action? = nil) { | ||
super.init(id: Self.id, action: action) | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
Sources/SPProfiling/Interface/Profile/Table/ProfileTableViewCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// The MIT License (MIT) | ||
// Copyright © 2022 Ivan Vorobei ([email protected]) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import UIKit | ||
import NativeUIKit | ||
import SparrowKit | ||
|
||
open class ProfileTableViewCell: SPTableViewCell { | ||
|
||
public let titleLabel = SPLabel().do { | ||
$0.numberOfLines = 1 | ||
$0.font = UIFont.preferredFont(forTextStyle: .title2, weight: .semibold) | ||
$0.textColor = .label | ||
} | ||
|
||
public let descriptionLabel = SPLabel().do { | ||
$0.numberOfLines = 1 | ||
$0.font = UIFont.preferredFont(forTextStyle: .footnote, weight: .regular) | ||
$0.textColor = .secondaryLabel | ||
} | ||
|
||
public let avatarView = NativeAvatarView().do { | ||
$0.isEditable = false | ||
$0.placeholderImage = UIImage.system("person.crop.circle.fill", font: .systemFont(ofSize: 52, weight: .medium)) | ||
$0.avatarAppearance = .placeholder | ||
} | ||
|
||
// MARK: - Init | ||
|
||
open override func commonInit() { | ||
super.commonInit() | ||
higlightStyle = .content | ||
contentView.addSubviews(avatarView, titleLabel, descriptionLabel) | ||
accessoryType = .disclosureIndicator | ||
updateAppearance() | ||
} | ||
|
||
open override func prepareForReuse() { | ||
super.prepareForReuse() | ||
configureObservers() | ||
updateAppearance() | ||
} | ||
|
||
deinit { | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
// MARK: - Ovveride | ||
|
||
open override func setHighlighted(_ highlighted: Bool, animated: Bool) { | ||
super.setHighlighted(highlighted, animated: animated) | ||
let higlightContent = (higlightStyle == .content) | ||
if higlightContent { | ||
[avatarView, titleLabel, descriptionLabel].forEach({ $0?.alpha = highlighted ? 0.6 : 1 }) | ||
} | ||
} | ||
|
||
// MARK: - Layout | ||
|
||
open override func layoutSubviews() { | ||
super.layoutSubviews() | ||
avatarView.sizeToFit() | ||
avatarView.setXToSuperviewLeftMargin() | ||
avatarView.frame.origin.y = contentView.layoutMargins.top | ||
|
||
let avatarRightSpace: CGFloat = NativeLayout.Spaces.default | ||
let labelVerticalSpace: CGFloat = NativeLayout.Spaces.step / 2 | ||
let labelWidth = contentView.layoutWidth - avatarView.frame.width - avatarRightSpace | ||
titleLabel.layoutDynamicHeight(width: labelWidth) | ||
descriptionLabel.layoutDynamicHeight(width: labelWidth) | ||
|
||
titleLabel.frame.origin.x = avatarView.frame.maxX + avatarRightSpace | ||
descriptionLabel.frame.origin.x = titleLabel.frame.origin.x | ||
|
||
let labelHeight = titleLabel.frame.height + labelVerticalSpace + descriptionLabel.frame.height | ||
if (avatarView.frame.origin.y + labelHeight) > avatarView.frame.maxY { | ||
titleLabel.frame.origin.y = contentView.layoutMargins.top | ||
} else { | ||
titleLabel.frame.origin.y = contentView.layoutMargins.top + (contentView.layoutHeight - labelHeight) / 2 | ||
} | ||
|
||
descriptionLabel.frame.origin.y = titleLabel.frame.maxY + labelVerticalSpace | ||
} | ||
|
||
open override func sizeThatFits(_ size: CGSize) -> CGSize { | ||
layoutSubviews() | ||
return .init(width: size.width, height: max(avatarView.frame.maxY, descriptionLabel.frame.maxY) + contentView.layoutMargins.bottom) | ||
} | ||
|
||
// MARK: - Internal | ||
|
||
internal func configureObservers() { | ||
// Clean | ||
NotificationCenter.default.removeObserver(self) | ||
|
||
// Configure New | ||
NotificationCenter.default.addObserver(forName: SPProfiling.didChangedAuthState, object: nil, queue: nil) { [weak self] _ in | ||
guard let self = self else { return } | ||
self.updateAppearance() | ||
} | ||
|
||
NotificationCenter.default.addObserver(forName: SPProfiling.didReloadedProfile, object: nil, queue: nil) { [weak self] _ in | ||
guard let self = self else { return } | ||
self.updateAppearance() | ||
} | ||
} | ||
|
||
internal func updateAppearance() { | ||
if ProfileModel.isAuthed, let profileModel = ProfileModel.currentProfile { | ||
setProfile(profileModel, completion: nil) | ||
} else { | ||
setAuthAppearance() | ||
} | ||
} | ||
|
||
internal func setAuthAppearance() { | ||
avatarView.avatarAppearance = .placeholder | ||
titleLabel.text = Texts.Auth.sign_in | ||
titleLabel.textColor = .tint | ||
} | ||
|
||
internal func setProfile(_ profileModel: ProfileModel, completion: (()->())? = nil) { | ||
titleLabel.text = profileModel.name ?? profileModel.email ?? Texts.Profile.placeholder_name | ||
avatarView.setAvatar(of: profileModel) { | ||
completion?() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters