forked from C3-PRO/c3-pro-ios-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudyIntroCollectionViewCell.swift
154 lines (118 loc) · 3.77 KB
/
StudyIntroCollectionViewCell.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
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
//
// StudyIntroCollectionViewCell.swift
// C3PRO
//
// Created by Pascal Pfiffner on 20/10/15.
// Copyright © 2015 Boston Children's Hospital. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
open class StudyIntroWelcomeCell: UICollectionViewCell {
@IBOutlet open var image: UIImageView?
@IBOutlet open var titleLabel: UILabel?
@IBOutlet open var subtitleLabel: UILabel?
@IBOutlet open var consentButton: UIButton?
@IBOutlet open var videoButton: UIButton?
@IBOutlet open var swipeLabel: UILabel?
open var onConsentTap: (() -> Void)?
open var onVideoTap: ((_ name: String) -> Void)?
open var item: StudyIntroWelcomeItem? {
didSet {
setupCell(with: item)
}
}
override open func prepareForReuse() {
super.prepareForReuse()
titleLabel?.numberOfLines = (bounds.size.height > 280.0) ? 0 : 1; // to force one line on iPhone 4S
}
func setupCell(with item: StudyIntroWelcomeItem?) {
if let item = item {
if let logo = item.logoName {
image?.image = UIImage(named: logo)
}
titleLabel?.text = item.title
subtitleLabel?.text = item.subtitle
}
videoButton?.isEnabled = (nil != item?.videoName)
videoButton?.isHidden = (nil == item?.videoName)
}
// MARK: - Actions
@IBAction func showConsent() {
if let exec = onConsentTap {
exec()
}
else {
c3_warn("Have not yet assigned `onConsentTap`")
}
}
@IBAction func showVideo() {
if let exec = onVideoTap, let video = item?.videoName {
exec(video)
}
else {
c3_warn("Have not assigned `onVideoTap` or the welcome item does not define `videoName`")
}
}
}
open class StudyIntroVideoCell: UICollectionViewCell {
@IBOutlet open var titleLabel: UILabel?
@IBOutlet open var videoButton: UIButton?
@IBOutlet open var videoMessage: UILabel?
open var onVideoTap: ((String) -> Void)?
open var item: StudyIntroVideoItem? {
didSet {
setupCell(with: item)
}
}
func setupCell(with item: StudyIntroVideoItem?) {
if let item = item {
videoButton?.setImage(UIImage(named: item.videoIconName), for: UIControlState())
}
videoButton?.isEnabled = (nil != item?.videoName)
}
@IBAction func showVideo() {
if let exec = onVideoTap, let video = item?.videoName {
exec(video)
}
else {
c3_warn("Have not assigned `onVideoTap` or the video item does not define `videoName`")
}
}
}
open class StudyIntroHTMLCell: UICollectionViewCell, UIWebViewDelegate {
@IBOutlet open var webView: UIWebView?
open var onPDFLinkTap: ((URL) -> Bool)?
open var item: StudyIntroHTMLItem? {
didSet {
setupCell(with: item)
}
}
func setupCell(with item: StudyIntroHTMLItem?) {
if let url = item?.url {
webView?.loadRequest(URLRequest(url: url as URL))
}
}
open func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if .linkClicked == navigationType, let url = request.url {
if "file" == url.scheme && "pdf" == url.pathExtension, let exec = onPDFLinkTap {
return exec(url)
}
return !UIApplication.shared.openURL(url)
}
return true
}
open func webViewDidFinishLoad(_ webView: UIWebView) {
webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect=\"none\"") // disable text selection
}
}