@@ -11,6 +11,8 @@ import Combine
1111import SwiftUI
1212import CasePaths
1313
14+ typealias PhotoListViewStoreType = ViewStore < PhotoListViewStore . ViewState , PhotoListViewStore . Action >
15+
1416/// Coordinates state for use in `PhotoListView`
1517final class PhotoListViewStore : ViewStore {
1618
@@ -24,12 +26,19 @@ final class PhotoListViewStore: ViewStore {
2426 }
2527
2628 fileprivate static let defaultNavigationTitle = LocalizedStringKey ( " Photos " )
27- fileprivate static let initial = ViewState ( status : . loading , showsPhotoCount : false , navigationTitle : defaultNavigationTitle , searchText : " " )
29+ fileprivate static let initial = ViewState ( )
2830
2931 let status : Status
3032 let showsPhotoCount : Bool
3133 let navigationTitle : LocalizedStringKey
3234 fileprivate let searchText : String
35+
36+ init ( status: PhotoListViewStore . ViewState . Status = . loading, showsPhotoCount: Bool = false , navigationTitle: LocalizedStringKey = ViewState . defaultNavigationTitle, searchText: String = " " ) {
37+ self . status = status
38+ self . showsPhotoCount = showsPhotoCount
39+ self . navigationTitle = navigationTitle
40+ self . searchText = searchText
41+ }
3342 }
3443
3544 enum Action {
@@ -45,22 +54,6 @@ final class PhotoListViewStore: ViewStore {
4554 private let showsPhotosCountPublisher = PassthroughSubject < Bool , Never > ( )
4655 private let searchTextPublisher = PassthroughSubject < String , Never > ( )
4756
48- var showsPhotoCount : Binding < Bool > {
49- //
50- // return Binding<Bool> {
51- // self.viewState.showsPhotoCount
52- // } set: { newValue in
53- // self.send(.toggleShowsPhotoCount(newValue))
54- // }
55- //
56- // Note: This 👇 is just a shorthand version of this 👆
57- makeBinding ( viewStateKeyPath: \. showsPhotoCount, actionCasePath: / Action. toggleShowsPhotoCount)
58- }
59-
60- var searchText : Binding < String > {
61- makeBinding ( viewStateKeyPath: \. searchText, actionCasePath: / Action. search)
62- }
63-
6457 /// Creates a new `PhotoListViewStore`
6558 /// - Parameters:
6659 /// - provider: The provider responsible for fetching photos.
@@ -98,6 +91,24 @@ final class PhotoListViewStore: ViewStore {
9891 }
9992}
10093
94+ extension PhotoListViewStoreType {
95+ var showsPhotoCount : Binding < Bool > {
96+ //
97+ // return Binding<Bool> {
98+ // self.viewState.showsPhotoCount
99+ // } set: { newValue in
100+ // self.send(.toggleShowsPhotoCount(newValue))
101+ // }
102+ //
103+ // Note: This 👇 is just a shorthand version of this 👆
104+ makeBinding ( viewStateKeyPath: \. showsPhotoCount, actionCasePath: / Action. toggleShowsPhotoCount)
105+ }
106+
107+ var searchText : Binding < String > {
108+ makeBinding ( viewStateKeyPath: \. searchText, actionCasePath: / Action. search)
109+ }
110+ }
111+
101112private extension Provider {
102113 func providePhotos( ) -> AnyPublisher < Result < [ Photo ] , ProviderError > , Never > {
103114 provideItems ( request: APIRequest . photos, decoder: JSONDecoder ( ) , providerBehaviors: [ ] , requestBehaviors: [ ] , allowExpiredItems: true )
0 commit comments