Przeglądaj źródła

Improvements widget

Signed-off-by: marinofaggiana <marino.faggiana@nextcloud.com>
marinofaggiana 2 lat temu
rodzic
commit
00c1bea71c

+ 21 - 0
Widget/DashboardData.swift

@@ -35,4 +35,25 @@ struct DashboardDataEntry: TimelineEntry {
     let date: Date
     let dashboardDatas: [DashboardData]
     let isPlaceholder: Bool
+    let title: String
+}
+
+func getTitle(account: tableAccount?) -> String {
+
+    let hour = Calendar.current.component(.hour, from: Date())
+    var good = ""
+
+    switch hour {
+    case 6..<12: good = NSLocalizedString("_good_morning_", value: "Good morning", comment: "")
+    case 12: good = NSLocalizedString("_good_noon_", value: "Good noon", comment: "")
+    case 13..<17: good = NSLocalizedString("_good_afternoon_", value: "Good afternoon", comment: "")
+    case 17..<22: good = NSLocalizedString("_good_evening_", value: "Good evening", comment: "")
+    default: good = NSLocalizedString("_good_night_", value: "Good night", comment: "")
+    }
+
+    if let account = account {
+        return good + ", " + account.displayName
+    } else {
+        return good
+    }
 }

+ 4 - 4
Widget/DashboardNetworking.swift

@@ -32,11 +32,11 @@ let dashboardDatasTest: [DashboardData] = [
     .init(id: 4, image: "nextcloud", title: "title 5", subTitle: "subTitle - description 5", url: URL(string: "https://nextcloud.com/")!)
 ]
 
-func readDashboard(completion: @escaping (_ dashboardData: [DashboardData], _ isPlaceholder: Bool) -> Void) {
+func readDashboard(completion: @escaping (_ dashboardData: [DashboardData], _ isPlaceholder: Bool, _ title: String) -> Void) {
 
-    guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(dashboardDatasTest, true)
+    guard let account = NCManageDatabase.shared.getActiveAccount() else {
+        return completion(dashboardDatasTest, true, getTitle(account: nil))
     }
 
-    completion(dashboardDatasTest, false)
+    completion(dashboardDatasTest, false, getTitle(account: account))
 }

+ 10 - 5
Widget/DashboardWidgetView.swift

@@ -45,11 +45,12 @@ struct DashboardWidgetView: View {
     var entry: DashboardDataEntry
     let date = Date().formatted()
     var body: some View {
-        VStack(alignment: .center) {
-            Text("\(date)")
-                .font(.title)
+        VStack {
+            Text(entry.title)
+                .font(.title3)
                 .bold()
-            VStack {
+                .fixedSize(horizontal: false, vertical: true)
+            VStack(spacing: 5) {
                 ForEach(entry.dashboardDatas, id: \.id) { element in
                     Link(destination: element.url) {
                         HStack {
@@ -71,6 +72,10 @@ struct DashboardWidgetView: View {
                     }
                 }
             }
+            Text("\(date)")
+                .font(.caption2)
+                .padding(.trailing)
+                .frame(maxWidth: .infinity, alignment: .trailing)
         }.padding(5)
             .redacted(reason: entry.isPlaceholder ? .placeholder : [])
     }
@@ -78,7 +83,7 @@ struct DashboardWidgetView: View {
 
 struct NCElementDashboard_Previews: PreviewProvider {
     static var previews: some View {
-        let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: false)
+        let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: false, title: getTitle(account: nil))
         DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
     }
 }

+ 6 - 6
Widget/Widget.swift

@@ -29,18 +29,18 @@ struct Provider: TimelineProvider {
     typealias Entry = DashboardDataEntry
 
     func placeholder(in context: Context) -> Entry {
-        return Entry(date: Date(), dashboardDatas: [], isPlaceholder: true)
+        return Entry(date: Date(), dashboardDatas: [], isPlaceholder: true, title: getTitle(account: nil))
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
-        readDashboard { dashboardDatas, isPlaceholder in
-            completion(Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder))
+        readDashboard { dashboardDatas, isPlaceholder, title in
+            completion(Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder, title: title))
         }
     }
 
     func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
-        readDashboard { dashboardDatas, isPlaceholder in
-            let timeLine = Timeline(entries: [Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder)], policy: .atEnd)
+        readDashboard { dashboardDatas, isPlaceholder, title in
+            let timeLine = Timeline(entries: [Entry(date: Date(), dashboardDatas: dashboardDatas, isPlaceholder: isPlaceholder, title: title)], policy: .atEnd)
             completion(timeLine)
         }
     }
@@ -63,7 +63,7 @@ struct DashboardWidget: Widget {
 struct DashboardWidget_Previews: PreviewProvider {
 
     static var previews: some View {
-        let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: false)
+        let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest, isPlaceholder: false, title: getTitle(account: nil))
         DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
     }
 }