DashboardData.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. let dashboaardItems = 6
  27. struct DashboardDataEntry: TimelineEntry {
  28. let date: Date
  29. let datas: [DashboardData]
  30. let isPlaceholder: Bool
  31. let titleImage: UIImage
  32. let title: String
  33. let footerImage: String
  34. let footerText: String
  35. }
  36. struct DashboardData: Identifiable, Hashable {
  37. let id: Int
  38. let title: String
  39. let subTitle: String
  40. let link: URL
  41. let icon: UIImage
  42. let buttons: [DashboardDataButton]?
  43. }
  44. struct DashboardDataButton: Hashable {
  45. let type: String
  46. let Text: String
  47. let link: String
  48. }
  49. let dashboardDatasTest: [DashboardData] = [
  50. .init(id: 0, title: "title0", subTitle: "subTitle-description0", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  51. .init(id: 1, title: "title1", subTitle: "subTitle-description1", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  52. .init(id: 2, title: "title2", subTitle: "subTitle-description2", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  53. .init(id: 3, title: "title3", subTitle: "subTitle-description3", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  54. .init(id: 4, title: "title4", subTitle: "subTitle-description4", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  55. .init(id: 5, title: "title5", subTitle: "subTitle-description5", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  56. .init(id: 6, title: "title6", subTitle: "subTitle-description6", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  57. .init(id: 7, title: "title7", subTitle: "subTitle-description7", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  58. .init(id: 8, title: "title8", subTitle: "subTitle-description8", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil),
  59. .init(id: 9, title: "title9", subTitle: "subTitle-description9", link: URL(string: "https://nextcloud.com/")!, icon: UIImage(named: "widget")!, buttons: nil)
  60. ]
  61. func getDashboardDataEntry(intent: Applications, isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: DashboardDataEntry) -> Void) {
  62. let datasPlaceholder = Array(dashboardDatasTest[0...dashboaardItems - 1])
  63. var id = "recommendations"
  64. switch intent {
  65. case .unknown:
  66. id = "recommendations"
  67. case .notes:
  68. id = "notes"
  69. case .deck:
  70. id = "deck"
  71. case .recommendations:
  72. id = "recommendations"
  73. case .activity:
  74. id = "activity"
  75. case .user_status:
  76. id = "user_status"
  77. }
  78. if isPreview {
  79. return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
  80. }
  81. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  82. return completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, titleImage: UIImage(named: "widget")!, title: "Dashboard", footerImage: "xmark.icloud", footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
  83. }
  84. // NETWORKING
  85. let password = CCUtility.getPassword(account.account)!
  86. NKCommon.shared.setup(
  87. account: account.account,
  88. user: account.user,
  89. userId: account.userId,
  90. password: password,
  91. urlBase: account.urlBase,
  92. userAgent: CCUtility.getUserAgent(),
  93. nextcloudVersion: 0,
  94. delegate: NCNetworking.shared)
  95. // LOG
  96. let levelLog = CCUtility.getLogLevel()
  97. let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
  98. let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
  99. NKCommon.shared.levelLog = levelLog
  100. if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
  101. NKCommon.shared.pathLog = pathDirectoryGroup
  102. }
  103. if isSimulatorOrTestFlight {
  104. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
  105. } else {
  106. NKCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) dashboard widget session with level \(levelLog) " + versionNextcloudiOS)
  107. }
  108. let result = NCManageDatabase.shared.getDashboardWidget(account: account.account, id: id)
  109. let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue)
  110. let title = result?.title ?? id
  111. var titleImage = UIImage(named: "widget")!
  112. if let fileName = result?.iconClass {
  113. let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
  114. if let image = UIImage(contentsOfFile: fileNamePath) {
  115. titleImage = image.imageColor(NCBrandColor.shared.label)
  116. }
  117. }
  118. NextcloudKit.shared.getDashboardWidgetsApplication(id, options: options) { account, results, data, error in
  119. var datas = [DashboardData]()
  120. if let results = results {
  121. for result in results {
  122. if let items = result.items {
  123. var counter: Int = 0
  124. for item in items {
  125. counter += 1
  126. let title = item.title ?? ""
  127. let subtitle = item.subtitle ?? ""
  128. var link = URL(string: "https://")!
  129. if let entryLink = item.link, let url = URL(string: entryLink){ link = url }
  130. var icon = UIImage(named: "file")!
  131. var iconFileName: String?
  132. if let iconUrl = item.iconUrl, let url = URL(string: iconUrl) {
  133. if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
  134. let queryItems = urlComponents.queryItems
  135. if let item = CCUtility.value(forKey: "fileId", fromQueryItems: queryItems) {
  136. iconFileName = item
  137. } else {
  138. let path = (urlComponents.path as NSString)
  139. iconFileName = ((path.lastPathComponent) as NSString).deletingPathExtension
  140. }
  141. }
  142. let semaphore = Semaphore()
  143. NCUtility.shared.getImageUserData(url: url, fileName: iconFileName , size: 128) { image in
  144. if let image = image {
  145. icon = image
  146. }
  147. semaphore.continue()
  148. }
  149. semaphore.wait()
  150. }
  151. let data = DashboardData(id: counter, title: title, subTitle: subtitle, link: link, icon: icon, buttons: nil)
  152. datas.append(data)
  153. if datas.count == dashboaardItems { break}
  154. }
  155. }
  156. }
  157. }
  158. if error != .success {
  159. completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "xmark.icloud", footerText: error.errorDescription))
  160. } else if datas.isEmpty {
  161. completion(DashboardDataEntry(date: Date(), datas: datasPlaceholder, isPlaceholder: true, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NSLocalizedString("_no_data_available_", comment: "")))
  162. } else {
  163. completion(DashboardDataEntry(date: Date(), datas: datas, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " dashboard"))
  164. }
  165. }
  166. }