NCSession.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NCSession.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/08/24.
  6. // Copyright © 2024 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 Foundation
  24. import UIKit
  25. public class NCSession: NSObject {
  26. static let shared = NCSession()
  27. public class Session {
  28. var account: String
  29. var urlBase: String
  30. var user: String
  31. var userId: String
  32. init(account: String, urlBase: String, user: String, userId: String) {
  33. self.account = account
  34. self.urlBase = urlBase
  35. self.user = user
  36. self.userId = userId
  37. }
  38. }
  39. private var sessions: ThreadSafeArray<Session> = ThreadSafeArray()
  40. override private init() {}
  41. /// SESSION
  42. ///
  43. public func appendSession(account: String, urlBase: String, user: String, userId: String) {
  44. if sessions.filter({ $0.account == account }).first != nil {
  45. return updateSession(account, userId: userId)
  46. }
  47. self.sessions.append(Session(account: account, urlBase: urlBase, user: user, userId: userId))
  48. }
  49. public func updateSession(_ account: String, userId: String? = nil) {
  50. guard let session = sessions.filter({ $0.account == account }).first else { return }
  51. if let userId {
  52. session.userId = userId
  53. }
  54. }
  55. public func removeSession(account: String) {
  56. sessions.remove(where: { $0.account == account })
  57. }
  58. public func getSession(account: String?) -> Session {
  59. if let session = sessions.filter({ $0.account == account }).first {
  60. return session
  61. }
  62. return Session(account: "", urlBase: "", user: "", userId: "")
  63. }
  64. #if !EXTENSION
  65. public func getSession(controller: UIViewController?) -> Session {
  66. if let account = (controller as? NCMainTabBarController)?.account {
  67. return getSession(account: account)
  68. } else if let tableAccount = NCManageDatabase.shared.getActiveTableAccount() {
  69. return getSession(account: tableAccount.account)
  70. }
  71. return Session(account: "", urlBase: "", user: "", userId: "")
  72. }
  73. #endif
  74. /// UTILITY
  75. ///
  76. public func getFileName(urlBase: String, user: String) -> String {
  77. let url = (URL(string: urlBase)?.host) ?? "localhost"
  78. let fileName = user + "@" + url + ".png"
  79. return fileName
  80. }
  81. }