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

Alert is deprecated in current version #19

Open
jeemyeong opened this issue Apr 17, 2023 · 1 comment
Open

Alert is deprecated in current version #19

jeemyeong opened this issue Apr 17, 2023 · 1 comment
Labels
adjustment Changes to existing functionality

Comments

@jeemyeong
Copy link

https://developer.apple.com/documentation/swiftui/alert

I just wanted to give you a heads up that the Alert component used in your package is deprecated in the latest version of SwiftUI. I've created a custom alert as a temporary solution, but it would be awesome if you could update the package to support the current SwiftUI version.

Thanks for creating this package - it's been super helpful for my app, and I really appreciate your work. Looking forward to seeing the update!

Cheers!

// CustomAlert.swift
import SwiftUI

public class CustomAlert: ObservableObject {
    let title: Text
    let message: AnyView
    let content: AnyView

    public init(title: Text, message: some View, content: some View) {
        self.title = title
        self.message = AnyView(message)
        self.content = AnyView(content)
    }

    public init(title: Text, message: some View) {
        self.title = title
        self.message = AnyView(message)
        self.content = AnyView(Button("OK") {})
    }
}
// CustomAlertContext.swift
import SwiftUI

public class CustomAlertContext: ObservableObject {

    public init() {}

    @Published public var isActive = false

    @Published public internal(set) var alertObject: CustomAlert? {
        didSet { isActive = alertObject != nil }
    }

    public var isActiveBinding: Binding<Bool> {
        .init(get: { self.isActive },
                set: { self.isActive = $0 }
        )
    }

    public func dismiss() {
        isActive = false
    }

    public func present(_ alert: CustomAlert) {
        self.alertObject = alert
    }
}
// View+CustomAlert.swift
import SwiftUI

public extension View {

    /**
     Present an alert from a certain context. The alert will
     be presented when the context is active.
     */
    func customAlert(context: CustomAlertContext) -> some View {
        alert(context.alertObject?.title ?? Text(""), isPresented: context.isActiveBinding) {
            context.alertObject?.content
        } message: {
            context.alertObject?.message ?? AnyView(Text(""))
        }
    }
}
@danielsaidi danielsaidi added the adjustment Changes to existing functionality label Apr 17, 2023
@danielsaidi
Copy link
Owner

Hi @jeemyeong

Happy to hear that the library has helped you out! Thank you for notifying me regarding the deprecated API usage - I'll fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adjustment Changes to existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants