Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented 'OnAppFirstRun' View Modifier #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Sources/SwiftfulUI/ViewModifiers/OnAppFirstRunModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// OnAppFirstRunModifier.swift
//
//
// Created by Ricky Stone on 17/11/2023.
//

import SwiftUI

struct OnAppFirstRunModifier: ViewModifier {
let action: @MainActor () -> Void

func body(content: Content) -> some View {
content
.onAppear {
FirstRunManager.shared.executeFirstRunAction(action: action)
}
}
}

extension View {
public func onAppFirstRun(perform action: @MainActor @escaping () -> Void) -> some View {
modifier(OnAppFirstRunModifier(action: action))
}
}

class FirstRunManager {
static let shared = FirstRunManager()
private var hasFirstRunActionExecuted = false

private init() {}

func executeFirstRunAction(action: () -> Void) {
if UserDefaults.standard.bool(forKey: "appHasBeenLaunchedOnce") || hasFirstRunActionExecuted {
return
}

action()
hasFirstRunActionExecuted = true
UserDefaults.standard.set(true, forKey: "appHasBeenLaunchedOnce")
}
}