Search code examples
swiftswiftuicalendarprogress

Progress Calendar in SwiftUI


I'm building a progress calendar feature in my app. There is a progress bar in my app, if the user reaches 100% at the current day, I want to show them a checkmark in the calendar so they can see in which days they completed their goal. What would be the way to build it? I guess I can have a variable for each day of the month and make it true if the user reaches 100% for that day, but that seems like a really bad solution.

Here is my calendar usage.

CalendarView(interval: year) { date in
  if calendar.component(.day, from: date) == calendarCurrent.component(.day, from: currentDate) {
    ZStack {
      Text("30")
        .hidden()
        .padding(8)
        .overlay(
          RoundedRectangle(cornerRadius: 10, style: .continuous)
            .stroke(Color(red: 50/255, green: 104/255, blue: 144/255), style: StrokeStyle(lineWidth: 2))
        )
        .padding(.vertical, 4)
        .overlay(
          Text(String(self.calendar.component(.day, from: date))).bold()
        )
        .foregroundColor(.white)
    }
  } else {
    ZStack {
      Text("30")
        .hidden()
        .padding(8)
        .background(Color(red: 50/255, green: 104/255, blue: 144/255))
        .clipShape(RoundedRectangle(cornerRadius: 10))
        .padding(.vertical, 4)
        .overlay(
          Text(String(self.calendar.component(.day, from: date))).bold()
        )
        .foregroundColor(.white)
    }
  }
}

Solution

  • I was able to fix this problem by saving the data into a dictionary and update its value daily.