-
Notifications
You must be signed in to change notification settings - Fork 43
implement navbutton to userProfile #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// CodeBaseView.swift | ||
// Pods | ||
// | ||
// Created by Pedro Alvarez on 08/12/22. | ||
// | ||
import UIKit | ||
|
||
public class CodeBaseView<ViewModel>: UIView { | ||
|
||
open var hierarchies: [HierarchyRelation] { [] } | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setup() | ||
} | ||
|
||
@available(*,unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
private func setup() { | ||
setupHierarchy() | ||
setupConstraints() | ||
} | ||
|
||
private var viewModel: ViewModel? { | ||
didSet { | ||
configureView(viewModel) | ||
} | ||
} | ||
|
||
public func setupViewModel(_ viewModel: ViewModel) { | ||
self.viewModel = viewModel | ||
} | ||
|
||
private func setupHierarchy() { | ||
for relation in hierarchies { | ||
for subview in relation.subviews { | ||
if let stackView = relation.parentView as? UIStackView { | ||
stackView.addArrangedSubview(subview) | ||
} else { | ||
relation.parentView.addSubview(subview) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func setupConstraints() { | ||
NSLayoutConstraint.activate(constraints) | ||
} | ||
|
||
open func configureView(_ viewModel: ViewModel?) { } | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// HierarchyRelation.swift | ||
// FinanceApp | ||
// | ||
// Created by Gustavo Rigor on 12/12/22. | ||
// | ||
|
||
import UIKit | ||
|
||
public struct HierarchyRelation { | ||
let parentView: UIView | ||
let subviews: [UIView] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// ProfileButtonView.swift | ||
// FinanceApp | ||
// | ||
// Created by Gustavo Rigor on 12/12/22. | ||
// | ||
|
||
import UIKit | ||
|
||
final class ProfileButtonView: CodeBaseView<String> { | ||
|
||
enum Constants { | ||
static let imageName: String = "avatar-placeholder" | ||
static let imageHeight: CGFloat = 36 | ||
static let imageWidth: CGFloat = 36 | ||
static let half: CGFloat = 0.5 | ||
} | ||
|
||
private lazy var profileButton: UIButton = { | ||
let button = UIButton() | ||
button.translatesAutoresizingMaskIntoConstraints = false | ||
return button | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
//MARK: - View Coding | ||
override public var hierarchies: [HierarchyRelation] { | ||
[ | ||
.init(parentView: self, subviews: [profileButton]), | ||
] | ||
} | ||
|
||
override public var constraints: [NSLayoutConstraint] { | ||
[ | ||
profileButton.topAnchor.constraint(equalTo: topAnchor), | ||
profileButton.trailingAnchor.constraint(equalTo: trailingAnchor), | ||
profileButton.widthAnchor.constraint(equalToConstant: Constants.imageWidth), | ||
profileButton.heightAnchor.constraint(equalToConstant: Constants.imageHeight), | ||
profileButton.bottomAnchor.constraint(equalTo: bottomAnchor) | ||
] | ||
} | ||
|
||
override public func configureView(_ viewModel: String?) { | ||
let image = UIImage(named: viewModel ?? Constants.imageName) | ||
DispatchQueue.main.async { | ||
if let image = image { | ||
UIGraphicsBeginImageContextWithOptions(self.profileButton.frame.size, false, image.scale) | ||
let rect = CGRectMake(.zero, .zero, self.profileButton.frame.size.width, self.profileButton.frame.size.height) | ||
UIBezierPath(roundedRect: rect, cornerRadius: Constants.half * rect.width).addClip() | ||
image.draw(in: rect) | ||
let newImage = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
let color = UIColor(patternImage: newImage!) | ||
self.profileButton.backgroundColor = color | ||
self.profileButton.layer.cornerRadius = Constants.half * self.profileButton.bounds.size.width | ||
} | ||
} | ||
|
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opa @GusRigor, poderiamos remover esse espaço a mais ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. É uma prática que eu curto fazer. (Coisa de maluco). A ideia é ter uma linha a mais no começo e no final para fica mais fácil de ver até onde é classe até onde é implementação de função. Me ajuda na hora de bater o olho e ver o código verticalmente. Mas se quiserem eu tiro. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh boa!! Olha, por mim pode deixar |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Como é um conteúdo variável, pode ser injetado como propriedade