LockscreenData.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // LockscreenData.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 13/10/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. struct LockscreenData: TimelineEntry {
  26. let date: Date
  27. let isPlaceholder: Bool
  28. let activity: String
  29. let link: URL
  30. let quotaRelative: Float
  31. let quotaUsed: String
  32. let quotaTotal: String
  33. let error: Bool
  34. }
  35. func getLockscreenDataEntry(configuration: AccountIntent?, isPreview: Bool, family: WidgetFamily, completion: @escaping (_ entry: LockscreenData) -> Void) {
  36. var account: tableAccount?
  37. var quotaRelative: Float = 0
  38. if isPreview {
  39. return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
  40. }
  41. let accountIdentifier: String = configuration?.accounts?.identifier ?? "active"
  42. if accountIdentifier == "active" {
  43. account = NCManageDatabase.shared.getActiveAccount()
  44. } else {
  45. account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", accountIdentifier))
  46. }
  47. guard let account = account else {
  48. return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
  49. }
  50. // Capabilities
  51. NCManageDatabase.shared.setCapabilities(account: account.account)
  52. if NCGlobal.shared.capabilityServerVersionMajor < NCGlobal.shared.nextcloudVersion25 {
  53. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: NSLocalizedString("_widget_available_nc25_", comment: ""), link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: true))
  54. }
  55. // NETWORKING
  56. let password = CCUtility.getPassword(account.account)!
  57. NextcloudKit.shared.setup(
  58. account: account.account,
  59. user: account.user,
  60. userId: account.userId,
  61. password: password,
  62. urlBase: account.urlBase,
  63. userAgent: userAgent,
  64. nextcloudVersion: 0,
  65. delegate: NCNetworking.shared)
  66. let options = NKRequestOptions(timeout: 90, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  67. if #available(iOSApplicationExtension 16.0, *) {
  68. if family == .accessoryCircular {
  69. NextcloudKit.shared.getUserProfile(options: options) { _, userProfile, _, error in
  70. if error == .success, let userProfile = userProfile {
  71. if userProfile.quotaRelative > 0 {
  72. quotaRelative = Float(userProfile.quotaRelative) / 100
  73. }
  74. let quotaUsed: String = CCUtility.transformedSize(userProfile.quotaUsed)
  75. var quotaTotal: String = ""
  76. switch userProfile.quotaTotal {
  77. case -1:
  78. quotaTotal = ""
  79. case -2:
  80. quotaTotal = ""
  81. case -3:
  82. quotaTotal = ""
  83. default:
  84. quotaTotal = CCUtility.transformedSize(userProfile.quotaTotal)
  85. }
  86. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal, error: false))
  87. } else {
  88. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: true))
  89. }
  90. }
  91. } else if family == .accessoryRectangular {
  92. NextcloudKit.shared.getDashboardWidgetsApplication("activity", options: options) { _, results, _, error in
  93. var activity: String = NSLocalizedString("_no_data_available_", comment: "")
  94. var link = URL(string: "https://")!
  95. if error == .success, let result = results?.first {
  96. if let item = result.items?.first {
  97. if let title = item.title { activity = title }
  98. if let itemLink = item.link, let url = URL(string: itemLink) { link = url }
  99. }
  100. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: activity, link: link, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: false))
  101. } else {
  102. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: "", error: true))
  103. }
  104. }
  105. }
  106. }
  107. }