-
Notifications
You must be signed in to change notification settings - Fork 0
✨ [Feat] LoadingView 컴포넌트 구현 #10
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // | ||
| // LoadingView.swift | ||
| // AppProduct | ||
| // | ||
| // Created by 이예지 on 1/4/26. | ||
| // | ||
|
|
||
| import SwiftUI | ||
|
|
||
| // MARK: - LoadingView | ||
|
|
||
| struct LoadingView<Content: View>: View { | ||
|
|
||
| // MARK: - Properties | ||
| private let content: Content | ||
|
|
||
| @Environment(\.loadingViewIsPresented) private var isPresented | ||
| @Environment(\.loadingViewMessage) private var message | ||
| @Environment(\.loadingViewSize) private var size | ||
|
|
||
| // MARK: - Initializer | ||
|
|
||
| init(@ViewBuilder content: () -> Content) { | ||
| self.content = content() | ||
| } | ||
|
|
||
| // MARK: - Body | ||
|
|
||
| var body: some View { | ||
| ZStack { | ||
| content | ||
|
|
||
| if isPresented { | ||
| LoadingViewContent(message: message, size: size) | ||
| .equatable() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - LoadingViewContent (Presenter) | ||
| private struct LoadingViewContent: View, Equatable { | ||
| let message: String? | ||
| let size: LoadingViewSize | ||
|
|
||
| static func == (lhs: LoadingViewContent, rhs: LoadingViewContent) -> Bool { | ||
| lhs.message == rhs.message && | ||
| lhs.size == rhs.size | ||
| } | ||
|
|
||
| var body: some View { | ||
| VStack(spacing: 10) { | ||
| ProgressView() | ||
| .progressViewStyle(.circular) | ||
| .frame(width: size.size.width , height: size.size.height) | ||
|
|
||
| if let message { | ||
| Text(message) | ||
| .font(size.font) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MARK: - LoadingView + AnyLoadingView | ||
|
|
||
| extension LoadingView: AnyLoadingView { } | ||
|
|
||
| // MARK: - Preview | ||
|
|
||
| #Preview("Loading ON + Message + Size") { | ||
| LoadingView { | ||
| RoundedRectangle(cornerRadius: 16) | ||
| .foregroundStyle(Color.accent100) | ||
| .frame(height: 160) | ||
| .overlay { Text("Content") } | ||
| .padding() | ||
| } | ||
| .presented(.constant(true)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // | ||
| // LoadingViewEnvironment.swift | ||
| // AppProduct | ||
| // | ||
| // Created by 이예지 on 1/4/26. | ||
| // | ||
|
|
||
| import SwiftUI | ||
|
|
||
| // MARK: - Environment Keys | ||
|
|
||
| struct LoadingViewSizeKey: EnvironmentKey { | ||
| static let defaultValue: LoadingViewSize = .large | ||
| } | ||
|
|
||
| struct LoadingViewIsPresentedKey: EnvironmentKey { | ||
| static let defaultValue: Bool = false | ||
| } | ||
|
|
||
| struct LoadingViewMessageKey: EnvironmentKey { | ||
| static let defaultValue: String = "Loading..." | ||
| } | ||
|
||
|
|
||
| // MARK: - EnvironmentValues Extension | ||
|
|
||
| extension EnvironmentValues { | ||
| var loadingViewSize: LoadingViewSize { | ||
| get { self[LoadingViewSizeKey.self] } | ||
| set { self[LoadingViewSizeKey.self] = newValue } | ||
| } | ||
|
|
||
| var loadingViewIsPresented: Bool { | ||
| get { self[LoadingViewIsPresentedKey.self] } | ||
| set { self[LoadingViewIsPresentedKey.self] = newValue } | ||
| } | ||
|
|
||
| var loadingViewMessage: String { | ||
| get { self[LoadingViewMessageKey.self] } | ||
| set { self[LoadingViewMessageKey.self] = newValue } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // | ||
| // LoadingViewModifier.swift | ||
| // AppProduct | ||
| // | ||
| // Created by 이예지 on 1/4/26. | ||
| // | ||
|
|
||
| import SwiftUI | ||
|
|
||
| // MARK: - AnyLoadingView Protocol | ||
|
|
||
| /// LoadingView 전용 프로토콜 | ||
| /// 이 프로토콜을 준수하는 View만 LoadingView modifier 사용 가능 | ||
| protocol AnyLoadingView: View { } | ||
|
|
||
|
|
||
| // MARK: - ViewModifiers | ||
|
|
||
| struct LoadingViewSizeModifier: ViewModifier { | ||
| let size: LoadingViewSize | ||
|
|
||
| func body(content: Content) -> some View { | ||
| content.environment(\.loadingViewSize, size) | ||
| } | ||
| } | ||
|
|
||
| struct LoadingViewIsPresentedModifier: ViewModifier { | ||
| @Binding var isPresented: Bool | ||
|
|
||
| func body(content: Content) -> some View { | ||
| content.environment(\.loadingViewIsPresented, isPresented) | ||
| } | ||
| } | ||
|
|
||
| struct LoadingViewMessageModifier: ViewModifier { | ||
| let message: String | ||
|
|
||
| func body(content: Content) -> some View { | ||
| content.environment(\.loadingViewMessage, message) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - AnyLoadingView Extension | ||
|
|
||
| extension AnyLoadingView { | ||
|
|
||
| /// 로딩 사이즈 설정 | ||
| /// - Parameter size: small, large | ||
| func loadingSize(_ size: LoadingViewSize) -> some View { | ||
| self.modifier(LoadingViewSizeModifier(size: size)) | ||
| } | ||
|
|
||
| /// 로딩 isPresented | ||
| func presented(_ isPresented: Binding<Bool>) -> some View { | ||
| self.modifier(LoadingViewIsPresentedModifier(isPresented: isPresented)) | ||
| } | ||
|
|
||
| /// 로딩 메시지 설정 | ||
| func loadingMessage(_ message: String) -> some View { | ||
| self.modifier(LoadingViewMessageModifier(message: message)) | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // | ||
| // LoadingViewSize.swift | ||
| // AppProduct | ||
| // | ||
| // Created by 이예지 on 1/4/26. | ||
| // | ||
|
|
||
| import SwiftUI | ||
|
|
||
| // MARK: - LoadingViewSize | ||
|
|
||
| /// LoadingView 사이즈 유형 (향후 확장용) | ||
| enum LoadingViewSize { | ||
| case small | ||
| case large | ||
|
|
||
| var size: CGSize { | ||
| switch self { | ||
| case .small: | ||
| return CGSize(width: 40, height: 40) | ||
| case .large: | ||
| return CGSize(width: 80, height: 80) | ||
| } | ||
| } | ||
|
|
||
| var font: Font { | ||
| switch self { | ||
| case .small: | ||
| return .app(.footnote, weight: .regular) | ||
| case .large: | ||
| return .app(.body, weight: .bold) | ||
| } | ||
| } | ||
| } |
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.
해당 부분은
controlSize()수정자를 사용해서 아래와 같이 작성하는게 좋을거 같아요