Skip to content

Commit 264f17a

Browse files
woko666woko
and
woko
authored
Initialize partial sheet using binding (#67)
* PartialSheetAddModifier initial implementation * Add onDismiss handling to the modifier * Public modifier * Rewritten ViewModifier, added example * Added method comment * Make update fields optional * Update sheet content while shown Co-authored-by: woko <[email protected]>
1 parent 1e89905 commit 264f17a

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

Example/PartialSheetExample.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
3B9841FC24E880870052A996 /* PickerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B9841FA24E880870052A996 /* PickerExample.swift */; };
2727
3B9841FD24E880870052A996 /* DatePickerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B9841FB24E880870052A996 /* DatePickerExample.swift */; };
2828
3BF874DF24E6F4DE004F4550 /* BlurredSheetExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */; };
29+
F8E410DE25015F710064D3A6 /* ViewModifierExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E410DD25015F710064D3A6 /* ViewModifierExample.swift */; };
2930
/* End PBXBuildFile section */
3031

3132
/* Begin PBXFileReference section */
@@ -51,6 +52,7 @@
5152
3B9841FA24E880870052A996 /* PickerExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PickerExample.swift; sourceTree = "<group>"; };
5253
3B9841FB24E880870052A996 /* DatePickerExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatePickerExample.swift; sourceTree = "<group>"; };
5354
3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurredSheetExample.swift; sourceTree = "<group>"; };
55+
F8E410DD25015F710064D3A6 /* ViewModifierExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModifierExample.swift; sourceTree = "<group>"; };
5456
/* End PBXFileReference section */
5557

5658
/* Begin PBXFrameworksBuildPhase section */
@@ -74,6 +76,7 @@
7476
01A013982458E4D800D0F5DD /* ListExample.swift */,
7577
0174F416245962B80053C454 /* PushNavigationExample.swift */,
7678
3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */,
79+
F8E410DD25015F710064D3A6 /* ViewModifierExample.swift */,
7780
);
7881
path = Examples;
7982
sourceTree = "<group>";
@@ -217,6 +220,7 @@
217220
3B9841FD24E880870052A996 /* DatePickerExample.swift in Sources */,
218221
0174F419245A569C0053C454 /* BlurEffectView.swift in Sources */,
219222
1F177AA723E1F0E0006F59D0 /* DragState.swift in Sources */,
223+
F8E410DE25015F710064D3A6 /* ViewModifierExample.swift in Sources */,
220224
1F177AA623E1F0E0006F59D0 /* PartialSheetViewModifier.swift in Sources */,
221225
01A013992458E4D800D0F5DD /* ListExample.swift in Sources */,
222226
1F177A7023E1ECC3006F59D0 /* SceneDelegate.swift in Sources */,

Example/PartialSheetExample/ContentView.swift

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ struct ContentView: View {
6565
destination: BlurredExample(),
6666
label: {Text("Blurred Example")
6767
})
68+
NavigationLink(
69+
destination: ViewModifierExample(),
70+
label: {Text("ViewModifier Example")
71+
})
6872
}
6973
Spacer()
7074
Spacer()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// ViewModifierExample.swift
3+
// PartialSheetExample
4+
//
5+
// Created by woko on 03/09/2020.
6+
// Copyright © 2020 Swift. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct ViewModifierExample: View {
12+
@State var isSheetShown = false
13+
@State var viewContent = "Initial content"
14+
var body: some View {
15+
HStack {
16+
Spacer()
17+
Button(action: {
18+
self.isSheetShown = true
19+
}, label: {
20+
Text("Display the ViewModifier sheet")
21+
})
22+
.padding()
23+
Spacer()
24+
}
25+
.navigationBarTitle("ViewModifier Example")
26+
.navigationViewStyle(StackNavigationViewStyle())
27+
.partialSheet(isPresented: $isSheetShown) {
28+
ViewModifierView(content: self.$viewContent)
29+
.padding(.vertical, 50)
30+
}
31+
}
32+
}
33+
34+
struct ViewModifierExample_Previews: PreviewProvider {
35+
static var previews: some View {
36+
NavigationView {
37+
ViewModifierExample()
38+
}
39+
.addPartialSheet()
40+
.navigationViewStyle(StackNavigationViewStyle())
41+
.environmentObject(PartialSheetManager())
42+
}
43+
}
44+
45+
struct ViewModifierView: View {
46+
@Binding var content: String
47+
48+
var body: some View {
49+
VStack {
50+
Text(content)
51+
Button(action: {
52+
self.content = "Inner content"
53+
}, label: {
54+
Text("Change content")
55+
})
56+
}
57+
}
58+
}

Sources/PartialSheet/PartialSheetManager.swift

+19-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PartialSheetManager: ObservableObject {
3535
}
3636
}
3737
/// The content of the sheet
38-
private(set) var content: AnyView
38+
@Published private(set) var content: AnyView
3939
/// the onDismiss code runned when the partial sheet is closed
4040
private(set) var onDismiss: (() -> Void)?
4141

@@ -54,6 +54,24 @@ public class PartialSheetManager: ObservableObject {
5454
self.isPresented = true
5555
}
5656

57+
/**
58+
Updates some properties of the **Partial Sheet**
59+
- parameter isPresented: If the partial sheet is presented
60+
- parameter content: The content to place inside of the Partial Sheet.
61+
- parameter onDismiss: This code will be runned when the sheet is dismissed.
62+
*/
63+
public func updatePartialSheet<T>(isPresented: Bool? = nil, content: (() -> T)? = nil, onDismiss: (() -> Void)? = nil) where T: View {
64+
if let content = content {
65+
self.content = AnyView(content())
66+
}
67+
if let onDismiss = onDismiss {
68+
self.onDismiss = onDismiss
69+
}
70+
if let isPresented = isPresented {
71+
self.isPresented = isPresented
72+
}
73+
}
74+
5775
/// Close the Partial Sheet and run the onDismiss function if it has been previously specified
5876
public func closePartialSheet() {
5977
self.isPresented = false

Sources/PartialSheet/PartialSheetViewModifier.swift

+39
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,42 @@ extension PartialSheet {
349349
}
350350

351351
}
352+
353+
struct PartialSheetAddView<Base: View, InnerContent: View>: View {
354+
@EnvironmentObject var partialSheetManager: PartialSheetManager
355+
356+
@Binding var isPresented: Bool
357+
let content: () -> InnerContent
358+
let base: Base
359+
360+
@State var model = Model()
361+
362+
var body: some View {
363+
if model.update(value: isPresented) {
364+
DispatchQueue.main.async(execute: updateContent)
365+
}
366+
return base
367+
}
368+
369+
func updateContent() {
370+
partialSheetManager.updatePartialSheet(isPresented: isPresented, content: content, onDismiss: {
371+
self.isPresented = false
372+
})
373+
}
374+
375+
// hack around .onChange not being available in iOS13
376+
class Model {
377+
private var savedValue: Bool?
378+
func update(value: Bool) -> Bool {
379+
guard value != savedValue else { return false }
380+
savedValue = value
381+
return true
382+
}
383+
}
384+
}
385+
386+
public extension View {
387+
func partialSheet<Content: View>(isPresented: Binding<Bool>, @ViewBuilder content: @escaping () -> Content) -> some View {
388+
PartialSheetAddView(isPresented: isPresented, content: content, base: self)
389+
}
390+
}

0 commit comments

Comments
 (0)