Skip to content

Commit

Permalink
refactor: BottomSheet ViewModifier 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok committed Aug 16, 2024
1 parent 86b2ff3 commit 00a9651
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@

import SwiftUI

extension View {
public func bottomSheet<
Item: Identifiable,
Content: View
>(
item: Binding<Item?>,
@ViewBuilder content: @escaping (Item) -> Content
) -> some View {
struct BottomSheetViewModifier<
Item: Identifiable,
BottomSheetContent: View
>: ViewModifier {
@Binding var item: Item?
let bottomSheetContent: (Item) -> BottomSheetContent

func body(content: Content) -> some View {
ZStack(alignment: .bottom) {
self
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.zIndex(1)

if let wrappedItem = item.wrappedValue {
if let item {
Global.Color.black.opacity(Global.Opacity._50d)
.ignoresSafeArea()
.onTapGesture {
item.wrappedValue = nil
self.item = nil
}
.transition(
.opacity.animation(.easeInOut)
Expand All @@ -44,24 +45,34 @@ extension View {
DragGesture(minimumDistance: 20)
.onEnded { value in
if value.translation.height > 100 {
withAnimation {
item.wrappedValue = nil
}
self.item = nil
}
}
)

content(wrappedItem)
bottomSheetContent(item)
.frame(maxWidth: .infinity)
.fixedSize(horizontal: false, vertical: true)
.background(Global.Color.white)
}
.frame(maxWidth: .infinity)
.frame(maxHeight: UIScreen.main.bounds.height * 0.9, alignment: .bottom)
.transition(.move(edge: .bottom))
.animation(.spring(duration: 0.4))
.zIndex(3)
}
}
.animation(.spring(duration: 0.4), value: self.item == nil)
}
}

extension View {
public func bottomSheet<
Item: Identifiable,
Content: View
>(
item: Binding<Item?>,
@ViewBuilder content: @escaping (Item) -> Content
) -> some View {
return self.modifier(BottomSheetViewModifier(item: item, bottomSheetContent: content))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@

import SwiftUI

public struct ToastViewModifier<T: Toast>: ViewModifier {
struct ToastViewModifier<T: Toast>: ViewModifier {
@Binding var toast: T?

public init(toast: Binding<T?>) {
init(toast: Binding<T?>) {
_toast = toast
}

public func body(content: Content) -> some View {
func body(content: Content) -> some View {
ZStack(alignment: .bottom ) {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.zIndex(1)

if let toast {
HStack(alignment: .center, spacing: 8) {
Expand All @@ -45,7 +46,7 @@ public struct ToastViewModifier<T: Toast>: ViewModifier {
.onAppear {
if toast.hideAutomatically {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
if self.toast != nil {
if self.toast != nil {
self.toast = nil
}
}
Expand All @@ -54,6 +55,7 @@ public struct ToastViewModifier<T: Toast>: ViewModifier {
.onTapGesture {
self.toast = nil
}
.zIndex(2)
}
}
.animation(.spring(duration: 0.3), value: self.toast)
Expand Down

0 comments on commit 00a9651

Please sign in to comment.