Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Oct 18, 2023
1 parent c56743f commit 5e06e3f
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions iOSClient/Media/NCMediaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import UIKit
import LRUCache
import NextcloudKit
import Queuer

struct ScaledThumbnail: Hashable {
let image: UIImage
var isPlaceholderImage = false
var scaledSize: CGSize = .zero
let ocId: String

func hash(into hasher: inout Hasher) {
hasher.combine(ocId)
}
}

@objc class NCMediaManager: NSObject {

Expand All @@ -17,7 +29,7 @@ import NextcloudKit
return instance
}()

private typealias ThumbnailLRUCache = LRUCache<String, UIImage>
typealias ThumbnailLRUCache = LRUCache<String, ScaledThumbnail>
private let cache: ThumbnailLRUCache = ThumbnailLRUCache(countLimit: 2000)

@objc func createCache(account: String) {
Expand All @@ -37,9 +49,7 @@ import NextcloudKit
var files: [FileInfo] = []

let startDate = Date()
let startMemory = NCUtility.shared.getMemoryUsedAndDeviceTotalInMegabytes()
NextcloudKit.shared.nkCommonInstance.writeLog("--------- start ThumbnailLRUCache image process ---------")
NextcloudKit.shared.nkCommonInstance.writeLog("Memory used in MB: \(startMemory.0)")

// Get files only image / video
if let enumerator = manager.enumerator(at: URL(fileURLWithPath: directory), includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {
Expand Down Expand Up @@ -68,30 +78,31 @@ import NextcloudKit
for file in files {
autoreleasepool {
if let image = UIImage(contentsOfFile: file.path.path) {
cache.setValue(image, forKey: file.ocId)
let scaledThumbnail = ScaledThumbnail(image: image, ocId: file.ocId)
cache.setValue(scaledThumbnail, forKey: file.ocId)
}
}
}

let endDate = Date()
let diffDate = endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
let endMemory = NCUtility.shared.getMemoryUsedAndDeviceTotalInMegabytes()
let usedMemory = endMemory.0 - startMemory.0
NextcloudKit.shared.nkCommonInstance.writeLog("Counter process: \(cache.count)")
NextcloudKit.shared.nkCommonInstance.writeLog("Time process: \(diffDate)")
NextcloudKit.shared.nkCommonInstance.writeLog("Memory used in MB: \(endMemory.0)")
NextcloudKit.shared.nkCommonInstance.writeLog("Memory used for cache in MB: \(usedMemory)")
NextcloudKit.shared.nkCommonInstance.writeLog("--------- stop ThumbnailLRUCache image process ---------")
}

func getImage(ocId: String) -> UIImage? {

return cache.value(forKey: ocId)
if let scaledThumbnail = cache.value(forKey: ocId) {
return scaledThumbnail.image
}
return nil
}

func setImage(ocId: String, image: UIImage) {

cache.setValue(image, forKey: ocId)
let scaledThumbnail = ScaledThumbnail(image: image, ocId: ocId)
cache.setValue(scaledThumbnail, forKey: ocId)
}

@objc func clearCache() {
Expand Down

0 comments on commit 5e06e3f

Please sign in to comment.