Skip to content

Commit

Permalink
Mutual (#3225)
Browse files Browse the repository at this point in the history
* fileExists

Signed-off-by: Marino Faggiana <[email protected]>

* improvements

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Dec 5, 2024
1 parent c8380af commit d2545f5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
12 changes: 12 additions & 0 deletions iOSClient/Data/NCManageDatabase+Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,18 @@ extension NCManageDatabase {
}
}

func deleteMetadataOcIds(_ ocIds: [String]) {
do {
let realm = try Realm()
try realm.write {
let results = realm.objects(tableMetadata.self).filter("ocId IN %@", ocIds)
realm.delete(results)
}
} catch let error as NSError {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
}
}

func deleteMetadatas(_ metadatas: [tableMetadata]) {
do {
let realm = try Realm()
Expand Down
50 changes: 27 additions & 23 deletions iOSClient/Media/NCMedia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class NCMedia: UIViewController {
@IBOutlet weak var menuButton: UIButton!
@IBOutlet weak var gradientView: UIView!

let semaphore = DispatchSemaphore(value: 1)
var hasRunSearchMedia: Bool = false
let semaphoreSearchMedia = DispatchSemaphore(value: 1)
let semaphoreNotificationCenter = DispatchSemaphore(value: 1)

let layout = NCMediaLayout()
var layoutType = NCGlobal.shared.mediaLayoutRatio
Expand All @@ -54,6 +54,8 @@ class NCMedia: UIViewController {
var isEditMode = false
var fileSelect: [String] = []
var filesExists: [String] = []
var ocIdDoNotExists: [String] = []
var hasRunSearchMedia: Bool = false
var attributesZoomIn: UIMenuElement.Attributes = []
var attributesZoomOut: UIMenuElement.Attributes = []
let gradient: CAGradientLayer = CAGradientLayer()
Expand Down Expand Up @@ -255,12 +257,21 @@ class NCMedia: UIViewController {
guard let userInfo = notification.userInfo as NSDictionary?,
let error = userInfo["error"] as? NKError else { return }

if error.errorCode == self.global.errorResourceNotFound, let ocId = userInfo["ocId"] as? String {
semaphoreNotificationCenter.wait()

if error.errorCode == self.global.errorResourceNotFound,
let ocId = userInfo["ocId"] as? String {
self.database.deleteMetadataOcId(ocId)
self.loadDataSource()
self.loadDataSource {
self.semaphoreNotificationCenter.signal()
}
} else if error != .success {
NCContentPresenter().showError(error: error)
self.loadDataSource()
self.loadDataSource {
self.semaphoreNotificationCenter.signal()
}
} else {
semaphoreNotificationCenter.signal()
}
}

Expand All @@ -271,28 +282,21 @@ class NCMedia: UIViewController {
@objc func fileExists(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let ocId = userInfo["ocId"] as? String,
let fileExists = userInfo["fileExists"] as? Bool else { return }
var indexPaths: [IndexPath] = []
let fileExists = userInfo["fileExists"] as? Bool
else {
return
}

filesExists.append(ocId)

if !fileExists {
if let index = dataSource.metadatas.firstIndex(where: {$0.ocId == ocId}),
index < self.dataSource.metadatas.count,
let cell = collectionView.cellForItem(at: IndexPath(row: index, section: 0)) as? NCMediaCell,
dataSource.metadatas[index].ocId == cell.ocId {
indexPaths.append(IndexPath(row: index, section: 0))
}

dataSource.removeMetadata([ocId])
database.deleteMetadataOcId(ocId)
ocIdDoNotExists.append(ocId)
}

if !indexPaths.isEmpty,
!indexPaths.filter({ $0.item < self.dataSource.metadatas.count }).isEmpty {
collectionView.deleteItems(at: indexPaths)
} else {
collectionViewReloadData()
}
if NCNetworking.shared.fileExistsQueue.operationCount == 0, !ocIdDoNotExists.isEmpty {
dataSource.removeMetadata(ocIdDoNotExists)
database.deleteMetadataOcIds(ocIdDoNotExists)
ocIdDoNotExists.removeAll()
collectionViewReloadData()
}
}

Expand Down
7 changes: 4 additions & 3 deletions iOSClient/Media/NCMediaDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import NextcloudKit
import RealmSwift

extension NCMedia {
func loadDataSource() {
func loadDataSource(completion: @escaping () -> Void = {}) {
let session = self.session
DispatchQueue.global().async {
if let metadatas = self.database.getResultsMetadatas(predicate: self.imageCache.getMediaPredicate(filterLivePhotoFile: true, session: session, showOnlyImages: self.showOnlyImages, showOnlyVideos: self.showOnlyVideos), sortedByKeyPath: "date") {
self.dataSource = NCMediaDataSource(metadatas: metadatas)
}
self.collectionViewReloadData()
completion()
}
}

Expand Down Expand Up @@ -61,7 +62,7 @@ extension NCMedia {
let visibleCells = self.collectionView?.indexPathsForVisibleItems.sorted(by: { $0.row < $1.row }).compactMap({ self.collectionView?.cellForItem(at: $0) })

DispatchQueue.global(qos: .background).async {
self.semaphore.wait()
self.semaphoreSearchMedia.wait()
self.hasRunSearchMedia = true

var lessDate = Date.distantFuture
Expand Down Expand Up @@ -156,7 +157,7 @@ extension NCMedia {
self.collectionViewReloadData()
}

self.semaphore.signal()
self.semaphoreSearchMedia.signal()

DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
Expand Down
3 changes: 2 additions & 1 deletion iOSClient/Networking/NCNetworking+WebDAV.swift
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,13 @@ class NCOperationFileExists: ConcurrentOperation, @unchecked Sendable {
requestBody: nil,
account: account,
options: options) { _, _, _, error in
self.finish()

if error == .success {
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterFileExists, userInfo: ["ocId": self.ocId, "fileExists": true])
} else if error.errorCode == NCGlobal.shared.errorResourceNotFound {
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterFileExists, userInfo: ["ocId": self.ocId, "fileExists": false])
}
self.finish()
}
}
}
Expand Down

0 comments on commit d2545f5

Please sign in to comment.