Skip to content

Commit 6e894dd

Browse files
committed
implement navbutton to userProfile
1 parent 559165c commit 6e894dd

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

solutions/devsprint-pedro-alvarez-2/FinanceApp.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1CD90C6F29475D1100237BD9 /* ProfileButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90C6E29475D1100237BD9 /* ProfileButtonView.swift */; };
11+
1CD90C7229475E6500237BD9 /* CodeBaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90C7129475E6500237BD9 /* CodeBaseView.swift */; };
12+
1CD90C742947629000237BD9 /* HierarchyRelation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90C732947629000237BD9 /* HierarchyRelation.swift */; };
1013
1DBC0432294176F6000501FA /* TabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DBC0431294176F6000501FA /* TabBarController.swift */; };
1114
98584A6D277E32C30028DBEA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98584A6C277E32C30028DBEA /* AppDelegate.swift */; };
1215
98584A6F277E32C30028DBEA /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98584A6E277E32C30028DBEA /* SceneDelegate.swift */; };
@@ -71,6 +74,9 @@
7174
/* End PBXContainerItemProxy section */
7275

7376
/* Begin PBXFileReference section */
77+
1CD90C6E29475D1100237BD9 /* ProfileButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileButtonView.swift; sourceTree = "<group>"; };
78+
1CD90C7129475E6500237BD9 /* CodeBaseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeBaseView.swift; sourceTree = "<group>"; };
79+
1CD90C732947629000237BD9 /* HierarchyRelation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HierarchyRelation.swift; sourceTree = "<group>"; };
7480
1DBC0431294176F6000501FA /* TabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarController.swift; sourceTree = "<group>"; };
7581
98584A69277E32C30028DBEA /* FinanceApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FinanceApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
7682
98584A6C277E32C30028DBEA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -146,6 +152,23 @@
146152
/* End PBXFrameworksBuildPhase section */
147153

148154
/* Begin PBXGroup section */
155+
1CD90C6D29475CDF00237BD9 /* Subviews */ = {
156+
isa = PBXGroup;
157+
children = (
158+
1CD90C6E29475D1100237BD9 /* ProfileButtonView.swift */,
159+
);
160+
path = Subviews;
161+
sourceTree = "<group>";
162+
};
163+
1CD90C7029475E2700237BD9 /* Commons */ = {
164+
isa = PBXGroup;
165+
children = (
166+
1CD90C7129475E6500237BD9 /* CodeBaseView.swift */,
167+
1CD90C732947629000237BD9 /* HierarchyRelation.swift */,
168+
);
169+
path = Commons;
170+
sourceTree = "<group>";
171+
};
149172
1DBC043029417602000501FA /* TabBar */ = {
150173
isa = PBXGroup;
151174
children = (
@@ -238,6 +261,7 @@
238261
98584A9E277E35700028DBEA /* Screens */ = {
239262
isa = PBXGroup;
240263
children = (
264+
1CD90C7029475E2700237BD9 /* Commons */,
241265
98906BEC29392209001D1975 /* Components */,
242266
98584B08277E602C0028DBEA /* ContactList */,
243267
98584ADF277E50430028DBEA /* Confirmation */,
@@ -252,6 +276,7 @@
252276
98584A9F277E35780028DBEA /* Home */ = {
253277
isa = PBXGroup;
254278
children = (
279+
1CD90C6D29475CDF00237BD9 /* Subviews */,
255280
98584AA6277E35F10028DBEA /* HomeView.swift */,
256281
98584AA4277E35E90028DBEA /* HomeViewController.swift */,
257282
);
@@ -486,6 +511,7 @@
486511
98906BEE2939221B001D1975 /* AccountSummaryView.swift in Sources */,
487512
98584B10277E605F0028DBEA /* ActivityDetailsViewController.swift in Sources */,
488513
98584B20277E60740028DBEA /* ContactListViewController.swift in Sources */,
514+
1CD90C6F29475D1100237BD9 /* ProfileButtonView.swift in Sources */,
489515
98584AF4277E50430028DBEA /* ConfirmationViewController.swift in Sources */,
490516
98C8A4DD27C818A800A630ED /* HomeData.swift in Sources */,
491517
98C8A4D927C8152200A630ED /* String+Extensions.swift in Sources */,
@@ -496,6 +522,7 @@
496522
98906BF42939225B001D1975 /* ContactCellView.swift in Sources */,
497523
98584B14277E605F0028DBEA /* ActivityDetailsView.swift in Sources */,
498524
98584AE7277E50430028DBEA /* TransfersView.swift in Sources */,
525+
1CD90C742947629000237BD9 /* HierarchyRelation.swift in Sources */,
499526
98906BF62939226B001D1975 /* UserProfileHeaderView.swift in Sources */,
500527
98C8A4DB27C815C000A630ED /* UITableViewCell+Extensions.swift in Sources */,
501528
98584AF3277E50430028DBEA /* ConfirmationView.swift in Sources */,
@@ -504,6 +531,7 @@
504531
98584A6F277E32C30028DBEA /* SceneDelegate.swift in Sources */,
505532
98906BF02939223C001D1975 /* ActivityCellView.swift in Sources */,
506533
98584AA7277E35F10028DBEA /* HomeView.swift in Sources */,
534+
1CD90C7229475E6500237BD9 /* CodeBaseView.swift in Sources */,
507535
98584B1F277E60740028DBEA /* ContactListView.swift in Sources */,
508536
1DBC0432294176F6000501FA /* TabBarController.swift in Sources */,
509537
989627E027ADC2F60009A07F /* DebugViewController.swift in Sources */,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// CodeBaseView.swift
3+
// Pods
4+
//
5+
// Created by Pedro Alvarez on 08/12/22.
6+
//
7+
import UIKit
8+
9+
public class CodeBaseView<ViewModel>: UIView {
10+
open var hierarchies: [HierarchyRelation] { [] }
11+
12+
override init(frame: CGRect) {
13+
super.init(frame: frame)
14+
setupHierarchy()
15+
setupConstraints()
16+
}
17+
18+
required init?(coder: NSCoder) {
19+
fatalError("init(coder:) has not been implemented")
20+
}
21+
22+
private var viewModel: ViewModel? {
23+
didSet {
24+
configureView(viewModel)
25+
}
26+
}
27+
28+
public func setupViewModel(_ viewModel: ViewModel) {
29+
self.viewModel = viewModel
30+
}
31+
32+
private func setupHierarchy() {
33+
for relation in hierarchies {
34+
for subview in relation.subviews {
35+
if let stackView = relation.parentView as? UIStackView {
36+
stackView.addArrangedSubview(subview)
37+
} else {
38+
relation.parentView.addSubview(subview)
39+
}
40+
}
41+
}
42+
}
43+
44+
private func setupConstraints() {
45+
NSLayoutConstraint.activate(constraints)
46+
}
47+
48+
open func configureView(_ viewModel: ViewModel?) { }
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HierarchyRelation.swift
3+
// FinanceApp
4+
//
5+
// Created by Gustavo Rigor on 12/12/22.
6+
//
7+
8+
import UIKit
9+
10+
public struct HierarchyRelation {
11+
let parentView: UIView
12+
let subviews: [UIView]
13+
}

solutions/devsprint-pedro-alvarez-2/FinanceApp/Screens/Home/HomeViewController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class HomeViewController: UIViewController {
2121

2222
navigationItem.title = "Finance App 💰"
2323
navigationController?.navigationBar.prefersLargeTitles = true
24+
setupProfileImage()
2425

2526
service.fetchHomeData { homeData in
2627

@@ -40,4 +41,11 @@ class HomeViewController: UIViewController {
4041
override func loadView() {
4142
self.view = homeView
4243
}
44+
45+
private func setupProfileImage() {
46+
let barButton = UIBarButtonItem()
47+
barButton.customView = ProfileButtonView()
48+
self.navigationItem.rightBarButtonItem = barButton
49+
}
50+
4351
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// ProfileButtonView.swift
3+
// FinanceApp
4+
//
5+
// Created by Gustavo Rigor on 12/12/22.
6+
//
7+
8+
import UIKit
9+
10+
class ProfileButtonView: CodeBaseView<ProfileButtonView.ViewModel> {
11+
12+
//MARK: - Custom Data
13+
struct ViewModel {
14+
let image: String
15+
16+
init(image: String) {
17+
self.image = image
18+
}
19+
}
20+
21+
enum Constants {
22+
static let imageName: String = "avatar-placeholder"
23+
static let imageHeight: CGFloat = 36
24+
static let imageWidth: CGFloat = 36
25+
static let half: CGFloat = 0.5
26+
}
27+
28+
private lazy var profileButton: UIButton = {
29+
let button = UIButton()
30+
button.frame = CGRectMake(.zero, .zero, Constants.imageHeight, Constants.imageWidth)
31+
let image = UIImage(named: Constants.imageName)
32+
if let image = image {
33+
UIGraphicsBeginImageContextWithOptions(button.frame.size, false, image.scale)
34+
35+
let rect = CGRectMake(.zero, .zero, button.frame.size.width, button.frame.size.height)
36+
UIBezierPath(roundedRect: rect, cornerRadius: Constants.half * rect.width).addClip()
37+
image.draw(in: rect)
38+
39+
let newImage = UIGraphicsGetImageFromCurrentImageContext()
40+
UIGraphicsEndImageContext()
41+
42+
43+
let color = UIColor(patternImage: newImage!)
44+
button.backgroundColor = color
45+
button.layer.cornerRadius = Constants.half * button.bounds.size.width
46+
}
47+
return button
48+
}()
49+
50+
override init(frame: CGRect) {
51+
super.init(frame: frame)
52+
}
53+
54+
@available(*, unavailable)
55+
required init?(coder: NSCoder) {
56+
fatalError("init(coder:) has not been implemented")
57+
}
58+
59+
//MARK: - View Coding
60+
override public var hierarchies: [HierarchyRelation] {
61+
[
62+
.init(parentView: self, subviews: [profileButton]),
63+
]
64+
}
65+
66+
override public var constraints: [NSLayoutConstraint] {
67+
[
68+
profileButton.topAnchor.constraint(equalTo: topAnchor),
69+
profileButton.leadingAnchor.constraint(equalTo: leadingAnchor),
70+
profileButton.trailingAnchor.constraint(equalTo: trailingAnchor),
71+
profileButton.bottomAnchor.constraint(equalTo: bottomAnchor)
72+
]
73+
}
74+
75+
override public func configureView(_ viewModel: ViewModel?) {
76+
// Configure
77+
}
78+
79+
}

0 commit comments

Comments
 (0)