LockscreenData.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  34. func getLockscreenDataEntry(isPreview: Bool, completion: @escaping (_ entry: LockscreenData) -> Void) {
  35. if isPreview {
  36. return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
  37. }
  38. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  39. return completion(LockscreenData(date: Date(), isPlaceholder: true, activity: "", link: URL(string: "https://")!, quotaRelative: 0, quotaUsed: "", quotaTotal: ""))
  40. }
  41. var quotaRelative: Float = 0
  42. if account.quotaRelative > 0 {
  43. quotaRelative = Float(account.quotaRelative) / 100
  44. }
  45. let quotaUsed: String = CCUtility.transformedSize(account.quotaUsed)
  46. var quotaTotal: String = ""
  47. switch account.quotaTotal {
  48. case -1:
  49. quotaTotal = ""
  50. case -2:
  51. quotaTotal = ""
  52. case -3:
  53. quotaTotal = ""
  54. default:
  55. quotaTotal = CCUtility.transformedSize(account.quotaTotal)
  56. }
  57. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  58. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion25 {
  59. // NETWORKING
  60. let password = CCUtility.getPassword(account.account)!
  61. NKCommon.shared.setup(
  62. account: account.account,
  63. user: account.user,
  64. userId: account.userId,
  65. password: password,
  66. urlBase: account.urlBase,
  67. userAgent: CCUtility.getUserAgent(),
  68. nextcloudVersion: 0,
  69. delegate: NCNetworking.shared)
  70. let options = NKRequestOptions(timeout: 15, queue: NKCommon.shared.backgroundQueue)
  71. NextcloudKit.shared.getDashboardWidgetsApplication("activity", options: options) { _, results, _, error in
  72. var activity: String = NSLocalizedString("_no_data_available_", comment: "")
  73. var link = URL(string: "https://")!
  74. if error == .success, let result = results?.first {
  75. if let item = result.items?.first {
  76. if let title = item.title { activity = title }
  77. if let itemLink = item.link, let url = URL(string: itemLink) { link = url }
  78. }
  79. }
  80. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: activity, link: link, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
  81. }
  82. } else {
  83. completion(LockscreenData(date: Date(), isPlaceholder: false, activity: NSLocalizedString("_widget_available_nc25_", comment: ""), link: URL(string: "https://")!, quotaRelative: quotaRelative, quotaUsed: quotaUsed, quotaTotal: quotaTotal))
  84. }
  85. }