Skip to content

Commit 7335632

Browse files
code improved
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent c5f7428 commit 7335632

12 files changed

Lines changed: 45 additions & 46 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-FileCopyrightText: Nextcloud GmbH
2+
// SPDX-FileCopyrightText: 2025 Marino Faggiana
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
import Foundation
6+
7+
extension Date {
8+
func formatted(using format: String) -> String {
9+
NKLogFileManager.shared.convertDate(self, format: format)
10+
}
11+
}

Sources/NextcloudKit/Extensions/String+Extension.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ extension String {
2828
public var fileExtension: String {
2929
return String(NSString(string: self).pathExtension)
3030
}
31+
32+
func parsedDate(using format: String) -> Date? {
33+
NKLogFileManager.shared.convertDate(self, format: format)
34+
}
3135
}

Sources/NextcloudKit/Log/NKLog.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public func nkLog(stop message: String) {
5050
/// Logs a custom tagged message.
5151
/// - Parameters:
5252
/// - tag: A custom uppercase tag, e.g. \"PUSH\", \"SYNC\", \"AUTH\".
53-
/// - typeTag: the type tag .info, .debug, .warning, .error, .success ..
53+
/// - emoji: the type tag .info, .debug, .warning, .error, .success ..
5454
/// - message: The message to log.
5555
@inlinable
56-
public func nkLog(tag: String, emonji: NKLogTagEmoji = .debug, message: String) {
57-
NKLogFileManager.shared.writeLog(tag: tag, emonji: emonji, message: message)
56+
public func nkLog(tag: String, emoji: NKLogTagEmoji = .debug, message: String) {
57+
NKLogFileManager.shared.writeLog(tag: tag, emoji: emoji, message: message)
5858
}

Sources/NextcloudKit/NKCommon.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -509,24 +509,6 @@ public struct NKCommon: Sendable {
509509
return serverUrl.asUrl
510510
}
511511

512-
public func convertDate(_ dateString: String, format: String) -> Date? {
513-
if dateString.isEmpty { return nil }
514-
let dateFormatter = DateFormatter()
515-
516-
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
517-
dateFormatter.dateFormat = format
518-
guard let date = dateFormatter.date(from: dateString) else { return nil }
519-
return date
520-
}
521-
522-
func convertDate(_ date: Date, format: String) -> String? {
523-
let dateFormatter = DateFormatter()
524-
525-
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
526-
dateFormatter.dateFormat = format
527-
return dateFormatter.string(from: date)
528-
}
529-
530512
func findHeader(_ header: String, allHeaderFields: [AnyHashable: Any]?) -> String? {
531513
guard let allHeaderFields = allHeaderFields else { return nil }
532514
let keyValues = allHeaderFields.map { (String(describing: $0.key).lowercased(), String(describing: $0.value)) }

Sources/NextcloudKit/NKDataFileXML.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ public class NKDataFileXML: NSObject {
308308

309309
let propstat = element["d:propstat"][0]
310310

311-
if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text, let date = self.nkCommonInstance.convertDate(getlastmodified, format: "EEE, dd MMM y HH:mm:ss zzz") {
311+
if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text,
312+
let date = getlastmodified.parsedDate(using: "EEE, dd MMM y HH:mm:ss zzz") {
312313
file.date = date
313314
}
314315

@@ -600,7 +601,8 @@ public class NKDataFileXML: NSObject {
600601

601602
let propstat = element["d:propstat"][0]
602603

603-
if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text, let date = self.nkCommonInstance.convertDate(getlastmodified, format: "EEE, dd MMM y HH:mm:ss zzz") {
604+
if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text,
605+
let date = getlastmodified.parsedDate(using: "EEE, dd MMM y HH:mm:ss zzz") {
604606
file.date = date
605607
}
606608

@@ -678,7 +680,8 @@ public class NKDataFileXML: NSObject {
678680
item.actorType = value
679681
}
680682

681-
if let creationDateTime = element["d:propstat", "d:prop", "oc:creationDateTime"].text, let date = self.nkCommonInstance.convertDate(creationDateTime, format: "EEE, dd MMM y HH:mm:ss zzz") {
683+
if let creationDateTime = element["d:propstat", "d:prop", "oc:creationDateTime"].text,
684+
let date = creationDateTime.parsedDate(using: "EEE, dd MMM y HH:mm:ss zzz") {
682685
item.creationDateTime = date
683686
}
684687

Sources/NextcloudKit/NKMonitor.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ final class NKMonitor: EventMonitor, Sendable {
4444
nkCommonInstance.appendServerErrorAccount(account, errorCode: statusCode)
4545
}
4646

47-
let date = nkCommonInstance.convertDate(Date(), format: "yyyy-MM-dd' 'HH:mm:ss") ?? "unknown"
48-
let resultString = String(describing: response.result)
49-
5047
DispatchQueue.global(qos: .utility).async {
5148
switch NKLogFileManager.shared.logLevel {
5249
case .normal:
50+
let resultString = String(describing: response.result)
51+
5352
if let request = response.request {
5453
nkLog(info: "Network response request: \(request), result: \(resultString)")
5554
} else {
@@ -68,6 +67,8 @@ final class NKMonitor: EventMonitor, Sendable {
6867
case .verbose:
6968
let debugDesc = String(describing: response)
7069
let headerFields = String(describing: response.response?.allHeaderFields ?? [:])
70+
let date = Date().formatted(using: "yyyy-MM-dd' 'HH:mm:ss")
71+
7172

7273
nkLog(debug: "Network response result: \(date) " + debugDesc)
7374
nkLog(debug: "Network response all headers: \(date) " + headerFields)

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,9 @@ public extension NextcloudKit {
772772

773773
activity.app = subJson["app"].stringValue
774774
activity.idActivity = subJson["activity_id"].intValue
775-
if let datetime = subJson["datetime"].string {
776-
if let date = self.nkCommonInstance.convertDate(datetime, format: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") {
777-
activity.date = date
778-
}
775+
if let datetime = subJson["datetime"].string,
776+
let date = datetime.parsedDate(using: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") {
777+
activity.date = date
779778
}
780779
activity.icon = subJson["icon"].stringValue
781780
activity.link = subJson["link"].stringValue
@@ -851,10 +850,9 @@ public extension NextcloudKit {
851850
} catch {}
852851
}
853852
notification.app = subJson["app"].stringValue
854-
if let datetime = subJson["datetime"].string {
855-
if let date = self.nkCommonInstance.convertDate(datetime, format: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") {
856-
notification.date = date as Date
857-
}
853+
if let datetime = subJson["datetime"].string,
854+
let date = datetime.parsedDate(using: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") {
855+
notification.date = date
858856
}
859857
notification.icon = subJson["icon"].string
860858
notification.idNotification = subJson["notification_id"].intValue

Sources/NextcloudKit/NextcloudKit+Download.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public extension NextcloudKit {
5959
if etag != nil {
6060
etag = etag?.replacingOccurrences(of: "\"", with: "")
6161
}
62-
if let dateString = self.nkCommonInstance.findHeader("Date", allHeaderFields: response.response?.allHeaderFields) {
63-
date = self.nkCommonInstance.convertDate(dateString, format: "EEE, dd MMM y HH:mm:ss zzz")
62+
if let dateRaw = self.nkCommonInstance.findHeader("Date", allHeaderFields: response.response?.allHeaderFields) {
63+
date = dateRaw.parsedDate(using: "yyyy-MM-dd HH:mm:ss")
6464
}
6565

6666
options.queue.async { completionHandler(account, etag, date, length, response, nil, .success) }

Sources/NextcloudKit/NextcloudKit+Share.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public extension NextcloudKit {
435435
share.canEdit = json["can_edit"].boolValue
436436
share.displaynameFileOwner = json["displayname_file_owner"].stringValue
437437
share.displaynameOwner = json["displayname_owner"].stringValue
438-
if let expiration = json["expiration"].string, let date = self.nkCommonInstance.convertDate(expiration, format: "YYYY-MM-dd HH:mm:ss") {
438+
if let expiration = json["expiration"].string, let date = expiration.parsedDate(using: "YYYY-MM-dd HH:mm:ss") {
439439
share.expirationDate = date as NSDate
440440
}
441441
share.fileParent = json["file_parent"].intValue

Sources/NextcloudKit/NextcloudKit+Upload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public extension NextcloudKit {
6969
if etag != nil {
7070
etag = etag?.replacingOccurrences(of: "\"", with: "")
7171
}
72-
if let dateString = self.nkCommonInstance.findHeader("date", allHeaderFields: response.response?.allHeaderFields) {
73-
date = self.nkCommonInstance.convertDate(dateString, format: "EEE, dd MMM y HH:mm:ss zzz")
72+
if let dateRaw = self.nkCommonInstance.findHeader("date", allHeaderFields: response.response?.allHeaderFields) {
73+
date = dateRaw.parsedDate(using: "EEE, dd MMM y HH:mm:ss zzz")
7474
}
7575

7676
if !uploadCompleted {

0 commit comments

Comments
 (0)