function with automatically increasing percentage (watchOS)

I'm working on a tool for my studies and is designed to display all exams and additionally, it has the functionality of automatic study progress. The idea is that when I click on an exam, it automatically gets marked in green and the progress automatically increases in proportion to the ratio of the marked exam to the total ECTS credits of the program.

The app is structured so that upon launching, the sections "Erster Studienabschnitt" (=First Study Phase), "Zweiter Studienabschnitt" (=Second Study Phase), "Freie Studienleistungen" (=Free Study Achievements) appear, each with their own detailed views. The "Erster Studienabschnitt" includes the exams "Privatrecht I", "Öffentliches Recht I", "Strafrecht I", "Österreichische und europäische Rechtsgeschichte", "Romanistische Grundlagen der europäischen Zivilrechtsdogmatik". Privatrecht I, Öffentliches Recht I and Strafrecht I each lead to their own detail view containing the exams. For these, the function works flawlessly.

However, the whole thing does not work at all for the free study achievements. When you go to "Freie Studienleistungen", you see "KS", "AG", "UE", "KK", "VO", "VL", "ECTS aus anderen Studien", "KV", "Repetitorium". These 9 fields also lead to detailed views where one can see individual exams, which should be added to the progress when marked. The issue is that all exams from these 9 views should only count up to a total of 24 ECTS towards the study progress, so beyond 24 ECTS, the percentage of progress does not increase anymore.

The problem occurs, for example, when I mark the exam "AG Europarecht in "AG", which should be worth 2 ECTS, the percentage remains the same and does not increase at all...

Does anyone see a mistake? The studyprogressview is the "main" view where (I guess) something's missing

studyprogressview

agview

freestudyservicesview

example where it's working

Already tried to get help from AI, but did not solve the problem.

I think there's a bit too much code here for us to figure out what's going on. Can you provide a minimum reproducible example? How about explaining exactly where things are going wrong?

Also, "Already tried to get help from AI, but did not solve the problem." Don't resort to "AI" to fix your problems. It doesn't work. Large Language Models work on patterns, and aren't really going to figure out where you're going wrong with this code.

If I had to guess, I would say somerewehre right there's the mistake or something's missing:

    ]
    
    let maxECTSPerSection: [CourseCategory: Int] = [
            .firstSection: 44,
            .secondSection: 172,
            .freeStudy: 24
        ]

        init() {
            loadCourses()
        }

        var progress: Double {
            let completedECTS = courses.filter { $0.isCompleted }.reduce(0) { $0 + $1.ects }
            let totalECTS = 240 // Gesamtwert für ECTS im Studium
            return (Double(completedECTS) / Double(totalECTS)) * 100
        }

    func toggleCourseCompleted(courseID: String) {
        if let index = courses.firstIndex(where: { $0.id == courseID }) {
            courses[index].isCompleted.toggle()
            saveCourses()
        }
    }

    private func completedECTS(for category: CourseCategory) -> Int {
        let completedECTSAsDouble = courses.filter { $0.category == category && $0.isCompleted }.reduce(0.0) { $0 + $1.ects }
        return Int(completedECTSAsDouble)
    }
    }

    // Extension außerhalb der Klassendefinition
    extension StudyProgressViewModel {
        func saveCourses() {
            do {
                let encodedCourses = try JSONEncoder().encode(courses)
                UserDefaults.standard.set(encodedCourses, forKey: "savedCourses")
            } catch {
                print("Fehler beim Speichern der Kurse: \(error)")
            }
        }
        
        
        func loadCourses() {
            guard let savedCoursesData = UserDefaults.standard.data(forKey: "savedCourses") else { return }
            do {
                let decodedCourses = try JSONDecoder().decode([Course].self, from: savedCoursesData)
                DispatchQueue.main.async {
                    self.courses = decodedCourses
                }
            } catch {
                print("Fehler beim Laden der Kurse: \(error)")
            }
        }
    }
extension StudyProgressViewModel {
    // Ihre neue Funktion...
    func updateFreeStudyECTS(selectedCourses: Set<String>) {
        let selectedCoursesECTS = courses.filter { selectedCourses.contains($0.name) && $0.category == .freeStudy }
                                        .reduce(0.0) { $0 + $1.ects }
        print("Aktualisiere Free Study ECTS für ausgewählte Kurse: \(selectedCourses)")

        
        if selectedCoursesECTS <= Double(maxECTSPerSection[.freeStudy] ?? 24) {
            for course in courses where selectedCourses.contains(course.name) && course.category == .freeStudy {
                if !course.isCompleted {
                    toggleCourseCompleted(courseID: course.id)
                }
            }
        } else {
            print("Die Gesamtanzahl der ECTS für freie Studienleistungen darf 24 nicht überschreiten.")
        }
    }
}
extension StudyProgressViewModel {
    func isCourseCompleted(courseName: String) -> Bool {
        return courses.first { $0.name == courseName }?.isCompleted ?? false
    }

    func toggleCourseCompletedByName(courseName: String) {
            print("Looking for course: \(courseName)")
            print("Available courses: \(courses.map { $0.name })")
            if let index = courses.firstIndex(where: { $0.name == courseName }) {
                print("Course found: \(courseName), toggling completion")
                toggleCourseCompleted(courseID: courses[index].id)
            } else {
                print("Course not found: \(courseName)")
            }
        }
    }

Maybe somewhere in the extension, but not sure..

@pabra99 that's not a reproductible example. That's essential, to analyse what occurs.

If I had to guess, I would say somewhere right there's the mistake or something's missing:

Debugging is not about guessing, but about getting evidence.

maybe my friend, but since this is my first ever project, it's not as easy for me as it is for you to get some evidence what the problem could be :)

function with automatically increasing percentage (watchOS)
 
 
Q