Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] add refresh button #139

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 130 additions & 98 deletions Spha/Spha/Views/Home/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,136 @@ import SwiftUI

struct MainView: View {
@EnvironmentObject var router: RouterManager
@Environment(\.scenePhase) private var scenePhase

@StateObject private var viewModel = MainViewModel(HealthKitManager(), MindfulSessionManager())

@State private var introOpacity = 0.0
@State private var isFirstLaunch: Bool = !UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
@State private var isBreathingViewPresented = false
@State private var isLoading = false

var body: some View {
ZStack {
// ๊ทธ๋ผ๋ฐ์ด์…˜ ๋ฐฐ๊ฒฝ
Color.backgroundGB
.edgesIgnoringSafeArea(.all)

// ๋ฉ”์ธ ํ™”๋ฉด ์š”์†Œ
VStack{
HStack{
Spacer()

Button {
router.push(view: .dailyStatisticsView)
} label: {
Image(systemName: "chart.bar.fill")
.resizable()
.frame(width: 28, height: 20)
.foregroundStyle(Color.buttonGraph)
ScrollView {
VStack{
HStack{
Spacer()

Button {
router.push(view: .dailyStatisticsView)
} label: {
Image(systemName: "chart.bar.fill")
.resizable()
.frame(width: 28, height: 22)
.foregroundStyle(Color.buttonGraph)
}
.padding(.trailing, 8)
}
.padding(.trailing, 8)
}
.padding(.top, 16)
.padding(.horizontal, 16)

Spacer()

HStack{
Text("ํ˜„์žฌ ๋งˆ์Œ๊ตฌ์Šฌ ์ƒํƒœ")
.customFont(.caption_0)
.foregroundStyle(.white)
.padding(.top, 32)
.padding(.bottom, 24)
.padding(.horizontal, 16)

Button {
router.push(view: .mainInfoView)
} label: {
Image(systemName: "info.circle")
.resizable()
.frame(width: 15, height: 15)
.foregroundStyle(.white)
}
Spacer()

}

Text("\(String(describing: viewModel.mindDustLvDescription))")
.customFont(.title_1)
.foregroundStyle(
viewModel.isHRVRecordedToday ?
.white : Color.gray0
)
.bold()
.padding(.top, 8)

Spacer()

MP4PlayerView(videoURLString: viewModel.mindDustLevel)
.frame(width: 300, height: 300)
.padding(.bottom, 16)


HStack{
VStack{
HStack{
Text("\(viewModel.recommendedCount)")
.customFont(.title_0)
.foregroundStyle(.white)
.bold()

Text("ํšŒ")
.customFont(.caption_0)
HStack{
Text("ํ˜„์žฌ ๋งˆ์Œ๊ตฌ์Šฌ ์ƒํƒœ")
.customFont(.caption_0)
.foregroundStyle(.white)

Button {
router.push(view: .mainInfoView)
} label: {
Image(systemName: "info.circle")
.resizable()
.frame(width: 15, height: 15)
.foregroundStyle(.white)
.bold()
}
.padding(.bottom, 2)

Text("๊ถŒ์žฅ ์ฒญ์†Œ ํšŸ์ˆ˜")
.customFont(.caption_1)
.foregroundStyle(.gray)
}

Rectangle()
.frame(width:1, height: 45)
.foregroundStyle(.gray)
.padding(.horizontal, 24)
Text("\(String(describing: viewModel.mindDustLvDescription))")
.customFont(.title_1)
.foregroundStyle(
viewModel.isHRVRecordedToday ?
.white : Color.gray0
)
.bold()
.padding(.top, 16)

VStack{
HStack{
Text("\(viewModel.completedCount)")
.customFont(.title_0)
.foregroundStyle(.white)
.bold()
Spacer()

MP4PlayerView(videoURLString: viewModel.mindDustLevel)
.frame(width: 300, height: 300)
.padding(.bottom, 32)


HStack{
VStack{
HStack{
Text("\(viewModel.recommendedCount)")
.customFont(.title_0)
.foregroundStyle(.white)
.bold()

Text("ํšŒ")
.customFont(.caption_0)
.foregroundStyle(.white)
.bold()
}
.padding(.bottom, 2)

Text("ํšŒ")
.customFont(.caption_0)
.foregroundStyle(.white)
.bold()
Text("๊ถŒ์žฅ ์ฒญ์†Œ ํšŸ์ˆ˜")
.customFont(.caption_1)
.foregroundStyle(.gray)
}
.padding(.bottom, 2)

Text("์‹คํ–‰ํ•œ ์ฒญ์†Œ ํšŸ์ˆ˜")
.customFont(.caption_1)
Rectangle()
.frame(width:1, height: 45)
.foregroundStyle(.gray)
.padding(.horizontal, 24)

VStack{
HStack{
Text("\(viewModel.completedCount)")
.customFont(.title_0)
.foregroundStyle(.white)
.bold()

Text("ํšŒ")
.customFont(.caption_0)
.foregroundStyle(.white)
.bold()
}
.padding(.bottom, 2)

Text("์‹คํ–‰ํ•œ ์ฒญ์†Œ ํšŸ์ˆ˜")
.customFont(.caption_1)
.foregroundStyle(.gray)
}
}
}
.padding(.bottom, 12)

Spacer()

Button {
isBreathingViewPresented.toggle()
//.padding(.bottom, 12)

} label: {
// ์ž„์‹œ ๋ฒ„ํŠผ ๋ผ๋ฒจ
Image("mainButton")
.resizable()
.frame(width: 90, height: 90)
.foregroundStyle(.gray)
Spacer()

Button {
isBreathingViewPresented.toggle()
} label: {
// ์ž„์‹œ ๋ฒ„ํŠผ ๋ผ๋ฒจ
Image("mainButton")
.resizable()
.frame(width: 90, height: 90)
.foregroundStyle(.gray)
}
.padding(.top, 48)
}
Spacer()

}
.refreshable {
triggerRefresh()
}

// OnboardingStartView ์˜ค๋ฒ„๋ ˆ์ด
Expand All @@ -135,11 +140,17 @@ struct MainView: View {
.transition(.opacity) // ํŽ˜์ด๋“œ ํšจ๊ณผ
.zIndex(1) // ํ•ญ์ƒ ์ตœ์ƒ์œ„์— ์œ„์น˜
}

// ๋กœ๋”ฉ ์ค‘ ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”
if isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle()).foregroundColor(.white)
.background(Color.black.opacity(0.5).edgesIgnoringSafeArea(.all))
}
}
.sheet(isPresented: $isBreathingViewPresented) {
BreathingMainView(breathManager: BreathingMainViewModel())
}

.onAppear {
if !isFirstLaunch {
viewModel.updateTodayRecord()
Expand All @@ -148,8 +159,29 @@ struct MainView: View {
.onChange(of: self.isFirstLaunch) { oldValue, newValue in
viewModel.updateTodayRecord()
}
.onChange(of: scenePhase) { _, newPhase in // ์ถ”๊ฐ€: ์•ฑ ์ƒํƒœ ๋ณ€ํ™” ๊ฐ์ง€
if newPhase == .active { // ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ๋ณต๊ท€ ์‹œ
viewModel.updateTodayRecord()
}
}
}

private func triggerRefresh() {
// ํ–…ํ‹ฑ ๋ฐœ์ƒ
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

// ๋กœ๋”ฉ ์ƒํƒœ ํ™œ์„ฑํ™”
isLoading = true

// ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ ์‹คํ–‰
viewModel.updateTodayRecord()

// ๋กœ๋”ฉ ์ƒํƒœ ๋น„ํ™œ์„ฑํ™” (๋”œ๋ ˆ์ด ์‹œ๋ฎฌ๋ ˆ์ด์…˜)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
isLoading = false
}
}
Comment on lines +169 to +184
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋กœ๋”ฉ์ด๋‚˜ ๋ฐ์ดํ„ฐ ๊ฐฑ์‹  ๋ถ€๋ถ„ MainViewModel๋กœ ๋‚˜๋ˆ„๊ธฐ์—” ๋„ˆ๋ฌด ์ž‘์„๊นŒ์š”?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ณต๊ฐํ•ฉ๋‹ˆ๋‹ค! ํ•ด๋‹น ๋ถ€๋ถ„ MainViewModel๋กœ ์˜ฎ๊ฒจ๋„ ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ํ•ด๋‹น ๋ถ€๋ถ„์ด ๊ณ ๋ฏผ๋˜์–ด GPT ์„ผ์„ธํ•œํ…Œ ๋ฌผ์–ด๋ดค๋Š”๋ฐ ํ•ด๋‹น ๊ธฐ๋Šฅ์€ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๋ณด๋‹ค ํ™”๋ฉด ์ฒ˜๋ฆฌ์— ๊ฐ€๊นŒ์›Œ View์— ์–ด์šธ๋ฆฐ๋‹ค๊ณ  ํ•˜์‹œ๋”๊ตฐ์š”...! ์šฐ์„  ์ด๊ฑด ์ด๋Œ€๋กœ ํ•ฉ์น˜๊ณ  ๋‹ค์Œ๋ฒˆ ์ฝ”๋“œ ์ˆ˜์ •์—์„œ ์žฌ๋…ผ์˜ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

}

#Preview {
Expand Down