Browse Source

add Lockscreen widget

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

+ 2 - 0
.swiftlint.yml

@@ -45,6 +45,8 @@ excluded:
   - Widget/Dashboard/DashboardWidgetView.swift
   - Widget/Files/FilesData.swift
   - Widget/Files/FilesWidgetView.swift
+  - Widget/Lockscreen/LockscreenData.swift
+  - Widget/Lockscreen/LockscreenWidgetView.swift
   - iOSClient/Activity/NCActivity.swift
   - iOSClient/Activity/NCActivityTableViewCell.swift
   - iOSClient/AppDelegate.swift

+ 3 - 3
Nextcloud.xcodeproj/project.pbxproj

@@ -1490,7 +1490,7 @@
 				F7346E2228B0FEBA006CE2D2 /* Assets.xcassets */,
 				F72EA95528B7BAD100C88F0C /* Dashboard */,
 				F72EA95628B7BAE700C88F0C /* Files */,
-				F76DEE9A28F808BC0041B1C9 /* LockScreen */,
+				F76DEE9A28F808BC0041B1C9 /* Lockscreen */,
 				F77ED59628C9CEEE00E24ED0 /* Toolbar */,
 				F7346E2028B0FA3A006CE2D2 /* Widget-Brinding-header.h */,
 				F7346E1528B0EF5C006CE2D2 /* Widget.swift */,
@@ -1584,14 +1584,14 @@
 			path = NCViewerPDF;
 			sourceTree = "<group>";
 		};
-		F76DEE9A28F808BC0041B1C9 /* LockScreen */ = {
+		F76DEE9A28F808BC0041B1C9 /* Lockscreen */ = {
 			isa = PBXGroup;
 			children = (
 				F76DEE9428F808AF0041B1C9 /* LockscreenData.swift */,
 				F76DEE9528F808AF0041B1C9 /* LockscreenWidgetProvider.swift */,
 				F76DEE9628F808AF0041B1C9 /* LockscreenWidgetView.swift */,
 			);
-			path = LockScreen;
+			path = Lockscreen;
 			sourceTree = "<group>";
 		};
 		F771E3D120E2392D00AFB62D /* File Provider Extension */ = {

+ 12 - 0
Widget/Assets.xcassets/activity.imageset/Contents.json

@@ -0,0 +1,12 @@
+{
+  "images" : [
+    {
+      "filename" : "activity.png",
+      "idiom" : "universal"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

BIN
Widget/Assets.xcassets/activity.imageset/activity.png


+ 0 - 1
Widget/Dashboard/DashboardData.swift

@@ -96,7 +96,6 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
     let id: String = intent?.identifier ?? (result?.id ?? "recommendations")
 
     let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
-
     guard serverVersionMajor >= NCGlobal.shared.nextcloudVersion25 else {
         return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_widget_available_nc25_", comment: "")))
     }

+ 0 - 64
Widget/LockScreen/LockscreenData.swift

@@ -1,64 +0,0 @@
-//
-//  LockscreenData.swift
-//  Widget
-//
-//  Created by Marino Faggiana on 13/10/22.
-//  Copyright © 2022 Marino Faggiana. All rights reserved.
-//
-//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
-//
-//  This program is free software: you can redistribute it and/or modify
-//  it under the terms of the GNU General Public License as published by
-//  the Free Software Foundation, either version 3 of the License, or
-//  (at your option) any later version.
-//
-//  This program is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//  GNU General Public License for more details.
-//
-//  You should have received a copy of the GNU General Public License
-//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-//
-
-import WidgetKit
-
-struct LockscreenData: TimelineEntry {
-    let date: Date
-    let isPlaceholder: Bool
-    let displayName: String
-    let quotaRelative: Float
-    let quotaUsed: String
-    let quotaTotal: String
-}
-
-func getLockscreenDataEntry(isPreview: Bool, completion: @escaping (_ entry: LockscreenData) -> Void) {
-
-    if isPreview {
-        return completion(LockscreenData(date: Date(), isPlaceholder: true, displayName: "", quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
-    }
-
-    guard let account = NCManageDatabase.shared.getActiveAccount() else {
-        return completion(LockscreenData(date: Date(), isPlaceholder: true, displayName: "", quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
-    }
-
-    var quotaRelative: Float = 0
-    if account.quotaRelative > 0 {
-        quotaRelative = Float(account.quotaRelative) / 100
-    }
-    let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
-    var quotaTotal: String = ""
-
-    switch account.quotaTotal {
-    case -1:
-        quotaTotal = ""
-    case -2:
-        quotaTotal = ""
-    case -3:
-        quotaTotal = ""
-    default:
-        quotaTotal = CCUtility.transformedSize(account.quotaTotal)
-    }
-
-    completion(LockscreenData(date: Date(), isPlaceholder: false, displayName: account.displayName, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
-}

+ 95 - 0
Widget/Lockscreen/LockscreenData.swift

@@ -0,0 +1,95 @@
+//
+//  LockscreenData.swift
+//  Widget
+//
+//  Created by Marino Faggiana on 13/10/22.
+//  Copyright © 2022 Marino Faggiana. All rights reserved.
+//
+//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+import WidgetKit
+import NextcloudKit
+
+struct LockscreenData: TimelineEntry {
+    let date: Date
+    let isPlaceholder: Bool
+    let activity: String
+    let link: URL
+    let quotaRelative: Float
+    let quotaUsed: String
+    let quotaTotal: String
+}
+
+func getLockscreenDataEntry(isPreview: Bool, completion: @escaping (_ entry: LockscreenData) -> Void) {
+
+    if isPreview {
+        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
+    }
+
+    guard let account = NCManageDatabase.shared.getActiveAccount() else {
+        return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
+    }
+
+    var quotaRelative: Float = 0
+    if account.quotaRelative > 0 {
+        quotaRelative = Float(account.quotaRelative) / 100
+    }
+    let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
+    var quotaTotal: String = ""
+
+    switch account.quotaTotal {
+    case -1:
+        quotaTotal = ""
+    case -2:
+        quotaTotal = ""
+    case -3:
+        quotaTotal = ""
+    default:
+        quotaTotal = CCUtility.transformedSize(account.quotaTotal)
+    }
+
+    let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
+    if serverVersionMajor >= NCGlobal.shared.nextcloudVersion25 {
+
+        // NETWORKING
+        let password = CCUtility.getPassword(account.account)!
+        NKCommon.shared.setup(
+            account: account.account,
+            user: account.user,
+            userId: account.userId,
+            password: password,
+            urlBase: account.urlBase,
+            userAgent: CCUtility.getUserAgent(),
+            nextcloudVersion: 0,
+            delegate: NCNetworking.shared)
+
+        let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
+        NextcloudKit.shared.getDashboardWidgetsApplication("activity", options: options) { _, results, _, error in
+            var activity: String = NSLocalizedString("_no_data_available_", comment: "")
+            var link = URL(string: "https://")!
+            if error == .success, let result = results?.first {
+                if let item = result.items?.first {
+                    if let title = item.title {  activity = title }
+                    if let itemLink = item.link, let url = URL(string: itemLink) { link = url }
+                }
+            }
+            completion(LockscreenData(date: Date(), isPlaceholder: false, activity: activity, link: link, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
+        }
+    } else {
+        completion(LockscreenData(date: Date(), isPlaceholder: false, activity: NSLocalizedString("_widget_available_nc25_", comment: ""), link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
+    }
+}

+ 1 - 1
Widget/LockScreen/LockscreenWidgetProvider.swift → Widget/Lockscreen/LockscreenWidgetProvider.swift

@@ -29,7 +29,7 @@ struct LockscreenWidgetProvider: TimelineProvider {
     typealias Entry = LockscreenData
 
     func placeholder(in context: Context) -> Entry {
-        return Entry(date: Date(), isPlaceholder: true, displayName: "", quotaRelative: 0, quotaUsed: "", quotaTotal: "")
+        return Entry(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "")
     }
 
     func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {

+ 25 - 6
Widget/LockScreen/LockscreenWidgetView.swift → Widget/Lockscreen/LockscreenWidgetView.swift

@@ -27,17 +27,35 @@ import WidgetKit
 @available(iOSApplicationExtension 16.0, *)
 struct LockscreenWidgetView: View {
 
-    var entry: LockscreenData
+    let entry: LockscreenData
+    @Environment(\.widgetFamily) private var family
 
     var body: some View {
-        HStack {
-            Text(entry.displayName)
+        switch family {
+        case .accessoryCircular:
             Gauge(
                 value: entry.quotaRelative,
-                label: { Text(entry.quotaTotal) },
+                label: { Text("  " + entry.quotaTotal + "  ") },
                 currentValueLabel: { Text(entry.quotaUsed) }
             )
             .gaugeStyle(.accessoryCircular)
+        case .accessoryRectangular:
+            VStack(alignment: .leading, spacing: 3) {
+                HStack(spacing: 3) {
+                    Image("activity")
+                        .renderingMode(.template)
+                        .resizable()
+                        .scaledToFill()
+                        .frame(width: 12, height: 12)
+                    Text(NSLocalizedString("_recent_activity_", comment: ""))
+                        .font(.system(size: 11)).bold()
+                }
+                Text(entry.activity)
+                    .font(.system(size: 9))
+                    .fontWeight(.light)
+            }.widgetURL(entry.link)
+        default:
+            Text("Not implemented")
         }
     }
 }
@@ -45,7 +63,8 @@ struct LockscreenWidgetView: View {
 @available(iOSApplicationExtension 16.0, *)
 struct LockscreenWidgetView_Previews: PreviewProvider {
     static var previews: some View {
-        let entry = LockscreenData(date: Date(), isPlaceholder: true, displayName: "Marino Faggiana", quotaRelative: 0.5, quotaUsed: "22 GB", quotaTotal: "50 GB")
-        LockscreenWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .accessoryRectangular))
+        let entry = LockscreenData(date: Date(), isPlaceholder: true, activity: "Alba Mayoral changed Marketing/Regional Marketing/Agenda Meetings/Q4 2022/OCTOBER/13.11 Afrah Kahlid.md", link: URL(string: "https://")!, quotaRelative: 0.5, quotaUsed: "22 GB", quotaTotal: "50 GB")
+        LockscreenWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .accessoryRectangular)).previewDisplayName("Rectangular")
+        LockscreenWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .accessoryCircular)).previewDisplayName("Circular")
     }
 }

+ 3 - 3
Widget/Widget.swift

@@ -80,10 +80,10 @@ struct LockscreenWidget: Widget {
 
     var body: some WidgetConfiguration {
         if #available(iOSApplicationExtension 16.0, *) {
-            return StaticConfiguration(kind: kind, provider: ToolbarWidgetProvider()) { entry in
-                ToolbarWidgetView(entry: entry)
+            return StaticConfiguration(kind: kind, provider: LockscreenWidgetProvider()) { entry in
+                LockscreenWidgetView(entry: entry)
             }
-            .supportedFamilies([.accessoryRectangular])
+            .supportedFamilies([.accessoryRectangular, .accessoryCircular])
             .configurationDisplayName("Lockscreen")
             .description(NSLocalizedString("_description_lockscreenwidget_", comment: ""))
         } else {

+ 1 - 0
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -891,6 +891,7 @@
 "_no_data_available_"       = "No data available";
 "_widget_available_nc25_"   = "Widget only available starting with Nextcloud 25";
 "_keep_running_"            = "Keep the app running for a better user experience";
+"_recent_activity_"         = "Recent activity";
 
 // Video
 "_select_trace_"            = "Select the trace";