Skip to content

Commit

Permalink
feat: refactor code structure and remove unnecessary comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuschaves committed Mar 6, 2024
1 parent 73bde5e commit ea33191
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 119 deletions.
8 changes: 1 addition & 7 deletions PulsePomodoro Watch App/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
//
// ContentView.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 02/03/24.
//
import WatchKit
import SwiftUI
import UIKit
Expand All @@ -17,7 +11,7 @@ struct ContentView: View {
let heartRateMonitor = HeartRateMonitor()

var body: some View {
NavigationView {
NavigationStack {
VStack {
Stepper(value: $minutes, in: 0...60, step: 1) {
Text("\(minutes) minutes")
Expand Down
7 changes: 0 additions & 7 deletions PulsePomodoro Watch App/PulsePomodoroApp.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// PulsePomodoroApp.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 02/03/24.
//

import SwiftUI

@main
Expand Down
7 changes: 0 additions & 7 deletions PulsePomodoro Watch App/models/DateModel.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// DateModel.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 05/03/24.
//

import Foundation

extension Date {
Expand Down
7 changes: 0 additions & 7 deletions PulsePomodoro Watch App/models/PomodoroModel.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// PomodoroModel.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 05/03/24.
//

import Foundation

struct Pomodoro: Codable {
Expand Down
7 changes: 0 additions & 7 deletions PulsePomodoro Watch App/services/HealhKitService.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// HealhKitService.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 05/03/24.
//

import Foundation
import HealthKit

Expand Down
7 changes: 0 additions & 7 deletions PulsePomodoro Watch App/services/LocalStorageService.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// LocalStorageService.swift
// PulsePomodoro Watch App
//
// Created by Mateus Henrique on 05/03/24.
//

import Foundation

class LocalStorageManager {
Expand Down
168 changes: 168 additions & 0 deletions PulsePomodoro Watch App/views/PomodoroResumeView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import Foundation
import SwiftUI

struct PomodoroResumeView: View {

var timeFocused: Int
var startDate: Date

@State private var averageHr = 0
@State private var averageBreath = 0

let heartRateMonitor = HeartRateMonitor()

var body: some View {
NavigationView {
ScrollView {
VStackLayout {
Text("Time focused")
.frame(maxWidth: .infinity, alignment: .leading)
.fontWeight(Font.Weight.regular)
.padding(EdgeInsets(
top: 2, leading: 0, bottom: 0, trailing: 0
))
Text("\(formatSecondsToMMSS(seconds: timeFocused))")
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundStyle(Color.yellow)
.fontWeight(Font.Weight.bold)
Divider()
}
.padding(EdgeInsets(top: 4, leading: 0, bottom: 0, trailing: 0))

VStackLayout {
HStackLayout {
VStackLayout {
Text("Average HR")
.frame(maxWidth: .infinity, alignment: .leading)
.fontWeight(Font.Weight.regular)
Text("\(self.averageHr) bpm")
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundStyle(
Color.red
)
.fontWeight(Font.Weight.bold)
}

Image(systemName: "heart.fill")
.foregroundStyle(Color.red)
.font(.subheadline)
.symbolEffect(
.pulse
)
}

Divider()
}
.padding(EdgeInsets(top: 4, leading: 0, bottom: 0, trailing: 0))

VStackLayout {
HStackLayout {
VStackLayout {
Text("Average breaths")
.frame(maxWidth: .infinity, alignment: .leading)
.fontWeight(Font.Weight.regular)
Text("\(self.averageBreath) per minute")
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundStyle(
Color.blue
)
.fontWeight(Font.Weight.bold)
.font(.caption2)
}

Image(systemName: "lungs.fill")
.foregroundStyle(Color.blue)
.font(.subheadline)
.symbolEffect(
.pulse
)
}
Divider()
}
.padding(EdgeInsets(top: 4, leading: 0, bottom: 0, trailing: 0))
}
.navigationBarTitle("Summary")
.navigationBarTitleDisplayMode(.inline)
.padding()
}.onAppear(perform: {
self.savePomodoro()
})
}

func formatSecondsToMMSS(seconds: Int) -> String {
let minutes = seconds / 60
let seconds = seconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}

func savePomodoro() {
let dispatchGroup = DispatchGroup()

var heartRate: Int?
var respiratoryRate: Int?

// Enter dispatch group before starting the first asynchronous task
dispatchGroup.enter()
heartRateMonitor.getHeartRateFromLastSeconds(seconds: timeFocused) { receivedHeartRate in
heartRate = receivedHeartRate
dispatchGroup.leave() // Leave the dispatch group when the first task is done
}

// Enter dispatch group before starting the second asynchronous task
dispatchGroup.enter()
heartRateMonitor.getRespirationRateFromLastSeconds(seconds: timeFocused) { receivedRespiratoryRate in
respiratoryRate = receivedRespiratoryRate
dispatchGroup.leave() // Leave the dispatch group when the second task is done
}

// Notify when both tasks are completed
dispatchGroup.notify(queue: .main) {
// Both tasks are finished
if let heartRate = heartRate, let respiratoryRate = respiratoryRate {
self.averageHr = heartRate
self.averageBreath = respiratoryRate

print("Heart rate on last \(timeFocused) seconds was \(heartRate)")
let isHeartRateNormal = heartRateMonitor.isRestingHeartRateNormal(heartRate: heartRate)

print("Respiratory rate on last \(timeFocused) seconds was \(respiratoryRate)")
let isRespirationRateNormal = heartRateMonitor.isRespirationRateNormal(respirationRate: respiratoryRate)
// Save pomodoro as JSON array
var previousPomodoros = LocalStorageManager.shared.get(key: "pomodoros") as? [Data] ?? []
var currentPomodoroJson: Data?
do {
currentPomodoroJson = try JSONEncoder().encode(
Pomodoro(
heartRate: heartRate,
respiratoryRate: respiratoryRate,
startDate: startDate,
endDate: Date(),
duration: timeFocused,
isHeartRateNormal: isHeartRateNormal,
isRespirationRateNormal: isRespirationRateNormal,
isCompleted: true
)
)
} catch {
print("Error encoding current pomodoro: \(error)")
}

if let currentPomodoroJson = currentPomodoroJson {
previousPomodoros.append(currentPomodoroJson)
LocalStorageManager.shared.save(key: "pomodoros", value: previousPomodoros)
}
}
}
}
}

struct PomodoroResumeView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
PomodoroResumeView(
timeFocused: 300,
startDate: Date()
)
}
}
}
Loading

0 comments on commit ea33191

Please sign in to comment.