NCLoginWeb+Menu.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // NCLoginWeb+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/03/2021.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import FloatingPanel
  25. extension NCLoginWeb {
  26. func toggleMenu() {
  27. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  28. var actions = [NCMenuAction]()
  29. let accounts = NCManageDatabase.shared.getAllAccount()
  30. var avatar = NCUtility.shared.loadImage(named: "person.crop.circle")
  31. for account in accounts {
  32. let title = account.user + " " + (URL(string: account.urlBase)?.host ?? "")
  33. let fileName = String(CCUtility.getUserUrlBase(account.user, urlBase: account.urlBase)) + "-" + account.user + ".png"
  34. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  35. if let image = UIImage(contentsOfFile: fileNamePath) {
  36. avatar = image
  37. }
  38. actions.append(
  39. NCMenuAction(
  40. title: title,
  41. icon: avatar,
  42. onTitle: title,
  43. onIcon: avatar,
  44. selected: account.active == true,
  45. on: account.active == true,
  46. action: { menuAction in
  47. if self.appDelegate.account != account.account {
  48. NCManageDatabase.shared.setAccountActive(account.account)
  49. self.dismiss(animated: true) {
  50. self.appDelegate.settingAccount(account.account, urlBase: account.urlBase, user: account.user, userId: account.userId, password: CCUtility.getPassword(account.account))
  51. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterInitialize)
  52. }
  53. }
  54. }
  55. )
  56. )
  57. }
  58. actions.append(
  59. NCMenuAction(
  60. title: NSLocalizedString("_delete_active_account_", comment: ""),
  61. icon: NCUtility.shared.loadImage(named: "trash", color: NCBrandColor.shared.gray),
  62. onTitle: NSLocalizedString("_delete_active_account_", comment: ""),
  63. onIcon: avatar,
  64. selected: false,
  65. on: false,
  66. action: { menuAction in
  67. self.appDelegate.deleteAccount(self.appDelegate.account, wipe: false)
  68. self.dismiss(animated: true) {
  69. let accounts = NCManageDatabase.shared.getAllAccount()
  70. if accounts.count > 0 {
  71. self.appDelegate.changeAccount(accounts.first!.account)
  72. } else {
  73. self.appDelegate.openLogin(viewController: nil, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  74. }
  75. }
  76. }
  77. )
  78. )
  79. menuViewController.actions = actions
  80. let menuPanelController = NCMenuPanelController()
  81. menuPanelController.parentPresenter = self
  82. menuPanelController.delegate = menuViewController
  83. menuPanelController.set(contentViewController: menuViewController)
  84. menuPanelController.track(scrollView: menuViewController.tableView)
  85. self.present(menuPanelController, animated: true, completion: nil)
  86. }
  87. }