remove internal dismiss timer from snackbar

This commit is contained in:
Satindar Dhillon
2022-04-26 21:25:08 -07:00
parent 502546eace
commit ebc0b7573d
2 changed files with 17 additions and 58 deletions

View File

@ -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

View File

@ -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<Presenting>(
init<PresentingView>(
isShowing: Binding<Bool>,
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<Bool>,
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<Bool>, message: String?) -> some View {
Snackbar(isShowing: isShowing, presentingView: self, text: Text(message ?? ""))
}
}