-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTechnologyStackDetailViewController.swift
More file actions
159 lines (130 loc) · 6.52 KB
/
Copy pathTechnologyStackDetailViewController.swift
File metadata and controls
159 lines (130 loc) · 6.52 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// TechnologyStackDetailViewController.swift
// TechStacks
//
// Created by Demis Bellot on 2/5/15.
// Copyright (c) 2015 ServiceStack LLC. All rights reserved.
//
import UIKit
import Foundation
class TechnologyStackDetailViewController : UIViewController {
var slug:String!
var goBackToTab:MainTab?
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblDescription: UITextView!
@IBOutlet weak var imgScreenshot: UIImageView!
@IBOutlet var scrollView: UIScrollView!
var result:TechStackDetails?
@IBAction func btnAppUrlGo(_ sender: AnyObject) {
if result?.appUrl != nil {
if let url = URL(string: result!.appUrl!) {
UIApplication.shared.openURL(url)
}
}
}
override func viewWillDisappear(_ animated: Bool) {
if goBackToTab != nil {
self.storyboard?.switchTab(goBackToTab!)
}
}
override func viewDidLoad() {
let name = slug.replace("-", withString: " ")
self.title = "loading \(name)..."
lblName.text = "loading \(name)..."
appData.loadTechnologyStack(slug)
.then { r in
if let result = r.result {
self.result = result
self.title = "TechStack"
self.lblName.text = result.name
self.lblDescription.text = result.Description
self.calculateLayout()
self.imgScreenshot.loadAsync(result.screenshotUrl, withSize:self.imgScreenshot.frame.size)
self.loadTechnologies(r.result!.technologyChoices)
}
}
}
func calculateLayout() {
let pad = Style.padding
let fullWidth = view.frame.width
let innerWidth = fullWidth - (pad * 2)
lblName.frame = CGRect(x: pad, y: pad, width: innerWidth, height: Style.titleSize.lineHeight)
lblName.font = lblName.font.withSize(Style.titleSize)
lblDescription.frame = CGRect(x: pad, y:pad + lblName.frame.size.height + pad, width:innerWidth, height: Style.detailSize.lineHeight)
if let f = lblDescription.font?.withSize(Style.detailSize) {
lblDescription.font = f
}
lblDescription.sizeToFit()
imgScreenshot.frame = CGRect(
x: (view.frame.size.width - Style.screenshotWidth) / 2,
y: lblDescription.frame.origin.y + lblDescription.frame.height + pad,
width: Style.screenshotWidth,
height: Style.screenshotHeight)
let btnAppUrl = UIButton(type: UIButtonType.system)
btnAppUrl.frame = CGRect(x: imgScreenshot.frame.origin.x,
y: imgScreenshot.frame.origin.y + imgScreenshot.frame.size.height,
width: imgScreenshot.frame.width,
height: Style.detailSize.lineHeight)
btnAppUrl.setTitle(result!.appUrl?.toHumanFriendlyUrl(), for: UIControlState())
btnAppUrl.addTarget(self, action: #selector(TechnologyStackDetailViewController.btnAppUrlGo(_:)), for: .touchUpInside)
scrollView.addSubview(btnAppUrl)
}
var techSlugs = [String]()
func loadTechnologies(_ techChoices:[TechnologyInStack]) {
let imageUrls = techChoices.filter { $0.logoUrl != nil }.map { $0.logoUrl! }
let fullWidth = self.view.frame.width
self.appData.loadAllImagesAsync(imageUrls)
.then { (images:[String:UIImage?]) -> Void in
let pad = Style.padding
let btnAppUrlSize:CGFloat = 20
var startPos = self.imgScreenshot.frame.origin.y + self.imgScreenshot.frame.size.height + pad + btnAppUrlSize
for tier in self.appData.allTiers {
let techologiesInTier = techChoices.filter { $0.tier == tier.value }
if techologiesInTier.count > 0 {
startPos += pad
let title = UILabel(frame: CGRect(x: pad, y: startPos + pad, width: fullWidth, height: Style.headingSize.lineHeight))
title.font = UIFont(name: title.font.fontName, size: Style.headingSize)
startPos += Style.headingSize
title.text = tier.title
title.textColor = UIColor.lightGray
self.scrollView.addSubview(title)
startPos += pad
var i = 0
for tech in techologiesInTier {
if tech.logoUrl == nil {
continue
}
if let img = images[tech.logoUrl!] {
if img == nil {
continue
}
i += 1
startPos += pad
let x = i % 2 == 1 ? pad : fullWidth / 2 + pad
let imgBtn = UIButton(frame: CGRect(x: x, y: startPos, width: fullWidth / 2 - (2 * pad), height: Style.techLogoHeight))
if i % 2 == 0 {
startPos += Style.techLogoHeight
}
imgBtn.setImage(img!.scaledInto(imgBtn.frame.size), for: UIControlState())
self.techSlugs.append(tech.slug!)
imgBtn.tag = self.techSlugs.count - 1
imgBtn.addTarget(self,
action: #selector(TechnologyStackDetailViewController.onTechnologySelected(_:)),
for: .touchUpInside)
self.scrollView.addSubview(imgBtn)
}
}
if i % 2 == 1 {
startPos += Style.techLogoHeight
}
}
}
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: startPos)
}
}
func onTechnologySelected(_ sender:UIButton) {
let slug = techSlugs[sender.tag]
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
self.navigationController?.openTechnology(slug)
}
}