From 99330cceeea6a9068d40ccbe27efb75d240098ef Mon Sep 17 00:00:00 2001 From: sanzaru Date: Mon, 21 Oct 2024 21:48:35 +0200 Subject: [PATCH] Fixed Swift 6 warnings --- Sources/SimpleToast/SimpleToast.swift | 15 ++++++++------- Sources/SimpleToast/SimpleToastModifier.swift | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/SimpleToast/SimpleToast.swift b/Sources/SimpleToast/SimpleToast.swift index 17daba9..335935e 100644 --- a/Sources/SimpleToast/SimpleToast.swift +++ b/Sources/SimpleToast/SimpleToast.swift @@ -15,10 +15,10 @@ struct SimpleToast: ViewModifier { let options: SimpleToastOptions let onDismiss: (() -> Void)? - @State private var timer: Timer? @State private var offset: CGSize = .zero @State private var isInit = false @State private var viewState = false + @State private var cancelable: Cancellable? private let toastInnerContent: SimpleToastContent @@ -118,18 +118,19 @@ struct SimpleToast: ViewModifier { /// Dismiss the sheet after the timeout specified in the options private func dismissAfterTimeout() { if let timeout = options.hideAfter, showToast, options.hideAfter != nil { - DispatchQueue.main.async { [self] in - timer?.invalidate() - timer = Timer.scheduledTimer(withTimeInterval: timeout, repeats: false, block: { _ in dismiss() }) - } + cancelable = Timer.publish(every: timeout, on: .main, in: .common) + .autoconnect() + .sink { _ in + cancelable?.cancel() + dismiss() + } } } /// Dismiss the toast and reset all nessasary parameters private func dismiss() { withAnimation(options.animation) { - timer?.invalidate() - timer = nil + cancelable?.cancel() showToast = false viewState = false offset = .zero diff --git a/Sources/SimpleToast/SimpleToastModifier.swift b/Sources/SimpleToast/SimpleToastModifier.swift index 911f8b5..2a99e16 100644 --- a/Sources/SimpleToast/SimpleToastModifier.swift +++ b/Sources/SimpleToast/SimpleToastModifier.swift @@ -10,6 +10,7 @@ import SwiftUI /// Protocol defining the structure of a SimpleToast view modifier /// The basic building blocks are a boolean value determining whether to show the toast or not and an instance of a SimpleToastOptions object, which is optional. +@MainActor protocol SimpleToastModifier: ViewModifier { var showToast: Bool { get set } var options: SimpleToastOptions? { get }