-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
426 lines (264 loc) · 11.6 KB
/
ViewController.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// ViewController.swift
// Baby in car check IOS
//
// Created by Areeb Waseem on 6/5/18.
// Copyright © 2018 Areeb Waseem. All rights reserved.
//
import UIKit
import CoreLocation
import UserNotifications
import AVFoundation
import MediaPlayer
import QuartzCore
class ViewController: UIViewController, CLLocationManagerDelegate,AVAudioPlayerDelegate {
var audio_plauyer : AVAudioPlayer!
@IBOutlet weak var baby_in_car_check_logo: UILabel!
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(uuidString: "27cf161a-df24-43f2-a5a2-049dacf221d1")! as UUID, identifier: "Estimots")
@IBOutlet weak var next_button_rounded: UIButton!
@IBOutlet weak var starter_congo_view: UIView!
@IBOutlet weak var sunny_view: UIImageView!
@IBAction func next_button(_ sender: UIButton) {
performSegue(withIdentifier: "first_to_second_segue", sender: self)
}
@IBAction func dismiss_button(_ sender: UIButton) {
starter_congo_view.isHidden = true
}
@IBAction func stop_monitoring(_ sender: UIButton) {
/*
let sound_url = Bundle.main.url(forResource: "alarm_tone", withExtension: "mp3")
do {
audio_plauyer = try AVAudioPlayer(contentsOf: sound_url!)
}catch{
print(error)
}
do {
// try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
/*
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.duckOthers, .defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.beginReceivingRemoteControlEvents()
*/
// Removed deprecated use of AVAudioSessionDelegate protocol
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
// try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print(error)
// report for an error
}
MPVolumeView.setVolume(0.5)
// player.play()
audio_plauyer.play()
*/
DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
self.play_sound()
})
// play_sound()
}
@IBAction func start_monitoring(_ sender: UIButton) {
start_tasks()
}
@IBAction func first_button(_ sender: UIButton) {
// call_notif()
}
var is_true = false
override func viewDidAppear(_ animated: Bool) {
print("appeared")
if (isKeyPresentInUserDefaults(key: "is_first_set"))
{
if (UserDefaults.standard.bool(forKey: "is_first_set"))
{
UserDefaults.standard.set(false, forKey: "is_first_set")
performSegue(withIdentifier: "first_to_congo_segue", sender: self)
}else{
performSegue(withIdentifier: "first_to_main_segue", sender: self)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
baby_in_car_check_logo.layer.borderColor = UIColor.blue.cgColor
baby_in_car_check_logo.layer.borderWidth = 2.5
baby_in_car_check_logo.layer.cornerRadius = 10.0
baby_in_car_check_logo.layer.shadowColor = UIColor.black.cgColor
baby_in_car_check_logo.layer.shadowRadius = 0.4
baby_in_car_check_logo.layer.shadowOpacity=0.2
baby_in_car_check_logo.layer.shadowOffset = CGSize(width: 0.5, height: 1.5)
next_button_rounded.layer.borderColor = UIColor.blue.cgColor
next_button_rounded.layer.borderWidth = 2.5
next_button_rounded.layer.cornerRadius = 10.0
next_button_rounded.layer.shadowColor = UIColor.black.cgColor
next_button_rounded.layer.shadowRadius = 0.4
next_button_rounded.layer.shadowOpacity=0.2
next_button_rounded.layer.shadowOffset = CGSize(width: 0.5, height: 1.5)
rotateView(targetView: sunny_view , duration: 10)
/*
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedAlways)
{
locationManager.requestAlwaysAuthorization()
}
else{
// self.region.notifyEntryStateOnDisplay = true
// locationManager.startMonitoringVisits()
}
*/
}
func isKeyPresentInUserDefaults(key: String) -> Bool {
return UserDefaults.standard.object(forKey: key) != nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("I am in")
// call_notif()
play_sound()
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("I left")
// call_notif()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error monitoring")
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("monitoring started")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
// print("det state")
// call_notif()
/*
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let content = UNMutableNotificationContent()
content.title = "bla bla"
content.subtitle = "blew blew"
content.body = "sjdnfkdsjnknfjkdsnf"
let request = UNNotificationRequest(identifier: "custom notification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if (error != nil)
{
// print("error with notification")
}
else{
//print("Noti done")
}
}
*/
/*
if (state == .inside)
{
print("i am inside by state")
}
else{
print("I ditermined state")
}
*/
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
// print(beacons)
/*
print("monitored region")
if (!is_true)
{
is_true=true
print("first time")
locationManager.startMonitoring(for: region)
}
*/
/*
let knownBeacons = beacons.filter{$0.proximity != CLProximity.unknown}
if (knownBeacons.count>0)
{
let closestBeacon = knownBeacons[0] as CLBeacon
print(knownBeacons)
}
*/
}
func start_tasks() {
self.region.notifyOnEntry = true
self.region.notifyOnExit = true
// self.region.notifyEntryStateOnDisplay = true
//
locationManager.pausesLocationUpdatesAutomatically=false
if (CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self))
{
print("yes available")
// locationManager.startMonitoringSignificantLocationChanges()
if (locationManager.monitoredRegions.contains(region))
{
// locationManager.stopUpdatingLocation()
//locationManager.stopMonitoringVisits()
locationManager.stopRangingBeacons(in: region)
locationManager.stopMonitoring(for: region)
// locationManager.stopUpdatingLocation()
print("already there")
// print(<#T##items: Any...##Any#>)
}
else{
// locationManager.startUpdatingLocation()
// locationManager.startMonitoringVisits()
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates=true
} else {
}
// locationManager.startUpdatingLocation()
locationManager.startRangingBeacons(in: region)
locationManager.startMonitoring(for: region)
}
// locationManager.startMonitoring(for: region)
//locationManager.stopMonitoring(for: region)
}
else{
print("not available")
}
}
func play_sound() {
let sound_url = Bundle.main.url(forResource: "alarm_tone", withExtension: "mp3")
do {
audio_plauyer = try AVAudioPlayer(contentsOf: sound_url!)
do {
// try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
/*
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.duckOthers, .defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.beginReceivingRemoteControlEvents()
*/
// Removed deprecated use of AVAudioSessionDelegate protocol
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.duckOthers, .defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.beginReceivingRemoteControlEvents()
// MPVolumeView.setVolume(1)
print("I started my playing")
//audio_plauyer.play()
if audio_plauyer.prepareToPlay() &&
audio_plauyer.play(){
print("Successfully started playing")
} else {
print("Failed to play")
}
//try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print("Error from setting session", error)
// report for an error
}
}catch{
print("error from creating audio player")
}
// player.play()
//
}
private func rotateView(targetView: UIView, duration: Double = 1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
}