DashboardData.swift 8.5 KB

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