Browse Source

Improved Widget

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 years ago
parent
commit
b2dbea845b

+ 12 - 6
Widget/Dashboard/DashboardData.swift

@@ -26,8 +26,6 @@ import NextcloudKit
 import Queuer
 import Queuer
 import RealmSwift
 import RealmSwift
 
 
-let dashboaardItems = 5
-
 struct DashboardDataEntry: TimelineEntry {
 struct DashboardDataEntry: TimelineEntry {
     let date: Date
     let date: Date
     let datas: [DashboardData]
     let datas: [DashboardData]
@@ -67,9 +65,17 @@ let dashboardDatasTest: [DashboardData] = [
     .init(id: 9, title: "title9", subTitle: "subTitle-description9", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!)
     .init(id: 9, title: "title9", subTitle: "subTitle-description9", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!)
 ]
 ]
 
 
+func getDashboardItems(displaySize: CGSize, withButton: Bool) -> Int {
+    
+    let height = Int((displaySize.height - 55) / 50)
+    return withButton ? (height - 1) : height
+}
+
 func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
 func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
 
 
-    let datasPlaceholder = Array(dashboardDatasTest[0...dashboaardItems - 1])
+    let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: false)
+    let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems - 1])
+    
     var id = "recommendations"
     var id = "recommendations"
     switch intent {
     switch intent {
     case .unknown:
     case .unknown:
@@ -128,7 +134,7 @@ func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: C
     }
     }
     
     
     let (tableDashboard, tableButton) = NCManageDatabase.shared.getDashboardWidget(account: account.account, id: id)
     let (tableDashboard, tableButton) = NCManageDatabase.shared.getDashboardWidget(account: account.account, id: id)
-    let existsButton: Int = tableButton == nil ? 0 : 1
+    let existsButton = (tableButton?.isEmpty ?? true) ? false : true
     let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue)
     let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue)
     let title = tableDashboard?.title ?? id
     let title = tableDashboard?.title ?? id
     var titleImage = UIImage(named: "widget")!
     var titleImage = UIImage(named: "widget")!
@@ -148,7 +154,7 @@ func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: C
             for result in results {
             for result in results {
                 if let items = result.items {
                 if let items = result.items {
                     var counter: Int = 0
                     var counter: Int = 0
-                    let maxCounter = dashboaardItems - existsButton
+                    let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: existsButton)
                     for item in items {
                     for item in items {
                         counter += 1
                         counter += 1
                         let title = item.title ?? ""
                         let title = item.title ?? ""
@@ -181,7 +187,7 @@ func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: C
                         let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon)
                         let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon)
                         datas.append(data)
                         datas.append(data)
                         
                         
-                        if datas.count == maxCounter { break }
+                        if datas.count == dashboardItems { break }
                     }
                     }
                 }
                 }
             }
             }

+ 2 - 1
Widget/Dashboard/DashboardWidgetProvider.swift

@@ -31,7 +31,8 @@ struct DashboardWidgetProvider: IntentTimelineProvider {
     typealias Entry = DashboardDataEntry
     typealias Entry = DashboardDataEntry
 
 
     func placeholder(in context: Context) -> Entry {
     func placeholder(in context: Context) -> Entry {
-        let datasPlaceholder = Array(dashboardDatasTest[0...dashboaardItems - 1])
+        let dashboardItems = getDashboardItems(displaySize: context.displaySize, withButton: false)
+        let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems])
         let title = "Dashboard"
         let title = "Dashboard"
         let titleImage = UIImage(named: "widget")!
         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, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")

+ 4 - 4
Widget/Dashboard/DashboardWidgetView.swift

@@ -95,14 +95,14 @@ struct DashboardWidgetView: View {
                                     Spacer()
                                     Spacer()
                                 }
                                 }
                                 .padding(.leading, 10)
                                 .padding(.leading, 10)
-                                .frame(height: 45)
+                                .frame(height: 50)
                             }
                             }
                             Divider()
                             Divider()
                                 .padding(.leading, 54)
                                 .padding(.leading, 54)
                         }
                         }
                     }
                     }
                 }
                 }
-                .padding(.top, 40)
+                .padding(.top, 30)
                 .redacted(reason: entry.isPlaceholder ? .placeholder : [])
                 .redacted(reason: entry.isPlaceholder ? .placeholder : [])
 
 
                 if let tableButton = entry.tableButton, !tableButton.isEmpty {
                 if let tableButton = entry.tableButton, !tableButton.isEmpty {
@@ -125,7 +125,7 @@ struct DashboardWidgetView: View {
                             })
                             })
                         }
                         }
                     }
                     }
-                    .frame(width: geo.size.width - 10, height: geo.size.height - 28, alignment: .bottomTrailing)
+                    .frame(width: geo.size.width - 10, height: geo.size.height - 25, alignment: .bottomTrailing)
                 }
                 }
                 
                 
                 HStack {
                 HStack {
@@ -152,7 +152,7 @@ struct DashboardWidgetView: View {
 
 
 struct DashboardWidget_Previews: PreviewProvider {
 struct DashboardWidget_Previews: PreviewProvider {
     static var previews: some View {
     static var previews: some View {
-        let datas = Array(dashboardDatasTest[0...dashboaardItems - 1])
+        let datas = Array(dashboardDatasTest[0...5])
         let title = "Dashboard"
         let title = "Dashboard"
         let titleImage = UIImage(named: "widget")!
         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, tableDashboard: nil, tableButton: nil, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")

+ 4 - 4
Widget/Files/FilesData.swift

@@ -69,7 +69,7 @@ func getTitleFilesWidget() -> String {
     }
     }
 }
 }
 
 
-func getItems(displaySize: CGSize) -> Int {
+func getFilesItems(displaySize: CGSize) -> Int {
     
     
     let height = Int((displaySize.height - 100) / 50)
     let height = Int((displaySize.height - 100) / 50)
     return height
     return height
@@ -77,8 +77,8 @@ func getItems(displaySize: CGSize) -> Int {
 
 
 func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: FilesDataEntry) -> Void) {
 func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: FilesDataEntry) -> Void) {
 
 
-    let items = getItems(displaySize: displaySize)
-    let datasPlaceholder = Array(filesDatasTest[0...items - 1])
+    let filesItems = getFilesItems(displaySize: displaySize)
+    let datasPlaceholder = Array(filesDatasTest[0...filesItems - 1])
     let title = getTitleFilesWidget()
     let title = getTitleFilesWidget()
     
     
     
     
@@ -220,7 +220,7 @@ func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escapi
             }
             }
             let data = FilesData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
             let data = FilesData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
             datas.append(data)
             datas.append(data)
-            if datas.count == items { break}
+            if datas.count == filesItems { break}
         }
         }
 
 
         if error != .success {
         if error != .success {

+ 2 - 2
Widget/Files/FilesWidgetProvider.swift

@@ -29,8 +29,8 @@ struct FilesWidgetProvider: TimelineProvider {
     typealias Entry = FilesDataEntry
     typealias Entry = FilesDataEntry
 
 
     func placeholder(in context: Context) -> Entry {
     func placeholder(in context: Context) -> Entry {
-        let items = getItems(displaySize: context.displaySize)
-        let datasPlaceholder = Array(filesDatasTest[0...items - 1])
+        let filesItems = getFilesItems(displaySize: context.displaySize)
+        let datasPlaceholder = Array(filesDatasTest[0...filesItems - 1])
         let title = getTitleFilesWidget()
         let title = getTitleFilesWidget()
         return Entry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " files")
         return Entry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, tile: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " files")
     }
     }