BolusStateModel.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. import CoreData
  2. import Foundation
  3. import LoopKit
  4. import SwiftUI
  5. import Swinject
  6. extension Bolus {
  7. final class StateModel: BaseStateModel<Provider> {
  8. @Injected() var unlockmanager: UnlockManager!
  9. @Injected() var apsManager: APSManager!
  10. @Injected() var broadcaster: Broadcaster!
  11. @Injected() var pumpHistoryStorage: PumpHistoryStorage!
  12. // added for bolus calculator
  13. @Injected() var settings: SettingsManager!
  14. @Injected() var nsManager: NightscoutManager!
  15. @Injected() var carbsStorage: CarbsStorage!
  16. @Injected() var glucoseStorage: GlucoseStorage!
  17. @Published var suggestion: Suggestion?
  18. @Published var predictions: Predictions?
  19. @Published var amount: Decimal = 0
  20. @Published var insulinRecommended: Decimal = 0
  21. @Published var insulinRequired: Decimal = 0
  22. @Published var units: GlucoseUnits = .mmolL
  23. @Published var percentage: Decimal = 0
  24. @Published var threshold: Decimal = 0
  25. @Published var maxBolus: Decimal = 0
  26. @Published var errorString: Decimal = 0
  27. @Published var evBG: Int = 0
  28. @Published var insulin: Decimal = 0
  29. @Published var isf: Decimal = 0
  30. @Published var error: Bool = false
  31. @Published var minGuardBG: Decimal = 0
  32. @Published var minDelta: Decimal = 0
  33. @Published var expectedDelta: Decimal = 0
  34. @Published var minPredBG: Decimal = 0
  35. @Published var waitForSuggestion: Bool = false
  36. @Published var carbRatio: Decimal = 0
  37. @Published var addButtonPressed: Bool = false
  38. var waitForSuggestionInitial: Bool = false
  39. // added for bolus calculator
  40. @Published var recentGlucose: BloodGlucose?
  41. @Published var target: Decimal = 0
  42. @Published var cob: Decimal = 0
  43. @Published var iob: Decimal = 0
  44. @Published var currentBG: Decimal = 0
  45. @Published var fifteenMinInsulin: Decimal = 0
  46. @Published var deltaBG: Decimal = 0
  47. @Published var targetDifferenceInsulin: Decimal = 0
  48. @Published var targetDifference: Decimal = 0
  49. @Published var wholeCob: Decimal = 0
  50. @Published var wholeCobInsulin: Decimal = 0
  51. @Published var iobInsulinReduction: Decimal = 0
  52. @Published var wholeCalc: Decimal = 0
  53. @Published var insulinCalculated: Decimal = 0
  54. @Published var fraction: Decimal = 0
  55. @Published var useCalc: Bool = false
  56. @Published var basal: Decimal = 0
  57. @Published var fattyMeals: Bool = false
  58. @Published var fattyMealFactor: Decimal = 0
  59. @Published var useFattyMealCorrectionFactor: Bool = false
  60. @Published var displayPresets: Bool = true
  61. @Published var currentBasal: Decimal = 0
  62. @Published var sweetMeals: Bool = false
  63. @Published var sweetMealFactor: Decimal = 0
  64. @Published var useSuperBolus: Bool = false
  65. @Published var superBolusInsulin: Decimal = 0
  66. @Published var meal: [CarbsEntry]?
  67. @Published var carbs: Decimal = 0
  68. @Published var fat: Decimal = 0
  69. @Published var protein: Decimal = 0
  70. @Published var note: String = ""
  71. @Published var date = Date()
  72. @Published var carbsRequired: Decimal?
  73. @Published var useFPUconversion: Bool = false
  74. @Published var dish: String = ""
  75. @Published var selection: Presets?
  76. @Published var summation: [String] = []
  77. @Published var maxCarbs: Decimal = 0
  78. @Published var id_: String = ""
  79. @Published var summary: String = ""
  80. @Published var skipBolus: Bool = false
  81. @Published var externalInsulin: Bool = false
  82. let now = Date.now
  83. let context = CoreDataStack.shared.persistentContainer.viewContext
  84. override func subscribe() {
  85. setupInsulinRequired()
  86. broadcaster.register(SuggestionObserver.self, observer: self)
  87. broadcaster.register(BolusFailureObserver.self, observer: self)
  88. units = settingsManager.settings.units
  89. percentage = settingsManager.settings.insulinReqPercentage
  90. threshold = provider.suggestion?.threshold ?? 0
  91. maxBolus = provider.pumpSettings().maxBolus
  92. // added
  93. fraction = settings.settings.overrideFactor
  94. useCalc = settings.settings.useCalc
  95. fattyMeals = settings.settings.fattyMeals
  96. fattyMealFactor = settings.settings.fattyMealFactor
  97. sweetMeals = settings.settings.sweetMeals
  98. sweetMealFactor = settings.settings.sweetMealFactor
  99. displayPresets = settings.settings.displayPresets
  100. carbsRequired = provider.suggestion?.carbsReq
  101. maxCarbs = settings.settings.maxCarbs
  102. skipBolus = settingsManager.settings.skipBolusScreenAfterCarbs
  103. useFPUconversion = settingsManager.settings.useFPUconversion
  104. if waitForSuggestionInitial {
  105. apsManager.determineBasal()
  106. .receive(on: DispatchQueue.main)
  107. .sink { [weak self] ok in
  108. guard let self = self else { return }
  109. if !ok {
  110. self.waitForSuggestion = false
  111. self.insulinRequired = 0
  112. self.insulinRecommended = 0
  113. }
  114. }.store(in: &lifetime)
  115. }
  116. if let notNilSugguestion = provider.suggestion {
  117. suggestion = notNilSugguestion
  118. if let notNilPredictions = suggestion?.predictions {
  119. predictions = notNilPredictions
  120. }
  121. }
  122. }
  123. func getCurrentBasal() {
  124. let basalEntries = provider.getProfile()
  125. let dateFormatter = DateFormatter()
  126. dateFormatter.dateFormat = "HH:mm:ss"
  127. let currentTime = dateFormatter.string(from: Date())
  128. // loop throug entries and get current basal entry
  129. for (index, entry) in basalEntries.enumerated() {
  130. if let entryStartTimeDate = dateFormatter.date(from: entry.start) {
  131. var entryEndTimeDate: Date
  132. if index < basalEntries.count - 1 {
  133. let nextEntry = basalEntries[index + 1]
  134. if let nextEntryStartTimeDate = dateFormatter.date(from: nextEntry.start) {
  135. let timeDifference = nextEntryStartTimeDate.timeIntervalSince(entryStartTimeDate)
  136. entryEndTimeDate = entryStartTimeDate.addingTimeInterval(timeDifference)
  137. } else {
  138. continue
  139. }
  140. } else {
  141. entryEndTimeDate = Date()
  142. }
  143. // if currenTime is between start and end of basal entry -> basal = currentBasal
  144. if let currentTimeDate = dateFormatter.date(from: currentTime) {
  145. if currentTimeDate >= entryStartTimeDate, currentTimeDate <= entryEndTimeDate {
  146. if let basal = entry.rate as? Decimal {
  147. currentBasal = basal
  148. break
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. func getDeltaBG() {
  156. let glucose = provider.fetchGlucose()
  157. guard glucose.count >= 3 else { return }
  158. let lastGlucose = glucose.first?.glucose ?? 0
  159. let thirdLastGlucose = glucose[2]
  160. let delta = Decimal(lastGlucose) - Decimal(thirdLastGlucose.glucose)
  161. deltaBG = delta
  162. }
  163. // MARK: CALCULATIONS FOR THE BOLUS CALCULATOR
  164. /// Calculate insulin recommendation
  165. func calculateInsulin() -> Decimal {
  166. // ensure that isf is in mg/dL
  167. var conversion: Decimal {
  168. units == .mmolL ? 0.0555 : 1
  169. }
  170. let isfForCalculation = isf / conversion
  171. // insulin needed for the current blood glucose
  172. targetDifference = (currentBG - target)
  173. targetDifferenceInsulin = targetDifference / isfForCalculation
  174. // more or less insulin because of bg trend in the last 15 minutes
  175. fifteenMinInsulin = deltaBG / isfForCalculation
  176. // determine whole COB for which we want to dose insulin for and then determine insulin for wholeCOB
  177. wholeCob = cob + carbs
  178. wholeCobInsulin = wholeCob / carbRatio
  179. // determine how much the calculator reduces/ increases the bolus because of IOB
  180. iobInsulinReduction = (-1) * iob
  181. // adding everything together
  182. // add a calc for the case that no fifteenMinInsulin is available
  183. if deltaBG != 0 {
  184. wholeCalc = (targetDifferenceInsulin + iobInsulinReduction + wholeCobInsulin + fifteenMinInsulin)
  185. } else {
  186. // add (rare) case that no glucose value is available -> maybe display warning?
  187. // if no bg is available, ?? sets its value to 0
  188. if currentBG == 0 {
  189. wholeCalc = (iobInsulinReduction + wholeCobInsulin)
  190. } else {
  191. wholeCalc = (targetDifferenceInsulin + iobInsulinReduction + wholeCobInsulin)
  192. }
  193. }
  194. // apply custom factor at the end of the calculations
  195. let result = wholeCalc * fraction
  196. // apply custom factor if fatty meal toggle in bolus calc config settings is on and the box for fatty meals is checked (in RootView)
  197. if useFattyMealCorrectionFactor {
  198. insulinCalculated = result * fattyMealFactor
  199. } else if useSuperBolus {
  200. superBolusInsulin = sweetMealFactor * currentBasal
  201. insulinCalculated = result + superBolusInsulin
  202. } else {
  203. insulinCalculated = result
  204. }
  205. // display no negative insulinCalculated
  206. insulinCalculated = max(insulinCalculated, 0)
  207. insulinCalculated = min(insulinCalculated, maxBolus)
  208. return apsManager
  209. .roundBolus(amount: max(insulinCalculated, 0))
  210. }
  211. func setupInsulinRequired() {
  212. DispatchQueue.main.async {
  213. self.insulinRequired = self.provider.suggestion?.insulinReq ?? 0
  214. var conversion: Decimal = 1.0
  215. if self.units == .mmolL {
  216. conversion = 0.0555
  217. }
  218. self.evBG = self.provider.suggestion?.eventualBG ?? 0
  219. self.insulin = self.provider.suggestion?.insulinForManualBolus ?? 0
  220. self.target = self.provider.suggestion?.current_target ?? 0
  221. self.isf = self.provider.suggestion?.isf ?? 0
  222. self.iob = self.provider.suggestion?.iob ?? 0
  223. self.currentBG = (self.provider.suggestion?.bg ?? 0)
  224. self.cob = self.provider.suggestion?.cob ?? 0
  225. self.basal = self.provider.suggestion?.rate ?? 0
  226. self.carbRatio = self.provider.suggestion?.carbRatio ?? 0
  227. if self.settingsManager.settings.insulinReqPercentage != 100 {
  228. self.insulinRecommended = self.insulin * (self.settingsManager.settings.insulinReqPercentage / 100)
  229. } else { self.insulinRecommended = self.insulin }
  230. self.errorString = self.provider.suggestion?.manualBolusErrorString ?? 0
  231. if self.errorString != 0 {
  232. self.error = true
  233. self.minGuardBG = (self.provider.suggestion?.minGuardBG ?? 0) * conversion
  234. self.minDelta = (self.provider.suggestion?.minDelta ?? 0) * conversion
  235. self.expectedDelta = (self.provider.suggestion?.expectedDelta ?? 0) * conversion
  236. self.minPredBG = (self.provider.suggestion?.minPredBG ?? 0) * conversion
  237. } else { self.error = false }
  238. self.insulinRecommended = self.apsManager
  239. .roundBolus(amount: max(self.insulinRecommended, 0))
  240. if self.useCalc {
  241. self.getCurrentBasal()
  242. self.getDeltaBG()
  243. self.insulinCalculated = self.calculateInsulin()
  244. }
  245. }
  246. }
  247. // MARK: - Button tasks
  248. @MainActor func invokeTreatmentsTask() {
  249. Task {
  250. let isInsulinGiven = amount > 0
  251. let isCarbsPresent = carbs > 0
  252. if isInsulinGiven {
  253. try await handleInsulin(isExternal: externalInsulin)
  254. } else if isCarbsPresent {
  255. waitForSuggestion = true
  256. } else {
  257. hideModal()
  258. return
  259. }
  260. saveMeal()
  261. addButtonPressed = true
  262. // if glucose data is stale end the custom loading animation by hiding the modal
  263. // guard glucoseOfLast20Min.first?.date ?? now >= Date().addingTimeInterval(-12.minutes.timeInterval) else {
  264. // return hideModal()
  265. // }
  266. }
  267. }
  268. // MARK: - Insulin
  269. @MainActor private func handleInsulin(isExternal: Bool) async throws {
  270. if !isExternal {
  271. await addPumpInsulin()
  272. } else {
  273. await addExternalInsulin()
  274. }
  275. waitForSuggestion = true
  276. }
  277. @MainActor func addPumpInsulin() async {
  278. guard amount > 0 else {
  279. showModal(for: nil)
  280. return
  281. }
  282. let maxAmount = Double(min(amount, provider.pumpSettings().maxBolus))
  283. do {
  284. let authenticated = try await unlockmanager.unlock()
  285. if authenticated {
  286. apsManager.enactBolus(amount: maxAmount, isSMB: false)
  287. savePumpInsulin(amount: amount)
  288. } else {
  289. print("authentication failed")
  290. }
  291. } catch {
  292. print("authentication error for pump bolus: \(error.localizedDescription)")
  293. DispatchQueue.main.async {
  294. self.waitForSuggestion = false
  295. if self.addButtonPressed {
  296. self.hideModal()
  297. }
  298. }
  299. }
  300. }
  301. private func savePumpInsulin(amount: Decimal) {
  302. let newItem = InsulinStored(context: context)
  303. newItem.id = UUID()
  304. newItem.amount = amount as NSDecimalNumber
  305. newItem.date = Date()
  306. newItem.external = false
  307. newItem.isSMB = false
  308. self.context.perform {
  309. do {
  310. try self.context.save()
  311. debugPrint(
  312. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved pump insulin to core data"
  313. )
  314. } catch {
  315. debugPrint(
  316. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save pump insulin to core data"
  317. )
  318. }
  319. }
  320. }
  321. // MARK: - EXTERNAL INSULIN
  322. @MainActor func addExternalInsulin() async {
  323. guard amount > 0 else {
  324. showModal(for: nil)
  325. return
  326. }
  327. amount = min(amount, maxBolus * 3)
  328. do {
  329. let authenticated = try await unlockmanager.unlock()
  330. if authenticated {
  331. storeExternalInsulinEvent()
  332. } else {
  333. print("authentication failed")
  334. }
  335. } catch {
  336. print("authentication error for external insulin: \(error.localizedDescription)")
  337. DispatchQueue.main.async {
  338. self.waitForSuggestion = false
  339. if self.addButtonPressed {
  340. self.hideModal()
  341. }
  342. }
  343. }
  344. }
  345. private func storeExternalInsulinEvent() {
  346. pumpHistoryStorage.storeEvents(
  347. [
  348. PumpHistoryEvent(
  349. id: UUID().uuidString,
  350. type: .bolus,
  351. timestamp: date,
  352. amount: amount,
  353. duration: nil,
  354. durationMin: nil,
  355. rate: nil,
  356. temp: nil,
  357. carbInput: nil,
  358. isExternal: true
  359. )
  360. ]
  361. )
  362. debug(.default, "External insulin saved to pumphistory.json")
  363. // save to core data asynchronously
  364. self.context.perform {
  365. let newItem = InsulinStored(context: self.context)
  366. newItem.amount = (self.amount) as NSDecimalNumber
  367. newItem.date = Date()
  368. newItem.external = true
  369. newItem.isSMB = false
  370. do {
  371. try self.context.save()
  372. debugPrint(
  373. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved carbs to core data"
  374. )
  375. } catch {
  376. debugPrint(
  377. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save carbs to core data"
  378. )
  379. }
  380. }
  381. // perform determine basal sync
  382. apsManager.determineBasalSync()
  383. }
  384. // MARK: - Carbs
  385. // we need to also fetch the data after we have saved them in order to update the array and the UI because of the MVVM Architecture
  386. func saveMeal() {
  387. guard carbs > 0 || fat > 0 || protein > 0 else { return }
  388. carbs = min(carbs, maxCarbs)
  389. id_ = UUID().uuidString
  390. let carbsToStore = [CarbsEntry(
  391. id: id_,
  392. createdAt: now,
  393. actualDate: date,
  394. carbs: carbs,
  395. fat: fat,
  396. protein: protein,
  397. note: note,
  398. enteredBy: CarbsEntry.manual,
  399. isFPU: false, fpuID: UUID().uuidString
  400. )]
  401. carbsStorage.storeCarbs(carbsToStore)
  402. if carbs > 0 {
  403. saveCarbsToCoreData(carbsToStore)
  404. // only perform determine basal sync if the user doesn't use the pump bolus, otherwise the enact bolus func in the APSManger does a sync
  405. if amount <= 0 {
  406. apsManager.determineBasalSync()
  407. }
  408. }
  409. }
  410. func saveCarbsToCoreData(_: [CarbsEntry]) {
  411. context.performAndWait {
  412. // create new object in the view context
  413. let newCarbEntry = MealsStored(context: self.context)
  414. newCarbEntry.id = UUID()
  415. newCarbEntry.note = ""
  416. newCarbEntry.carbs = Double(carbs)
  417. newCarbEntry.fat = Double(fat)
  418. newCarbEntry.protein = Double(protein)
  419. self.context.perform {
  420. do {
  421. try self.context.save()
  422. debugPrint(
  423. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved carbs to core data"
  424. )
  425. } catch {
  426. debugPrint(
  427. "Bolus State: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save carbs to core data"
  428. )
  429. }
  430. }
  431. }
  432. }
  433. // MARK: - Presets
  434. func deletePreset() {
  435. if selection != nil {
  436. try? context.delete(selection!)
  437. try? context.save()
  438. carbs = 0
  439. fat = 0
  440. protein = 0
  441. }
  442. selection = nil
  443. }
  444. func removePresetFromNewMeal() {
  445. let a = summation.firstIndex(where: { $0 == selection?.dish! })
  446. if a != nil, summation[a ?? 0] != "" {
  447. summation.remove(at: a!)
  448. }
  449. }
  450. func addPresetToNewMeal() {
  451. let test: String = selection?.dish ?? "dontAdd"
  452. if test != "dontAdd" {
  453. summation.append(test)
  454. }
  455. }
  456. func addNewPresetToWaitersNotepad(_ dish: String) {
  457. summation.append(dish)
  458. }
  459. func addToSummation() {
  460. summation.append(selection?.dish ?? "")
  461. }
  462. func waitersNotepad() -> String {
  463. var filteredArray = summation.filter { !$0.isEmpty }
  464. if carbs == 0, protein == 0, fat == 0 {
  465. filteredArray = []
  466. }
  467. guard filteredArray != [] else {
  468. return ""
  469. }
  470. var carbs_: Decimal = 0.0
  471. var fat_: Decimal = 0.0
  472. var protein_: Decimal = 0.0
  473. var presetArray = [Presets]()
  474. context.performAndWait {
  475. let requestPresets = Presets.fetchRequest() as NSFetchRequest<Presets>
  476. try? presetArray = context.fetch(requestPresets)
  477. }
  478. var waitersNotepad = [String]()
  479. var stringValue = ""
  480. for each in filteredArray {
  481. let countedSet = NSCountedSet(array: filteredArray)
  482. let count = countedSet.count(for: each)
  483. if each != stringValue {
  484. waitersNotepad.append("\(count) \(each)")
  485. }
  486. stringValue = each
  487. for sel in presetArray {
  488. if sel.dish == each {
  489. carbs_ += (sel.carbs)! as Decimal
  490. fat_ += (sel.fat)! as Decimal
  491. protein_ += (sel.protein)! as Decimal
  492. break
  493. }
  494. }
  495. }
  496. let extracarbs = carbs - carbs_
  497. let extraFat = fat - fat_
  498. let extraProtein = protein - protein_
  499. var addedString = ""
  500. if extracarbs > 0, filteredArray.isNotEmpty {
  501. addedString += "Additional carbs: \(extracarbs) ,"
  502. } else if extracarbs < 0 { addedString += "Removed carbs: \(extracarbs) " }
  503. if extraFat > 0, filteredArray.isNotEmpty {
  504. addedString += "Additional fat: \(extraFat) ,"
  505. } else if extraFat < 0 { addedString += "Removed fat: \(extraFat) ," }
  506. if extraProtein > 0, filteredArray.isNotEmpty {
  507. addedString += "Additional protein: \(extraProtein) ,"
  508. } else if extraProtein < 0 { addedString += "Removed protein: \(extraProtein) ," }
  509. if addedString != "" {
  510. waitersNotepad.append(addedString)
  511. }
  512. var waitersNotepadString = ""
  513. if waitersNotepad.count == 1 {
  514. waitersNotepadString = waitersNotepad[0]
  515. } else if waitersNotepad.count > 1 {
  516. for each in waitersNotepad {
  517. if each != waitersNotepad.last {
  518. waitersNotepadString += " " + each + ","
  519. } else { waitersNotepadString += " " + each }
  520. }
  521. }
  522. return waitersNotepadString
  523. }
  524. func loadEntries(_ editMode: Bool) {
  525. if editMode {
  526. context.performAndWait {
  527. var mealToEdit = [Meals]()
  528. let requestMeal = Meals.fetchRequest() as NSFetchRequest<Meals>
  529. let sortMeal = NSSortDescriptor(key: "createdAt", ascending: false)
  530. requestMeal.sortDescriptors = [sortMeal]
  531. requestMeal.fetchLimit = 1
  532. try? mealToEdit = self.context.fetch(requestMeal)
  533. self.carbs = Decimal(mealToEdit.first?.carbs ?? 0)
  534. self.fat = Decimal(mealToEdit.first?.fat ?? 0)
  535. self.protein = Decimal(mealToEdit.first?.protein ?? 0)
  536. self.note = mealToEdit.first?.note ?? ""
  537. self.id_ = mealToEdit.first?.id ?? ""
  538. }
  539. }
  540. }
  541. }
  542. }
  543. extension Bolus.StateModel: SuggestionObserver, BolusFailureObserver {
  544. func suggestionDidUpdate(_: Suggestion) {
  545. DispatchQueue.main.async {
  546. self.waitForSuggestion = false
  547. if self.addButtonPressed {
  548. self.hideModal()
  549. }
  550. }
  551. setupInsulinRequired()
  552. }
  553. func bolusDidFail() {
  554. DispatchQueue.main.async {
  555. self.waitForSuggestion = false
  556. if self.addButtonPressed {
  557. self.hideModal()
  558. }
  559. }
  560. }
  561. }