Overridden date dependency not having an effect on previews #121
Unanswered
LudvigIversen
asked this question in
Q&A
Replies: 1 comment
-
@LudvigIversen In your example, you are overriding the dependencies of the initialization of One thing you can do is hold onto the struct Model {
+ @Dependency(\.date.now) var now
var date = Date("2023-09-25")!
var isBeforeToday: Bool {
- @Dependency(\.date.now) var now
return date.isBeforeDate(now, granularity: .day)
}
} This should fix things. Since this isn't a bug with the library, and more a question of use, I'm going to convert to a discussion. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Let's say as an example that we have this view and model;
Here, the text in the view should be blue if the models date is before todays date. In this case the text would be blue since the day of writing is 2023-09-26. However I also want to see that the logic for the text is correct and that it can turn red. Normally this date would not be hardcoded, but gotten from an API or similar. So, I create a preview, like so;
However, the date would still be blue, when I believe it should be red. The dependency determining what todays date is does not get overwritten.
Checklist
main
branch of this package.Expected behavior
The date dependency should be overwritten by the preview and, in this case, the text should be displayed as red.
Actual behavior
The dependency is not overwritten, and the text remains blue. Instead of using the value I provide in the preview, it instead uses the standard value, that being;
An important note to add, overriding the dependency works in unit tests, the logic also works when compiling normally/in production.
Steps to reproduce
Reuse the example code given here above.
Dependencies version information
No response
Destination operating system
No response
Xcode version information
15.0
Swift Compiler version information
No response
Beta Was this translation helpful? Give feedback.
All reactions