@@ -12,7 +12,17 @@ class CardView: UIView {
1212
1313 var post: WallPost? {
1414 didSet {
15− print(post?.owner)
15+// print(post?.owner)
16+ update()
17+ }
18+ }
19+ var width: CGFloat = 300 {
20+ didSet {
21+ update()
22+ }
23+ }
24+ var height: CGFloat = 400 {
25+ didSet {
1626 update()
1727 }
1828 }
@@ -28,6 +38,11 @@ class CardView: UIView {
2838 fatalError("init(coder:) has not been implemented")
2939 }
3040
41+ private let padding = CGPoint(x: 32, y: 32)
42+ private let vPadding1: CGFloat = 16
43+ private let vPadding2: CGFloat = 16
44+ private let minTextHeight: CGFloat = 50
45+
3146// private let avatar = UIImage()
3247// private let name = UIImage()
3348
@@ -35,7 +50,7 @@ class CardView: UIView {
3550 subviews.map {$0.removeFromSuperview()}
3651
3752 let largeStack = StackView(axis: .vertical, alignment: .left)
38− largeStack.padding = CGPoint(x: 32, y: 32)
53+ largeStack.padding = padding
3954 addSubview(largeStack)
4055
4156 let userView = StackView(axis: .horizontal, alignment: .center)
@@ -52,7 +67,7 @@ class CardView: UIView {
5267 avatar.frame.size = CGSize(width: avatarDim, height: avatarDim)
5368 userView.addArrangedSubview(.view(avatar))
5469
55− userView.addArrangedSubview(.spacer(8))
70+ userView.addArrangedSubview(.spacer(16))
5671
5772 let userName = UILabel()
5873 let firstName = post?.owner?.firstName ?? ""
@@ -61,21 +76,79 @@ class CardView: UIView {
6176 userName.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightHeavy)
6277 userName.sizeToFit()
6378 userView.addArrangedSubview(.view(userName))
64− userView.frame.origin = CGPoint(x: 16, y: 16)
79+
80+ userView.layoutIfNeeded()
6581
6682 largeStack.addArrangedSubview(.view(userView))
6783
68− largeStack.addArrangedSubview(.spacer(16))
84+ largeStack.addArrangedSubview(.spacer(vPadding1))
85+
86+
87+ let photoView = CollectionView()
88+ photoView.alignment = .top
89+ photoView.horizontalPadding = 8
90+ photoView.verticalPadding = 8
91+ photoView.padding = CGPoint.zero
92+ var maxPhotoDim: CGFloat = 150
93+ if let attachments = post?.attachments {
94+ for attachment in attachments {
95+ if let v = attachment.value {
96+ switch v {
97+ case .photo(let photo):
98+ if var pWidth = photo.width.value >>> {CGFloat($0)},
99+ var pHeight = photo.height.value >>> {CGFloat($0)} {
100+ let photoV = UIImageView()
101+ photo.image?.load(into: photoV)
102+ var k: CGFloat!
103+ if pWidth > pHeight {
104+ k = CGFloat(pWidth) / maxPhotoDim
105+ } else {
106+ k = CGFloat(pHeight) / maxPhotoDim
107+ }
108+ pWidth /= k
109+ pHeight /= k
110+ photoV.frame.size = CGSize(width: pWidth, height: pHeight)
111+ photoView.addArrangedSubview(photoV)
112+ }
113+
114+ default:
115+ break // TODO: Handle other data types
116+ }
117+ }
118+ }
119+ }
120+ photoView.layoutIfNeeded()
121+
122+ var textHeight = height
123+ textHeight -= padding.y + vPadding1 + userView.frame.height
124+ if let count = post?.attachments.count{
125+ if count > 0 {
126+ textHeight -= vPadding2 + photoView.frame.height
127+ }
128+ }
129+ print(textHeight)
130+ textHeight = max(textHeight, minTextHeight)
131+ print(textHeight)
69132
70133 let body = UILabel()
71134 body.numberOfLines = 0
72− let bodyWidth: Double = 300
135+ let bodyWidth: Double = Double(width - padding.x * 2)
73136 body.font = UIFont.systemFont(ofSize: 22, weight: UIFontWeightSemibold)
74− body.text = post?.body ?? "Spicy jalapeno flank landjaeger sausage tongue tail fatback frankfurter. Meatball tongue tail short ribs pastrami. Short ribs chicken pig tenderloin sausage pork. Corned beef t-bone pancetta jerky chuck picanha venison, turducken tenderloin shoulder boudin drumstick hamburger. Pork meatball meatloaf prosciutto pig andouille. Sirloin bacon hamburger short ribs pork cupim shank biltong turducken bresaola kevin prosciutto shoulder meatball. Strip steak frankfurter pork chop biltong jowl beef rump venison pancetta turkey tongue."
137+ body.text = post?.body
75138 let size = body.textRect(forBounds: CGRect(x: 0, y: 0, width: bodyWidth, height: Double.infinity), limitedToNumberOfLines: 0).size
76− body.frame.size = size
139+ body.frame.size.height = min(size.height, textHeight)
140+ body.frame.size.width = size.width
77141
78142 largeStack.addArrangedSubview(.view(body))
79143
144+ if let count = post?.attachments.count{
145+ if count > 0 {
146+ largeStack.addArrangedSubview(.spacer(vPadding2))
147+ largeStack.addArrangedSubview(.view(photoView))
148+ }
149+ }
150+
151+ frame.size = CGSize(width: width, height: height)
152+
80153 }
81154}