swift drag and drop functionality is not working properly

92 views Asked by At

drag and drop functionality is not working properly the tasks are not proper drag and drop on particular position.

    private func addTaskData(document: [QueryDocumentSnapshot]) {
        allTasks.removeAll()
        for query in document {
            let aTask = Task()
            aTask.initWithDict(snapshot: query)
            self.allTasks.append(aTask)
                sortedArray = self.allTasks.sorted(by: { $0.scheduleDate.timeIntervalSince1970 > $1.scheduleDate.timeIntervalSince1970})
            for task in allTasks {
                if (currentTasks.contains(where: { $0.title == task.title }))
                {
                    print(task.title,"contain")
                }
                else
                {
                    if (sortedArray.contains(where: { $0.title == task.title }))
                    {
                        print(task.title,"contain")
                    }
                    else{
                        print(task.title,"not contain")
                        task.scheduleDate = Date()
                        sortedArray.insert(task, at: 0)
                    }
                  
                }
            }
            self.allTasks = sortedArray
        }
        for task in allTasks {
            if isPremium{
                if nextDateString == "12 AM" {
                    print("schedule date is past")
                    if task.isDone == false {
                        isDateUpdate = true
                        isUpdateingDateStatus = true
                    }
                }
            }
            else {
                print("schedule date is current or future")
            }
        }
        updateSelectedTasks()
        taskTableView.reloadData()
    }

this moveRowAt function first time performing drag and drop functionality perfectly but the second time i am performing drag and drop it is not working.
i think this issue occurred because of improper assignment of schedule date like this, positions of tasks before drag and drop :
Test 2 Test 1 Test 3 Test 4 Test 5
and i have dragged and dropped 'Test 1' task it should be like this:
Test 2 Test 3 Test 4 Test 1 Test 5
but it shows tasks like this :
Test 5 Test 1 Test 4 Test 3 Test 2

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        // Update the model
        let movedTask = currentTasks.remove(at: sourceIndexPath.row)
        currentTasks.insert(movedTask, at: destinationIndexPath.row)
        
        for (index, task) in currentTasks.enumerated() {
            let date = Calendar.current.date(byAdding: .minute, value: index , to: Date())!
            task.scheduleDate = date
            db.collection(udid).document(task.id).updateData(["scheduleDate": date])
        }
        
        // Reload table data
        taskTableView.reloadData()
    }
0

There are 0 answers