Browse Source

coding

Signed-off-by: marinofaggiana <marino.faggiana@nextcloud.com>
marinofaggiana 2 years ago
parent
commit
cbd21a751c

+ 22 - 15
Widget/Nextcloud/NextcloudData.swift

@@ -24,6 +24,10 @@
 import WidgetKit
 import NCCommunication
 
+let imageSize:CGFloat = 30
+let spacingImageUpload:CGFloat = 10
+
+
 struct NextcloudDataEntry: TimelineEntry {
     let date: Date
     let recentDatas: [RecentData]
@@ -44,6 +48,7 @@ struct UploadData: Identifiable, Hashable {
     var id: String
     var image: UIImage
     var task: Int
+    var num: Int
 }
 
 let recentDatasTest: [RecentData] = [
@@ -55,19 +60,19 @@ let recentDatasTest: [RecentData] = [
 ]
 
 let uploadDatasTest: [UploadData] = [
-    .init(id: "1", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "2", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "3", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "4", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "5", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "6", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "7", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "8", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "9", image: UIImage(named: "nextcloud")!, task: 0),
-    .init(id: "0", image: UIImage(named: "nextcloud")!, task: 0)
+    .init(id: "1", image: UIImage(named: "nextcloud")!, task: 0, num: 0),
+    .init(id: "2", image: UIImage(named: "nextcloud")!, task: 0, num: 1),
+    .init(id: "3", image: UIImage(named: "nextcloud")!, task: 0, num: 2),
+    .init(id: "4", image: UIImage(named: "nextcloud")!, task: 0, num: 3),
+    .init(id: "5", image: UIImage(named: "nextcloud")!, task: 0, num: 4),
+    .init(id: "6", image: UIImage(named: "nextcloud")!, task: 0, num: 5),
+    .init(id: "7", image: UIImage(named: "nextcloud")!, task: 0, num: 6),
+    .init(id: "8", image: UIImage(named: "nextcloud")!, task: 0, num: 7),
+    .init(id: "9", image: UIImage(named: "nextcloud")!, task: 0, num: 8),
+    .init(id: "0", image: UIImage(named: "nextcloud")!, task: 0, num: 9)
 ]
 
-func getDataEntry(isPreview: Bool, size: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
+func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
 
     if isPreview {
         return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, uploadDatas: uploadDatasTest, isPlaceholder: true, footerText: ""))
@@ -209,21 +214,23 @@ func getDataEntry(isPreview: Bool, size: CGSize, completion: @escaping (_ entry:
             }
 
             // Get upload files
+            let limit = Int(displaySize.width / (imageSize + spacingImageUpload))
             var uploadDatas: [UploadData] = []
             let metadatas = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "status == %i || status == %i || status == %i", NCGlobal.shared.metadataStatusWaitUpload, NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading), page: 1, limit: 10, sorted: "sessionTaskIdentifier", ascending: false)
             for metadata in metadatas {
                 // image
                 let image:UIImage = NCUtilityGUI.shared.createFilePreviewImage(metadata: metadata) ?? UIImage(named: "file")!
                 // Upload Data
-                uploadDatas.append(UploadData(id: metadata.ocId, image: image, task: metadata.sessionTaskIdentifier))
-                if uploadDatas.count == 5 { break}
+                uploadDatas.append(UploadData(id: metadata.ocId, image: image, task: metadata.sessionTaskIdentifier, num: 0))
             }
 
             // Completion
             if errorCode != 0 {
-                completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, uploadDatas: uploadDatasTest, isPlaceholder: true, footerText: errorDescription))
+                let uploadDatas = uploadDatasTest.filter({ $0.num < limit})
+                completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, uploadDatas: uploadDatas, isPlaceholder: true, footerText: errorDescription))
             } else if recentDatas.isEmpty {
-                completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, uploadDatas: uploadDatasTest, isPlaceholder: true, footerText: "Auto upoload: \(items), \(Date().formatted())"))
+                let uploadDatas = uploadDatasTest.filter({ $0.num < limit})
+                completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, uploadDatas: uploadDatas, isPlaceholder: true, footerText: "Auto upoload: \(items), \(Date().formatted())"))
             } else {
                 completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatas, uploadDatas: uploadDatas, isPlaceholder: false, footerText: "Auto upoload: \(items), \(Date().formatted())"))
             }

+ 2 - 2
Widget/Nextcloud/NextcloudWidgetProvider.swift

@@ -33,13 +33,13 @@ struct NextcloudWidgetProvider: TimelineProvider {
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
-        getDataEntry(isPreview: context.isPreview, size: context.displaySize) { entry in
+        getDataEntry(isPreview: context.isPreview, displaySize: context.displaySize) { entry in
             completion(entry)
         }
     }
 
     func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
-        getDataEntry(isPreview: context.isPreview, size: context.displaySize) { entry in
+        getDataEntry(isPreview: context.isPreview, displaySize: context.displaySize) { entry in
             let timeLine = Timeline(entries: [entry], policy: .atEnd)
             completion(timeLine)
         }

+ 3 - 3
Widget/Nextcloud/NextcloudWidgetView.swift

@@ -49,7 +49,7 @@ struct NextcloudWidgetView: View {
                                     Image(uiImage: element.image)
                                         .resizable()
                                         .scaledToFill()
-                                        .frame(width: 30, height: 30)
+                                        .frame(width: imageSize, height: imageSize)
                                         .clipped()
                                         .cornerRadius(4)
                                     VStack(alignment: .leading) {
@@ -77,13 +77,13 @@ struct NextcloudWidgetView: View {
                             .textCase(.uppercase)
                     }
                     .padding(.leading, 10)
-                    HStack(spacing: 10) {
+                    HStack(spacing: spacingImageUpload) {
                         ForEach(entry.uploadDatas, id: \.id) { element in
                             VStack {
                                 Image(uiImage: element.image)
                                     .resizable()
                                     .scaledToFill()
-                                    .frame(width: 30, height: 30)
+                                    .frame(width: imageSize, height: imageSize)
                                     .clipped()
                                     .cornerRadius(4)
                                 Text("\(element.task)")