feat(android): implement snackbar overlay behavior

This commit is contained in:
Daniel Arantes Loverde
2026-04-17 14:39:29 -03:00
parent 0ebb854213
commit 9b00c472dc
3 changed files with 102 additions and 4 deletions

View File

@@ -3,13 +3,51 @@ import SwiftUI
struct SnackbarOverlay: View {
#if os(Android)
let center: SnackbarCenter
@State var current: SnackbarMessage?
@State var observer: Any?
#else
@ObservedObject var center: SnackbarCenter
#endif
var body: some View {
#if os(Android)
EmptyView()
VStack {
if let message = current {
HStack(spacing: 10) {
if let icon = message.iconSystemName, !icon.isEmpty {
Image(systemName: icon)
.font(.system(size: 16, weight: .semibold))
}
Text(message.title)
.font(AppTypography.heading3)
.multilineTextAlignment(.leading)
.lineLimit(3)
Spacer(minLength: 0)
}
.foregroundStyle(Color.white)
.padding(.top, 14)
.padding(.horizontal, 16)
.padding(.bottom, 16)
.frame(maxWidth: .infinity, alignment: .leading)
.background(message.style.backgroundColor)
.appContentShape(Rectangle())
.onTapGesture {
center.handleTap()
}
.transition(.move(edge: .top).combined(with: .opacity))
.zIndex(999)
}
Spacer()
}
.animation(.spring(response: 0.3, dampingFraction: 0.9), value: current?.id)
.allowsHitTesting(current != nil)
.onAppear {
current = center.current
attachObserverIfNeeded()
}
.onDisappear {
detachObserver()
}
#else
VStack {
if let message = center.current {
@@ -43,4 +81,25 @@ struct SnackbarOverlay: View {
.allowsHitTesting(center.current != nil)
#endif
}
#if os(Android)
private func attachObserverIfNeeded() {
guard observer == nil else { return }
observer = NotificationCenter.default.addObserver(
forName: .snackbarDidChange,
object: nil,
queue: nil
) { _ in
Task { @MainActor in
current = center.current
}
}
}
private func detachObserver() {
guard let observer else { return }
NotificationCenter.default.removeObserver(observer)
self.observer = nil
}
#endif
}

View File

@@ -1,11 +1,16 @@
import Foundation
import SwiftUI
extension Notification.Name {
static let snackbarDidChange = Notification.Name("snackbarDidChange")
}
#if os(Android)
@MainActor
final class SnackbarCenter {
static let shared = SnackbarCenter()
var current: SnackbarMessage?
private var dismissTask: Task<Void, Never>?
func show(
title: String,
@@ -14,11 +19,40 @@ final class SnackbarCenter {
duration: TimeInterval = 3.5,
action: (() -> Void)? = nil
) {
dismissTask?.cancel()
current = SnackbarMessage(
title: title,
style: style,
iconSystemName: icon,
duration: duration,
action: action
)
notifyChange()
dismissTask = Task { [weak self] in
let nanos = UInt64(max(0.2, duration) * 1_000_000_000)
try? await Task.sleep(nanoseconds: nanos)
guard !Task.isCancelled else { return }
self?.dismiss(animated: true)
}
}
func handleTap() {
let action = current?.action
dismiss(animated: true)
action?()
}
func handleTap() {}
func dismiss(animated: Bool) {}
func dismiss(animated: Bool) {
dismissTask?.cancel()
dismissTask = nil
current = nil
notifyChange()
}
private func notifyChange() {
NotificationCenter.default.post(name: .snackbarDidChange, object: nil)
}
}
#else
@MainActor