-
Notifications
You must be signed in to change notification settings - Fork 277
/
Utils.swift
237 lines (201 loc) · 8.62 KB
/
Utils.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Utils.swift
// HomeAssistant
//
// Created by Robbie Trencheny on 4/3/16.
// Copyright © 2016 Robbie Trencheny. All rights reserved.
//
import Foundation
import FontAwesomeKit
import Crashlytics
import KeychainAccess
func getIconForIdentifier(_ iconIdentifier: String, iconWidth: Double, iconHeight: Double, color: UIColor) -> UIImage {
if let iconCodes = FontAwesomeKit.FAKMaterialDesignIcons.allIcons() as? [String: String] {
Crashlytics.sharedInstance().setFloatValue(Float(iconWidth), forKey: "iconWidth")
Crashlytics.sharedInstance().setFloatValue(Float(iconHeight), forKey: "iconHeight")
Crashlytics.sharedInstance().setObjectValue(iconIdentifier, forKey: "iconIdentifier")
let fixedIconIdentifier = iconIdentifier.replacingOccurrences(of: ":", with: "-")
let iconCode = iconCodes[fixedIconIdentifier]
CLSLogv("Requesting MaterialDesignIcon: Identifier: %@, Fixed Identifier: %@, Width: %f, Height: %f",
getVaList([iconIdentifier, fixedIconIdentifier, iconWidth, iconHeight]))
let theIcon = FontAwesomeKit.FAKMaterialDesignIcons(code: iconCode, size: CGFloat(iconWidth))
theIcon?.addAttribute(NSAttributedStringKey.foregroundColor.rawValue, value: color)
return theIcon!.image(with: CGSize(width: CGFloat(iconWidth), height: CGFloat(iconHeight)))
} else {
CLSLogv("Error loading Material Design Icons while requesting icon: %@, Width: %f, Height: %f",
getVaList([iconIdentifier, iconWidth, iconHeight]))
return UIImage()
}
}
func colorWithHexString(_ hexString: String, alpha: CGFloat? = 1.0) -> UIColor {
// Convert hex string to an integer
let hexint = Int(intFromHexString(hexString))
let red = CGFloat((hexint & 0xff0000) >> 16) / 255.0
let green = CGFloat((hexint & 0xff00) >> 8) / 255.0
let blue = CGFloat((hexint & 0xff) >> 0) / 255.0
let alpha = alpha!
// Create color object, specifying alpha as well
let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
return color
}
func intFromHexString(_ hexStr: String) -> UInt32 {
var hexInt: UInt32 = 0
// Create scanner
let scanner: Scanner = Scanner(string: hexStr)
// Tell scanner to skip the # character
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
// Scan hex value
scanner.scanHexInt32(&hexInt)
return hexInt
}
// Thanks to http://stackoverflow.com/a/35624018/486182
// Must reboot device after installing new push sounds (http://stackoverflow.com/q/34998278/486182)
// swiftlint:disable:next function_body_length
func movePushNotificationSounds() -> Int {
var movedFiles = 0
let fileManager: FileManager = FileManager()
let libraryPath: URL
do {
libraryPath = try fileManager.url(for: .libraryDirectory,
in: FileManager.SearchPathDomainMask.userDomainMask,
appropriateFor: nil, create: false)
} catch let error as NSError {
print("Error when building URL for library directory", error)
return 0
}
let librarySoundsPath = libraryPath.appendingPathComponent("Sounds")
do {
print("Creating sounds directory at", librarySoundsPath)
try fileManager.createDirectory(at: librarySoundsPath, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print("Error creating /Library/Sounds directory", error)
return 0
}
let documentsPath: URL
do {
documentsPath = try fileManager.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
} catch let error as NSError {
print("Error building documents path URL", error)
return 0
}
let fileList: [URL]
do {
fileList = try fileManager.contentsOfDirectory(at: documentsPath,
includingPropertiesForKeys: nil,
options: FileManager.DirectoryEnumerationOptions())
} catch let error as NSError {
print("Error getting contents of documents directory", error)
return 0
}
for file in fileList {
let finalUrl = librarySoundsPath.appendingPathComponent(file.lastPathComponent)
print("Moving", file, "to", finalUrl)
do {
print("Checking for existence of file")
try fileManager.removeItem(at: finalUrl)
} catch let rmError as NSError {
print("Error removing existing file", rmError)
}
do {
try fileManager.moveItem(at: file, to: finalUrl)
movedFiles += 1
} catch let error as NSError {
print("Error when attempting to move files", error)
}
}
return movedFiles
}
func listAllInstalledPushNotificationSounds() -> [String] {
let fileManager: FileManager = FileManager()
let libraryPath: URL
do {
libraryPath = try fileManager.url(for: .libraryDirectory,
in: FileManager.SearchPathDomainMask.userDomainMask,
appropriateFor: nil,
create: false)
} catch let error as NSError {
print("Error when building URL for library directory", error)
return [String]()
}
let librarySoundsPath = libraryPath.appendingPathComponent("Sounds")
let librarySoundsContents = fileManager.enumerator(at: librarySoundsPath,
includingPropertiesForKeys: nil,
options: FileManager.DirectoryEnumerationOptions(),
errorHandler: nil)!
var allSounds = [String]()
for obj in librarySoundsContents.allObjects {
if let fileUrl = obj as? URL {
allSounds.append(fileUrl.lastPathComponent)
}
}
return allSounds
}
func resetStores() {
do {
try keychain.removeAll()
} catch {
print("Error when trying to delete everything from Keychain!")
}
if let groupDefaults = UserDefaults(suiteName: "group.io.robbie.homeassistant") {
for key in groupDefaults.dictionaryRepresentation().keys {
groupDefaults.removeObject(forKey: key)
}
groupDefaults.synchronize()
}
}
func openURLStringInBrowser(url: String) {
openURLInBrowser(urlToOpen: URL(string: url)!)
}
func openURLInBrowser(urlToOpen: URL) {
if OpenInChromeController.sharedInstance.isChromeInstalled() && prefs.bool(forKey: "openInChrome") {
_ = OpenInChromeController.sharedInstance.openInChrome(urlToOpen, callbackURL: nil)
} else {
if #available(iOS 10, *) {
UIApplication.shared.open(urlToOpen, options: [:], completionHandler: nil)
} else {
_ = UIApplication.shared.openURL(urlToOpen)
}
}
}
func removeSpecialCharsFromString(text: String) -> String {
let okayChars: Set<Character> =
Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890".characters)
return String(text.characters.filter {okayChars.contains($0) })
}
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
func showAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message,
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: L10n.okLabel, style: UIAlertActionStyle.default, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true,
completion: nil)
}
extension UIImage {
func scaledToSize(_ size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
extension String {
subscript (i: Int) -> Character {
return self[self.characters.index(self.startIndex, offsetBy: i)]
}
subscript (i: Int) -> String {
return String(self[i] as Character)
}
}