浏览代码

widget

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 年之前
父节点
当前提交
6c997c4375

+ 16 - 9
Widget/Dashboard/DashboardData.swift

@@ -29,8 +29,8 @@ import SVGKit
 struct DashboardDataEntry: TimelineEntry {
     let date: Date
     let datas: [DashboardData]
-    let tableDashboard: tableDashboardWidget?
-    let tableButton: [tableDashboardWidgetButton]?
+    let dashboard: tableDashboardWidget?
+    let buttons: [tableDashboardWidgetButton]?
     let isPlaceholder: Bool
     let titleImage: UIImage
     let title: String
@@ -84,11 +84,11 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
     let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems - 1])
 
     if isPreview {
-        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
+        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
     }
 
     guard let account = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", comment: "")))
+        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", comment: "")))
     }
 
     // Default widget
@@ -98,7 +98,7 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
     let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
 
     guard serverVersionMajor >= NCGlobal.shared.nextcloudVersion25 else {
-        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_widget_available_nc25_", comment: "")))
+        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_widget_available_nc25_", comment: "")))
     }
         
     // NETWORKING
@@ -146,10 +146,12 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
 
         Task {
             var datas = [DashboardData]()
+            var numberItems = 0
 
             if let results = results {
                 for result in results {
                     if let items = result.items {
+                        numberItems = result.items?.count ?? 0
                         var counter: Int = 0
                         let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: existsButton)
                         for item in items {
@@ -157,7 +159,7 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
                             let title = item.title ?? ""
                             let subtitle = item.subtitle ?? ""
                             var link = URL(string: "https://")!
-                            if let entryLink = item.link, let url = URL(string: entryLink){ link = url }
+                            if let entryLink = item.link, let url = URL(string: entryLink) { link = url }
                             var icon = UIImage(named: "file")!
                             var iconFileName: String?
                             var template: Bool = false
@@ -210,12 +212,17 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
                 }
             }
 
+            var buttons = tableButton
+            if numberItems == datas.count, let tableButton = tableButton, tableButton.contains(where: { $0.type == "more"}) {
+                buttons = tableButton.filter(({ $0.type != "more" }))
+            }
+
             if error != .success {
-                completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
+                completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: tableDashboard, buttons: buttons, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
             } else if datas.isEmpty {
-                completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NSLocalizedString("_no_data_available_", comment: "")))
+                completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: tableDashboard, buttons: buttons, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NSLocalizedString("_no_data_available_", comment: "")))
             } else {
-                completion(DashboardDataEntry(date: Date(), datas: datas, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
+                completion(DashboardDataEntry(date: Date(), datas: datas, dashboard: tableDashboard, buttons: buttons, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
             }
         }
     }

+ 1 - 1
Widget/Dashboard/DashboardWidgetProvider.swift

@@ -35,7 +35,7 @@ struct DashboardWidgetProvider: IntentTimelineProvider {
         let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems])
         let title = "Dashboard"
         let titleImage = UIImage(named: "widget")!
-        return Entry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
+        return Entry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
     }
 
     func getSnapshot(for configuration: DashboardIntent, in context: Context, completion: @escaping (DashboardDataEntry) -> Void) {

+ 6 - 6
Widget/Dashboard/DashboardWidgetView.swift

@@ -69,7 +69,7 @@ struct DashboardWidgetView: View {
                                             .fill(Color(.systemGray4))
                                             .frame(width: 35, height: 35)
                                     } else if element.template {
-                                        if entry.tableDashboard?.itemIconsRound ?? false {
+                                        if entry.dashboard?.itemIconsRound ?? false {
                                             Image(uiImage: element.icon)
                                                 .renderingMode(.template)
                                                 .resizable()
@@ -90,7 +90,7 @@ struct DashboardWidgetView: View {
                                                 .cornerRadius(5)
                                         }
                                     } else {
-                                        if entry.tableDashboard?.itemIconsRound ?? false || element.avatar {
+                                        if entry.dashboard?.itemIconsRound ?? false || element.avatar {
                                             Image(uiImage: element.icon)
                                                 .resizable()
                                                 .scaledToFill()
@@ -129,14 +129,14 @@ struct DashboardWidgetView: View {
                 .padding(.top, 35)
                 .redacted(reason: entry.isPlaceholder ? .placeholder : [])
 
-                if let tableButton = entry.tableButton, !tableButton.isEmpty, !entry.isPlaceholder {
-                    
+                if let buttons = entry.buttons, !buttons.isEmpty, !entry.isPlaceholder {
+
                     HStack(spacing: 10) {
 
                         let brandColor = Color(NCBrandColor.shared.brand)
                         let brandTextColor = Color(NCBrandColor.shared.brandText)
 
-                        ForEach(tableButton, id: \.index) { element in
+                        ForEach(buttons, id: \.index) { element in
                             Link(destination: URL(string: element.link)! , label: {
                                 
                                 Text(element.text)
@@ -176,7 +176,7 @@ struct DashboardWidget_Previews: PreviewProvider {
         let datas = Array(dashboardDatasTest[0...4])
         let title = "Dashboard"
         let titleImage = UIImage(named: "widget")!
-        let entry = DashboardDataEntry(date: Date(), datas: datas, tableDashboard: nil, tableButton: nil, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
+        let entry = DashboardDataEntry(date: Date(), datas: datas, dashboard: nil, buttons: nil, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
         DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
     }
 }