-
Notifications
You must be signed in to change notification settings - Fork 43
Support custom metric card icons from URLs #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,12 +18,16 @@ | |
| limitations under the License. | ||
| */ | ||
|
|
||
| import AppKit | ||
| import DataSource | ||
| import Model | ||
| import SwiftUI | ||
|
|
||
| struct CustomMetricsCardView: View { | ||
| var snapshot: CustomMetricsSnapshot | ||
| var isFailed: Bool | ||
| @Environment(\.appDependencies) private var appDependencies | ||
| @State private var loadedIcon: NSImage? | ||
|
|
||
| init(customMetricsBundle: CustomMetricsBundle) { | ||
| self.snapshot = customMetricsBundle.snapshot | ||
|
|
@@ -40,9 +44,7 @@ struct CustomMetricsCardView: View { | |
|
|
||
| var body: some View { | ||
| HStack(alignment: .center, spacing: 16) { | ||
| Image(systemName: snapshot.displaySymbol) | ||
| .resizable() | ||
| .scaledToFit() | ||
| icon | ||
| .frame(width: 24, height: 24) | ||
| VStack(alignment: .leading, spacing: 2) { | ||
| Text(verbatim: snapshot.title) | ||
|
|
@@ -66,5 +68,28 @@ struct CustomMetricsCardView: View { | |
| .frame(maxWidth: .infinity, alignment: .leading) | ||
| .padding(8) | ||
| .materialCellStyle() | ||
| .task(id: snapshot.iconURL) { | ||
| loadedIcon = await loadIcon(from: snapshot.iconURL) | ||
| } | ||
|
Comment on lines
+71
to
+73
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not invoke this kind of logic without using a Store (Composable class). |
||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var icon: some View { | ||
| if let loadedIcon { | ||
| Image(nsImage: loadedIcon) | ||
| .resizable() | ||
| .scaledToFit() | ||
| .accessibilityHidden(true) | ||
| } else { | ||
| Image(systemName: snapshot.displaySymbol) | ||
| .resizable() | ||
| .scaledToFit() | ||
| .accessibilityHidden(true) | ||
| } | ||
| } | ||
|
|
||
| private func loadIcon(from url: URL?) async -> NSImage? { | ||
| guard let url, let data = try? await appDependencies.dataClient.fetch(url) else { return nil } | ||
| return NSImage(data: data) | ||
| } | ||
|
Comment on lines
+91
to
94
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not write this kind of logic in the View layer. Also, you must not use DependencyClient functions within the View layer. This is because we do not write tests for the View layer in LUCA. All logic must be written in the Store. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LUCA's
DependencyClientis not a place to dump convenience functions. Theconvertfunction right above it is an exception and should not be used as a reference. As you can see fromreadandwrite, its purpose is to use the actual implementation as much as possible, right up to the point where mocking is necessary. If you want to achieve whatfetchdoes, you should implementURLSessionClientand use a combination ofreadandURLSessionClient.data(URL).