Skip to content

Commit 8c36e1f

Browse files
authored
Merge pull request #3 from Ryu0118/feature/delay
Add option to delay display of popups
2 parents fdb9719 + 9ce883d commit 8c36e1f

File tree

5 files changed

+282
-35
lines changed

5 files changed

+282
-35
lines changed

Examples/Examples/ContentView.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import FullscreenPopup
44
struct ContentView: View {
55
@State var isExample1Presented = false
66
@State var isExample2Presented = false
7-
@State var example3Item: MyItem?
7+
@State var isExample3Presented = false
8+
@State var example4Item: MyItem?
89

910
var body: some View {
1011
Color.white
@@ -24,21 +25,30 @@ struct ContentView: View {
2425
}
2526

2627
Button {
27-
example3Item = .init(id: UUID())
28+
isExample3Presented = true
2829
} label: {
29-
Text("Show Alert 3")
30+
Text("Show Delay Alert 3")
31+
}
32+
33+
Button {
34+
example4Item = .init(id: UUID())
35+
} label: {
36+
Text("Show Alert 4")
3037
}
3138
}
3239
.navigationTitle("Title")
3340
.toolbarTitleDisplayMode(.inline)
3441
.popup(isPresented: $isExample1Presented) {
3542
Example1Alert(isPresented: $isExample1Presented)
3643
}
37-
.popup(item: $example3Item) { item in
44+
.popup(isPresented: $isExample3Presented, delay: .seconds(1)) {
45+
Example1Alert(isPresented: $isExample3Presented)
46+
}
47+
.popup(item: $example4Item) { item in
3848
VStack {
3949
Text(item.id.uuidString)
4050
Button {
41-
example3Item = nil
51+
example4Item = nil
4252
} label: {
4353
Text("Close")
4454
}

Sources/FullscreenPopup/AnimatableFullScreenViewModifier.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extension View {
55
func animatableFullScreenCover(
66
isPresented: Binding<Bool>,
77
duration nanoseconds: UInt64,
8+
delay: UInt64? = nil,
89
content: @escaping () -> some View,
910
onAppear: @escaping () -> Void,
1011
onDisappear: @escaping () -> Void
@@ -13,6 +14,7 @@ extension View {
1314
AnimatableFullScreenViewModifier(
1415
isPresented: isPresented,
1516
duration: nanoseconds,
17+
delay: delay,
1618
fullScreenContent: content,
1719
onAppear: onAppear,
1820
onDisappear: onDisappear
@@ -23,6 +25,7 @@ extension View {
2325
func animatableFullScreenCover<Item: Identifiable & Equatable>(
2426
item: Binding<Item?>,
2527
duration nanoseconds: UInt64,
28+
delay: UInt64? = nil,
2629
content: @escaping (Item) -> some View,
2730
onAppear: @escaping () -> Void,
2831
onDisappear: @escaping () -> Void
@@ -31,6 +34,7 @@ extension View {
3134
AnimatableFullScreenItemViewModifier(
3235
item: item,
3336
duration: nanoseconds,
37+
delay: delay,
3438
fullScreenContent: content,
3539
onAppear: onAppear,
3640
onDisappear: onDisappear
@@ -44,19 +48,22 @@ private struct AnimatableFullScreenItemViewModifier<FullScreenContent: View, Ite
4448
@State var isActualPresented: Item?
4549

4650
let nanoseconds: UInt64
51+
let delay: UInt64?
4752
let fullScreenContent: (Item) -> (FullScreenContent)
4853
let onAppear: () -> Void
4954
let onDisappear: () -> Void
5055

5156
init(
5257
item: Binding<Item?>,
5358
duration nanoseconds: UInt64,
59+
delay: UInt64?,
5460
fullScreenContent: @escaping (Item) -> FullScreenContent,
5561
onAppear: @escaping () -> Void,
5662
onDisappear: @escaping () -> Void
5763
) {
5864
self._isUserInstructToPresentItem = item
5965
self.nanoseconds = nanoseconds
66+
self.delay = delay
6067
self.fullScreenContent = fullScreenContent
6168
self.onAppear = onAppear
6269
self.onDisappear = onDisappear
@@ -68,7 +75,14 @@ private struct AnimatableFullScreenItemViewModifier<FullScreenContent: View, Ite
6875
.onChange(of: isUserInstructToPresentItem) { isUserInstructToPresent in
6976
UIView.setAnimationsEnabled(false)
7077
if isUserInstructToPresent != nil {
71-
isActualPresented = isUserInstructToPresent
78+
if let delay {
79+
Task {
80+
try await Task.sleep(nanoseconds: delay)
81+
isActualPresented = isUserInstructToPresent
82+
}
83+
} else {
84+
isActualPresented = isUserInstructToPresent
85+
}
7286
} else {
7387
Task {
7488
try await Task.sleep(nanoseconds: nanoseconds)
@@ -100,19 +114,22 @@ private struct AnimatableFullScreenViewModifier<FullScreenContent: View>: ViewMo
100114
@State var isActualPresented: Bool
101115

102116
let nanoseconds: UInt64
117+
let delay: UInt64?
103118
let fullScreenContent: () -> (FullScreenContent)
104119
let onAppear: () -> Void
105120
let onDisappear: () -> Void
106121

107122
init(
108123
isPresented: Binding<Bool>,
109124
duration nanoseconds: UInt64,
125+
delay: UInt64?,
110126
fullScreenContent: @escaping () -> FullScreenContent,
111127
onAppear: @escaping () -> Void,
112128
onDisappear: @escaping () -> Void
113129
) {
114130
self._isUserInstructToPresent = isPresented
115131
self.nanoseconds = nanoseconds
132+
self.delay = delay
116133
self.fullScreenContent = fullScreenContent
117134
self.onAppear = onAppear
118135
self.onDisappear = onDisappear
@@ -124,7 +141,14 @@ private struct AnimatableFullScreenViewModifier<FullScreenContent: View>: ViewMo
124141
.onChange(of: isUserInstructToPresent) { isUserInstructToPresent in
125142
UIView.setAnimationsEnabled(false)
126143
if isUserInstructToPresent {
127-
isActualPresented = isUserInstructToPresent
144+
if let delay {
145+
Task {
146+
try await Task.sleep(nanoseconds: delay)
147+
isActualPresented = isUserInstructToPresent
148+
}
149+
} else {
150+
isActualPresented = isUserInstructToPresent
151+
}
128152
} else {
129153
Task {
130154
try await Task.sleep(nanoseconds: nanoseconds)

Sources/FullscreenPopup/PopupItemModifier.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ struct PopupItemModifier<Popup: View, Background: View, Item: Identifiable & Equ
1111

1212
let animation: Animation
1313
let duration: UInt64
14+
let delay: UInt64?
1415
let popup: (Item) -> Popup
1516
let background: (Bool) -> Background
1617

1718
init(
1819
item: Binding<Item?>,
1920
duration nanoseconds: UInt64,
21+
delay: UInt64? = nil,
2022
animation: Animation,
2123
@ViewBuilder popup: @escaping (_ item: Item) -> Popup,
2224
@ViewBuilder background: @escaping (_ isPresented: Bool) -> Background
@@ -25,6 +27,7 @@ struct PopupItemModifier<Popup: View, Background: View, Item: Identifiable & Equ
2527
self._isUserInstructToPresent = item
2628
self._item = item
2729
self.duration = nanoseconds
30+
self.delay = delay
2831
self.animation = animation
2932
self.popup = popup
3033
self.background = background
@@ -33,7 +36,8 @@ struct PopupItemModifier<Popup: View, Background: View, Item: Identifiable & Equ
3336
func body(content: Content) -> some View {
3437
content.animatableFullScreenCover(
3538
item: $isUserInstructToPresent,
36-
duration: duration
39+
duration: duration,
40+
delay: delay
3741
) { item in
3842
popup(item)
3943
.frame(maxWidth: .infinity, maxHeight: .infinity)

Sources/FullscreenPopup/PopupModifier.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ struct PopupModifier<Popup: View, Background: View>: ViewModifier {
1010

1111
let animation: Animation
1212
let duration: UInt64
13+
let delay: UInt64?
1314
let popup: (Bool) -> Popup
1415
let background: (Bool) -> Background
1516

1617
init(
1718
isPresented: Binding<Bool>,
1819
duration nanoseconds: UInt64,
20+
delay: UInt64? = nil,
1921
animation: Animation,
2022
@ViewBuilder popup: @escaping (_ isPresented: Bool) -> Popup,
2123
@ViewBuilder background: @escaping (_ isPresented: Bool) -> Background
2224
) {
2325
self._isUserInstructToPresent = isPresented
2426
self.duration = nanoseconds
27+
self.delay = delay
2528
self.animation = animation
2629
self.popup = popup
2730
self.background = background
@@ -30,7 +33,8 @@ struct PopupModifier<Popup: View, Background: View>: ViewModifier {
3033
func body(content: Content) -> some View {
3134
content.animatableFullScreenCover(
3235
isPresented: $isUserInstructToPresent,
33-
duration: duration
36+
duration: duration,
37+
delay: delay
3438
) {
3539
popup(presentationAnimationTrigger)
3640
.frame(maxWidth: .infinity, maxHeight: .infinity)

0 commit comments

Comments
 (0)