// // NCManageDatabase+Metadata+Session.swift // Nextcloud // // Created by Marino Faggiana on 12/02/24. // Copyright © 2024 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // import Foundation import RealmSwift import NextcloudKit extension NCManageDatabase { func setMetadataSession(ocId: String, newFileName: String? = nil, session: String? = nil, sessionError: String? = nil, selector: String? = nil, status: Int? = nil, etag: String? = nil, errorCode: Int? = nil) { do { let realm = try Realm() try realm.write { if let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first { if let newFileName = newFileName { result.fileName = newFileName result.fileNameView = newFileName } if let session { result.session = session } if let sessionError { result.sessionError = sessionError if sessionError.isEmpty { result.errorCode = 0 } } if let selector { result.sessionSelector = selector } if let status { result.status = status if status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload { result.sessionDate = Date() } else if status == NCGlobal.shared.metadataStatusNormal { result.sessionDate = nil } } if let etag { result.etag = etag } if let errorCode { result.errorCode = errorCode } } } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)") } } func setMetadataSession(ocId: String, status: Int? = nil, taskIdentifier: Int? = nil) { do { let realm = try Realm() try realm.write { if let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first { if let status { result.status = status if status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload { result.sessionDate = Date() } else if status == NCGlobal.shared.metadataStatusNormal { result.sessionDate = nil } } if let taskIdentifier { result.sessionTaskIdentifier = taskIdentifier } } } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)") } } @discardableResult func setMetadatasSessionInWaitDownload(metadatas: [tableMetadata], session: String, selector: String) -> tableMetadata? { if metadatas.isEmpty { return nil } var metadataUpdated: tableMetadata? do { let realm = try Realm() try realm.write { for metadata in metadatas { if let result = realm.objects(tableMetadata.self).filter("ocId == %@", metadata.ocId).first { result.session = session result.sessionError = "" result.sessionSelector = selector result.status = NCGlobal.shared.metadataStatusWaitDownload result.sessionDate = Date() metadataUpdated = tableMetadata(value: result) } else { metadata.session = session metadata.sessionError = "" metadata.sessionSelector = selector metadata.status = NCGlobal.shared.metadataStatusWaitDownload metadata.sessionDate = Date() realm.add(metadata, update: .all) metadataUpdated = tableMetadata(value: metadata) } } } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)") } return metadataUpdated } func clearMetadataSession(metadatas: Results) { do { let realm = try Realm() try realm.write { for metadata in metadatas { metadata.session = "" metadata.sessionError = "" metadata.sessionSelector = "" metadata.sessionDate = nil metadata.status = NCGlobal.shared.metadataStatusNormal } } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)") } } @discardableResult func setMetadataStatus(ocId: String, status: Int) -> tableMetadata? { var result: tableMetadata? do { let realm = try Realm() try realm.write { result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first result?.status = status } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)") } if let result { return tableMetadata.init(value: result) } else { return nil } } }