Skip to content

Commit

Permalink
Improvements varie (#2973)
Browse files Browse the repository at this point in the history
* lint

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

* coding

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

* fix User status

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

* fix

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

* fix

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

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jul 3, 2024
1 parent 553f481 commit 789567d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 189 deletions.
27 changes: 9 additions & 18 deletions Widget/Files/FilesData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi
<d:resourcetype/>
<d:getcontentlength/>
<d:getlastmodified/>
<d:getetag/>
<id xmlns=\"http://owncloud.org/ns\"/>
<fileid xmlns=\"http://owncloud.org/ns\"/>
<size xmlns=\"http://owncloud.org/ns\"/>
Expand Down Expand Up @@ -163,7 +164,7 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi
</d:order>
</d:orderby>
<d:limit>
<d:nresults>25</d:nresults>
<d:nresults>50</d:nresults>
</d:limit>
</d:basicsearch>
</d:searchrequest>
Expand Down Expand Up @@ -198,6 +199,7 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi

for file in files {
var useTypeIconFile = false
var image: UIImage?

if file.directory || (!file.livePhotoFile.isEmpty && file.classFile == NKCommon.TypeClassFile.video.rawValue) {
continue
Expand All @@ -215,10 +217,12 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi
guard let url = URL(string: urlString) else { continue }

// IMAGE
var image = await createFilePreviewImage(ocId: file.ocId, etag: file.etag, fileNameView: file.fileName, classFile: file.classFile, status: 0, createPreviewMedia: false)
let fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(file.ocId, etag: file.etag)
let fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(file.ocId, etag: file.etag)
if FileManager.default.fileExists(atPath: fileNameIconLocalPath) {
image = UIImage(contentsOfFile: fileNameIconLocalPath)
}
if image == nil, file.hasPreview {
let fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(file.ocId, etag: file.etag)
let fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(file.ocId, etag: file.etag)
let sizePreview = NCUtility().getSizePreview(width: Int(file.width), height: Int(file.height))
let (_, _, imageIcon, _, _, _) = await NCNetworking.shared.downloadPreview(fileId: file.fileId, fileNamePreviewLocalPath: fileNamePreviewLocalPath, fileNameIconLocalPath: fileNameIconLocalPath, widthPreview: Int(sizePreview.width), heightPreview: Int(sizePreview.height), sizeIcon: NCGlobal.shared.sizeIcon, options: options)
image = imageIcon
Expand All @@ -232,7 +236,7 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi
let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)

// DATA
let data = FilesData(id: metadata.ocId, image: image!, title: metadata.fileNameView, subTitle: subTitle, url: url, useTypeIconFile: useTypeIconFile)
let data = FilesData(id: metadata.ocId, image: image ?? UIImage(), title: metadata.fileNameView, subTitle: subTitle, url: url, useTypeIconFile: useTypeIconFile)
datas.append(data)
if datas.count == filesItems { break}
}
Expand All @@ -247,17 +251,4 @@ func getFilesDataEntry(configuration: AccountIntent?, isPreview: Bool, displaySi
}
}
}

@Sendable func createFilePreviewImage(ocId: String,
etag: String,
fileNameView: String,
classFile: String,
status: Int,
createPreviewMedia: Bool) async -> UIImage? {
await withUnsafeContinuation({ continuation in
NCUtility().createFilePreviewImage(ocId: ocId, etag: etag, fileNameView: fileNameView, classFile: classFile, status: status, createPreviewMedia: createPreviewMedia) { image in
continuation.resume(returning: image)
}
})
}
}
4 changes: 2 additions & 2 deletions iOSClient/Account Settings/NCAccountSettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ class NCAccountSettingsModel: ObservableObject, ViewOnAppearHandling {
}

/// Function to update the user data
func getUserStatus() -> (statusImage: UIImage, statusMessage: String, descriptionMessage: String) {
func getUserStatus() -> (statusImage: UIImage?, statusMessage: String, descriptionMessage: String) {
guard let activeAccount else { return (UIImage(), "", "") }
if NCGlobal.shared.capabilityUserStatusEnabled,
let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", activeAccount.account)) {
return NCUtility().getUserStatus(userIcon: tableAccount.userStatusIcon, userStatus: tableAccount.userStatusStatus, userMessage: tableAccount.userStatusMessage)
}
return (UIImage(), "", "")
return (nil, "", "")
}

/// Function to know the height of "account" data
Expand Down
60 changes: 32 additions & 28 deletions iOSClient/Account Settings/NCAccountSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ struct NCAccountSettingsView: View {
.resizable()
.scaledToFit()
.frame(width: UIScreen.main.bounds.width, height: 75)
ZStack {
Circle()
.fill(.white)
.frame(width: 30, height: 30)
Image(uiImage: status.statusImage)
.resizable()
.scaledToFit()
.frame(width: 30, height: 30)
if let statusImage = status.statusImage {
ZStack {
Circle()
.fill(.white)
.frame(width: 30, height: 30)
Image(uiImage: statusImage)
.resizable()
.scaledToFit()
.frame(width: 30, height: 30)
}
.offset(x: 30, y: 30)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
Text(model.getUserName())
Expand Down Expand Up @@ -153,28 +155,30 @@ struct NCAccountSettingsView: View {
}
///
/// User Status
Button(action: {
showUserStatus = true
}, label: {
HStack {
Image(systemName: "moon.fill")
.resizable()
.scaledToFit()
.font(Font.system(.body).weight(.light))
.frame(width: 20, height: 20)
.foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
Text(NSLocalizedString("_set_user_status_", comment: ""))
.lineLimit(1)
.truncationMode(.middle)
.foregroundStyle(Color(NCBrandColor.shared.textColor))
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
if NCGlobal.shared.capabilityUserStatusEnabled {
Button(action: {
showUserStatus = true
}, label: {
HStack {
Image(systemName: "moon.fill")
.resizable()
.scaledToFit()
.font(Font.system(.body).weight(.light))
.frame(width: 20, height: 20)
.foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
Text(NSLocalizedString("_set_user_status_", comment: ""))
.lineLimit(1)
.truncationMode(.middle)
.foregroundStyle(Color(NCBrandColor.shared.textColor))
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
}
.font(.system(size: 14))
})
.sheet(isPresented: $showUserStatus) {
UserStatusView(showUserStatus: $showUserStatus)
}
.font(.system(size: 14))
})
.sheet(isPresented: $showUserStatus) {
UserStatusView(showUserStatus: $showUserStatus)
.onChange(of: showUserStatus) { _ in }
}
.onChange(of: showUserStatus) { _ in }
///
/// Certificate server
Button(action: {
Expand Down
29 changes: 1 addition & 28 deletions iOSClient/Networking/NCNetworking+Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ import Alamofire
import Queuer

extension NCNetworking {

func download(metadata: tableMetadata,
withNotificationProgressTask: Bool,
start: @escaping () -> Void = { },
requestHandler: @escaping (_ request: DownloadRequest) -> Void = { _ in },
progressHandler: @escaping (_ progress: Progress) -> Void = { _ in },
completion: @escaping (_ afError: AFError?, _ error: NKError) -> Void = { _, _ in }) {

if metadata.session == NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload {
downloadFile(metadata: metadata, withNotificationProgressTask: withNotificationProgressTask) {
start()
Expand All @@ -59,7 +57,6 @@ extension NCNetworking {
requestHandler: @escaping (_ request: DownloadRequest) -> Void = { _ in },
progressHandler: @escaping (_ progress: Progress) -> Void = { _ in },
completion: @escaping (_ afError: AFError?, _ error: NKError) -> Void = { _, _ in }) {

guard !metadata.isInTransfer else { return completion(nil, NKError()) }
var downloadTask: URLSessionTask?
let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
Expand All @@ -71,27 +68,21 @@ extension NCNetworking {
}

NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, options: options, requestHandler: { request in

self.downloadRequest[fileNameLocalPath] = request
NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
status: NCGlobal.shared.metadataStatusDownloading)
requestHandler(request)

}, taskHandler: { task in

downloadTask = task
NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
taskIdentifier: task.taskIdentifier)

NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile),
object: nil,
userInfo: ["ocId": metadata.ocId,
"serverUrl": metadata.serverUrl,
"account": metadata.account])
start()

}, progressHandler: { progress in

NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask),
object: nil,
userInfo: ["account": metadata.account,
Expand All @@ -102,13 +93,11 @@ extension NCNetworking {
"progress": NSNumber(value: progress.fractionCompleted),
"totalBytes": NSNumber(value: progress.totalUnitCount),
"totalBytesExpected": NSNumber(value: progress.completedUnitCount)])

progressHandler(progress)

}) { _, etag, date, length, allHeaderFields, afError, error in

var error = error
var dateLastModified: Date?

self.downloadRequest.removeValue(forKey: fileNameLocalPath)
// this delay was added because for small file the "taskHandler: { task" is not called, so this part of code is not executed
NextcloudKit.shared.nkCommonInstance.backgroundQueue.asyncAfter(deadline: .now() + 0.5) {
Expand All @@ -131,30 +120,24 @@ extension NCNetworking {
requestHandler: @escaping (_ request: DownloadRequest) -> Void = { _ in },
progressHandler: @escaping (_ progress: Progress) -> Void = { _ in },
completion: @escaping (_ afError: AFError?, _ error: NKError) -> Void = { _, _ in }) {

let session: URLSession = sessionManagerDownloadBackground
let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
let fileNameLocalPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)

start()

if let task = nkBackground.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, session: session) {

NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Download file \(metadata.fileNameView) with task with taskIdentifier \(task.taskIdentifier)")

NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
status: NCGlobal.shared.metadataStatusDownloading,
taskIdentifier: task.taskIdentifier)

NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile),
object: nil,
userInfo: ["ocId": metadata.ocId,
"serverUrl": metadata.serverUrl,
"account": metadata.account])
completion(nil, NKError())

} else {

NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
session: "",
sessionError: "",
Expand All @@ -165,7 +148,6 @@ extension NCNetworking {
}

func downloadingFinish(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {

if let httpResponse = (downloadTask.response as? HTTPURLResponse) {
if httpResponse.statusCode >= 200 && httpResponse.statusCode < 300,
let url = downloadTask.currentRequest?.url,
Expand All @@ -188,15 +170,13 @@ extension NCNetworking {
length: Int64,
task: URLSessionTask,
error: NKError) {

DispatchQueue.global(qos: .userInteractive).async {
guard let url = task.currentRequest?.url,
let metadata = NCManageDatabase.shared.getMetadata(from: url, sessionTaskIdentifier: task.taskIdentifier) else { return }

self.downloadMetadataInBackground.removeValue(forKey: FileNameServerUrl(fileName: fileName, serverUrl: serverUrl))

if error == .success {

self.removeTransferInError(ocId: metadata.ocId)
#if !EXTENSION
if let result = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "fileNameIdentifier == %@ AND serverUrl == %@", metadata.fileName, metadata.serverUrl)) {
Expand All @@ -216,9 +196,7 @@ extension NCNetworking {
"account": metadata.account,
"selector": metadata.sessionSelector,
"error": error])

} else if error.errorCode == NSURLErrorCancelled || error.errorCode == NCGlobal.shared.errorRequestExplicityCancelled {

self.removeTransferInError(ocId: metadata.ocId)
NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
session: "",
Expand All @@ -230,9 +208,7 @@ extension NCNetworking {
userInfo: ["ocId": metadata.ocId,
"serverUrl": metadata.serverUrl,
"account": metadata.account])

} else {

self.transferInError(ocId: metadata.ocId)
NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
session: "",
Expand All @@ -257,7 +233,6 @@ extension NCNetworking {
serverUrl: String,
session: URLSession,
task: URLSessionTask) {

DispatchQueue.global(qos: .userInteractive).async {
var metadata: tableMetadata?

Expand Down Expand Up @@ -332,7 +307,6 @@ extension NCNetworking {
}

class NCOperationDownload: ConcurrentOperation {

var metadata: tableMetadata
var selector: String

Expand All @@ -342,7 +316,6 @@ class NCOperationDownload: ConcurrentOperation {
}

override func start() {

guard !isCancelled else { return self.finish() }

metadata.session = NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload
Expand Down
Loading

0 comments on commit 789567d

Please sign in to comment.