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

[#86] 토스트 구현 #88

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions FiveGuyes/FiveGuyes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
1A54142B2CD9FEF400283FBD /* BookSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A54142A2CD9FEF400283FBD /* BookSearchView.swift */; };
1AA14A782CDDA30D00B763A6 /* TotalCalendarTextBubble.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AA14A772CDDA30D00B763A6 /* TotalCalendarTextBubble.swift */; };
1AA14A7A2CDDD96200B763A6 /* NotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AA14A792CDDD96200B763A6 /* NotificationManager.swift */; };
240445B42CED9074005C3623 /* ToastViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240445B32CED9074005C3623 /* ToastViewModel.swift */; };
24360D482CD8BAF100E83D2B /* EmptyNotiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24360D472CD8BAF100E83D2B /* EmptyNotiView.swift */; };
24360D4A2CD8BE3C00E83D2B /* FinishGoalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24360D492CD8BE3C00E83D2B /* FinishGoalView.swift */; };
24360D532CD9F3AF00E83D2B /* DailyProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24360D522CD9F3AF00E83D2B /* DailyProgressView.swift */; };
24A2063F2CDB180000964FBB /* CompletionCalendarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24A2063E2CDB180000964FBB /* CompletionCalendarView.swift */; };
24F8A2172CEB290B00A01A76 /* Config.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 24F8A2162CEB290B00A01A76 /* Config.xcconfig */; };
24FAF9452CEC5224004FEC3F /* ToastView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24FAF9442CEC5224004FEC3F /* ToastView.swift */; };
261714D62CDDDA4000A3241D /* BookSettingsManagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 261714D52CDDDA4000A3241D /* BookSettingsManagerView.swift */; };
2644404E2CD8A39E0031A290 /* CompletionCelebrationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2644404D2CD8A39E0031A290 /* CompletionCelebrationView.swift */; };
264440502CD8A3AC0031A290 /* CompletionReviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2644404F2CD8A3AC0031A290 /* CompletionReviewView.swift */; };
Expand Down Expand Up @@ -65,11 +67,13 @@
1A54142A2CD9FEF400283FBD /* BookSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BookSearchView.swift; path = FiveGuyes/Sources/Views/Screen/BookSetting/BookSearch/BookSearchView.swift; sourceTree = SOURCE_ROOT; };
1AA14A772CDDA30D00B763A6 /* TotalCalendarTextBubble.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TotalCalendarTextBubble.swift; sourceTree = "<group>"; };
1AA14A792CDDD96200B763A6 /* NotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManager.swift; sourceTree = "<group>"; };
240445B32CED9074005C3623 /* ToastViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastViewModel.swift; sourceTree = "<group>"; };
24360D472CD8BAF100E83D2B /* EmptyNotiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyNotiView.swift; sourceTree = "<group>"; };
24360D492CD8BE3C00E83D2B /* FinishGoalView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinishGoalView.swift; sourceTree = "<group>"; };
24360D522CD9F3AF00E83D2B /* DailyProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DailyProgressView.swift; sourceTree = "<group>"; };
24A2063E2CDB180000964FBB /* CompletionCalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompletionCalendarView.swift; sourceTree = "<group>"; };
24F8A2162CEB290B00A01A76 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
24FAF9442CEC5224004FEC3F /* ToastView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = "<group>"; };
261714D52CDDDA4000A3241D /* BookSettingsManagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookSettingsManagerView.swift; sourceTree = "<group>"; };
2644404D2CD8A39E0031A290 /* CompletionCelebrationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompletionCelebrationView.swift; sourceTree = "<group>"; };
2644404F2CD8A3AC0031A290 /* CompletionReviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompletionReviewView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -255,6 +259,7 @@
264440542CD8E0100031A290 /* KeyboardObserver.swift */,
26F19A712CDB267600F41D6D /* NavigationCoordinator.swift */,
26EBDE252CDE3C6000B3A2BC /* BookSettingInputModel.swift */,
240445B32CED9074005C3623 /* ToastViewModel.swift */,
);
path = Stores;
sourceTree = "<group>";
Expand Down Expand Up @@ -290,6 +295,7 @@
26F19A6B2CDA0A2B00F41D6D /* CompletionListView.swift */,
26F19A6D2CDA74A600F41D6D /* WeeklyPageCalendarView.swift */,
1AA14A772CDDA30D00B763A6 /* TotalCalendarTextBubble.swift */,
24FAF9442CEC5224004FEC3F /* ToastView.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -403,6 +409,7 @@
1A010EC22CD8DA4300FBE3B3 /* BookPageSettingView.swift in Sources */,
1A010EC02CD8DA3800FBE3B3 /* BookLowView.swift in Sources */,
1A010EAC2CD8A7E500FBE3B3 /* APIBooksModel.swift in Sources */,
240445B42CED9074005C3623 /* ToastViewModel.swift in Sources */,
26890B972CAE811A008DFF49 /* ContentView.swift in Sources */,
261714D62CDDDA4000A3241D /* BookSettingsManagerView.swift in Sources */,
1AA14A782CDDA30D00B763A6 /* TotalCalendarTextBubble.swift in Sources */,
Expand All @@ -419,6 +426,7 @@
26F19A682CD9EFD800F41D6D /* MainHomeView.swift in Sources */,
26F19A742CDB271E00F41D6D /* NavigationRootView.swift in Sources */,
1AA14A7A2CDDD96200B763A6 /* NotificationManager.swift in Sources */,
24FAF9452CEC5224004FEC3F /* ToastView.swift in Sources */,
264440502CD8A3AC0031A290 /* CompletionReviewView.swift in Sources */,
264440502CD8A3AC0031A290 /* CompletionReviewView.swift in Sources */,
26890B952CAE811A008DFF49 /* FiveGuyesApp.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions FiveGuyes/FiveGuyes/Sources/Stores/ToastViewModel.swift
Copy link
Collaborator

Choose a reason for hiding this comment

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

message 프로퍼티의 타입을 옵셔널로 선언하고
hideToast 메서드에서 로직이 끝나면 nil 값을 할당해주는 이유가 있을까요?

isVisible 프로퍼티로만 토스트를 온/오프 하고 있다고 생각이 들어서 질문 남깁니다.

Copy link
Collaborator Author

@hyeyeonie hyeyeonie Nov 20, 2024

Choose a reason for hiding this comment

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

토스트가 비활성화될 때 더이상 표시할 메시지가 없다는 명시적인 상태를 나타내려고 했던 것 같아요. 지금 다시 보니 굳이 nil로 표시하지 않아도 된다고 판단되네요 수정하겠습니당 ~

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ToastViewModel.swift
// FiveGuyes
//
// Created by 신혜연 on 11/20/24.
//

import SwiftUI

class ToastViewModel: ObservableObject {
@Published var message: String = ""
@Published var isVisible: Bool = false
private var timer: Timer?

// 타이머 1.5초
func showToast(message: String, duration: TimeInterval = 1.0) {
self.message = message
self.isVisible = true

timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: duration, repeats: false) { [weak self] _ in
self?.hideToast()
}
}

private func hideToast() {
withAnimation {
self.isVisible = false
}
}
}
53 changes: 53 additions & 0 deletions FiveGuyes/FiveGuyes/Sources/Views/Components/ToastView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ToastView.swift
// FiveGuyes
//
// Created by 신혜연 on 11/19/24.
//

import SwiftUI

struct ToastView: View {
@ObservedObject var viewModel: ToastViewModel

var body: some View {
ZStack {
VStack {
Spacer()
if viewModel.isVisible {
Text(viewModel.message)
.font(.system(size: 16, weight: .semibold))
.padding(.horizontal, 20)
.padding(.vertical, 14)
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(16)
.transition(.opacity)
.animation(.easeInOut(duration: 0.5), value: viewModel.isVisible)
.zIndex(1)
}
}
}
}
}

struct ToastTestView: View {
@StateObject private var toastViewModel = ToastViewModel()

var body: some View {
ZStack {
VStack(spacing: 20) {
Button {
toastViewModel.showToast(message: "오늘은 완독하는 마지막날이에요!")
} label: {
Text("Show Toast")
}
}
ToastView(viewModel: toastViewModel)
}
}
}

#Preview {
ToastTestView()
}