Skip to content

Commit e3a31c1

Browse files
committed
Implemented automatic texting upon data retrieval
- Implemented automatic texting upon data retrieval from crash detector device. - Added functionality to change count down text color. - Made buttons look nicer in VCtest class
1 parent 37ab615 commit e3a31c1

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

iosApp/VC/Controllers/CountdownViewController.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111

1212
class CountdownViewController: UIViewController {
1313

14-
var countdownTimer: Timer?
14+
private var countdownTimer: Timer?
1515

1616
override func viewDidLoad() {
1717
super.viewDidLoad()
@@ -62,8 +62,9 @@ class CountdownViewController: UIViewController {
6262
countDownLabel.centerXAnchor.constraint(equalTo: alertController.view.centerXAnchor).isActive = true
6363
countDownLabel.centerYAnchor.constraint(equalTo: alertController.view.centerYAnchor, constant: 60).isActive = true
6464

65-
// Create the cancel action and add it to the alert controller
65+
var cancelPressed = false
6666
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { [weak self] _ in
67+
cancelPressed = true
6768
self?.dismiss(animated: true, completion: nil)
6869
}
6970
alertController.addAction(cancelAction)
@@ -73,8 +74,26 @@ class CountdownViewController: UIViewController {
7374
let countdownTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
7475
countdownSeconds -= 1
7576
countDownLabel.text = "\(countdownSeconds)"
77+
78+
// Change to text color to red
79+
if countdownSeconds <= 3 {
80+
if let titleString = countDownTitle as? NSString {
81+
let attributes: [NSAttributedString.Key: Any] = [
82+
.foregroundColor: UIColor.red,
83+
.font: UIFont.boldSystemFont(ofSize: 30)
84+
]
85+
let attributedTitle = NSAttributedString(string: titleString as String, attributes: attributes)
86+
alertController.setValue(attributedTitle, forKey: "attributedTitle")
87+
}
88+
countDownLabel.textColor = .red
89+
}
7690
if countdownSeconds == 0 {
7791
timer.invalidate()
92+
if !cancelPressed {
93+
let vcContact = VCContactsViewController()
94+
vcContact.textMessageWithTwilio()
95+
vcContact.callWithTwilio()
96+
}
7897
self.dismiss(animated: true, completion: nil)
7998
}
8099
}

iosApp/VC/Controllers/VCHomeViewController.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
1515
private var peripheralStatusLabel: UILabel!
1616
private let bluetoothManager = BluetoothManager()
1717
private var dataFromAdafruit: UILabel!
18+
private var countdownViewController = CountdownViewController()
1819
/**
1920
This method is called after the view controller has loaded its view hierarchy into memory.
2021
*/
@@ -48,10 +49,14 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
4849
BluetoothManager.shared.startScanning()
4950
print("Device started in console")
5051

51-
/* Fake retrieving data
52-
let fakeData = "Fake data".data(using: .utf8)!
53-
didReceiveData(fakeData)
54-
*/
52+
// //Fake retrieving data
53+
// let fakeData = "Fake data".data(using: .utf8)!
54+
// didReceiveData(fakeData)
55+
56+
// Instantiate countdown view controller and add it as a child view controller
57+
countdownViewController = CountdownViewController()
58+
addChild(countdownViewController)
59+
countdownViewController.didMove(toParent: self)
5560
}
5661

5762
// When the Adafruit Bluefruit LE connects -> Change the connection status text & color to green
@@ -65,6 +70,7 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
6570
func didDisconnectPeripheral() {
6671
print("Device disconnected in console")
6772
peripheralStatusLabel.text = "Device disconnected"
73+
peripheralStatusLabel.font = UIFont.systemFont(ofSize: 20)
6874
peripheralStatusLabel.textColor = UIColor.red
6975
}
7076

@@ -74,8 +80,12 @@ final class VCHomeViewController: UIViewController, BluetoothManagerDelegate {
7480
let receivedString = String(data: data, encoding: .utf8)
7581
// Update the label's text with the received string
7682
dataFromAdafruit.text = receivedString
77-
dataFromAdafruit.textColor = UIColor.black
83+
dataFromAdafruit.font = UIFont.systemFont(ofSize: 18)
84+
dataFromAdafruit.textColor = UIColor.blue
85+
86+
// delay 3 secs after receiving data for testing
87+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
88+
self.countdownViewController.showCountdownUI()
89+
}
7890
}
79-
80-
8191
}

iosApp/VC/Controllers/VCTestingViewController.swift

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,43 @@ final class VCTestingViewController: UIViewController {
1818
view.backgroundColor = .systemBackground
1919
title = "Testing"
2020

21-
let button = UIButton(type: .system)
22-
button.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
23-
button.center = view.center
24-
button.setTitle("Test MSG", for: .normal)
25-
button.addTarget(self, action: #selector(sendMessageButtonTapped), for: .touchUpInside)
26-
view.addSubview(button)
27-
28-
let button2 = UIButton(type: .system)
29-
button2.frame = CGRect(x: 0, y: button.frame.maxY + 20, width: 100, height: 50)
30-
button2.center.x = view.center.x
31-
button2.setTitle("Make Call", for: .normal)
32-
button2.addTarget(self, action: #selector(callButtonTapped), for: .touchUpInside)
33-
view.addSubview(button2)
34-
21+
let textMsgButton = UIButton(type: .custom)
22+
textMsgButton.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
23+
textMsgButton.center = view.center
24+
textMsgButton.setTitle("Test MSG", for: .normal)
25+
textMsgButton.setTitleColor(.white, for: .normal)
26+
textMsgButton.backgroundColor = .systemBlue
27+
textMsgButton.layer.cornerRadius = 10
28+
textMsgButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
29+
textMsgButton.addTarget(self, action: #selector(sendMessageButtonTapped), for: .touchUpInside)
30+
view.addSubview(textMsgButton)
31+
32+
let makeCallButton = UIButton(type: .custom)
33+
makeCallButton.frame = CGRect(x: 0, y: textMsgButton.frame.maxY + 20, width: 100, height: 50)
34+
makeCallButton.center.x = view.center.x
35+
makeCallButton.setTitle("Make Call", for: .normal)
36+
makeCallButton.setTitleColor(.white, for: .normal)
37+
makeCallButton.backgroundColor = .systemBlue
38+
makeCallButton.layer.cornerRadius = 10
39+
makeCallButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
40+
makeCallButton.addTarget(self, action: #selector(callButtonTapped), for: .touchUpInside)
41+
view.addSubview(makeCallButton)
42+
3543
// Instantiate countdown view controller and add it as a child view controller
3644
countdownViewController = CountdownViewController()
3745
addChild(countdownViewController)
3846
countdownViewController.didMove(toParent: self)
39-
40-
let button3 = UIButton(type: .system)
41-
button3.frame = CGRect(x: 0, y: button2.frame.maxY + 20, width: 100, height: 50)
42-
button3.center.x = view.center.x
43-
button3.setTitle("Countdown", for: .normal)
44-
button3.addTarget(self, action: #selector(countdownButtonTapped), for: .touchUpInside)
45-
view.addSubview(button3)
47+
48+
let startCountDownButton = UIButton(type: .custom)
49+
startCountDownButton.frame = CGRect(x: 0, y: makeCallButton.frame.maxY + 20, width: 100, height: 50)
50+
startCountDownButton.center.x = view.center.x
51+
startCountDownButton.setTitle("Countdown", for: .normal)
52+
startCountDownButton.setTitleColor(.white, for: .normal)
53+
startCountDownButton.backgroundColor = .systemBlue
54+
startCountDownButton.layer.cornerRadius = 10
55+
startCountDownButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
56+
startCountDownButton.addTarget(self, action: #selector(countdownButtonTapped), for: .touchUpInside)
57+
view.addSubview(startCountDownButton)
4658
}
4759

4860
@objc func sendMessageButtonTapped() {

0 commit comments

Comments
 (0)