Browse Source

Improve

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 năm trước cách đây
mục cha
commit
f567cee143

+ 2 - 2
Nextcloud.xcodeproj/project.pbxproj

@@ -3619,7 +3619,7 @@
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 8;
+				CURRENT_PROJECT_VERSION = 9;
 				DEVELOPMENT_TEAM = NKUJUXUJ3B;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;
@@ -3682,7 +3682,7 @@
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 8;
+				CURRENT_PROJECT_VERSION = 9;
 				DEVELOPMENT_TEAM = NKUJUXUJ3B;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;

+ 54 - 43
Widget/Lockscreen/LockscreenData.swift

@@ -32,14 +32,17 @@ struct LockscreenData: TimelineEntry {
     let quotaRelative: Float
     let quotaUsed: String
     let quotaTotal: String
+    let error: Bool
 }
 
-func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, completion: @escaping (_ entry: LockscreenData) -> Void) {
+func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, family: WidgetFamily, completion: @escaping (_ entry: LockscreenData) -> Void) {
 
     var account: tableAccount?
+    var quotaRelative: Float = 0
+    let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
 
     if isPreview {
-        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
+        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
     }
 
     let accountIdentifier: String = configuration?.accounts?.identifier ?? "active"
@@ -50,55 +53,63 @@ func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, comp
     }
 
     guard let account = account else {
-        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
+        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
     }
 
-    var quotaRelative: Float = 0
-    if account.quotaRelative > 0 {
-        quotaRelative = Float(account.quotaRelative) / 100
+    let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
+    if serverVersionMajor < NCGlobal.shared.nextcloudVersion25 {
+        completion(LockscreenData(date: Date(), isPlaceholder: false, activity: NSLocalizedString("_widget_available_nc25_", comment: ""), link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: true))
     }
-    let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
-    var quotaTotal: String = ""
 
-    switch account.quotaTotal {
-    case -1:
-        quotaTotal = ""
-    case -2:
-        quotaTotal = ""
-    case -3:
-        quotaTotal = ""
-    default:
-        quotaTotal = CCUtility.transformedSize(account.quotaTotal)
-    }
+    // NETWORKING
+    let password = CCUtility.getPassword(account.account)!
+    NKCommon.shared.setup(
+        account: account.account,
+        user: account.user,
+        userId: account.userId,
+        password: password,
+        urlBase: account.urlBase,
+        userAgent: CCUtility.getUserAgent(),
+        nextcloudVersion: 0,
+        delegate: NCNetworking.shared)
 
-    let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
-    if serverVersionMajor >= NCGlobal.shared.nextcloudVersion25 {
+    if #available(iOSApplicationExtension 16.0, *) {
+        if family == .accessoryCircular {
+            NextcloudKit.shared.getUserProfile(options: options) { _, userProfile, _, error in
+                if error == .success, let userProfile = userProfile, let account = NCManageDatabase.shared.setAccountUserProfile(userProfile) {
+                    if account.quotaRelative > 0 {
+                        quotaRelative = Float(account.quotaRelative) / 100
+                    }
+                    let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
+                    var quotaTotal: String = ""
 
-        // NETWORKING
-        let password = CCUtility.getPassword(account.account)!
-        NKCommon.shared.setup(
-            account: account.account,
-            user: account.user,
-            userId: account.userId,
-            password: password,
-            urlBase: account.urlBase,
-            userAgent: CCUtility.getUserAgent(),
-            nextcloudVersion: 0,
-            delegate: NCNetworking.shared)
-
-        let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
-        NextcloudKit.shared.getDashboardWidgetsApplication("activity", options: options) { _, results, _, error in
-            var activity: String = NSLocalizedString("_no_data_available_", comment: "")
-            var link = URL(string: "https://")!
-            if error == .success, let result = results?.first {
-                if let item = result.items?.first {
-                    if let title = item.title {  activity = title }
-                    if let itemLink = item.link, let url = URL(string: itemLink) { link = url }
+                    switch account.quotaTotal {
+                    case -1:
+                        quotaTotal = ""
+                    case -2:
+                        quotaTotal = ""
+                    case -3:
+                        quotaTotal = ""
+                    default:
+                        quotaTotal = CCUtility.transformedSize(account.quotaTotal)
+                    }
+                    completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal, error: false))
+                } else {
+                    completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: true))
+                }
+            }
+        } else if family == .accessoryRectangular {
+            NextcloudKit.shared.getDashboardWidgetsApplication("activity", options: options) { _, results, _, error in
+                var activity: String = NSLocalizedString("_no_data_available_", comment: "")
+                var link = URL(string: "https://")!
+                if error == .success, let result = results?.first {
+                    if let item = result.items?.first {
+                        if let title = item.title {  activity = title }
+                        if let itemLink = item.link, let url = URL(string: itemLink) { link = url }
+                    }
                 }
+                completion(LockscreenData(date: Date(), isPlaceholder: false, activity: activity, link: link, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
             }
-            completion(LockscreenData(date: Date(), isPlaceholder: false, activity: activity, link: link, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
         }
-    } else {
-        completion(LockscreenData(date: Date(), isPlaceholder: false, activity: NSLocalizedString("_widget_available_nc25_", comment: ""), link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
     }
 }

+ 3 - 3
Widget/Lockscreen/LockscreenWidgetProvider.swift

@@ -31,17 +31,17 @@ struct LockscreenWidgetProvider: IntentTimelineProvider {
     typealias Intent = AccountIntent
 
     func placeholder(in context: Context) -> Entry {
-        return Entry(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "")
+        return Entry(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false)
     }
 
     func getSnapshot(for configuration: AccountIntent, in context: Context, completion: @escaping (Entry) -> Void) {
-        getLockscreenDataEntry(configuration: configuration, isPreview: false) { entry in
+        getLockscreenDataEntry(configuration: configuration, isPreview: false, family: context.family) { entry in
             completion(entry)
         }
     }
 
     func getTimeline(for configuration: AccountIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
-        getLockscreenDataEntry(configuration: configuration, isPreview: context.isPreview) { entry in
+        getLockscreenDataEntry(configuration: configuration, isPreview: context.isPreview, family: context.family) { entry in
             let timeLine = Timeline(entries: [entry], policy: .atEnd)
             completion(timeLine)
         }

+ 20 - 8
Widget/Lockscreen/LockscreenWidgetView.swift

@@ -33,13 +33,25 @@ struct LockscreenWidgetView: View {
     var body: some View {
         switch family {
         case .accessoryCircular:
-            Gauge(
-                value: entry.quotaRelative,
-                label: { Text("  " + entry.quotaTotal + "  ") },
-                currentValueLabel: { Text(entry.quotaUsed) }
-            )
-            .gaugeStyle(.accessoryCircular)
-            .redacted(reason: entry.isPlaceholder ? .placeholder : [])
+            if entry.error {
+                Gauge(
+                    value: 0,
+                    label: {},
+                    currentValueLabel: {
+                        Image(systemName: "xmark.icloud")
+                            .font(.system(size: 25.0))
+                    }
+                )
+                .gaugeStyle(.accessoryCircularCapacity)
+            } else {
+                Gauge(
+                    value: entry.quotaRelative,
+                    label: { Text("  " + entry.quotaTotal + "  ") },
+                    currentValueLabel: { Text(entry.quotaUsed) }
+                )
+                .gaugeStyle(.accessoryCircular)
+                .redacted(reason: entry.isPlaceholder ? .placeholder : [])
+            }
         case .accessoryRectangular:
             VStack(alignment: .leading, spacing: 1) {
                 HStack(spacing: 1) {
@@ -68,7 +80,7 @@ struct LockscreenWidgetView: View {
 @available(iOSApplicationExtension 16.0, *)
 struct LockscreenWidgetView_Previews: PreviewProvider {
     static var previews: some View {
-        let entry = LockscreenData(date: Date(), isPlaceholder: false, activity: "Alba Mayoral changed Marketing / Regional Marketing / Agenda Meetings / Q4 2022 / OCTOBER / 13.11 Afrah Kahlid.md", link: URL(string: "https://")!, quotaRelative: 0.5, quotaUsed: "22 GB", quotaTotal: "50 GB")
+        let entry = LockscreenData(date: Date(), isPlaceholder: false, activity: "Alba Mayoral changed Marketing / Regional Marketing / Agenda Meetings / Q4 2022 / OCTOBER / 13.11 Afrah Kahlid.md", link: URL(string: "https://")!, quotaRelative: 0.5, quotaUsed: "22 GB", quotaTotal: "50 GB", error: true)
         LockscreenWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .accessoryRectangular)).previewDisplayName("Rectangular")
         LockscreenWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .accessoryCircular)).previewDisplayName("Circular")
     }