-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeTableViewController.swift
179 lines (145 loc) · 6.88 KB
/
HomeTableViewController.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// HomeTableViewController.swift
// BaseConverter
//
// Created by Nathan FALLET on 10/12/2018.
// Copyright © 2018 Nathan FALLET. All rights reserved.
//
import UIKit
import StoreKit
import DonateViewController
class HomeTableViewController: UITableViewController, InputChangedDelegate, DonateViewControllerDelegate {
let bases = [
BaseSection(name: "Convert a number", bases: [.dec, .bin, .oct, .hex]),
BaseSection(name: "Convert a color", bases: [.rgb]),
BaseSection(name: "Convert a text", bases: [.txt])
]
var currents: [Int64] = [0]
var source = Source(base: Base(id: 0, name: "", value: 0, cell: .base), string: "0")
override func viewDidLoad() {
super.viewDidLoad()
// Navigation bar
navigationItem.title = "BaseConverter"
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
}
// Make cells auto sizing
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableView.automaticDimension
// Register cells
tableView.register(LabelTableViewCell.self, forCellReuseIdentifier: "labelCell")
tableView.register(BaseTableViewCell.self, forCellReuseIdentifier: CellType.base.id)
tableView.register(ColorTableViewCell.self, forCellReuseIdentifier: CellType.color.id)
tableView.register(TextTableViewCell.self, forCellReuseIdentifier: CellType.text.id)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Select first cell
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? BaseTableViewCell {
cell.input.field.becomeFirstResponder()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return bases.count + 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == bases.count ? 2 : bases[section].bases.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == bases.count ? "More" : bases[section].name
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// More cells
if indexPath.section == bases.count {
return (tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell).with(text: indexPath.row == 0 ? "Groupe MINASTE" : "Donate", accessory: .disclosureIndicator)
}
// Classic cells
let base = bases[indexPath.section].bases[indexPath.row]
return (tableView.dequeueReusableCell(withIdentifier: base.cell.id, for: indexPath) as! BaseCell).with(base: base, values: currents, source: self.source, delegate: self)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// More cells
if indexPath.section == bases.count {
// Groupe MINASTE
if indexPath.row == 0 {
// Open URL
if let url = URL(string: "https://www.groupe-minaste.org/") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
// Donate
else if indexPath.row == 1 {
// Create a controller
let controller = DonateViewController()
// Set delegate
controller.delegate = self
// Add donations
controller.add(identifier: "me.nathanfallet.BaseConverter.donation1")
controller.add(identifier: "me.nathanfallet.BaseConverter.donation2")
controller.add(identifier: "me.nathanfallet.BaseConverter.donation3")
// Set strings
controller.title = "Donate"
controller.header = "Select an amount to donate:"
controller.footer = "This donation will help our organization to develop its projects, mainly Extopy, the social network we are working on, which will require some storage for its servers."
// Show it
navigationController?.pushViewController(controller, animated: true)
}
}
}
func inputChanged(_ value: String, for base: Base) {
// Convert values
let values = value.components(separatedBy: .whitespacesAndNewlines)
.map({ Int64($0, radix: base.value) })
.filter({ $0 != nil })
.map({ $0! })
// Call with values
inputChanged(values, for: base, source: value)
}
func inputChanged(_ values: [Int64], for base: Base, source: String) {
// Update value
self.currents = values
// Update source
self.source = Source(base: base, string: source.trimmingCharacters(in: .whitespacesAndNewlines))
// Get cells
let cells = (0 ..< bases.count).map({ section in
(0 ..< bases[section].bases.count).map({ row in
tableView.cellForRow(at: IndexPath(row: row, section: section))
})
}).reduce([], +)
// Update tableView
for cell in cells {
// Check cell type and get base
if let cell = cell as? BaseCell, let cbase = cell.base, base.id != cbase.id {
// Update text
cell.with(base: cbase, values: self.currents, source: self.source, delegate: self)
}
}
// Check for review
self.checkForReview()
}
func donateViewController(_ controller: DonateViewController, didDonationSucceed donation: Donation) {
let alert = UIAlertController(title: "Thanks for donating to our organization!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
func donateViewController(_ controller: DonateViewController, didDonationFailed donation: Donation) {
print("Donation failed.")
}
func checkForReview() {
// Check number of conversions to ask for a review
let datas = UserDefaults.standard
let conversionsCount = datas.integer(forKey: "conversionsCount") + 1
datas.set(conversionsCount, forKey: "conversionsCount")
datas.synchronize()
if conversionsCount % 100 == 0 {
if #available(iOS 10.3, *) {
SKStoreReviewController.requestReview()
}
}
}
}