NCManageDatabase+UserStatus.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // NCManageDatabase+UserStatus.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/23.
  6. // Copyright © 2023 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. import RealmSwift
  26. import NextcloudKit
  27. class tableUserStatus: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var clearAt: NSDate?
  30. @objc dynamic var clearAtTime: String?
  31. @objc dynamic var clearAtType: String?
  32. @objc dynamic var icon: String?
  33. @objc dynamic var id: String?
  34. @objc dynamic var message: String?
  35. @objc dynamic var predefined: Bool = false
  36. @objc dynamic var status: String?
  37. @objc dynamic var userId: String?
  38. }
  39. extension NCManageDatabase {
  40. func addUserStatus(_ userStatuses: [NKUserStatus], account: String, predefined: Bool) {
  41. do {
  42. let realm = try Realm()
  43. try realm.write {
  44. let results = realm.objects(tableUserStatus.self).filter("account == %@ AND predefined == %@", account, predefined)
  45. realm.delete(results)
  46. for userStatus in userStatuses {
  47. let object = tableUserStatus()
  48. object.account = account
  49. object.clearAt = userStatus.clearAt as? NSDate
  50. object.clearAtTime = userStatus.clearAtTime
  51. object.clearAtType = userStatus.clearAtType
  52. object.icon = userStatus.icon
  53. object.id = userStatus.id
  54. object.message = userStatus.message
  55. object.predefined = userStatus.predefined
  56. object.status = userStatus.status
  57. object.userId = userStatus.userId
  58. realm.add(object)
  59. }
  60. }
  61. } catch let error {
  62. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  63. }
  64. }
  65. }