88
99import LoopKit
1010import LoopKitUI
11+ import MockKit
1112import SwiftUI
1213
1314public struct SettingsView : View , HorizontalSizeClassOverride {
@@ -16,6 +17,8 @@ public struct SettingsView: View, HorizontalSizeClassOverride {
1617
1718 @ObservedObject var viewModel : SettingsViewModel
1819
20+ @State var showServiceChooser : Bool = false
21+
1922 public init ( viewModel: SettingsViewModel ) {
2023 self . viewModel = viewModel
2124 }
@@ -29,6 +32,9 @@ public struct SettingsView: View, HorizontalSizeClassOverride {
2932 }
3033 therapySettingsSection
3134 deviceSettingsSection
35+ if viewModel. servicesViewModel. showServices {
36+ servicesSection
37+ }
3238 if viewModel. pumpManagerSettingsViewModel. isTestingDevice {
3339 deletePumpDataSection
3440 }
@@ -81,13 +87,14 @@ extension SettingsView {
8187
8288 private var therapySettingsSection : some View {
8389 Section ( header: SectionHeader ( label: NSLocalizedString ( " Configuration " , comment: " The title of the Configuration section in settings " ) ) ) {
84- return NavigationLink ( destination: TherapySettingsView ( viewModel: TherapySettingsViewModel ( mode: . settings,
85- therapySettings: viewModel. therapySettings,
86- supportedInsulinModelSettings: viewModel. supportedInsulinModelSettings,
87- pumpSupportedIncrements: viewModel. pumpSupportedIncrements,
88- syncPumpSchedule: viewModel. syncPumpSchedule,
89- chartColors: . primary,
90- didSave: viewModel. didSave) ) ) {
90+ return NavigationLink ( destination: TherapySettingsView (
91+ viewModel: TherapySettingsViewModel ( mode: . settings,
92+ therapySettings: viewModel. therapySettings,
93+ supportedInsulinModelSettings: viewModel. supportedInsulinModelSettings,
94+ pumpSupportedIncrements: viewModel. pumpSupportedIncrements,
95+ syncPumpSchedule: viewModel. syncPumpSchedule,
96+ chartColors: . primary,
97+ didSave: viewModel. didSave) ) ) {
9198 LargeButton ( action: { } ,
9299 includeArrow: false ,
93100 imageView: AnyView ( Image ( " Therapy Icon " ) ) ,
@@ -136,6 +143,36 @@ extension SettingsView {
136143 }
137144 }
138145
146+ private var servicesSection : some View {
147+ Section ( header: SectionHeader ( label: NSLocalizedString ( " Services " , comment: " The title of the services section in settings " ) ) ) {
148+ ForEach ( viewModel. servicesViewModel. activeServices. indices, id: \. self) { index in
149+ // TODO: this "dismiss then call didTapService()" here is temporary, until we've completely gotten rid of SettingsTableViewController
150+ Button ( action: { self . dismiss ( ) ; self . viewModel. servicesViewModel. didTapService ( index) } , label: {
151+ Text ( self . viewModel. servicesViewModel. activeServices [ index] . localizedTitle)
152+ } )
153+ . accentColor ( . primary)
154+ }
155+ Button ( action: { self . showServiceChooser = true } , label: {
156+ Text ( " Add Service " , comment: " The title of the services section in settings " )
157+ } )
158+ . actionSheet ( isPresented: $showServiceChooser) {
159+ ActionSheet ( title: Text ( " Add Service " , comment: " The title of the services section in settings " ) , buttons: serviceChoices)
160+ }
161+ }
162+ }
163+
164+ private var serviceChoices : [ ActionSheet . Button ] {
165+ var result = viewModel. servicesViewModel. inactiveServices. map { availableService in
166+ ActionSheet . Button. default ( Text ( availableService. localizedTitle) ) {
167+ // TODO: this "dismiss then call didTapAddService()" here is temporary, until we've completely gotten rid of SettingsTableViewController
168+ self . dismiss ( )
169+ self . viewModel. servicesViewModel. didTapAddService ( availableService)
170+ }
171+ }
172+ result. append ( . cancel( ) )
173+ return result
174+ }
175+
139176 private var deletePumpDataSection : some View {
140177 Section {
141178 Button ( action: { self . viewModel. pumpManagerSettingsViewModel. deleteData ? ( ) } ) {
@@ -224,12 +261,34 @@ fileprivate struct LargeButton: View {
224261 }
225262}
226263
264+ fileprivate class FakeService1 : Service {
265+ static var localizedTitle : String = " Service 1 "
266+ static var serviceIdentifier : String = " FakeService1 "
267+ var serviceDelegate : ServiceDelegate ?
268+ var rawState : RawStateValue = [ : ]
269+ required init ? ( rawState: RawStateValue ) { }
270+ convenience init ( ) { self . init ( rawState: [ : ] ) ! }
271+ var available : AvailableService { AvailableService ( identifier: serviceIdentifier, localizedTitle: localizedTitle) }
272+ }
273+ fileprivate class FakeService2 : Service {
274+ static var localizedTitle : String = " Service 2 "
275+ static var serviceIdentifier : String = " FakeService2 "
276+ var serviceDelegate : ServiceDelegate ?
277+ var rawState : RawStateValue = [ : ]
278+ required init ? ( rawState: RawStateValue ) { }
279+ convenience init ( ) { self . init ( rawState: [ : ] ) ! }
280+ var available : AvailableService { AvailableService ( identifier: serviceIdentifier, localizedTitle: localizedTitle) }
281+ }
282+ fileprivate let servicesViewModel = ServicesViewModel ( showServices: true ,
283+ availableServices: [ FakeService1 ( ) . available, FakeService2 ( ) . available] ,
284+ activeServices: [ FakeService1 ( ) ] )
227285public struct SettingsView_Previews : PreviewProvider {
228286 public static var previews : some View {
229287 let viewModel = SettingsViewModel ( appNameAndVersion: " Tidepool Loop v1.2.3.456 " ,
230288 notificationsCriticalAlertPermissionsViewModel: NotificationsCriticalAlertPermissionsViewModel ( ) ,
231289 pumpManagerSettingsViewModel: DeviceViewModel ( ) ,
232290 cgmManagerSettingsViewModel: DeviceViewModel ( ) ,
291+ servicesViewModel: servicesViewModel,
233292 therapySettings: TherapySettings ( ) ,
234293 supportedInsulinModelSettings: SupportedInsulinModelSettings ( fiaspModelEnabled: true , walshModelEnabled: true ) ,
235294 pumpSupportedIncrements: nil ,
0 commit comments