|
@@ -24,12 +24,13 @@
|
|
|
import WidgetKit
|
|
|
import NextcloudKit
|
|
|
import RealmSwift
|
|
|
+import SVGKit
|
|
|
|
|
|
struct DashboardDataEntry: TimelineEntry {
|
|
|
let date: Date
|
|
|
let datas: [DashboardData]
|
|
|
- let tableDashboard: tableDashboardWidget?
|
|
|
- let tableButton: [tableDashboardWidgetButton]?
|
|
|
+ let dashboard: tableDashboardWidget?
|
|
|
+ let buttons: [tableDashboardWidgetButton]?
|
|
|
let isPlaceholder: Bool
|
|
|
let titleImage: UIImage
|
|
|
let title: String
|
|
@@ -83,20 +84,20 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
|
|
|
let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems - 1])
|
|
|
|
|
|
if isPreview {
|
|
|
- return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
|
|
|
+ return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
|
|
|
}
|
|
|
|
|
|
guard let account = NCManageDatabase.shared.getActiveAccount() else {
|
|
|
- return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", comment: "")))
|
|
|
+ return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", comment: "")))
|
|
|
}
|
|
|
|
|
|
- // Default widget: recommendations
|
|
|
- let id: String = intent?.identifier ?? "recommendations"
|
|
|
+ // Default widget
|
|
|
+ let result = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account).first
|
|
|
+ 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, tableDashboard: nil, tableButton: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_widget_available_nc25_", comment: "")))
|
|
|
+ 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: "")))
|
|
|
}
|
|
|
|
|
|
// NETWORKING
|
|
@@ -130,80 +131,98 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
|
|
|
let existsButton = (tableButton?.isEmpty ?? true) ? false : true
|
|
|
let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
|
|
|
let title = tableDashboard?.title ?? id
|
|
|
- var titleImage = UIImage(named: "widget")!
|
|
|
|
|
|
+ var imagetmp = UIImage(named: "widget")!
|
|
|
if let fileName = tableDashboard?.iconClass {
|
|
|
let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
|
|
|
if let image = UIImage(contentsOfFile: fileNamePath) {
|
|
|
- titleImage = image.withTintColor(.label, renderingMode: .alwaysOriginal)
|
|
|
+ imagetmp = image.withTintColor(.label, renderingMode: .alwaysOriginal)
|
|
|
}
|
|
|
}
|
|
|
+ let titleImage = imagetmp
|
|
|
|
|
|
NextcloudKit.shared.getDashboardWidgetsApplication(id, options: options) { account, results, data, error in
|
|
|
-
|
|
|
- var datas = [DashboardData]()
|
|
|
-
|
|
|
- if let results = results {
|
|
|
- for result in results {
|
|
|
- if let items = result.items {
|
|
|
- var counter: Int = 0
|
|
|
- let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: existsButton)
|
|
|
- for item in items {
|
|
|
- counter += 1
|
|
|
- let title = item.title ?? ""
|
|
|
- let subtitle = item.subtitle ?? ""
|
|
|
- var link = URL(string: "https://")!
|
|
|
- if let entryLink = item.link, let url = URL(string: entryLink){ link = url }
|
|
|
- var icon = UIImage(named: "file")!
|
|
|
- var iconFileName: String?
|
|
|
- var template: Bool = false
|
|
|
- var avatar: Bool = false
|
|
|
-
|
|
|
- if let iconUrl = item.iconUrl, let url = URL(string: iconUrl) {
|
|
|
- if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
|
|
|
-
|
|
|
- let path = (urlComponents.path as NSString)
|
|
|
- let pathComponents = path.components(separatedBy: "/")
|
|
|
- let queryItems = urlComponents.queryItems
|
|
|
-
|
|
|
- if (pathComponents.last as? NSString)?.pathExtension.lowercased() == "svg" {
|
|
|
- template = true
|
|
|
- }
|
|
|
|
|
|
- if let item = CCUtility.value(forKey: "fileId", fromQueryItems: queryItems) {
|
|
|
- iconFileName = item
|
|
|
- } else if pathComponents.contains("avatar") {
|
|
|
- iconFileName = pathComponents[pathComponents.count-2]
|
|
|
- avatar = true
|
|
|
- } else {
|
|
|
- iconFileName = ((path.lastPathComponent) as NSString).deletingPathExtension
|
|
|
+ Task {
|
|
|
+ var datas = [DashboardData]()
|
|
|
+ var numberItems = 0
|
|
|
+
|
|
|
+ if let results = results {
|
|
|
+ for result in results {
|
|
|
+ if let items = result.items {
|
|
|
+ numberItems = result.items?.count ?? 0
|
|
|
+ var counter: Int = 0
|
|
|
+ let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: existsButton)
|
|
|
+ for item in items {
|
|
|
+ counter += 1
|
|
|
+ let title = item.title ?? ""
|
|
|
+ let subtitle = item.subtitle ?? ""
|
|
|
+ var link = URL(string: "https://")!
|
|
|
+ if let entryLink = item.link, let url = URL(string: entryLink) { link = url }
|
|
|
+ var icon = UIImage(named: "file")!
|
|
|
+ var iconFileName: String?
|
|
|
+ var template: Bool = false
|
|
|
+ var avatar: Bool = false
|
|
|
+
|
|
|
+ if let iconUrl = item.iconUrl, let url = URL(string: iconUrl) {
|
|
|
+ if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
|
|
|
+
|
|
|
+ let path = (urlComponents.path as NSString)
|
|
|
+ let pathComponents = path.components(separatedBy: "/")
|
|
|
+ let queryItems = urlComponents.queryItems
|
|
|
+
|
|
|
+ if (pathComponents.last as? NSString)?.pathExtension.lowercased() == "svg" {
|
|
|
+ template = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if let item = CCUtility.value(forKey: "fileId", fromQueryItems: queryItems) {
|
|
|
+ iconFileName = item
|
|
|
+ } else if pathComponents.contains("avatar") {
|
|
|
+ iconFileName = pathComponents[pathComponents.count-2]
|
|
|
+ avatar = true
|
|
|
+ } else {
|
|
|
+ iconFileName = ((path.lastPathComponent) as NSString).deletingPathExtension
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- let semaphore = DispatchSemaphore(value: 0)
|
|
|
- NCUtility.shared.getWidgetImageUserData(url: url, fileName: iconFileName) { image in
|
|
|
- if let image = image {
|
|
|
- icon = image
|
|
|
+
|
|
|
+ do {
|
|
|
+ if let fileName = iconFileName {
|
|
|
+ let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
|
|
|
+ if FileManager().fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
|
|
|
+ icon = image
|
|
|
+ } else {
|
|
|
+ let (_, data) = try await NextcloudKit.shared.getPreview(url: url)
|
|
|
+ if let image = NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName) {
|
|
|
+ icon = image
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ print(error)
|
|
|
}
|
|
|
- semaphore.signal()
|
|
|
}
|
|
|
- semaphore.wait()
|
|
|
+
|
|
|
+ let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon, template: template, avatar: avatar)
|
|
|
+ datas.append(data)
|
|
|
+
|
|
|
+ if datas.count == dashboardItems { break }
|
|
|
}
|
|
|
-
|
|
|
- let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon, template: template, avatar: avatar)
|
|
|
- datas.append(data)
|
|
|
-
|
|
|
- if datas.count == dashboardItems { break }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if error != .success {
|
|
|
- completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
|
|
|
- } else if datas.isEmpty {
|
|
|
- completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NSLocalizedString("_no_data_available_", comment: "")))
|
|
|
- } else {
|
|
|
- completion(DashboardDataEntry(date: Date(), datas: datas, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
|
|
|
+
|
|
|
+ var buttons = tableButton
|
|
|
+ if numberItems == datas.count, let tableButton = tableButton, tableButton.contains(where: { $0.type == "more"}) {
|
|
|
+ buttons = tableButton.filter(({ $0.type != "more" }))
|
|
|
+ }
|
|
|
+
|
|
|
+ if error != .success {
|
|
|
+ completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: tableDashboard, buttons: buttons, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
|
|
|
+ } else if datas.isEmpty {
|
|
|
+ completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, dashboard: tableDashboard, buttons: buttons, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NSLocalizedString("_no_data_available_", comment: "")))
|
|
|
+ } else {
|
|
|
+ completion(DashboardDataEntry(date: Date(), datas: datas, dashboard: tableDashboard, buttons: buttons, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|