vendredi 24 mai 2019

Comparing one element to all other elements in an array that fulfil the predicate and then adding similar elements together

I am working on an time-tracking app, I have an array timetracks = timetracksController.getTimetracks() which just returns an array of [Timetrack], the Timetrack type has a property startTime = Date().

I have a dateFormatterService which can return a date in "yyyy-mm-dd" format.

What I need to do is the following: Go trough the timetracks array with a condition that specifies that the date of a timetrack has to be compared to the start date of all other timetracks and if they match, the algorithm needs to add these two (or more) timetracks together (this is done by taking the TimeInterval between timetrack.startTime and timetrack.endTime.

However there is another condition to be met, each empoyee that fills in their own timetracks has a "Workload preference", which is basically how many hours per day the employee prefers to work.

I need to take the whole timetracks duration for each day and compare this to the Workload of an employee.

Practical example: Employee A has 6 hours on 24. May 2019 and his workload preference is 8 hours a day. Therefore this 6-hour timetrack would go into the partialTimetracks array.

Employee B has timetrack1 for 4 hours on 24. May 2019 in the morning and then another timetrack2 for 4 hours on 24. May 2019 in the evening/night. So the algorithm needs to add these two up (since both these timetracks are on 2019-05-24) - so the full day would be 8 hours, therefore these two timetracks would go into the fullTimetracks array.

This is what I am trying to put together so far (will keep it updated), but I just cant get the whole mechanism together.

private func updateTimetracks() {
    let timetracks = timetracksController.getTimetracks()
    let workload = preferencesController.getPreferences().workload.getWorkloadRawValue()
    var fullTimetracks = [Timetrack]()
    var partialTimetracks = [Timetrack]()
    var currentIndex = 0
    for timetrack in timetracks {
        guard currentIndex < timetracks.count else { return }
        while timetracks.count > 2 && dateFormatterService.stringFromDate(timetracks[currentIndex].startTime, format: "yyyy-mm-dd") == 
        dateFormatterService.stringFromDate(timetracks[currentIndex+1], format: "yyyy-mm-dd"){

        }

//            if (timetrack.endTime.timeIntervalSince(timetrack.startTime) / 3600) < Double(workload) {
//                partialTimetracks.append(timetrack)
//            } else {
//                fullTimetracks.append(timetrack)
//            }

    }
    view?.show(fullyTracked: fullTimetracks.map { $0.startTime })
    view?.show(partiallyTracked: partialTimetracks.map { $0.startTime })
}

I appreciate any and all ideas, thank you.

Aucun commentaire:

Enregistrer un commentaire