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

In app toast for SwiftUI #405

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Enjoy
piercifani committed Jan 9, 2025
commit d5531f23521ba5219cebe7890842c6fc8f91171c
24 changes: 21 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "161c6f272582854c9c1b12bc453d667898b414f71cd18576b7b428c8291c83f3",
"originHash" : "e01fc2a70bb4de984f71dbd2e0e7289c76c90bf0ec476aa359b5fee2b75035b4",
"pins" : [
{
"identity" : "bswfoundation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/theleftbit/BSWFoundation.git",
"state" : {
"revision" : "deda02851472d789fb65391e0909641065028986",
"version" : "6.2.0"
"revision" : "399d4c279182a746082233b9e2ee53114bf78710",
"version" : "6.3.0"
}
},
{
@@ -28,6 +28,24 @@
"version" : "12.8.0"
}
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ff0f781cf7c6a22d52957e50b104f5768b50c779",
"version" : "3.10.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.17.4"),
.package(url: "https://github.com/theleftbit/BSWFoundation.git", from: "6.2.0"),
.package(url: "https://github.com/theleftbit/BSWFoundation.git", from: "6.3.0"),
.package(url: "https://github.com/kean/Nuke.git", from: "12.8.0"),
],
targets: [
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import SwiftUI

@available(iOS 17, macOS 14, *)
#Preview {
@Previewable
@State
var state: String? = nil

Button {
state = "HALA MADRID!"
} label: {
Text("Marc es muy del Madrid")
}
.inAppNotification(message: $state)
}

public extension SwiftUI.View {

/// Presents a sheet where the sheet's height is the contained view's intrinsic height
/// **Note:** It doesn't work if `Content` is embedded in a `NavigationView`
/// - Parameters:
/// - isPresented: the Binding that controls the presentation
/// - onDismiss: a callback to be called on dismissal
/// - content: the content to be presented
func inAppNotification(message: Binding<String?>) -> some View {
self.modifier(InAppToastModifier(isPresented: message))
}
}

private struct InAppToastModifier: ViewModifier {

let isPresented: Binding<String?>
@State private var anchorView = UIView()

Check failure on line 34 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find 'UIView' in scope

Check failure on line 34 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find 'UIView' in scope

func body(content: Content) -> some View {
if let value = isPresented.wrappedValue {
presentPopover(value: value)
}

return content
.background(InternalAnchorView(uiView: anchorView))
}

func presentPopover(value: String) {
let view = anchorView
guard let sourceVC = view.next() as UIViewController? else { return }
InAppNotifications.showNotification(
fromVC: sourceVC,
backgroundColor: .green,
image: nil,
title: TextStyler.styler.attributedString(value),
message: nil,
dismissDelay: 2) {
self.isPresented.wrappedValue = nil
}
}

struct InternalAnchorView: UIViewRepresentable {

Check failure on line 59 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find type 'UIViewRepresentable' in scope
let uiView: UIView

Check failure on line 60 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find type 'UIView' in scope

Check failure on line 60 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find type 'UIView' in scope

func makeUIView(context: Self.Context) -> UIView {

Check failure on line 62 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find type 'UIView' in scope

Check failure on line 62 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

'Context' is not a member type of struct 'BSWInterfaceKit.InAppToastModifier.InternalAnchorView'
uiView
}

func updateUIView(_ uiView: UIView, context: Self.Context) { }

Check failure on line 66 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

cannot find type 'UIView' in scope

Check failure on line 66 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

GitHub Actions / build

'Context' is not a member type of struct 'BSWInterfaceKit.InAppToastModifier.InternalAnchorView'
}
}