Skip to content

Commit

Permalink
Fixed the accuracy bug (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
keertirajmalik committed Sep 27, 2022
1 parent 0425598 commit 4f3f518
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Goal App/Controllers/HomeViewController.swift
Expand Up @@ -92,7 +92,6 @@ extension HomeViewController {
}

private func progressCircleSetup() {
let activityPercetage = Float(0.60)
overDueProgress.trackClr = UIColor.systemGray6
overDueProgress.progressClr = UIColor.red
completionRateProgress.trackClr = UIColor.systemGray6
Expand All @@ -101,7 +100,7 @@ extension HomeViewController {
accuracyProgress.progressClr = UIColor.systemGreen
updateCompletionRateProgressCircle()
updateOverDueRateProgressCircle()
accuracyProgressPercentage.text = "\(Int(activityPercetage * 100))"
updateAccuracyRateProgressCircle()
}

private func updateCompletionRateProgressCircle() {
Expand All @@ -119,6 +118,14 @@ extension HomeViewController {
overDueProgress.setProgressWithAnimation(duration: 0.75, value: goalFetchService.overDueRateCalculator(originalGoals: originalGoals))
}
}

private func updateAccuracyRateProgressCircle() {
Task {
originalGoals = await goalFetchService.fetchAllGoals()
accuracyProgressPercentage.text = "\(Int(goalFetchService.accuracyRateCalculator(originalGoals: originalGoals) * 100))"
accuracyProgress.setProgressWithAnimation(duration: 0.75, value: goalFetchService.accuracyRateCalculator(originalGoals: originalGoals))
}
}
}

extension HomeViewController {
Expand Down
21 changes: 17 additions & 4 deletions Goal App/Services/GoalsDataFetchService.swift
Expand Up @@ -15,6 +15,7 @@ protocol GoalDataFetch {
func completionRateCalculator(originalGoals: [Goal]?) -> Float
func overDueRateCalculator(originalGoals: [Goal]?) -> Float
func updateGoalsCompleteStatus(id: String?, completed: Bool?)
func accuracyRateCalculator(originalGoals: [Goal]?) -> Float
}

class GoalsDataFetchService: GoalDataFetch {
Expand Down Expand Up @@ -53,14 +54,26 @@ class GoalsDataFetchService: GoalDataFetch {
}

func overDueRateCalculator(originalGoals: [Goal]?) -> Float {
let overdueTaskCount = originalGoals?.filter { task in
let overdueGoalCount = originalGoals?.filter { task in
task.goalDueDate > Date() && task.completed == false
}.count
let totoalTaskCount = originalGoals?.count
if totoalTaskCount == 0 {
let totoalGoalCount = originalGoals?.count
if totoalGoalCount == 0 {
return 0
}
return Float(overdueGoalCount ?? 0) / Float(totoalGoalCount ?? 0)
}

func accuracyRateCalculator(originalGoals: [Goal]?) -> Float {
let overdueGoalCount = originalGoals?.filter { task in
task.goalDueDate > Date() && task.completed == false
}.count ?? 0
let completedGoalCount = originalGoals?.filter { task in task.completed == true }.count ?? 0
let totoalGoalCount = originalGoals?.count ?? 0
if totoalGoalCount == 0 {
return 0
}
return Float(overdueTaskCount ?? 0) / Float(totoalTaskCount ?? 0)
return Float(completedGoalCount - overdueGoalCount) / Float(totoalGoalCount)
}

func updateGoalsCompleteStatus(id: String?, completed: Bool?) {
Expand Down

0 comments on commit 4f3f518

Please sign in to comment.