add dataTask for cancel ongoing tasks

This commit is contained in:
desp0o
2025-04-19 22:41:41 +04:00
parent e49dd21fa8
commit aedbccc1d7
5 changed files with 25 additions and 4 deletions
@@ -10,5 +10,18 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>ToastKit</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>ToastKitTests</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
+1 -1
View File
@@ -1,4 +1,4 @@
// swift-tools-version: 6.1
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
+2 -1
View File
@@ -8,7 +8,7 @@ various built-in toast styles like success, warning, info, error, with icons....
You can quickly use ready-made toasts or create your own custom toast view with complete control over layout, colors, animations, icons, and more.
![Static Badge](https://img.shields.io/badge/Swit-6.1-orange) ![Static Badge](https://img.shields.io/badge/iOS-17.0%2B-white) ![Static Badge](https://img.shields.io/badge/Version%20-%201.2.0-skyblue) ![Static Badge](https://img.shields.io/badge/LICENSE-MIT-yellow) ![Static Badge](https://img.shields.io/badge/SPM-SUCCESS-green)
![Static Badge](https://img.shields.io/badge/Swit-6.1-orange) ![Static Badge](https://img.shields.io/badge/iOS-17.0%2B-white) ![Static Badge](https://img.shields.io/badge/Version%20-%201.3.0-skyblue) ![Static Badge](https://img.shields.io/badge/LICENSE-MIT-yellow) ![Static Badge](https://img.shields.io/badge/SPM-SUCCESS-green)
@@ -349,6 +349,7 @@ struct ProfileView: View {
| `innerVpadding` | CGFloat | `10` | Inner vertical padding. |
| `outterHpadding` | CGFloat | `20` | Outer horizontal padding. |
| `stackAligment` | Alignment | `.top` | Stack alignment inside the toast. |
| `isStackMaxHeight` | Bool | `true` | occupies the maximum available height |
| `cornerRadius` | CGFloat | `12` | Corner radius of the toast. |
| `shadowColor` | Color | `.black.opacity(0.2)` | Shadow color. |
| `shadowRadius` | CGFloat | `10` | Radius of the toast's shadow. |
+9 -2
View File
@@ -6,6 +6,7 @@ import SwiftUI
@available(macOS 14.0, *)
@available(iOS 17, *)
public struct CustomToast: View {
@State private var disappearTask: Task<(), Never>?
@Binding var isVisible: Bool
let title: String
let toastColor: ToastColorTypes
@@ -239,12 +240,18 @@ public struct CustomToast: View {
.if(isStackMaxHeight) { $0.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: stackAligment)}
.onChange(of: isVisible) { _, newValue in
if newValue {
disappearTask?.cancel()
if autoDisappear {
Task {
disappearTask = Task {
try? await Task.sleep(nanoseconds: UInt64(autoDisappearDuration * 1_000_000_000))
isVisible = false
if !Task.isCancelled {
isVisible = false
}
}
}
} else {
disappearTask?.cancel()
disappearTask = nil
}
}
.animation(animation, value: isVisible)