Skip to content

Commit 655d5ad

Browse files
committed
Adds LazyView untility
1 parent 207277c commit 655d5ad

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import SwiftUI
2+
3+
/// Lazily loads a `View` using a closure as a builder function.
4+
/// Useful for cases when it's not desired to load the `View` at the view definition time. EG: `NavigationLink`
5+
///
6+
struct LazyView<Wrapped: View>: View {
7+
8+
/// Builder closure
9+
///
10+
private let wrapped: () -> Wrapped
11+
12+
/// Stores the function as a closure using the `@autoclosure` attribute.
13+
///
14+
init(_ wrapped: @autoclosure @escaping () -> Wrapped) {
15+
self.wrapped = wrapped
16+
}
17+
18+
/// Receives the builder closure.
19+
///
20+
init(_ wrapped: @escaping () -> Wrapped) {
21+
self.wrapped = wrapped
22+
}
23+
24+
var body: Wrapped {
25+
wrapped()
26+
}
27+
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
26C6E8E426E2D87C00C7BB0F /* CountrySelectorViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E326E2D87C00C7BB0F /* CountrySelectorViewModelTests.swift */; };
447447
26C6E8E626E6B5F500C7BB0F /* StateSelectorViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E526E6B5F500C7BB0F /* StateSelectorViewModel.swift */; };
448448
26C6E8E826E6B6AC00C7BB0F /* StateSelectorCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E726E6B6AC00C7BB0F /* StateSelectorCommand.swift */; };
449+
26C6E8EA26E8FD3900C7BB0F /* LazyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E926E8FD3900C7BB0F /* LazyView.swift */; };
449450
26CCBE0B2523B3650073F94D /* RefundProductsTotalTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */; };
450451
26CCBE0D2523C2560073F94D /* RefundProductsTotalTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */; };
451452
26E0ADF12631D94D00A5EB3B /* TopBannerWrapperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E0ADF02631D94D00A5EB3B /* TopBannerWrapperView.swift */; };
@@ -1849,6 +1850,7 @@
18491850
26C6E8E326E2D87C00C7BB0F /* CountrySelectorViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountrySelectorViewModelTests.swift; sourceTree = "<group>"; };
18501851
26C6E8E526E6B5F500C7BB0F /* StateSelectorViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateSelectorViewModel.swift; sourceTree = "<group>"; };
18511852
26C6E8E726E6B6AC00C7BB0F /* StateSelectorCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateSelectorCommand.swift; sourceTree = "<group>"; };
1853+
26C6E8E926E8FD3900C7BB0F /* LazyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyView.swift; sourceTree = "<group>"; };
18521854
26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefundProductsTotalTableViewCell.swift; sourceTree = "<group>"; };
18531855
26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RefundProductsTotalTableViewCell.xib; sourceTree = "<group>"; };
18541856
26E0ADF02631D94D00A5EB3B /* TopBannerWrapperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopBannerWrapperView.swift; sourceTree = "<group>"; };
@@ -4365,6 +4367,7 @@
43654367
E10BC15D26CC06970064F5E2 /* ScrollableVStack.swift */,
43664368
DE126D0A26CA2331007F901D /* ValidationErrorRow.swift */,
43674369
DEE6437726D8DAD900888A75 /* InProgressView.swift */,
4370+
26C6E8E926E8FD3900C7BB0F /* LazyView.swift */,
43684371
);
43694372
path = "SwiftUI Components";
43704373
sourceTree = "<group>";
@@ -7409,6 +7412,7 @@
74097412
4506BD712461965300FE6377 /* ProductVisibilityViewController.swift in Sources */,
74107413
CE1F512B206985DF00C6C810 /* PaddedLabel.swift in Sources */,
74117414
26E0ADF12631D94D00A5EB3B /* TopBannerWrapperView.swift in Sources */,
7415+
26C6E8EA26E8FD3900C7BB0F /* LazyView.swift in Sources */,
74127416
7421344A210A323C00C13890 /* WooAnalyticsStat.swift in Sources */,
74137417
02A275BA23FE50AA005C560F /* ProductUIImageLoader.swift in Sources */,
74147418
02305353237454C700487A64 /* AztecInsertMoreFormatBarCommand.swift in Sources */,

0 commit comments

Comments
 (0)