on master help sign in
Now displays photos
commitfda9158bd50c38cdef8bc31a8084181bf395c0e7 authorilyakooo0 <ilyakooo0@gmail.com> dateWed 19 Jul 2017, 22:41 (9 years ago) parent317137e
6 files changed+87−13
Melk/CardView.swift+81−8 Melk/CollectionView.swift+1−1 Melk/SecondaryViewController.swift+2−1 Melk/StreamingParser.swift+1−1 Melk/VKData.swift+1−1 Melk/VKServer.swift+1−1
Melk/CardView.swift+81−8
@@ -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}
Melk/CollectionView.swift+1−1
@@ -68,7 +68,7 @@ class CollectionView: UIView {
6868 var firstStack = true
6969
7070 for view in arrangedSubviews {
71− print("new view")
71+// print("new view")
7272 if firstView {
7373 lastStack.addArrangedSubview(.view(view))
7474 firstView = false
Melk/SecondaryViewController.swift+2−1
@@ -34,10 +34,11 @@ class SecondaryView: UIView {
3434 }
3535 }
3636
37− let card = CardView(frame: CGRect(x: 20, y: 20, width: 600, height: 300))
37+ let card = CardView()
3838
3939 override init(frame: CGRect) {
4040 super.init(frame: frame)
41+ card.frame.origin = CGPoint(x: 20, y: 20)
4142 addSubview(card)
4243 }
4344
Melk/StreamingParser.swift+1−1
@@ -50,7 +50,7 @@ class StreamingParser {
5050 let complete = {
5151 comp -= 1
5252 if comp == 0 {
53− print(post.owner)
53+// print(post.owner)
5454 completion(.post(post))
5555 }
5656 }
Melk/VKData.swift+1−1
@@ -49,7 +49,7 @@ class VKData {
4949 }
5050 user.birthday = formatter.date(from: birthday)
5151 }
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"
5353 user.photo = RealmPhoto.by(user.photoID, surl: json["photo_max_orig"].string)
5454 if let friendStat = json["friend_status"].int {
5555 user.friendStatus = FriendStatus(rawValue: friendStat)
Melk/VKServer.swift+1−1
@@ -81,7 +81,7 @@ class VKServer {
8181 let id = user.id
8282 let params: Parameters = [
8383 "user_ids": id,
84− "fields": "photo_max"
84+ "fields": "photo_100"
8585 ]
8686 Alamofire.request(url(for: "users.get"), parameters: params)
8787 .validate().responseData { (response) in