danielsaidi / SystemNotification

SystemNotification is a Swift SDK that helps you mimic the native iOS system notification in SwiftUI.
MIT License
442 stars 15 forks source link

SystemNotification shows on top of the parent as opposed to the currently presented Modal view #8

Closed devraj closed 1 year ago

devraj commented 2 years ago

This is again more a discussion than a bug report

I am using SystemNotification and AlertContext and SheetContext from SwiftUIKit and they are configured on a NavigationView as such

    var body: some View {
        NavigationView {
            ControllerPickerView()
                .environmentObject(self.sheetContext)
                .environmentObject(self.notificationContext)
                .environmentObject(self.alertContext)
        }
        .navigationViewStyle(.automatic)
        .alert(context: self.alertContext)
        .sheet(context: self.sheetContext)
        .systemNotification(self.notificationContext)
}

All works well when the Children call the Sheet, Alert or SystemNotification until I present a sheet

    private func showControllerSettings() {
        let controllerSettings = ControllerSettingsView(sprinklerController:
                                                            self.sprinklerController)
                                    .environmentObject(self.sheetContext)
                                    .environmentObject(self.alertContext)
                                    .environmentObject(self.notificationContext)
        self.sheetContext.present(controllerSettings)
    }

if I raise a SystemNotification from the Modal, the notification does show but on the ControllerPicker view i.e behind the Modal view. This is the case for AlertContext as well, where it essentially does not show the Alert when triggered from the Modal.

The modal does have a NavigationView (not sure if this has an effect)

The only way I can get the SystemNotification to show is to apply the .systemNotification modifier to the NavigationView in the modal, resulting in something like this:

IMG_2B1DE0155256-1

I am wondering if this is the expected behaviour and if not what might I be configuring incorrectly to use SystemNotification from in a Modal

danielsaidi commented 2 years ago

This is strange. I was sure that I had already created an issue regarding presenting notifications from a sheet, which today requires some manual handling.

For instance, if you have a view that is wrapped in a navigation view, and applies a system notification to this navigation view, the notification will be visible from all screens that use this navigation view.

If you look at the demo app, you can see a similar approach, where applying the system notification modifier to a tab view makes the notification visible even if you switch tab.

However, when presenting a sheet, things get tricker. If you want to use the same notification context everywhere, for instance to ensure that system-wide notifications are always displayed, you must consider a few things.

First of all, you must apply the system notification modifier to the new view hierarchy, since the navigation view or tab view on the root level will be covered by the sheet.

However, and this is tricker, applying the same notification context to both the root level and the sheet, means that both notifications will be presented, which looks really strange on an iPad, where the sheet is a floating rect mid-screen.

Until the library has support for this, my suggestion is to experiment and have a look at the demo. For instance, I think the notification looks strange in your image above, where the notification is too far down. This doesn't happen in the demo.

Good luck!