forked from C3-PRO/c3-pro-ios-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudyIntroConfiguration.swift
127 lines (108 loc) · 3.66 KB
/
StudyIntroConfiguration.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
//
// StudyIntroConfiguration.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 Foundation
/**
Intro configuration can be stored to a JSON file and be read by an instance from this class.
A sample configuration, showing a `StudyIntroWelcomeItem`, a `StudyIntroVideoItem` and a `StudyIntroHTMLItem`, might look like this:
{
"title": "C Tracker",
"logo": "logo_institute",
"items": [
{
"type": "welcome",
"title": "Welcome to C\u00a0Tracker",
"subtitle": "A Hepatitis C Research Study",
"video": "CTracker"
},
{
"type": "video",
"video": "CTracker"
},
{
"title": "About this Study",
"filename": "Intro_about",
}
],
"eligibility": {
"letsCheckMessage": "Let's see if you're eligible to join this study",
"eligibleMessage": "Tap the button below to start the consenting process",
"ineligibleMessage": "Thank you for your interest"
}
}
*/
open class StudyIntroConfiguration {
open internal(set) var title: String?
open internal(set) var logoName: String?
open internal(set) var items: [StudyIntroItem]?
open internal(set) var eligibleLetsCheckMessage: String?
open internal(set) var eligibleTitle: String?
open internal(set) var eligibleMessage: String?
open internal(set) var ineligibleMessage: String?
public init(json filename: String, inBundle: Bundle? = nil) throws {
if let url = (inBundle ?? Bundle.main).url(forResource: filename, withExtension: "json"), let data = try? Data(contentsOf: url) {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [AnyHashable: Any]
// top level
if let ttl = json["title"] as? String {
title = ttl
}
if let logo = json["logo"] as? String {
logoName = logo
}
// eligibility
if let eligibility = json["eligibility"] as? [String: String] {
if let text = eligibility["letsCheckMessage"] {
eligibleLetsCheckMessage = text
}
if let text = eligibility["eligibleTitle"] {
eligibleTitle = text
}
if let text = eligibility["eligibleMessage"] {
eligibleMessage = text
}
if let text = eligibility["ineligibleMessage"] {
ineligibleMessage = text
}
}
// items
if let items = json["items"] as? [[String: String]] {
self.items = [StudyIntroItem]()
for item in items {
let type = item["type"] ?? "web"
var intro: StudyIntroItem? = nil
switch type {
case "welcome":
intro = StudyIntroWelcomeItem(title: item["title"] ?? "", subtitle: item["subtitle"], video: item["video"])
case "video":
intro = StudyIntroVideoItem(video: item["video"] ?? "")
default:
intro = StudyIntroHTMLItem(title: item["title"] ?? "", filename: item["filename"] ?? "")
}
self.items?.append(intro!)
}
}
else {
throw C3Error.invalidJSON("No “items” array of dictionaries found")
}
}
else {
throw C3Error.bundleFileNotFound(filename)
}
}
}