From 86b22cf4ab8e926f6b57f181f71bb90481311354 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 13 Jan 2021 13:06:50 -0500 Subject: [PATCH 1/2] Added new tab for testing logic. --- ProjectClock.xcodeproj/project.pbxproj | 8 +++ ProjectClock/Base.lproj/Main.storyboard | 39 +++++++++++- ProjectClock/NotificationLogic.swift | 5 +- ProjectClock/TestingViewController.swift | 75 ++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 ProjectClock/TestingViewController.swift diff --git a/ProjectClock.xcodeproj/project.pbxproj b/ProjectClock.xcodeproj/project.pbxproj index 03a8f43..bcbddf0 100644 --- a/ProjectClock.xcodeproj/project.pbxproj +++ b/ProjectClock.xcodeproj/project.pbxproj @@ -18,6 +18,8 @@ 4FF0684825A5F18800304E6B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4FF0684725A5F18800304E6B /* Assets.xcassets */; }; 4FF0684B25A5F18800304E6B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4FF0684925A5F18800304E6B /* LaunchScreen.storyboard */; }; 7C83963625AF375B0027A94C /* NotificationLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963525AF375B0027A94C /* NotificationLogic.swift */; }; + 7C83963925AF68980027A94C /* TestingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963825AF68980027A94C /* TestingViewController.swift */; }; + 7C83963C25AF6B6B0027A94C /* Alarm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963B25AF6B6B0027A94C /* Alarm.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -36,6 +38,8 @@ 7C83962D25AF34F00027A94C /* ProjectClock.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ProjectClock.entitlements; sourceTree = ""; }; 7C83962F25AF34F10027A94C /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; }; 7C83963525AF375B0027A94C /* NotificationLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationLogic.swift; sourceTree = ""; }; + 7C83963825AF68980027A94C /* TestingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestingViewController.swift; sourceTree = ""; }; + 7C83963B25AF6B6B0027A94C /* Alarm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alarm.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -82,6 +86,8 @@ 7C83963525AF375B0027A94C /* NotificationLogic.swift */, 4F98955125A9260400F51321 /* Net.swift */, 4F509BD125AE22D100726227 /* Models.swift */, + 7C83963825AF68980027A94C /* TestingViewController.swift */, + 7C83963B25AF6B6B0027A94C /* Alarm.swift */, ); path = ProjectClock; sourceTree = ""; @@ -167,9 +173,11 @@ 4F8A607125A9160400D88DC3 /* AddAlarmViewController.swift in Sources */, 4F98955225A9260400F51321 /* Net.swift in Sources */, 4F8A607525A919E600D88DC3 /* Logic.swift in Sources */, + 7C83963925AF68980027A94C /* TestingViewController.swift in Sources */, 4FF0684325A5F18700304E6B /* ViewController.swift in Sources */, 4FF0683F25A5F18700304E6B /* AppDelegate.swift in Sources */, 4FF0684125A5F18700304E6B /* SceneDelegate.swift in Sources */, + 7C83963C25AF6B6B0027A94C /* Alarm.swift in Sources */, 4F509BD225AE22D100726227 /* Models.swift in Sources */, 7C83963625AF375B0027A94C /* NotificationLogic.swift in Sources */, ); diff --git a/ProjectClock/Base.lproj/Main.storyboard b/ProjectClock/Base.lproj/Main.storyboard index a6eb690..02f1288 100644 --- a/ProjectClock/Base.lproj/Main.storyboard +++ b/ProjectClock/Base.lproj/Main.storyboard @@ -211,6 +211,42 @@ + + + + + + + + + + + + + + + + + + + + @@ -296,7 +332,7 @@ - + @@ -502,6 +538,7 @@ + diff --git a/ProjectClock/NotificationLogic.swift b/ProjectClock/NotificationLogic.swift index e491d08..44c3392 100644 --- a/ProjectClock/NotificationLogic.swift +++ b/ProjectClock/NotificationLogic.swift @@ -7,14 +7,15 @@ import Foundation import CoreMotion +import UserNotifications let motionManager = CMMotionManager() func getAccelerometer() { motionManager.startAccelerometerUpdates() - + //print(motionManager.accelerometerData) if let accelerometerData = motionManager.accelerometerData { - print(accelerometerData) + print("Acclerometer: \(accelerometerData)") } } diff --git a/ProjectClock/TestingViewController.swift b/ProjectClock/TestingViewController.swift new file mode 100644 index 0000000..634e77f --- /dev/null +++ b/ProjectClock/TestingViewController.swift @@ -0,0 +1,75 @@ +// +// TestingViewController.swift +// ProjectClock +// +// Created by Aaron Saporito on 1/13/21. +// + +import UIKit +import UserNotifications + +class TestingViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { + (granted, error) in + if granted { + print("Authorized Notifications") + } else { + print("Error: No notification access") + } + } + + // Do any additional setup after loading the view. + } + + @IBAction func getAccel(_ sender: Any) { + getAccelerometer() + } + + //Sends a test notification + @IBAction func sendNotification(_ sender: Any) { + var alarm = Alarm(alarmTime: Date(), text: "Hello there!", wakeMethod: WVM(name: "walking", desc: "Walk")) + + let content = UNMutableNotificationContent() + + //Date formatting to string + let today = Date() + let formatter1 = DateFormatter() + formatter1.dateStyle = .short + print(formatter1.string(from: today)) + + //Notification content + content.title = alarm.text + content.subtitle = formatter1.string(from: today) + content.body = "Wake method: \(alarm.wakeMethod)" + + // 2 + let imageName = "applelogo" + guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return } + + let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none) + + content.attachments = [attachment] + + // 3 + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0, repeats: false) + let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger) + + // 4 + UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) + } + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using segue.destination. + // Pass the selected object to the new view controller. + } + */ + +} From b089a79d859808433e98186b1381deedb4c3e05e Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 13 Jan 2021 13:16:30 -0500 Subject: [PATCH 2/2] [+] Create local storage and automatically add email and token in requests --- ProjectClock/Net.swift | 11 ++++++++++- ProjectClock/ProjectClock.entitlements | 8 ++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ProjectClock/Net.swift b/ProjectClock/Net.swift index 2536169..42d050f 100644 --- a/ProjectClock/Net.swift +++ b/ProjectClock/Net.swift @@ -9,6 +9,7 @@ import Foundation /// Base URL of the HTTP server let baseUrl = "http://localhost:8080/api" // TODO: Production settings let JSON = JSONDecoder() +let localStorage = UserDefaults(suiteName: "group.org.hydev.alarm.clock")! /// API class struct API @@ -65,7 +66,7 @@ func createUrl(_ node: String, _ params: [String: String]? = [:]) -> URL } /** - Send a HTTP request + Send a HTTP request. Email and authentication token (the initial hash of a password) is automatically added if they exist in localStorage. - Parameter api: API Node (Eg. APIs.register) - Parameter params: Parameters to send to the server (Check the documentation of the API node to see which parameters you need) @@ -74,6 +75,14 @@ func createUrl(_ node: String, _ params: [String: String]? = [:]) -> URL */ func send(_ api: API, _ params: [String: String]? = [:], _ success: @escaping (T) -> Void, err: @escaping (String) -> Void = {it in}) { + // Add default params + var params = params + if params != nil + { + if params!["email"] == nil { params!["email"] = localStorage.string(forKey: "email") } + if params!["pass"] == nil { params!["pass"] = localStorage.string(forKey: "pass") } + } + let url = createUrl(api.loc, params) // Create task diff --git a/ProjectClock/ProjectClock.entitlements b/ProjectClock/ProjectClock.entitlements index 2ab14a2..1ca1a54 100644 --- a/ProjectClock/ProjectClock.entitlements +++ b/ProjectClock/ProjectClock.entitlements @@ -2,9 +2,9 @@ - com.apple.developer.healthkit - - com.apple.developer.healthkit.access - + com.apple.security.application-groups + + group.org.hydev.alarm.clock +