I need to create a swipe down gesture that runs a operate when detected.
.gesture(
DragGesture()
.onChanged { worth in
if worth.translation.peak > 0 && abs(worth.translation.peak) > abs(worth.translation.width) {
addTask()
}
}
)
that is the closest i obtained however with this implementation it makes the swipeActions attatched to the forEach gradual and laggy.
is there a greater manner ?
Full code:
import SwiftUI
import SwiftData
import UserNotifications
// Customized Yellow
let yellowCustom = Shade(pink: 1, inexperienced: 0.811, blue: 0, opacity: 1.0)
struct ContentView: View {
//SwiftData
@Surroundings(.modelContext) var modelContext
@Question(kind: [SortDescriptor(Task.complete), SortDescriptor(Task.date, order: .reverse)]) var job: [Task]
@State personal var path = [Task]()
let firstLaunchKey = "hasLaunchedBefore"
var physique: some View {
NavigationStack(path: $path) {
Listing {
ForEach(job) { job in
TasksView(job: job)
.strikethrough(job.full)
.foregroundColor(job.full ? .grey : .main)
//Accomplished Gesture
.swipeActions(edge: .main) {
if job.full {
Button("Not Accomplished") {
notDone(job: job)
}
.tint(.yellow)
} else {
Button("Accomplished") {
completed(job: job)
}
.tint(.inexperienced)
}
}
}
//Delete Gesture
.onDelete(carry out: deleteTask)
}
.animation(.default, worth: job)
//addTask Gesture
.gesture(
DragGesture()
.onChanged { worth in
if worth.translation.peak > 0 && abs(worth.translation.peak) > abs(worth.translation.width) {
addTask()
}
}
)
.navigationTitle("Duties")
}
.onAppear {
requestNotificationPermission()
checkFirstLaunch()
}
}
// addTask
func addTask() {
let job = Process()
isEditing = true
modelContext.insert(job)
}
// deleteTask
func deleteTask(_ indexSet: IndexSet) {
for index in indexSet {
let job = job[index]
modelContext.delete(job)
}
}
// swipeDone
func completed(job: Process) {
job.full = true
}
// swipeNotDone
func notDone(job: Process) {
job.full = false
}
//Notifications permission
func requestNotificationPermission() {
UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .badge, .sound]) { success, error in
if success {
print("Permission granted")
} else if let error = error {
print(error.localizedDescription)
}
}
}
//Checks if consumer launched the app earlier than
func checkFirstLaunch() {
if !UserDefaults.normal.bool(forKey: firstLaunchKey) {
// First launch, initialize duties
initializeTasksWithCustom()
// Set the flag to true so this does not run once more
UserDefaults.normal.set(true, forKey: firstLaunchKey)
}
}
//Information duties
func initializeTasksWithCustom() {
let customTask1 = Process()
customTask1.title = "swipe proper to mark us uncompleted"
customTask1.full = true
let customTask2 = Process()
customTask2.title = "Swipe proper to finish"
let customTask3 = Process()
customTask3.title = "Swipe left to delete"
modelContext.insert(customTask1)
modelContext.insert(customTask2)
modelContext.insert(customTask3)
attempt? modelContext.save()
}
}
//Kind Boolean
extension Bool: Comparable {
public static func <(lhs: Self, rhs: Self) -> Bool {
// the one true inequality is fake < true
!lhs && rhs
}
}