浏览代码

improved widget

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

+ 15 - 7
Widget/Nextcloud/NextcloudData.swift

@@ -51,14 +51,23 @@ let recentDatasTest: [RecentData] = [
     .init(id: "5", image: UIImage(named: "AppIcon")!, title: "title5", subTitle: "subTitle-description5", url: URL(string: "https://nextcloud.com/")!)
 ]
 
+func getNumberItems(height: CGFloat) -> Int {
+    
+    let num: Int = Int((height - 150) / 40)
+    return num
+}
+
 func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
 
+    let num: Int = Int((displaySize.height - 150) / 40) - 1
+    let recentDatasPlaceholder = Array(recentDatasTest[0...num])
+    
     if isPreview {
-        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
+        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
     }
 
     guard let account = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
+        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
     }
 
     func isLive(file: NKFile, files: [NKFile]) -> Bool {
@@ -165,8 +174,6 @@ func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_
         NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS)
     }
     
-    let numRecentDatas: Int = Int((displaySize.height - 150) / 40)
-
     NextcloudKit.shared.searchBodyRequest(serverUrl: account.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, error in
 
         // Get recent files
@@ -193,7 +200,8 @@ func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_
             }
             let recentData = RecentData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
             recentDatas.append(recentData)
-            if recentDatas.count == numRecentDatas { break}
+            let numItems = getNumberItems(height: displaySize.height)
+            if recentDatas.count == numItems { break}
         }
 
         let fileInUpload = NCManageDatabase.shared.getNumMetadatasInUpload()
@@ -202,9 +210,9 @@ func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_
 
         // Completion
         if error != .success {
-            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerImage: "xmark.icloud", footerText: error.errorDescription))
+            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: error.errorDescription))
         } else if recentDatas.isEmpty {
-            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerImage: footerImage, footerText: footerText))
+            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: footerImage, footerText: footerText))
         } else {
             completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatas, isPlaceholder: false, footerImage: footerImage, footerText: footerText))
         }

+ 3 - 1
Widget/Nextcloud/NextcloudWidgetProvider.swift

@@ -29,7 +29,9 @@ struct NextcloudWidgetProvider: TimelineProvider {
     typealias Entry = NextcloudDataEntry
 
     func placeholder(in context: Context) -> Entry {
-        return Entry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
+        let num: Int = Int((context.displaySize.height - 150) / 40) - 1
+        let recentDatasPlaceholder = Array(recentDatasTest[0...num])
+        return Entry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {

+ 2 - 2
Widget/Nextcloud/NextcloudWidgetView.swift

@@ -33,8 +33,8 @@ struct NextcloudWidgetView: View {
                     Image("AppIcon")
                         .resizable()
                         .scaledToFit()
-                        .frame(width: 18, height: 18)
-                        .cornerRadius(4)
+                        .frame(width: 20, height: 20)
+                        .cornerRadius(5)
                     Text(NCBrandOptions.shared.brand + "")
                         .font(.system(size: 12))
                         .textCase(.uppercase)