Skip to content

Commit

Permalink
style: added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sadeelmu committed Jul 18, 2024
1 parent 7f865ef commit 1831c4d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
7 changes: 5 additions & 2 deletions Asthmaguard/Business/Usecases/AsthmaThreatCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -79,6 +79,7 @@ public class AsthmaThreatCalculatorUseCase {
}
}
}

func fetchData() {
guard let patientToken = getPatientToken() else {
print("Current username not available.")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions Asthmaguard/Business/Usecases/BioSignalData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> = [
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()

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions Asthmaguard/Business/Usecases/EnviromentalData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion Asthmaguard/Business/Usecases/HealthDataAnalyzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions Asthmaguard/Business/Usecases/WeatherKitData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Sadeel Muwahed on 16/05/2024.
//

import Foundation
import WeatherKit
import CoreLocation

Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 1831c4d

Please sign in to comment.