Skip to content

Commit 9cd6402

Browse files
committed
Adds LazyNavigationLink
1 parent 655d5ad commit 9cd6402

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import SwiftUI
2+
3+
/// `NavigationLink` wrapper that instantiates the `DestinationView` when the navigation occurs.
4+
///
5+
struct LazyNavigationLink<Destination: View, Label: View>: View {
6+
7+
/// Destination view builder
8+
///
9+
private let destination: () -> Destination
10+
11+
/// Set it to `true` to proceed with the desired navigation. Set it to `false` to remove the view from the navigation context.
12+
///
13+
@Binding var isActive: Bool
14+
15+
/// `NavigationLink` label
16+
///
17+
private let label: () -> Label
18+
19+
/// Creates a navigation link that creates and presents the destination view when active.
20+
/// - Parameters:
21+
/// - destination: A view for the navigation link to present.
22+
/// - isActive: A binding to a Boolean value that indicates whether `destination` is currently presented.
23+
/// - label: A view builder to produce a label describing the `destination` to present.
24+
init(destination: @autoclosure @escaping () -> Destination, isActive: Binding<Bool>, label: @escaping () -> Label) {
25+
self.destination = destination
26+
self._isActive = isActive
27+
self.label = label
28+
}
29+
30+
var body: some View {
31+
NavigationLink(destination: LazyView(destination), isActive: $isActive, label: label)
32+
}
33+
}

WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/LazyView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ struct LazyView<Wrapped: View>: View {
99
///
1010
private let wrapped: () -> Wrapped
1111

12-
/// Stores the function as a closure using the `@autoclosure` attribute.
13-
///
12+
/// Creates a wrapper for a view to be instantiated lazily.
13+
/// - Parameters:
14+
/// - wrapped: View builder function.
1415
init(_ wrapped: @autoclosure @escaping () -> Wrapped) {
1516
self.wrapped = wrapped
1617
}
1718

18-
/// Receives the builder closure.
19-
///
19+
/// Creates a wrapper for a view to be instantiated lazily.
20+
/// - Parameters:
21+
/// - wrapped: View builder closure.
2022
init(_ wrapped: @escaping () -> Wrapped) {
2123
self.wrapped = wrapped
2224
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@
447447
26C6E8E626E6B5F500C7BB0F /* StateSelectorViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E526E6B5F500C7BB0F /* StateSelectorViewModel.swift */; };
448448
26C6E8E826E6B6AC00C7BB0F /* StateSelectorCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E726E6B6AC00C7BB0F /* StateSelectorCommand.swift */; };
449449
26C6E8EA26E8FD3900C7BB0F /* LazyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8E926E8FD3900C7BB0F /* LazyView.swift */; };
450+
26C6E8EC26E8FF4800C7BB0F /* LazyNavigationLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26C6E8EB26E8FF4800C7BB0F /* LazyNavigationLink.swift */; };
450451
26CCBE0B2523B3650073F94D /* RefundProductsTotalTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */; };
451452
26CCBE0D2523C2560073F94D /* RefundProductsTotalTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */; };
452453
26E0ADF12631D94D00A5EB3B /* TopBannerWrapperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E0ADF02631D94D00A5EB3B /* TopBannerWrapperView.swift */; };
@@ -1851,6 +1852,7 @@
18511852
26C6E8E526E6B5F500C7BB0F /* StateSelectorViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateSelectorViewModel.swift; sourceTree = "<group>"; };
18521853
26C6E8E726E6B6AC00C7BB0F /* StateSelectorCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateSelectorCommand.swift; sourceTree = "<group>"; };
18531854
26C6E8E926E8FD3900C7BB0F /* LazyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyView.swift; sourceTree = "<group>"; };
1855+
26C6E8EB26E8FF4800C7BB0F /* LazyNavigationLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyNavigationLink.swift; sourceTree = "<group>"; };
18541856
26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefundProductsTotalTableViewCell.swift; sourceTree = "<group>"; };
18551857
26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RefundProductsTotalTableViewCell.xib; sourceTree = "<group>"; };
18561858
26E0ADF02631D94D00A5EB3B /* TopBannerWrapperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopBannerWrapperView.swift; sourceTree = "<group>"; };
@@ -4368,6 +4370,7 @@
43684370
DE126D0A26CA2331007F901D /* ValidationErrorRow.swift */,
43694371
DEE6437726D8DAD900888A75 /* InProgressView.swift */,
43704372
26C6E8E926E8FD3900C7BB0F /* LazyView.swift */,
4373+
26C6E8EB26E8FF4800C7BB0F /* LazyNavigationLink.swift */,
43714374
);
43724375
path = "SwiftUI Components";
43734376
sourceTree = "<group>";
@@ -7193,6 +7196,7 @@
71937196
024DF30B23742297006658FE /* AztecFormatBarCommand.swift in Sources */,
71947197
CC254F3426C4113B005F3C82 /* ShippingLabelPackageSelection.swift in Sources */,
71957198
AECD57D226DFDF7500A3B580 /* EditAddressForm.swift in Sources */,
7199+
26C6E8EC26E8FF4800C7BB0F /* LazyNavigationLink.swift in Sources */,
71967200
45E9A6E724DAE23300A600E8 /* ProductReviewsViewModel.swift in Sources */,
71977201
028BAC4222F30B05008BB4AF /* StoreStatsV4PeriodViewController.swift in Sources */,
71987202
740987B321B87760000E4C80 /* FancyAnimatedButton+Woo.swift in Sources */,

0 commit comments

Comments
 (0)