-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mlbonniec/5-create-a-modifier-to-present-i…
…f-needed Create a modifier to present if needed
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// | ||
// OnBoardingKitModel.swift | ||
// | ||
// | ||
// Created by Mathis Le Bonniec on 15/01/2024. | ||
// | ||
|
||
public enum OnBoardingKit {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Sources/OnBoardingKit/Models/OnBoardingPresentationModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// OnBoardingPresentationModel.swift | ||
// | ||
// | ||
// Created by Mathis Le Bonniec on 15/01/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension OnBoardingKit { | ||
public static func resetDatastore() { | ||
UserDefaults.standard.removeObject(forKey: OnBoardingAppStorage.hasOnBoardingBeenPresented.rawValue) | ||
} | ||
} | ||
|
||
internal enum OnBoardingAppStorage: String { | ||
case hasOnBoardingBeenPresented | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// PresentOnBoarding.swift | ||
// | ||
// | ||
// Created by Mathis Le Bonniec on 15/01/2024. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct OnBoardingPresentation: ViewModifier { | ||
// MARK: Properties | ||
public let onBoarding: OnBoarding | ||
public let mode: Mode | ||
public let action: () -> Void | ||
public enum Mode { | ||
case sheet, fullscreen | ||
} | ||
|
||
// MARK: Reactive Properties | ||
@State private var isPresented: Bool = false | ||
@AppStorage(OnBoardingAppStorage.hasOnBoardingBeenPresented.rawValue) private var hasBeenPresented: Bool = false | ||
|
||
// MARK: Body | ||
public func body(content: Content) -> some View { | ||
switch mode { | ||
case .sheet: | ||
initialContent(content: content) | ||
.sheet(isPresented: $isPresented) { | ||
onBoardingView | ||
.interactiveDismissDisabled() | ||
} | ||
case .fullscreen: | ||
initialContent(content: content) | ||
.fullScreenCover(isPresented: $isPresented) { | ||
onBoardingView | ||
} | ||
} | ||
} | ||
|
||
private var onBoardingView: OnBoardingView { | ||
OnBoardingView(onBoarding) { | ||
hasBeenPresented = true | ||
isPresented = false | ||
action() | ||
} | ||
} | ||
|
||
private func initialContent(content: Content) -> some View { | ||
content | ||
.onAppear { | ||
if !hasBeenPresented { | ||
isPresented = true | ||
} | ||
} | ||
.onChange(of: hasBeenPresented) { | ||
if !hasBeenPresented { | ||
isPresented = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
public func presentOnBoarding( | ||
_ onBoarding: OnBoarding, | ||
mode: OnBoardingPresentation.Mode = .fullscreen, | ||
action: @escaping () -> Void = {} | ||
) -> some View { | ||
self | ||
.modifier(OnBoardingPresentation(onBoarding: onBoarding, mode: mode, action: action)) | ||
} | ||
} |