@@ -8,9 +8,8 @@ import SWUtils
88struct DialogsListScreen : View {
99 @Environment ( \. isNetworkConnected) private var isNetworkConnected
1010 @EnvironmentObject private var defaults : DefaultsService
11- @State private var dialogs = [ DialogResponse ] ( )
11+ @EnvironmentObject private var viewModel : DialogsViewModel
1212 @State private var selectedDialog : DialogResponse ?
13- @State private var isLoading = false
1413 @State private var indexToDelete : Int ?
1514 @State private var openFriendList = false
1615 @State private var showDeleteConfirmation = false
@@ -42,7 +41,7 @@ private extension DialogsListScreen {
4241 var authorizedContentView : some View {
4342 dialogList
4443 . overlay { emptyContentView }
45- . loadingOverlay ( if: isLoading)
44+ . loadingOverlay ( if: viewModel . isLoading)
4645 . background ( Color . swBackground)
4746 . confirmationDialog (
4847 . init( Constants . Alert. deleteDialog) ,
@@ -58,9 +57,6 @@ private extension DialogsListScreen {
5857 friendListButton
5958 }
6059 }
61- . onDisappear {
62- [ refreshTask, deleteDialogTask] . forEach { $0? . cancel ( ) }
63- }
6460 }
6561
6662 var refreshButton : some View {
@@ -71,8 +67,8 @@ private extension DialogsListScreen {
7167 } label: {
7268 Icons . Regular. refresh. view
7369 }
74- . opacity ( showEmptyView || !DeviceOSVersionChecker . iOS16Available ? 1 : 0 )
75- . disabled ( isLoading)
70+ . opacity ( viewModel . showRefreshButton ? 1 : 0 )
71+ . disabled ( viewModel . isLoading)
7672 }
7773
7874 var friendListButton : some View {
@@ -86,28 +82,24 @@ private extension DialogsListScreen {
8682 Icons . Regular. plus. view
8783 . symbolVariant ( . circle)
8884 }
89- . opacity ( hasFriends || !dialogs . isEmpty ? 1 : 0 )
85+ . opacity ( hasFriends || viewModel . hasDialogs ? 1 : 0 )
9086 . disabled ( !isNetworkConnected)
9187 }
9288
9389 var emptyContentView : some View {
9490 EmptyContentView (
9591 mode: . dialogs,
96- action: emptyViewAction
92+ action: { openFriendList . toggle ( ) }
9793 )
98- . opacity ( showEmptyView ? 1 : 0 )
99- }
100-
101- var showEmptyView : Bool {
102- dialogs. isEmpty && !isLoading
94+ . opacity ( viewModel. showEmptyView ? 1 : 0 )
10395 }
10496
10597 @ViewBuilder
10698 var dialogList : some View {
10799 ZStack {
108100 Color . swBackground
109101 List {
110- ForEach ( dialogs) { model in
102+ ForEach ( viewModel . dialogs) { model in
111103 dialogListItem ( model)
112104 . listRowInsets ( . init( top: 12 , leading: 16 , bottom: 12 , trailing: 16 ) )
113105 . listRowBackground ( Color . swBackground)
@@ -116,9 +108,9 @@ private extension DialogsListScreen {
116108 . onDelete { initiateDeletion ( at: $0) }
117109 }
118110 . listStyle ( . plain)
119- . opacity ( dialogs . isEmpty ? 0 : 1 )
111+ . opacity ( viewModel . hasDialogs ? 1 : 0 )
120112 }
121- . animation ( . default, value: dialogs. count)
113+ . animation ( . default, value: viewModel . dialogs. count)
122114 . background (
123115 NavigationLink (
124116 destination: lazyDestination,
@@ -130,7 +122,12 @@ private extension DialogsListScreen {
130122 @ViewBuilder
131123 var lazyDestination : some View {
132124 if let selectedDialog {
133- DialogScreen ( dialog: selectedDialog) { markAsRead ( $0) }
125+ DialogScreen (
126+ dialog: selectedDialog,
127+ markedAsReadClbk: { dialog in
128+ viewModel. markAsRead ( dialog, defaults: defaults)
129+ }
130+ )
134131 }
135132 }
136133
@@ -162,39 +159,12 @@ private extension DialogsListScreen {
162159 defaults. hasFriends
163160 }
164161
165- func emptyViewAction( ) {
166- openFriendList. toggle ( )
167- }
168-
169- func markAsRead( _ dialog: DialogResponse ) {
170- dialogs = dialogs. map { item in
171- if item. id == dialog. id {
172- var updatedDialog = dialog
173- updatedDialog. unreadMessagesCount = 0
174- return updatedDialog
175- } else {
176- return item
177- }
178- }
179- guard dialog. unreadMessagesCount > 0 ,
180- defaults. unreadMessagesCount >= dialog. unreadMessagesCount
181- else { return }
182- let newValue = defaults. unreadMessagesCount - dialog. unreadMessagesCount
183- defaults. saveUnreadMessagesCount ( newValue)
184- }
185-
186162 func askForDialogs( refresh: Bool = false ) async {
187- guard defaults. isAuthorized else { return }
188- if isLoading || ( !dialogs. isEmpty && !refresh) { return }
189- if !refresh { isLoading = true }
190163 do {
191- dialogs = try await client. getDialogs ( )
192- let unreadMessagesCount = dialogs. map ( \. unreadMessagesCount) . reduce ( 0 , + )
193- defaults. saveUnreadMessagesCount ( unreadMessagesCount)
164+ try await viewModel. askForDialogs ( refresh: refresh, defaults: defaults)
194165 } catch {
195166 SWAlert . shared. presentDefaultUIKit ( message: error. localizedDescription)
196167 }
197- isLoading = false
198168 }
199169
200170 func initiateDeletion( at indexSet: IndexSet ) {
@@ -204,17 +174,11 @@ private extension DialogsListScreen {
204174
205175 func deleteAction( at index: Int ? ) {
206176 deleteDialogTask = Task {
207- guard let index, !isLoading else { return }
208- isLoading = true
209177 do {
210- let dialogID = dialogs [ index] . id
211- if try await client. deleteDialog ( dialogID) {
212- dialogs. remove ( at: index)
213- }
178+ try await viewModel. deleteDialog ( at: index, defaults: defaults)
214179 } catch {
215180 SWAlert . shared. presentDefaultUIKit ( message: error. localizedDescription)
216181 }
217- isLoading = false
218182 }
219183 }
220184}
0 commit comments