forked from tvidenov/appstore-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDetailController.swift
116 lines (81 loc) · 4.53 KB
/
AppDetailController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// AppDetailController.swift
// AppStore
//
// Created by Tihomir Videnov on 11/9/16.
// Copyright © 2016 Tihomir Videnov. All rights reserved.
//
import UIKit
class AppDetailViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var app: App? {
didSet {
if app?.screenshots != nil {
return
}
if let id = app?.id {
ApiService.sharedInstance.fetchAppDetails(id: Int(id), completionHandler: { (appDetail) in
self.app = appDetail
self.collectionView?.reloadData()
})
}
}
}
fileprivate let headerId = "headerId"
fileprivate let cellId = "cellId"
fileprivate let descriptionCell = "descriptionId"
fileprivate let moreInfoCell = "moreInfoCell"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.alwaysBounceVertical = true
collectionView?.backgroundColor = .white
collectionView?.register(AppDetailHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader , withReuseIdentifier: headerId)
collectionView?.register(ScreenshotsCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.register(AppDetailDescription.self, forCellWithReuseIdentifier: descriptionCell)
collectionView?.register(AppMoreInformation.self, forCellWithReuseIdentifier: moreInfoCell)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: descriptionCell, for: indexPath) as! AppDetailDescription
cell.textView.attributedText = descriptionAttributedText()
return cell
}
if indexPath.item == 2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: moreInfoCell, for: indexPath) as! AppMoreInformation
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ScreenshotsCell
cell.app = app
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 1 {
let dummySize = CGSize(width: view.frame.width - 8 - 8, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin)
let rect = descriptionAttributedText().boundingRect(with: dummySize, options: options, context: nil)
return CGSize(width: view.frame.width, height: rect.height + 30)
}
return CGSize(width: view.frame.width, height: 170)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! AppDetailHeader
header.app = app
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 170)
}
fileprivate func descriptionAttributedText() -> NSAttributedString {
let attributedText = NSMutableAttributedString(string: "Description\n", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)])
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
let range = NSMakeRange(0, attributedText.string.characters.count)
attributedText.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
if let desc = app?.desc {
attributedText.append(NSAttributedString(string: desc, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: UIColor.darkGray]))
}
return attributedText
}
}