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