11import SwiftUI
22import struct Yosemite. Site
3+ import struct Yosemite. NotificationSettings
34
45final class NotificationSettingsHostingController : UIHostingController < NotificationSettingsView > {
56 init ( ) {
@@ -36,13 +37,54 @@ struct NotificationSettingsView: View {
3637 }
3738 }
3839 . navigationTitle ( Localization . title)
40+ . toolbar {
41+ ToolbarItem ( placement: . confirmationAction) {
42+ Button {
43+ saveSettings ( )
44+ } label: {
45+ if viewModel. isSavingSettings {
46+ ProgressView ( ) . progressViewStyle ( . circular)
47+ } else {
48+ Text ( Localization . save)
49+ }
50+ }
51+ . disabled ( viewModel. hasSiteSettingsChanges == false )
52+ }
53+ }
3954 . task {
4055 await viewModel. checkNotificationPermission ( )
41- await viewModel. synchronizeSites ( )
56+ if viewModel. currentDeviceID != nil {
57+ await viewModel. retrieveNotificationSettings ( )
58+ await viewModel. synchronizeSites ( )
59+ }
4260 }
4361 . sheet ( item: $selectedSite) { site in
44- SiteNotificationSettingsView ( siteTitle: site. name)
62+ if let settings = viewModel. loadSettings ( for: site) {
63+ SiteNotificationSettingsView ( siteTitle: site. name,
64+ ordersNotificationsEnabled: settings. storeOrder,
65+ productReviewsNotificationsEnabled: settings. newComment,
66+ completionHandler: { newOrder, productReviews in
67+ viewModel. updateSettings ( siteID: site. siteID,
68+ ordersNotificationsEnabled: newOrder,
69+ productReviewsNotificationsEnabled: productReviews)
70+ } )
71+ }
72+ }
73+ . alert ( Localization . errorSavingSiteSettings, isPresented: $viewModel. savingSiteSettingsFailed) {
74+ Button ( Localization . cancel, role: . cancel) { }
75+ Button ( Localization . retry) {
76+ saveSettings ( )
77+ }
4578 }
79+ . alert ( Localization . errorLoadingSiteSettings, isPresented: $viewModel. loadingSiteSettingsFailed) {
80+ Button ( Localization . cancel, role: . cancel) { }
81+ Button ( Localization . retry) {
82+ Task {
83+ await viewModel. retrieveNotificationSettings ( )
84+ }
85+ }
86+ }
87+ . notice ( $viewModel. notice)
4688 }
4789}
4890
@@ -88,14 +130,20 @@ private extension NotificationSettingsView {
88130 }
89131
90132 Section {
91- ForEach ( viewModel. sites) { site in
92- detailRow ( for: site)
133+ if viewModel. isLoadingSiteSettings {
134+ ActivityIndicator ( isAnimating: . constant( true ) , style: . medium)
135+ . frame ( maxWidth: . infinity)
136+ } else if viewModel. siteSettings != nil {
137+ ForEach ( viewModel. sites) { site in
138+ detailRow ( for: site)
139+ }
93140 }
94141 } header: {
95142 Text ( Localization . storeListSectionHeader)
96143 } footer: {
97144 Text ( Localization . storeListSectionFooter)
98145 }
146+ . renderedIf ( viewModel. shouldShowSiteList)
99147 }
100148 }
101149
@@ -107,9 +155,11 @@ private extension NotificationSettingsView {
107155 VStack ( alignment: . leading) {
108156 Text ( site. name)
109157 . bodyStyle ( )
110- Text ( site. url)
111- . foregroundStyle ( Color . secondary)
112- . captionStyle ( )
158+ if let settings = viewModel. loadSettings ( for: site) {
159+ Text ( description ( for: settings) )
160+ . foregroundStyle ( Color . secondary)
161+ . captionStyle ( )
162+ }
113163 }
114164 . multilineTextAlignment ( . leading)
115165
@@ -130,6 +180,25 @@ private extension NotificationSettingsView {
130180 // Ask the system to open that URL.
131181 await UIApplication . shared. open ( url)
132182 }
183+
184+ func saveSettings( ) {
185+ Task {
186+ await viewModel. saveSettings ( )
187+ }
188+ }
189+
190+ func description( for settings: NotificationSettings . Device ) -> String {
191+ switch ( settings. storeOrder, settings. newComment) {
192+ case ( true , true ) :
193+ [ Localization . newOrders, Localization . productReviews] . joined ( separator: " , " )
194+ case ( true , false ) :
195+ Localization . newOrders
196+ case ( false , true ) :
197+ Localization . productReviews
198+ case ( false , false ) :
199+ Localization . off
200+ }
201+ }
133202}
134203
135204extension NotificationSettingsView {
@@ -179,6 +248,46 @@ extension NotificationSettingsView {
179248 value: " Customize your notification preferences for each store. " ,
180249 comment: " Footer of the store list section on the notification settings view "
181250 )
251+ static let errorLoadingSiteSettings = NSLocalizedString (
252+ " notificationSettingsView.errorLoadingSiteSettings " ,
253+ value: " Unable to load notification settings for your stores. " ,
254+ comment: " Error message when loading site settings fails on the notification settings view "
255+ )
256+ static let retry = NSLocalizedString (
257+ " notificationSettingsView.retry " ,
258+ value: " Try again " ,
259+ comment: " Button to reload site settings on the notification settings view "
260+ )
261+ static let save = NSLocalizedString (
262+ " notificationSettingsView.save " ,
263+ value: " Save " ,
264+ comment: " Button to save site settings on the notification settings view "
265+ )
266+ static let cancel = NSLocalizedString (
267+ " notificationSettingsView.cancel " ,
268+ value: " Cancel " ,
269+ comment: " Button to cancel saving site settings on the notification settings view "
270+ )
271+ static let errorSavingSiteSettings = NSLocalizedString (
272+ " notificationSettingsView.errorSavingSiteSettings " ,
273+ value: " Unable to save notification settings " ,
274+ comment: " Error message when saving site settings fails on the notification settings view "
275+ )
276+ static let newOrders = NSLocalizedString (
277+ " notificationSettingsView.newOrders " ,
278+ value: " New orders " ,
279+ comment: " Label indicating that new orders notifications are enabled for a site on the notification settings view "
280+ )
281+ static let productReviews = NSLocalizedString (
282+ " notificationSettingsView.productReviews " ,
283+ value: " Product reviews " ,
284+ comment: " Label indicating that product reviews notifications are enabled for a site on the notification settings view "
285+ )
286+ static let off = NSLocalizedString (
287+ " notificationSettingsView.off " ,
288+ value: " Off " ,
289+ comment: " Label indicating that notifications are disabled for a site on the notification settings view "
290+ )
182291 }
183292}
184293
0 commit comments