Browse Source

normalized

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

+ 30 - 17
Widget/Dashboard/DashboardData.swift

@@ -24,31 +24,35 @@
 import WidgetKit
 import NextcloudKit
 
-struct DashboardData: Identifiable, Codable, Hashable {
-    var id: Int
-    var image: String
-    var title: String
-    var subTitle: String
-    var url: URL
-}
+let dashboaardItems = 4
 
 struct DashboardDataEntry: TimelineEntry {
     let date: Date
-    let dashboardDatas: [DashboardData]
+    let datas: [DashboardData]
     let isPlaceholder: Bool
     let title: String
+    let footerImage: String
     let footerText: String
 }
 
+struct DashboardData: Identifiable, Hashable {
+    var id: String
+    var image: UIImage
+    var title: String
+    var subTitle: String
+    var url: URL
+}
+
 let dashboardDatasTest: [DashboardData] = [
-    .init(id: 0, image: "nextcloud", title: "title 1", subTitle: "subTitle - description 1", url: URL(string: "https://nextcloud.com/")!),
-    .init(id: 1, image: "nextcloud", title: "title 2", subTitle: "subTitle - description 2", url: URL(string: "https://nextcloud.com/")!),
-    .init(id: 2, image: "nextcloud", title: "title 3", subTitle: "subTitle - description 3", url: URL(string: "https://nextcloud.com/")!),
-    .init(id: 3, image: "nextcloud", title: "title 4", subTitle: "subTitle - description 4", url: URL(string: "https://nextcloud.com/")!),
-    .init(id: 4, image: "nextcloud", title: "title 5", subTitle: "subTitle - description 5", url: URL(string: "https://nextcloud.com/")!)
+    .init(id: "1", image: UIImage(named: "nextcloud")!, title: "title1", subTitle: "subTitle-description1", url: URL(string: "https://nextcloud.com/")!),
+    .init(id: "2", image: UIImage(named: "nextcloud")!, title: "title2", subTitle: "subTitle-description2", url: URL(string: "https://nextcloud.com/")!),
+    .init(id: "3", image: UIImage(named: "nextcloud")!, title: "title3", subTitle: "subTitle-description3", url: URL(string: "https://nextcloud.com/")!),
+    .init(id: "4", image: UIImage(named: "nextcloud")!, title: "title4", subTitle: "subTitle-description4", url: URL(string: "https://nextcloud.com/")!),
+    .init(id: "5", image: UIImage(named: "nextcloud")!, title: "title5", subTitle: "subTitle-description5", url: URL(string: "https://nextcloud.com/")!),
+    .init(id: "6", image: UIImage(named: "nextcloud")!, title: "title6", subTitle: "subTitle-description6", url: URL(string: "https://nextcloud.com/")!)
 ]
 
-func getTitle(account: tableAccount?) -> String {
+func getTitleDashboard(account: tableAccount?) -> String {
 
     let hour = Calendar.current.component(.hour, from: Date())
     var good = ""
@@ -68,11 +72,20 @@ func getTitle(account: tableAccount?) -> String {
     }
 }
 
-func readDashboardData(completion: @escaping (_ dashboardData: [DashboardData], _ isPlaceholder: Bool, _ title: String, _ footerText: String) -> Void) {
+func getDashboardDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
+
+    let datasPlaceholder = Array(dashboardDatasTest[0...dashboaardItems - 1])
+    
+    if isPreview {
+        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
+    }
 
     guard let account = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(dashboardDatasTest, true, getTitle(account: nil), NSLocalizedString("_no_active_account_", value: "No account found", comment: ""))
+        return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
     }
 
-    completion(dashboardDatasTest, false, getTitle(account: account), "\(Date().formatted())")
+    let datas = [DashboardData]()
+    
+    completion(DashboardDataEntry(date: Date(), datas: datas, isPlaceholder: false, title: getTitleDashboard(account: account), footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
+    
 }

+ 6 - 5
Widget/Dashboard/DashboardWidgetProvider.swift

@@ -29,18 +29,19 @@ struct DashboardWidgetProvider: TimelineProvider {
     typealias Entry = DashboardDataEntry
 
     func placeholder(in context: Context) -> Entry {
-        return Entry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: true, title: getTitle(account: nil), footerText: "Nextcloud Dashboard")
+        let datasPlaceholder = Array(dashboardDatasTest[0...nextcloudItems - 1])
+        return Entry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, title: "Dasboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
-        readDashboardData { dashboardDatas, isPlaceholder, title, footerText in
-            completion(Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder, title: title, footerText: footerText))
+        getDashboardDataEntry(isPreview: false, displaySize: context.displaySize) { entry in
+            completion(entry)
         }
     }
 
     func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
-        readDashboardData { dashboardDatas, isPlaceholder, title, footerText in
-            let timeLine = Timeline(entries: [Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder, title: title, footerText: footerText)], policy: .atEnd)
+        getDashboardDataEntry(isPreview: context.isPreview, displaySize: context.displaySize) { entry in
+            let timeLine = Timeline(entries: [entry], policy: .atEnd)
             completion(timeLine)
         }
     }

+ 4 - 20
Widget/Dashboard/DashboardWidgetView.swift

@@ -24,23 +24,6 @@
 import SwiftUI
 import WidgetKit
 
-/*
- @Environment(\.colorScheme) var colorScheme
-
-     var entry: Provider.Entry
-
-     var bgColor: some View {
-         colorScheme == .dark ? Color.red : Color.orange
-     }
-
-     var body: some View {
-         ZStack {
-             bgColor
-             Text(entry.date, style: .time)
-         }
-     }
- */
-
 struct DashboardWidgetView: View {
     var entry: DashboardDataEntry
     var body: some View {
@@ -51,10 +34,10 @@ struct DashboardWidgetView: View {
                     .bold()
                     .fixedSize(horizontal: false, vertical: true)
                 VStack(spacing: 5) {
-                    ForEach(entry.dashboardDatas, id: \.id) { element in
+                    ForEach(entry.datas, id: \.id) { element in
                         Link(destination: element.url) {
                             HStack {
-                                Image(element.image)
+                                Image(uiImage: element.image)
                                     .resizable()
                                     .aspectRatio(contentMode: .fit)
                                     .frame(width: 40, height: 40)
@@ -85,7 +68,8 @@ struct DashboardWidgetView: View {
 
 struct DashboardWidget_Previews: PreviewProvider {
     static var previews: some View {
-        let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: false, title: getTitle(account: nil), footerText: "Nextcloud Dashboard")
+        let datas = Array(dashboardDatasTest[0...3])
+        let entry = DashboardDataEntry(date: Date(), datas: datas, isPlaceholder: false, title: "Dashboard", footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
         DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
     }
 }

+ 15 - 15
Widget/Nextcloud/NextcloudData.swift

@@ -28,13 +28,13 @@ let nextcloudItems = 4
 
 struct NextcloudDataEntry: TimelineEntry {
     let date: Date
-    let recentDatas: [RecentData]
+    let datas: [NextcloudRecentData]
     let isPlaceholder: Bool
     let footerImage: String
     let footerText: String
 }
 
-struct RecentData: Identifiable, Hashable {
+struct NextcloudRecentData: Identifiable, Hashable {
     var id: String
     var image: UIImage
     var title: String
@@ -42,7 +42,7 @@ struct RecentData: Identifiable, Hashable {
     var url: URL
 }
 
-let recentDatasTest: [RecentData] = [
+let recentDatasTest: [NextcloudRecentData] = [
     .init(id: "1", image: UIImage(named: "nextcloud")!, title: "title1", subTitle: "subTitle-description1", url: URL(string: "https://nextcloud.com/")!),
     .init(id: "2", image: UIImage(named: "nextcloud")!, title: "title2", subTitle: "subTitle-description2", url: URL(string: "https://nextcloud.com/")!),
     .init(id: "3", image: UIImage(named: "nextcloud")!, title: "title3", subTitle: "subTitle-description3", url: URL(string: "https://nextcloud.com/")!),
@@ -51,16 +51,16 @@ let recentDatasTest: [RecentData] = [
     .init(id: "6", image: UIImage(named: "nextcloud")!, title: "title6", subTitle: "subTitle-description6", url: URL(string: "https://nextcloud.com/")!)
 ]
 
-func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
+func getNextcloudDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
 
-    let recentDatasPlaceholder = Array(recentDatasTest[0...nextcloudItems - 1])
+    let datasPlaceholder = Array(recentDatasTest[0...nextcloudItems - 1])
     
     if isPreview {
-        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
+        return completion(NextcloudDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
     }
 
     guard let account = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
+        return completion(NextcloudDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
     }
 
     func isLive(file: NKFile, files: [NKFile]) -> Bool {
@@ -170,7 +170,7 @@ func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_
     NextcloudKit.shared.searchBodyRequest(serverUrl: account.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, error in
 
         // Get recent files
-        var recentDatas: [RecentData] = []
+        var datas: [NextcloudRecentData] = []
         for file in files {
             guard !file.directory else { continue }
             guard !isLive(file: file, files: files) else { continue }
@@ -191,18 +191,18 @@ func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_
             } else {
                 imageRecent = UIImage(named: "file")!
             }
-            let recentData = RecentData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
-            recentDatas.append(recentData)
-            if recentDatas.count == nextcloudItems { break}
+            let recentData = NextcloudRecentData.init(id: file.ocId, image: imageRecent, title: file.fileName, subTitle: subTitle, url: url)
+            datas.append(recentData)
+            if datas.count == nextcloudItems { break}
         }
 
         // Completion
         if error != .success {
-            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: error.errorDescription))
-        } else if recentDatas.isEmpty {
-            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
+            completion(NextcloudDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, footerImage: "xmark.icloud", footerText: error.errorDescription))
+        } else if datas.isEmpty {
+            completion(NextcloudDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
         } else {
-            completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatas, isPlaceholder: false, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
+            completion(NextcloudDataEntry(date: Date(), datas: datas, isPlaceholder: false, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget"))
         }
     }
 }

+ 4 - 4
Widget/Nextcloud/NextcloudWidgetProvider.swift

@@ -29,18 +29,18 @@ struct NextcloudWidgetProvider: TimelineProvider {
     typealias Entry = NextcloudDataEntry
 
     func placeholder(in context: Context) -> Entry {
-        let recentDatasPlaceholder = Array(recentDatasTest[0...nextcloudItems - 1])
-        return Entry(date: Date(), recentDatas: recentDatasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
+        let datasPlaceholder = Array(recentDatasTest[0...nextcloudItems - 1])
+        return Entry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " widget")
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
-        getDataEntry(isPreview: false, displaySize: context.displaySize) { entry in
+        getNextcloudDataEntry(isPreview: false, displaySize: context.displaySize) { entry in
             completion(entry)
         }
     }
 
     func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
-        getDataEntry(isPreview: context.isPreview, displaySize: context.displaySize) { entry in
+        getNextcloudDataEntry(isPreview: context.isPreview, displaySize: context.displaySize) { entry in
             let timeLine = Timeline(entries: [entry], policy: .atEnd)
             completion(timeLine)
         }

+ 7 - 7
Widget/Nextcloud/NextcloudWidgetView.swift

@@ -50,12 +50,12 @@ struct NextcloudWidgetView: View {
                     
                     VStack(spacing: 0) {
                         
-                        let recentDatasCount = CGFloat(entry.recentDatas.count)
-                        let heightFrame = (geo.size.height - 120) / recentDatasCount
-                        let addSizeIcon = heightFrame / recentDatasCount
-                        let spacing = (addSizeIcon - recentDatasCount) / 2
+                        let datasCount = CGFloat(entry.datas.count)
+                        let heightFrame = (geo.size.height - 120) / datasCount
+                        let addSizeIcon = heightFrame / datasCount
+                        let spacing = (addSizeIcon - datasCount) / 2
                         
-                        ForEach(entry.recentDatas, id: \.id) { element in
+                        ForEach(entry.datas, id: \.id) { element in
                             
                             Link(destination: element.url) {
                                 
@@ -175,8 +175,8 @@ struct NextcloudWidgetView: View {
 
 struct NextcloudWidget_Previews: PreviewProvider {
     static var previews: some View {
-        let recentDatas = Array(recentDatasTest[0...3])
-        let entry = NextcloudDataEntry(date: Date(), recentDatas: recentDatas, isPlaceholder: false, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
+        let datas = Array(recentDatasTest[0...3])
+        let entry = NextcloudDataEntry(date: Date(), datas: datas, isPlaceholder: false, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
         NextcloudWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
     }
 }