diff --git a/Asthmaguard/Business/Usecases/AsthmaThreatCalculator.swift b/Asthmaguard/Business/Usecases/AsthmaThreatCalculator.swift index a7ba67a..e1ed3c8 100644 --- a/Asthmaguard/Business/Usecases/AsthmaThreatCalculator.swift +++ b/Asthmaguard/Business/Usecases/AsthmaThreatCalculator.swift @@ -41,7 +41,7 @@ public class AsthmaThreatCalculatorUseCase { } - // MARK: - Fetch Asthma and Calculate + // MARK: - Fetch Asthma Data and Calculate Severity of Threat func fetchDataAndCalculateAsthmaSeverity() { guard let patientToken = getPatientToken() else { @@ -79,6 +79,7 @@ public class AsthmaThreatCalculatorUseCase { } } } + func fetchData() { guard let patientToken = getPatientToken() else { print("Current username not available.") @@ -140,8 +141,9 @@ public class AsthmaThreatCalculatorUseCase { } } - // MARK: - Weather Data + + // MARK: - Weather Data private func fetchWeatherData(completion: @escaping (WeatherKitData.WeatherData?) -> Void) { guard let userLocation = LocationManager.shared.getCurrentLocation() else { print("User location not available") @@ -215,6 +217,7 @@ public class AsthmaThreatCalculatorUseCase { } } + //MARK: - Calculations for AQI and Pollen Severity based on Weather Data func calculateAQISeverity(aqiLevel: Int?) -> Double { guard let aqiLevel = aqiLevel else { return 0.0 diff --git a/Asthmaguard/Business/Usecases/BioSignalData.swift b/Asthmaguard/Business/Usecases/BioSignalData.swift index 5170014..31e20bd 100644 --- a/Asthmaguard/Business/Usecases/BioSignalData.swift +++ b/Asthmaguard/Business/Usecases/BioSignalData.swift @@ -5,36 +5,39 @@ // Created by Sadeel Muwahed on 29/04/2024. // -import Foundation import HealthKit class BioSignalData { static let healthStore: HKHealthStore = HKHealthStore() + //MARK: - requestHealthDataAccessIfNeeded + ///Function will request access to the Health app from the user class func requestHealthDataAccessIfNeeded(completion: @escaping (_ success: Bool) -> Void) { guard HKHealthStore.isHealthDataAvailable() else { completion(false) return } - + let readDataTypes: Set = [ HKObjectType.quantityType(forIdentifier: .heartRate)!, HKObjectType.quantityType(forIdentifier: .respiratoryRate)!, HKObjectType.quantityType(forIdentifier: .oxygenSaturation)! ] - + healthStore.requestAuthorization(toShare: nil, read: readDataTypes) { success, error in if let error = error { print("HealthKit authorization error:", error.localizedDescription) completion(false) return } - + completion(success) } } + //MARK: - fetchAllSamples + ///Function will use async calls based on queue data structure to retrive the samples of health data class func fetchAllSamples(completion: @escaping ([HKQuantitySample]?, Error?) -> Void) { let dispatchGroup = DispatchGroup() @@ -75,6 +78,8 @@ class BioSignalData { } } + // MARK: - fetchHeartRateSamples + ///Function will use async calls based on queue data structure to retrieve the samples of health data class func fetchHeartRateSamples(completion: @escaping ([HKQuantitySample]?, Error?) -> Void) { let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)! let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate) @@ -87,6 +92,8 @@ class BioSignalData { healthStore.execute(query) } + // MARK: - fetchRespiratoryRateSamples + ///Function will retrieve respiratory rate health data class func fetchRespiratoryRateSamples(completion: @escaping ([HKQuantitySample]?, Error?) -> Void) { let respiratoryRateType = HKObjectType.quantityType(forIdentifier: .respiratoryRate)! let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate) @@ -99,6 +106,8 @@ class BioSignalData { healthStore.execute(query) } + // MARK: - fetchOxygenSaturationSamples + ///Function will retrieve oxygen saturations health data class func fetchOxygenSaturationSamples(completion: @escaping ([HKQuantitySample]?, Error?) -> Void) { let oxygenSaturationType = HKObjectType.quantityType(forIdentifier: .oxygenSaturation)! let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate) diff --git a/Asthmaguard/Business/Usecases/EnviromentalData.swift b/Asthmaguard/Business/Usecases/EnviromentalData.swift index 66eef66..ec4aeff 100644 --- a/Asthmaguard/Business/Usecases/EnviromentalData.swift +++ b/Asthmaguard/Business/Usecases/EnviromentalData.swift @@ -34,7 +34,9 @@ class EnviromentalData{ let category: String let displayName: String } - + + // MARK: - fetchAirQuality + ///Function will call the API to get air quality data response func fetchAirQuality(latitude: Double, longitude: Double, completion: @escaping (AirQualityData?) -> Void) { let requestBody = AirQualityRequest(location: AirQualityRequest.Location(latitude: latitude, longitude: longitude)) @@ -44,7 +46,7 @@ class EnviromentalData{ return } - let urlString = "https://airquality.googleapis.com/v1/currentConditions:lookup?key=AIzaSyBsEgk_PWNwRVmGAA_ihIF1JmBtJskuur8" + let urlString = "https://airquality.googleapis.com/v1/currentConditions:lookup?key=key" guard let url = URL(string: urlString) else { print("Invalid URL") completion(nil) @@ -98,7 +100,9 @@ class EnviromentalData{ task.resume() } - + + // MARK: - parseAirQualityResponse + ///Function will parse the air quality data retrieved private func parseAirQualityResponse(_ data: Data) throws -> AirQualityData? { let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] guard let dateTime = json?["dateTime"] as? String, @@ -114,9 +118,10 @@ class EnviromentalData{ return AirQualityData(dateTime: dateTime, regionCode: regionCode, universalAQI: universalAQI) } - + // MARK: - fetchPollenForecast + ///Function will call the API to get pollen forecast data response func fetchPollenForecast(latitude: Double, longitude: Double, completion: @escaping (PollenForecastData?) -> Void){ - let urlString = "https://pollen.googleapis.com/v1/forecast:lookup?key=AIzaSyBsEgk_PWNwRVmGAA_ihIF1JmBtJskuur8&location.longitude=\(longitude)&location.latitude=\(latitude)&days=1&languageCode=en&plantsDescription=0" + let urlString = "https://pollen.googleapis.com/v1/forecast:lookup?key=key&location.longitude=\(longitude)&location.latitude=\(latitude)&days=1&languageCode=en&plantsDescription=0" guard let url = URL(string: urlString) else { completion(nil) return @@ -139,7 +144,8 @@ class EnviromentalData{ task.resume() } - + // MARK: - parsePollenForecastResponse + ///Function will parse the pollen forecast data retrieved func parsePollenForecastResponse(_ data: Data) -> PollenForecastData? { do { guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { @@ -190,6 +196,8 @@ class EnviromentalData{ } } + // MARK: - parseIndexInfo + //Private function used in the parsing of pollen data private func parseIndexInfo(_ indexInfoDict: [String: Any]) -> IndexInfo? { guard let category = indexInfoDict["category"] as? String, diff --git a/Asthmaguard/Business/Usecases/HealthDataAnalyzer.swift b/Asthmaguard/Business/Usecases/HealthDataAnalyzer.swift index e2791e7..e705b95 100644 --- a/Asthmaguard/Business/Usecases/HealthDataAnalyzer.swift +++ b/Asthmaguard/Business/Usecases/HealthDataAnalyzer.swift @@ -10,6 +10,7 @@ import HealthKit class HealthDataAnalyzer { + // MARK: - calculateHeartRateSeverity static func calculateHeartRateSeverity(samples: [HKQuantitySample]) -> Double { let normalHeartRateRange = 60.0...100.0 var severity = 0.0 @@ -21,7 +22,8 @@ class HealthDataAnalyzer { } return severity } - + + // MARK: - calculateRespiratoryRateSeverity static func calculateRespiratoryRateSeverity(samples: [HKQuantitySample]) -> Double { let normalRespiratoryRateRange = 12.0...20.0 var severity = 0.0 @@ -34,6 +36,7 @@ class HealthDataAnalyzer { return severity } + // MARK: - calculateOxygenSaturationSeverity static func calculateOxygenSaturationSeverity(samples: [HKQuantitySample]) -> Double { let normalOxygenSaturationRange = 95.0...100.0 var severity = 0.0 @@ -49,6 +52,7 @@ class HealthDataAnalyzer { return severity } + // MARK: - calculateOverallSeverity static func calculateOverallSeverity(heartRateSamples: [HKQuantitySample], respiratoryRateSamples: [HKQuantitySample], oxygenSaturationSamples: [HKQuantitySample]) -> Double { let heartRateSeverity = calculateHeartRateSeverity(samples: heartRateSamples) let respiratoryRateSeverity = calculateRespiratoryRateSeverity(samples: respiratoryRateSamples) diff --git a/Asthmaguard/Business/Usecases/WeatherKitData.swift b/Asthmaguard/Business/Usecases/WeatherKitData.swift index 3f026b5..51db243 100644 --- a/Asthmaguard/Business/Usecases/WeatherKitData.swift +++ b/Asthmaguard/Business/Usecases/WeatherKitData.swift @@ -5,7 +5,6 @@ // Created by Sadeel Muwahed on 16/05/2024. // -import Foundation import WeatherKit import CoreLocation @@ -17,7 +16,8 @@ class WeatherKitData { let temperature: Double } - // Method to fetch weather data based on latitude and longitude + // MARK: - fetchWeatherData + /// Function to fetch weather data based on latitude and longitude func fetchWeatherData(latitude: Double, longitude: Double, completion: @escaping (WeatherData?) -> Void) { let location = CLLocation(latitude: latitude, longitude: longitude) @@ -44,7 +44,8 @@ class WeatherKitData { } } - // Method to calculate severity based on humidity + // MARK: - calculateHumiditySeverity + /// Function to calculate severity based on humidity func calculateHumiditySeverity(humidity: Double) -> Double { if humidity >= 80 { return 1.0 @@ -59,12 +60,14 @@ class WeatherKitData { } } - // Method to calculate severity based on cloud cover + // MARK: - calculateCloudCoverSeverity + /// Function to calculate severity based on cloud cover func calculateCloudCoverSeverity(cloudCover: Double) -> Double { return cloudCover >= 0.8 ? 1.0 : 0.0 } - // Method to calculate severity based on temperature + // MARK: - calculateTemperatureSeverity + /// Function to calculate severity based on temperature func calculateTemperatureSeverity(temperature: Double) -> Double { if temperature <= -12 { return 1.0 @@ -79,7 +82,8 @@ class WeatherKitData { } } - // Method to calculate overall weather severity based on multiple factors + // MARK: - calculateOverallWeatherSeverity + /// Function to calculate overall weather severity based on multiple factors func calculateOverallWeatherSeverity(weatherData: WeatherData) -> Double { let humiditySeverity = calculateHumiditySeverity(humidity: weatherData.humidity) let cloudCoverSeverity = calculateCloudCoverSeverity(cloudCover: weatherData.cloudCover)