-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#86] 토스트 구현 #88
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
FiveGuyes/FiveGuyes/Sources/Views/Components/ToastView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message 프로퍼티의 타입을 옵셔널로 선언하고
hideToast 메서드에서 로직이 끝나면 nil 값을 할당해주는 이유가 있을까요?
isVisible 프로퍼티로만 토스트를 온/오프 하고 있다고 생각이 들어서 질문 남깁니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
토스트가 비활성화될 때 더이상 표시할 메시지가 없다는 명시적인 상태를 나타내려고 했던 것 같아요. 지금 다시 보니 굳이 nil로 표시하지 않아도 된다고 판단되네요 수정하겠습니당 ~