DashboardData.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // DashboardData.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 20/08/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import WidgetKit
  24. import NextcloudKit
  25. import Queuer
  26. import RealmSwift
  27. let dashboaardItems = 5
  28. struct DashboardDataEntry: TimelineEntry {
  29. let date: Date
  30. let datas: [DashboardData]
  31. let tableDashboard: tableDashboardWidget?
  32. let tableButton: Results<tableDashboardWidgetButton>?
  33. let isPlaceholder: Bool
  34. let titleImage: UIImage
  35. let title: String
  36. let footerImage: String
  37. let footerText: String
  38. }
  39. struct DashboardData: Identifiable, Hashable {
  40. let id: Int
  41. let title: String
  42. let subTitle: String
  43. let link: URL
  44. let icon: UIImage
  45. }
  46. struct DashboardDataButton: Hashable {
  47. let type: String
  48. let Text: String
  49. let link: String
  50. }
  51. let dashboardDatasTest: [DashboardData] = [
  52. .init(id: 0, title: "title0", subTitle: "subTitle-description0", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  53. .init(id: 1, title: "title1", subTitle: "subTitle-description1", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  54. .init(id: 2, title: "title2", subTitle: "subTitle-description2", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  55. .init(id: 3, title: "title3", subTitle: "subTitle-description3", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  56. .init(id: 4, title: "title4", subTitle: "subTitle-description4", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  57. .init(id: 5, title: "title5", subTitle: "subTitle-description5", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  58. .init(id: 6, title: "title6", subTitle: "subTitle-description6", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  59. .init(id: 7, title: "title7", subTitle: "subTitle-description7", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  60. .init(id: 8, title: "title8", subTitle: "subTitle-description8", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!),
  61. .init(id: 9, title: "title9", subTitle: "subTitle-description9", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!)
  62. ]
  63. func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
  64. let datasPlaceholder = Array(dashboardDatasTest[0...dashboaardItems - 1])
  65. var id = "recommendations"
  66. switch intent {
  67. case .unknown:
  68. id = "recommendations"
  69. case .notes:
  70. id = "notes"
  71. case .deck:
  72. id = "deck"
  73. case .recommendations:
  74. id = "recommendations"
  75. case .activity:
  76. id = "activity"
  77. case .user_status:
  78. id = "user_status"
  79. }
  80. if isPreview {
  81. 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"))
  82. }
  83. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  84. 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_", value: "No account found", comment: "")))
  85. }
  86. // NETWORKING
  87. let password = CCUtility.getPassword(account.account)!
  88. NKCommon.shared.setup(
  89. account: account.account,
  90. user: account.user,
  91. userId: account.userId,
  92. password: password,
  93. urlBase: account.urlBase,
  94. userAgent: CCUtility.getUserAgent(),
  95. nextcloudVersion: 0,
  96. delegate: NCNetworking.shared)
  97. // LOG
  98. let levelLog = CCUtility.getLogLevel()
  99. let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
  100. let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
  101. NKCommon.shared.levelLog = levelLog
  102. if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
  103. NKCommon.shared.pathLog = pathDirectoryGroup
  104. }
  105. if isSimulatorOrTestFlight {
  106. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
  107. } else {
  108. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS)
  109. }
  110. let (tableDashboard, tableButton) = NCManageDatabase.shared.getDashboardWidget(account: account.account, id: id)
  111. let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue)
  112. let title = tableDashboard?.title ?? id
  113. var titleImage = UIImage(named: "widget")!
  114. if let fileName = tableDashboard?.iconClass {
  115. let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
  116. if let image = UIImage(contentsOfFile: fileNamePath) {
  117. titleImage = image.imageColor(NCBrandColor.shared.label)
  118. }
  119. }
  120. NextcloudKit.shared.getDashboardWidgetsApplication(id, options: options) { account, results, data, error in
  121. var datas = [DashboardData]()
  122. if let results = results {
  123. for result in results {
  124. if let items = result.items {
  125. var counter: Int = 0
  126. for item in items {
  127. counter += 1
  128. let title = item.title ?? ""
  129. let subtitle = item.subtitle ?? ""
  130. var link = URL(string: "https://")!
  131. if let entryLink = item.link, let url = URL(string: entryLink){ link = url }
  132. var icon = UIImage(named: "file")!
  133. var iconFileName: String?
  134. if let iconUrl = item.iconUrl, let url = URL(string: iconUrl) {
  135. if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
  136. let queryItems = urlComponents.queryItems
  137. if let item = CCUtility.value(forKey: "fileId", fromQueryItems: queryItems) {
  138. iconFileName = item
  139. } else {
  140. let path = (urlComponents.path as NSString)
  141. iconFileName = ((path.lastPathComponent) as NSString).deletingPathExtension
  142. }
  143. }
  144. let semaphore = Semaphore()
  145. NCUtility.shared.getImageUserData(url: url, fileName: iconFileName , size: 128) { image in
  146. if let image = image {
  147. icon = image
  148. }
  149. semaphore.continue()
  150. }
  151. semaphore.wait()
  152. }
  153. let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon)
  154. datas.append(data)
  155. if datas.count == dashboaardItems { break}
  156. }
  157. }
  158. }
  159. }
  160. if error != .success {
  161. completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
  162. } else if datas.isEmpty {
  163. 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: "")))
  164. } else {
  165. completion(DashboardDataEntry(date: Date(), datas: datas, tableDashboard: tableDashboard, tableButton: tableButton, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
  166. }
  167. }
  168. }