@@ -44,41 +44,32 @@ struct TapShopBelowTip: Tip {
4444}
4545
4646struct ShopListView : View {
47- @Environment ( \. mainTab) private var mainTab
48- @Environment ( TabViewModel . self) private var tabViewModel
49- @Environment ( \. sessionController) private var sessionController
50- private var shopRepository : ShopRepositoryProtocol
51- private var itemTagRepository : ItemTagRepositoryProtocol
52- @State private var isShowingCreateSheet = false
47+ @State private var viewModel : ShopListViewModel
5348
54- init (
55- shopRepository: ShopRepositoryProtocol ,
56- itemTagRepository: ItemTagRepositoryProtocol
57- ) {
58- self . shopRepository = shopRepository
59- self . itemTagRepository = itemTagRepository
49+ init ( viewModel: ShopListViewModel ) {
50+ self . _viewModel = State ( wrappedValue: viewModel)
6051 }
6152}
6253
6354extension ShopListView {
6455 var body : some View {
6556 contentView
6657 . task {
67- reload ( )
58+ viewModel . reload ( )
6859 }
6960 . onAppear {
70- tabViewModel . showingDetailView [ mainTab ] = false
61+ viewModel . setTabViewModelShowingDetailViewToFalse ( )
7162 }
72- . onChange ( of: shopRepository . state) {
73- if shopRepository . state == . initial {
74- reload ( )
63+ . onChange ( of: viewModel . state) {
64+ if viewModel . state == . initial {
65+ viewModel . reload ( )
7566 }
7667 }
7768 // Avoid showing deleted shop.
78- . onChange ( of: sessionController . shouldPopToRootView) {
69+ . onChange ( of: viewModel . shouldPopToRootView) {
7970 Task {
8071 try await Task . sleep ( nanoseconds: 2_000_000_000 )
81- reload ( )
72+ viewModel . reload ( )
8273 }
8374 }
8475 }
@@ -88,7 +79,7 @@ extension ShopListView {
8879private extension ShopListView {
8980 var contentView : some View {
9081 @ViewBuilder var contentView : some View {
91- switch shopRepository . state {
82+ switch viewModel . state {
9283 case . initial, . loading:
9384 LoadingView ( )
9485 case . hasData:
@@ -101,12 +92,8 @@ private extension ShopListView {
10192 return contentView
10293 }
10394
104- func reload( ) {
105- shopRepository. reload ( )
106- }
107-
10895 var cardsView : some View {
109- ForEach ( shopRepository . shops) { shop in
96+ ForEach ( viewModel . shops) { shop in
11097 NavigationLink ( value: shop) {
11198 ShopListCardView ( shop: shop)
11299 }
@@ -115,11 +102,9 @@ private extension ShopListView {
115102 }
116103
117104 var shopListView : some View {
118- let leftInShopSlots = shopRepository. limitCount - shopRepository. createdShopsCount
119-
120- return VStack {
121- if shopRepository. isEmpty {
122- noResultsView ( leftInShopSlots: leftInShopSlots)
105+ VStack {
106+ if viewModel. isEmpty {
107+ noResultsView ( leftInShopSlots: viewModel. leftInShopSlots)
123108 } else {
124109 List {
125110 Section {
@@ -130,11 +115,11 @@ private extension ShopListView {
130115 . tint ( . alarm)
131116
132117 EmptyView ( )
133- . id ( ScrollToTopID ( mainTab : mainTab , detail : false ) )
118+ . id ( viewModel . scrollToTopID ( ) )
134119 } footer: {
135120 VStack ( spacing: 0 ) {
136121 HStack ( alignment: . firstTextBaseline) {
137- Text ( String ( leftInShopSlots) )
122+ Text ( String ( viewModel . leftInShopSlots) )
138123 . font ( . uiLabelBold)
139124 Text ( verbatim: " left in shop slots. " )
140125 . font ( . uiFootnote)
@@ -144,41 +129,41 @@ private extension ShopListView {
144129 }
145130 . navigationDestination ( for: Shop . self) { shop in
146131 ShopDetailView (
147- shopRepository: shopRepository,
148- itemTagRepository: itemTagRepository,
132+ shopRepository: viewModel . shopRepository,
133+ itemTagRepository: viewModel . itemTagRepository,
149134 shopId: shop. id
150135 )
151136 }
152137 . accessibility ( identifier: " shopListView " )
153138 . refreshable {
154- reload ( )
139+ viewModel . reload ( )
155140 }
156141 }
157142 }
158143 . navigationTitle ( String . shops)
159144 . navigationBarTitleDisplayMode ( . inline)
160145 . toolbar {
161- if leftInShopSlots > 0 {
146+ if viewModel . leftInShopSlots > 0 {
162147 ToolbarItem ( placement: . navigationBarTrailing) {
163148 Button {
164- isShowingCreateSheet . toggle ( )
149+ viewModel . showCreateView ( )
165150 } label: {
166151 Image ( systemName: " plus " )
167152 }
168153 }
169154 }
170155 }
171- . sheet ( isPresented: $isShowingCreateSheet,
156+ . sheet ( isPresented: $viewModel . isShowingCreateSheet,
172157 onDismiss: {
173- reload ( )
158+ viewModel . reload ( )
174159 } , content: {
175- ShopCreateView ( shopRepository: shopRepository)
160+ ShopCreateView ( shopRepository: viewModel . shopRepository)
176161 }
177162 )
178163 }
179164
180165 var reloadView : some View {
181- ErrorView ( buttonAction: reload)
166+ ErrorView ( buttonAction: viewModel . reload)
182167 }
183168
184169 func noResultsView( leftInShopSlots: Int ) -> some View {
@@ -195,7 +180,7 @@ private extension ShopListView {
195180 . padding ( )
196181
197182 MainButtonView ( title: String . addShop, type: . primary( withArrow: false ) ) {
198- isShowingCreateSheet . toggle ( )
183+ viewModel . showCreateView ( )
199184 }
200185 . padding ( )
201186
0 commit comments