-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeditationView.swift
162 lines (144 loc) · 5.14 KB
/
MeditationView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import SwiftUI
import SwiftData
import AVFoundation
struct MeditationView: View {
@Environment(\.modelContext) private var modelContext
@State private var meditationDuration: Int = 5
@State private var isMeditating = false
@State private var progress: CGFloat = 0.0
@State private var scale: CGFloat = 1.0
@State private var player: AVAudioPlayer?
@State private var isChimePlaying = false
@State private var showPopover = false
@State private var remainingTime: Int = 0
@State private var timer: Timer?
var body: some View {
VStack {
Spacer()
// Pulsating Animation
Circle()
.fill(Color.blue.opacity(1))
.frame(width: 200, height: 200)
.scaleEffect(scale)
.animation(isMeditating ? .easeInOut(duration: 3).repeatForever(autoreverses: true) : .default, value: scale)
.onAppear {
scale = 1.2 // Start pulsating
}
Spacer()
Text("Current duration: \(meditationDuration) minutes")
.padding()
if isMeditating {
Text("Remaining time: \(remainingTime) seconds")
.padding()
}
Text(isMeditating ? "Breathe in... Breathe out..." : "Ready to Start")
.font(.headline)
.padding()
Spacer()
HStack {
if !isMeditating {
Button(action: startMeditation) {
Text("Start")
.padding()
.frame(width: 100, height: 100)
.background(Color.blue)
.foregroundColor(.white)
.clipShape(Circle())
.animation(.easeInOut, value: isMeditating)
}
.padding()
}
if isMeditating {
Button(action: stopMeditation) {
Text("Stop")
.padding()
.frame(width: 100, height: 100)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
.padding()
}
Button(action: {
showPopover = true
}) {
Text("Duration")
.padding()
.frame(width: 100, height: 100)
.background(Color.green)
.foregroundColor(.white)
.clipShape(Circle())
}
.padding()
.popover(isPresented: $showPopover) {
VStack {
Text("Select Duration")
.font(.headline)
.padding()
Stepper(value: $meditationDuration, in: 1...30, step: 5) {
Text("\(meditationDuration) minutes")
}
.padding()
Button("Done") {
showPopover = false
}
.padding()
}
.padding()
}
if isChimePlaying {
Button(action: stopChimeSound) {
Text("Stop")
.padding()
.frame(width: 100, height: 100)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
.padding()
}
}
}
.padding()
}
func startMeditation() {
isMeditating = true
scale = 1.5 // Start animation
remainingTime = meditationDuration * 60
// Haptic feedback starts
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
// Start the timer
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
if remainingTime > 0 {
remainingTime -= 1
} else {
stopMeditation()
playChimeSound()
}
}
}
func stopMeditation() {
timer?.invalidate()
isMeditating = false
scale = 1.0 // Stop pulsating
}
func playChimeSound() {
guard let url = Bundle.main.url(forResource: "chime", withExtension: "mp3") else {
print("Chime sound file not found")
return
}
do {
player = try AVAudioPlayer(contentsOf: url)
player?.prepareToPlay()
player?.play()
isChimePlaying = true
} catch {
print("Error playing sound: \(error.localizedDescription)")
}
}
func stopChimeSound() {
player?.stop()
isChimePlaying = false
}
}