Skip to content

Commit

Permalink
Merge pull request #6 from mlbonniec/5-create-a-modifier-to-present-i…
Browse files Browse the repository at this point in the history
…f-needed

Create a modifier to present if needed
  • Loading branch information
mlbonniec authored Jan 15, 2024
2 parents a5239f7 + f9ac184 commit 350b274
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Sources/OnBoardingKit/Models/OnBoardingKitModel.swift
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 {}
2 changes: 1 addition & 1 deletion Sources/OnBoardingKit/Models/OnBoardingModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import SwiftUI

public enum OnBoardingKit {
extension OnBoardingKit {
public struct Feature: Identifiable {
public var id: UUID { UUID() }

Expand Down
18 changes: 18 additions & 0 deletions Sources/OnBoardingKit/Models/OnBoardingPresentationModel.swift
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
}
72 changes: 72 additions & 0 deletions Sources/OnBoardingKit/Modifiers/PresentOnBoarding.swift
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))
}
}

0 comments on commit 350b274

Please sign in to comment.