1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
//
// DashboardData.swift
// Widget
//
// Created by Marino Faggiana on 20/08/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
import RealmSwift
import SVGKit
struct DashboardDataEntry: TimelineEntry {
let date: Date
let datas: [DashboardData]
let dashboard: tableDashboardWidget?
let buttons: [tableDashboardWidgetButton]?
let isPlaceholder: Bool
let titleImage: UIImage
let title: String
let footerImage: String
let footerText: String
}
struct DashboardData: Identifiable, Hashable {
let id: Int
let title: String
let subTitle: String
let link: URL
let icon: UIImage
let template: Bool
let avatar: Bool
}
struct DashboardDataButton: Hashable {
let type: String
let Text: String
let link: String
}
let dashboardDatasTest: [DashboardData] = [
.init(id: 0, title: "title0", subTitle: "subTitle-description0", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 1, title: "title1", subTitle: "subTitle-description1", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 2, title: "title2", subTitle: "subTitle-description2", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 3, title: "title3", subTitle: "subTitle-description3", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 4, title: "title4", subTitle: "subTitle-description4", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 5, title: "title5", subTitle: "subTitle-description5", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 6, title: "title6", subTitle: "subTitle-description6", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 7, title: "title7", subTitle: "subTitle-description7", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 8, title: "title8", subTitle: "subTitle-description8", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false),
.init(id: 9, title: "title9", subTitle: "subTitle-description9", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, template: true, avatar: false)
]
func getDashboardItems(displaySize: CGSize, withButton: Bool) -> Int {
if withButton {
let height = Int((displaySize.height - 85) / 50)
return height
} else {
let height = Int((displaySize.height - 60) / 50)
return height
}
}
func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
let dashboardItems = getDashboardItems(displaySize: displaySize, withButton: false)
let datasPlaceholder = Array(dashboardDatasTest[0...dashboardItems - 1])
if isPreview {
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, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", comment: "")))
}
// 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, dashboard: nil, buttons: nil, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_widget_available_nc25_", comment: "")))
}
// 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)
// LOG
let levelLog = CCUtility.getLogLevel()
let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
NKCommon.shared.levelLog = levelLog
if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
NKCommon.shared.pathLog = pathDirectoryGroup
}
if isSimulatorOrTestFlight {
NKCommon.shared.writeLog("[INFO] Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
} else {
NKCommon.shared.writeLog("[INFO] Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS)
}
let (tableDashboard, tableButton) = NCManageDatabase.shared.getDashboardWidget(account: account.account, id: id)
let existsButton = (tableButton?.isEmpty ?? true) ? false : true
let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
let title = tableDashboard?.title ?? id
var imagetmp = UIImage(named: "widget")!
if let fileName = tableDashboard?.iconClass {
let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
if let image = UIImage(contentsOfFile: fileNamePath) {
imagetmp = image.withTintColor(.label, renderingMode: .alwaysOriginal)
}
}
let titleImage = imagetmp
NextcloudKit.shared.getDashboardWidgetsApplication(id, options: options) { account, results, data, error in
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
}
}
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, _) = await NCNetworking.shared.getPreview(url: url)
if let image = NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName) {
icon = image
}
}
}
}
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 }
}
}
}
}
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"))
}
}
}
}
|