Skip to content

Commit

Permalink
Initialize partial sheet using binding (#67)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
woko666 and woko authored Sep 12, 2020
1 parent 1e89905 commit 264f17a
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Example/PartialSheetExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
3B9841FC24E880870052A996 /* PickerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B9841FA24E880870052A996 /* PickerExample.swift */; };
3B9841FD24E880870052A996 /* DatePickerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B9841FB24E880870052A996 /* DatePickerExample.swift */; };
3BF874DF24E6F4DE004F4550 /* BlurredSheetExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */; };
F8E410DE25015F710064D3A6 /* ViewModifierExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E410DD25015F710064D3A6 /* ViewModifierExample.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -74,6 +76,7 @@
01A013982458E4D800D0F5DD /* ListExample.swift */,
0174F416245962B80053C454 /* PushNavigationExample.swift */,
3BF874DE24E6F4DE004F4550 /* BlurredSheetExample.swift */,
F8E410DD25015F710064D3A6 /* ViewModifierExample.swift */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -217,6 +220,7 @@
3B9841FD24E880870052A996 /* DatePickerExample.swift in Sources */,
0174F419245A569C0053C454 /* BlurEffectView.swift in Sources */,
1F177AA723E1F0E0006F59D0 /* DragState.swift in Sources */,
F8E410DE25015F710064D3A6 /* ViewModifierExample.swift in Sources */,
1F177AA623E1F0E0006F59D0 /* PartialSheetViewModifier.swift in Sources */,
01A013992458E4D800D0F5DD /* ListExample.swift in Sources */,
1F177A7023E1ECC3006F59D0 /* SceneDelegate.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Example/PartialSheetExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ struct ContentView: View {
destination: BlurredExample(),
label: {Text("Blurred Example")
})
NavigationLink(
destination: ViewModifierExample(),
label: {Text("ViewModifier Example")
})
}
Spacer()
Spacer()
Expand Down
58 changes: 58 additions & 0 deletions Example/PartialSheetExample/Examples/ViewModifierExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ViewModifierExample.swift
// PartialSheetExample
//
// Created by woko on 03/09/2020.
// Copyright © 2020 Swift. All rights reserved.
//

import SwiftUI

struct ViewModifierExample: View {
@State var isSheetShown = false
@State var viewContent = "Initial content"
var body: some View {
HStack {
Spacer()
Button(action: {
self.isSheetShown = true
}, label: {
Text("Display the ViewModifier sheet")
})
.padding()
Spacer()
}
.navigationBarTitle("ViewModifier Example")
.navigationViewStyle(StackNavigationViewStyle())
.partialSheet(isPresented: $isSheetShown) {
ViewModifierView(content: self.$viewContent)
.padding(.vertical, 50)
}
}
}

struct ViewModifierExample_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ViewModifierExample()
}
.addPartialSheet()
.navigationViewStyle(StackNavigationViewStyle())
.environmentObject(PartialSheetManager())
}
}

struct ViewModifierView: View {
@Binding var content: String

var body: some View {
VStack {
Text(content)
Button(action: {
self.content = "Inner content"
}, label: {
Text("Change content")
})
}
}
}
20 changes: 19 additions & 1 deletion Sources/PartialSheet/PartialSheetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PartialSheetManager: ObservableObject {
}
}
/// The content of the sheet
private(set) var content: AnyView
@Published private(set) var content: AnyView
/// the onDismiss code runned when the partial sheet is closed
private(set) var onDismiss: (() -> Void)?

Expand All @@ -54,6 +54,24 @@ public class PartialSheetManager: ObservableObject {
self.isPresented = true
}

/**
Updates some properties of the **Partial Sheet**
- parameter isPresented: If the partial sheet is presented
- parameter content: The content to place inside of the Partial Sheet.
- parameter onDismiss: This code will be runned when the sheet is dismissed.
*/
public func updatePartialSheet<T>(isPresented: Bool? = nil, content: (() -> T)? = nil, onDismiss: (() -> Void)? = nil) where T: View {
if let content = content {
self.content = AnyView(content())
}
if let onDismiss = onDismiss {
self.onDismiss = onDismiss
}
if let isPresented = isPresented {
self.isPresented = isPresented
}
}

/// Close the Partial Sheet and run the onDismiss function if it has been previously specified
public func closePartialSheet() {
self.isPresented = false
Expand Down
39 changes: 39 additions & 0 deletions Sources/PartialSheet/PartialSheetViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,42 @@ extension PartialSheet {
}

}

struct PartialSheetAddView<Base: View, InnerContent: View>: View {
@EnvironmentObject var partialSheetManager: PartialSheetManager

@Binding var isPresented: Bool
let content: () -> InnerContent
let base: Base

@State var model = Model()

var body: some View {
if model.update(value: isPresented) {
DispatchQueue.main.async(execute: updateContent)
}
return base
}

func updateContent() {
partialSheetManager.updatePartialSheet(isPresented: isPresented, content: content, onDismiss: {
self.isPresented = false
})
}

// hack around .onChange not being available in iOS13
class Model {
private var savedValue: Bool?
func update(value: Bool) -> Bool {
guard value != savedValue else { return false }
savedValue = value
return true
}
}
}

public extension View {
func partialSheet<Content: View>(isPresented: Binding<Bool>, @ViewBuilder content: @escaping () -> Content) -> some View {
PartialSheetAddView(isPresented: isPresented, content: content, base: self)
}
}

0 comments on commit 264f17a

Please sign in to comment.