on master help sign in
Now displays photos
fda9158ilyakooo0
Melk/CardView.swift+81−8
@@ -12,7 +12,17 @@ class CardView: UIView {
12
12
13 var post: WallPost? {
13 var post: WallPost? {
14 didSet {
14 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 {
16 update()
26 update()
17 }
27 }
18 }
28 }
@@ -28,6 +38,11 @@ class CardView: UIView {
28 fatalError("init(coder:) has not been implemented")
38 fatalError("init(coder:) has not been implemented")
29 }
39 }
30
40
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+
31// private let avatar = UIImage()
46// private let avatar = UIImage()
32// private let name = UIImage()
47// private let name = UIImage()
33
48
@@ -35,7 +50,7 @@ class CardView: UIView {
35 subviews.map {$0.removeFromSuperview()}
50 subviews.map {$0.removeFromSuperview()}
36
51
37 let largeStack = StackView(axis: .vertical, alignment: .left)
52 let largeStack = StackView(axis: .vertical, alignment: .left)
38− largeStack.padding = CGPoint(x: 32, y: 32)
53+ largeStack.padding = padding
39 addSubview(largeStack)
54 addSubview(largeStack)
40
55
41 let userView = StackView(axis: .horizontal, alignment: .center)
56 let userView = StackView(axis: .horizontal, alignment: .center)
@@ -52,7 +67,7 @@ class CardView: UIView {
52 avatar.frame.size = CGSize(width: avatarDim, height: avatarDim)
67 avatar.frame.size = CGSize(width: avatarDim, height: avatarDim)
53 userView.addArrangedSubview(.view(avatar))
68 userView.addArrangedSubview(.view(avatar))
54
69
55− userView.addArrangedSubview(.spacer(8))
70+ userView.addArrangedSubview(.spacer(16))
56
71
57 let userName = UILabel()
72 let userName = UILabel()
58 let firstName = post?.owner?.firstName ?? ""
73 let firstName = post?.owner?.firstName ?? ""
@@ -61,21 +76,79 @@ class CardView: UIView {
61 userName.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightHeavy)
76 userName.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightHeavy)
62 userName.sizeToFit()
77 userName.sizeToFit()
63 userView.addArrangedSubview(.view(userName))
78 userView.addArrangedSubview(.view(userName))
64− userView.frame.origin = CGPoint(x: 16, y: 16)
79+
80+ userView.layoutIfNeeded()
65
81
66 largeStack.addArrangedSubview(.view(userView))
82 largeStack.addArrangedSubview(.view(userView))
67
83
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)
69
132
70 let body = UILabel()
133 let body = UILabel()
71 body.numberOfLines = 0
134 body.numberOfLines = 0
72− let bodyWidth: Double = 300
135+ let bodyWidth: Double = Double(width - padding.x * 2)
73 body.font = UIFont.systemFont(ofSize: 22, weight: UIFontWeightSemibold)
136 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
75 let size = body.textRect(forBounds: CGRect(x: 0, y: 0, width: bodyWidth, height: Double.infinity), limitedToNumberOfLines: 0).size
138 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
77
141
78 largeStack.addArrangedSubview(.view(body))
142 largeStack.addArrangedSubview(.view(body))
79
143
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+
80 }
153 }
81}
154}
Melk/CollectionView.swift+1−1
@@ -68,7 +68,7 @@ class CollectionView: UIView {
68 var firstStack = true
68 var firstStack = true
69
69
70 for view in arrangedSubviews {
70 for view in arrangedSubviews {
71− print("new view")
71+// print("new view")
72 if firstView {
72 if firstView {
73 lastStack.addArrangedSubview(.view(view))
73 lastStack.addArrangedSubview(.view(view))
74 firstView = false
74 firstView = false
Melk/SecondaryViewController.swift+2−1
@@ -34,10 +34,11 @@ class SecondaryView: UIView {
34 }
34 }
35 }
35 }
36
36
37− let card = CardView(frame: CGRect(x: 20, y: 20, width: 600, height: 300))
37+ let card = CardView()
38
38
39 override init(frame: CGRect) {
39 override init(frame: CGRect) {
40 super.init(frame: frame)
40 super.init(frame: frame)
41+ card.frame.origin = CGPoint(x: 20, y: 20)
41 addSubview(card)
42 addSubview(card)
42 }
43 }
43
44
Melk/StreamingParser.swift+1−1
@@ -50,7 +50,7 @@ class StreamingParser {
50 let complete = {
50 let complete = {
51 comp -= 1
51 comp -= 1
52 if comp == 0 {
52 if comp == 0 {
53− print(post.owner)
53+// print(post.owner)
54 completion(.post(post))
54 completion(.post(post))
55 }
55 }
56 }
56 }
Melk/VKData.swift+1−1
@@ -49,7 +49,7 @@ class VKData {
49 }
49 }
50 user.birthday = formatter.date(from: birthday)
50 user.birthday = formatter.date(from: birthday)
51 }
51 }
52− user.avatar = RealmPhoto.by(user.avatarID, surl: json["photo_max"].string)
52+ user.avatar = RealmPhoto.by(user.avatarID, surl: json["photo_100"].string)// was "photo_max"
53 user.photo = RealmPhoto.by(user.photoID, surl: json["photo_max_orig"].string)
53 user.photo = RealmPhoto.by(user.photoID, surl: json["photo_max_orig"].string)
54 if let friendStat = json["friend_status"].int {
54 if let friendStat = json["friend_status"].int {
55 user.friendStatus = FriendStatus(rawValue: friendStat)
55 user.friendStatus = FriendStatus(rawValue: friendStat)
Melk/VKServer.swift+1−1
@@ -81,7 +81,7 @@ class VKServer {
81 let id = user.id
81 let id = user.id
82 let params: Parameters = [
82 let params: Parameters = [
83 "user_ids": id,
83 "user_ids": id,
84− "fields": "photo_max"
84+ "fields": "photo_100"
85 ]
85 ]
86 Alamofire.request(url(for: "users.get"), parameters: params)
86 Alamofire.request(url(for: "users.get"), parameters: params)
87 .validate().responseData { (response) in
87 .validate().responseData { (response) in