NCManageDatabase+Groupfolders.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // NCManageDatabase+LayoutForView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/11/22.
  6. // Copyright © 2022 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 TableGroupfolders: Object {
  28. @Persisted var account = ""
  29. @Persisted var acl: Bool = false
  30. @Persisted var groups: List<TableGroupfoldersGroups>
  31. @Persisted var id: Int = 0
  32. @Persisted var manage: Data?
  33. @Persisted var mountPoint = ""
  34. @Persisted var quota: Int = 0
  35. @Persisted var size: Int = 0
  36. }
  37. class TableGroupfoldersGroups: Object {
  38. @Persisted var account = ""
  39. @Persisted var group = ""
  40. @Persisted var permission: Int = 0
  41. convenience init(account: String, group: String, permission: Int) {
  42. self.init()
  43. self.account = account
  44. self.group = group
  45. self.permission = permission
  46. }
  47. }
  48. extension NCManageDatabase {
  49. func addGroupfolders(account: String, groupfolders: [NKGroupfolders]) {
  50. do {
  51. let realm = try Realm()
  52. try realm.write {
  53. let tableGroupfolders = realm.objects(TableGroupfolders.self).filter("account == %@", account)
  54. realm.delete(tableGroupfolders)
  55. let tableGroupfoldersGroups = realm.objects(TableGroupfoldersGroups.self).filter("account == %@", account)
  56. realm.delete(tableGroupfoldersGroups)
  57. for groupfolder in groupfolders {
  58. let obj = TableGroupfolders()
  59. obj.account = account
  60. obj.acl = groupfolder.acl
  61. for group in groupfolder.groups ?? [:] {
  62. let objGroups = TableGroupfoldersGroups(account: account, group: group.key, permission: (group.value as? Int ?? 0))
  63. obj.groups.append(objGroups)
  64. }
  65. obj.id = groupfolder.id
  66. obj.manage = groupfolder.manage
  67. obj.mountPoint = groupfolder.mountPoint
  68. obj.quota = groupfolder.quota
  69. obj.size = groupfolder.size
  70. realm.add(obj)
  71. }
  72. }
  73. } catch let error {
  74. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  75. }
  76. }
  77. }