NCChangeUserMenu.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // NCChangeUserMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/02/2021.
  6. // Copyright © 2020 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 FloatingPanel
  24. import NCCommunication
  25. class NCChangeUserMenu: NSObject {
  26. @objc func toggleMenu(viewController: UIViewController) {
  27. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  28. mainMenuViewController.actions = self.initUsersMenu()
  29. let menuPanelController = NCMenuPanelController()
  30. menuPanelController.parentPresenter = viewController
  31. menuPanelController.delegate = mainMenuViewController
  32. menuPanelController.set(contentViewController: mainMenuViewController)
  33. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  34. viewController.present(menuPanelController, animated: true, completion: nil)
  35. }
  36. private func initUsersMenu() -> [NCMenuAction] {
  37. var actions = [NCMenuAction]()
  38. let accounts = NCManageDatabase.shared.getAllAccount()
  39. var avatar = UIImage(named: "avatarCredentials")!.image(color: NCBrandColor.shared.icon, size: 50)
  40. for account in accounts {
  41. var fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(account.user, urlBase: account.urlBase) + "_" + account.user
  42. fileNamePath = fileNamePath + ".png"
  43. if var userImage = UIImage(contentsOfFile: fileNamePath) {
  44. userImage = userImage.resizeImage(size: CGSize(width: 50, height: 50), isAspectRation: true)!
  45. let userImageView = UIImageView(image: userImage)
  46. userImageView.avatar(roundness: 2, borderWidth: 1, borderColor: NCBrandColor.shared.avatarBorder, backgroundColor: .clear)
  47. UIGraphicsBeginImageContext(userImageView.bounds.size)
  48. userImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
  49. if let newAvatar = UIGraphicsGetImageFromCurrentImageContext() {
  50. avatar = newAvatar
  51. }
  52. UIGraphicsEndImageContext()
  53. }
  54. actions.append(
  55. NCMenuAction(
  56. title: account.account,
  57. icon: avatar,
  58. onTitle: account.account,
  59. onIcon: avatar,
  60. selected: account.active == true,
  61. on: account.active == true,
  62. action: { menuAction in
  63. }
  64. )
  65. )
  66. }
  67. return actions
  68. }
  69. }