Closed dankinsoid closed 4 months ago
Hey @dankinsoid, that way you won't be able to use different settings for different chats in the same app. If you need many chats which are all the same, please just create a custom view wrapping the chat with the setting you need and reuse it. Have a nice day
@f3dm76 Hi, why? Environments can be overriden. With environments you can define a default style at the app root view and customize it if needed for any specific chat. It works out of box
Yeah, but if you spread config code for chat across your project, it'll be easy to forget/lose part of it, and then get unexpected results. Also, I will have to use more specific names for all the setup - just using .type
is ok for ChatView modifier, but not ok if it'll become a global thing. In the current approach, you'll be sure to have all you code in one place exactly where it needs to be. What is the benefit of environment approach, compared to using a wrapper?
Benefits:
Strictly speaking, all these reasons are more important for small, highly reusable views like buttons, texts, checkmarks, etc., and they are not as important for a ChatView. However, I like environments a lot; they are highly useful, convenient and perfect suits for any styling.
These are good arguments, however, there are two sides to most of them:
Why would you duplicate everything? Just reuse what you need for every chat only creating params you need to change.
struct MyChatView: View {
// only params you are changing
var showDateHeaders: Bool = true
var body: some View {
ChatView() {
// all the other setup here - once per project
}
}
}
.renderingMode
on Image
. Setting up . tapAvatarClosure
is not at all the same as setting padding and fonts. For a chatTheme
I use the environment as is customary in SwiftUI.The bottomline is - unfortunately I don't like environment approach. Why make global that which can be local? Narrowing the scope is the first thing I've been taught as a programmer. We have encapsulation principal for a reason, and I think making everything global, while convenient, is potentially dangerous and gets hard to track fast.
Summing up:
Now all chat configuration like showDateHeaders, messageUseMarkdown, avatarSize, etc, are simple variables and configured through methods from
ChatView
extension. It means, that it's possible to set them only locally, directly with aChatView
. I suggest to useEnvironmentValues
instead that allows to setup all configs globally for the whole app and override them locally if needed. Example: