Reboot needed to set color

I have code that sets a displayed text to red if the date is today or sooner (the displayed date has passed).

          // If we're at or past the expiration date,
          // display it in red.
          let expired = item.expirationDate < Date()
          let isToday = Calendar.current.isDateInToday(item.expirationDate)

          Text("\(item.expirationDate, formatter: dateFormatter)")
            .foregroundColor(expired || isToday ? .red : .none)

Today a displayed date of today did not show up in red, even though this had worked previously. I decided to do what I usually do for Windows problems: reboot my phone.

After the reboot, the date correctly displayed in red.

Is this an iOS bug?

I doubt it is a bug.

What format are you storing expirationDate and how are you assigning the date to it for the purposes of testing? Is it just Year, month and day or is it Year Month day hours minutes and seconds

The format is Date and I’m just using year, month, and day.

Maybe since your item.expirationDate is in the format year, month, day you should compare with current date() formatted the same way so there is not additional time (hours, minutes, seconds).

Thanks for the suggestion.

The date is only displayed in m-d-y format. It’s stored as a Date structure.

I have a new clue. If I add a new item, causing the list to be redisplayed, the date turns red. I suspect the problem is that, when I open the app, the code is not executed until something causes the list to be refreshed.

So, how can I refresh the initial page of the app when the app is opened. I can do this by flicking it out of the home screen, but how can I do that programmatically?

The problem seems to be that, when starting the app, iOS sees that the View is intact and just displays it as it was when the app was exited. I’m trying to find something that will be called when the app is started, so I can do this:

    .onAppear()
    {
      // If we're at or past the expiration date,
      // display it in red.
      let expired = item.expirationDate < Date()
      let isToday = Calendar.current.isDateInToday(item.expirationDate)
      dateInRed = expired || isToday
    }
          .
          .
          .
          Text("\(item.expirationDate, formatter: dateFormatter)")
            .foregroundColor(dateInRed ? .red : .none)

dateInRed is an @State variable and my theory is that, by modifying it, it causes iOS to refresh the view. The problem is that onAppear doesn’t do the job. It does not activate when the app is started after the first activation.

So, my question is, is there something that will do the job the onAppear isn’t doing? Some way of determining that the app was started.

I’m getting desperate. I added a Text View to my main screen that displays today’s date. The problem is that the next morning I start my app and it still shows yesterday’s date. This stays like that until I do something that causes the view to refresh. I would like to have the home view refresh when the app starts.

I know that this is possible because the calendar app shows the correct date when I activate it in the morning.

Any ideas?