diff --git a/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift b/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift index fdb5533ee..596ff4d69 100644 --- a/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/RootView/RootView.swift @@ -60,20 +60,17 @@ struct InnerRootView: View { } }) #endif - .snackBar( - isShowing: $viewModel.showSnackbar, - text: Text(viewModel.snackbarMessage ?? "") - ) - // Adding an extra dispatch to dismiss the snackbar since the internal timer sometimes fails - .onChange(of: viewModel.showSnackbar) { newValue in - if newValue { - DispatchQueue.main.asyncAfter(deadline: .now() + 3) { - withAnimation { - viewModel.showSnackbar = false + .snackBar(isShowing: $viewModel.showSnackbar, message: viewModel.snackbarMessage) + // Schedule the dismissal every time we present the snackbar. + .onChange(of: viewModel.showSnackbar) { newValue in + if newValue { + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + withAnimation { + viewModel.showSnackbar = false + } } } } - } #if os(iOS) .customAlert(isPresented: $viewModel.showPushNotificationPrimer) { pushNotificationPrimerView diff --git a/apple/OmnivoreKit/Sources/Views/SnackBar.swift b/apple/OmnivoreKit/Sources/Views/SnackBar.swift index 89b8b86f5..6c8382d98 100644 --- a/apple/OmnivoreKit/Sources/Views/SnackBar.swift +++ b/apple/OmnivoreKit/Sources/Views/SnackBar.swift @@ -2,35 +2,25 @@ import SwiftUI public struct Snackbar: View { @Binding var isShowing: Bool - private let presenting: AnyView + private let presentingView: AnyView private let text: Text - private let actionText: Text? - private let action: (() -> Void)? - - private var isBeingDismissedByAction: Bool { - actionText != nil && action != nil - } @Environment(\.colorScheme) private var colorScheme: ColorScheme - init( + init( isShowing: Binding, - presenting: Presenting, - text: Text, - actionText: Text? = nil, - action: (() -> Void)? = nil - ) where Presenting: View { + presentingView: PresentingView, + text: Text + ) where PresentingView: View { self._isShowing = isShowing - self.presenting = AnyView(presenting) + self.presentingView = AnyView(presentingView) self.text = text - self.actionText = actionText - self.action = action } public var body: some View { GeometryReader { geometry in ZStack(alignment: .center) { - self.presenting + presentingView VStack { Spacer() if self.isShowing { @@ -39,17 +29,6 @@ public struct Snackbar: View { .font(.appCallout) .foregroundColor(self.colorScheme == .light ? .appGrayText : .appTextDefault) Spacer() - if self.actionText != nil, self.action != nil { - self.actionText! - .bold() - .foregroundColor(self.colorScheme == .light ? .appGrayText : .appTextDefault) - .onTapGesture { - self.action?() - withAnimation { - self.isShowing = false - } - } - } } .padding() .frame(width: min(380, geometry.size.width * 0.96), height: 44) @@ -58,14 +37,6 @@ public struct Snackbar: View { .offset(x: 0, y: -8) .shadow(color: .gray, radius: 2) .animation(Animation.spring()) - .onAppear { - guard !self.isBeingDismissedByAction else { return } - DispatchQueue.main.asyncAfter(deadline: .now() + 2) { - withAnimation { - self.isShowing = false - } - } - } } } } @@ -74,16 +45,7 @@ public struct Snackbar: View { } public extension View { - func snackBar( - isShowing: Binding, - text: Text, - actionText: Text? = nil, - action: (() -> Void)? = nil - ) -> some View { - Snackbar(isShowing: isShowing, - presenting: self, - text: text, - actionText: actionText, - action: action) + func snackBar(isShowing: Binding, message: String?) -> some View { + Snackbar(isShowing: isShowing, presentingView: self, text: Text(message ?? "")) } }