|
| 1 | +import Foundation |
| 2 | +import Yosemite |
| 3 | +import protocol Storage.StorageManagerType |
| 4 | + |
| 5 | +final class PluginDetailsViewModel: ObservableObject { |
| 6 | + /// ID of the site to load plugins for |
| 7 | + /// |
| 8 | + private let siteID: Int64 |
| 9 | + |
| 10 | + /// Name of the plugin to show details for |
| 11 | + /// |
| 12 | + private let pluginName: String |
| 13 | + |
| 14 | + /// Reference to the StoresManager to dispatch Yosemite Actions. |
| 15 | + /// |
| 16 | + private let storesManager: StoresManager |
| 17 | + |
| 18 | + /// StorageManager to load plugins from storage |
| 19 | + /// |
| 20 | + private let storageManager: StorageManagerType |
| 21 | + |
| 22 | + /// Results controller for the plugin list |
| 23 | + /// |
| 24 | + private lazy var resultsController: ResultsController<StorageSystemPlugin> = { |
| 25 | + let predicate = NSPredicate(format: "siteID = %ld AND name = %@", self.siteID, pluginName) |
| 26 | + let resultsController = ResultsController<StorageSystemPlugin>( |
| 27 | + storageManager: storageManager, |
| 28 | + matching: predicate, |
| 29 | + sortedBy: [] |
| 30 | + ) |
| 31 | + |
| 32 | + do { |
| 33 | + try resultsController.performFetch() |
| 34 | + plugin = resultsController.fetchedObjects.first |
| 35 | + } catch { |
| 36 | + DDLogError("⛔️ Error fetching WooCommerce plugin details!") |
| 37 | + } |
| 38 | + return resultsController |
| 39 | + }() |
| 40 | + |
| 41 | + /// Title for the plugin details row |
| 42 | + /// |
| 43 | + let title: String |
| 44 | + |
| 45 | + var updateAvailable: Bool { |
| 46 | + guard let plugin = plugin else { |
| 47 | + return false |
| 48 | + } |
| 49 | + return !VersionHelpers.isVersionSupported(version: plugin.version, minimumRequired: plugin.versionLatest) |
| 50 | + } |
| 51 | + |
| 52 | + /// URL for the plugins page in WP-admin, used for the update webview when an update is available |
| 53 | + /// |
| 54 | + @Published var updateURL: URL? |
| 55 | + |
| 56 | + /// Version of the plugin installed on the current site |
| 57 | + /// |
| 58 | + @Published var version: String |
| 59 | + |
| 60 | + /// Latest version of the plugin installed on the current site |
| 61 | + /// |
| 62 | + @Published var versionLatest: String? |
| 63 | + |
| 64 | + var plugin: SystemPlugin? { |
| 65 | + didSet { |
| 66 | + version = plugin?.version ?? Localization.unknownVersionValue |
| 67 | + versionLatest = plugin?.versionLatest |
| 68 | + updateURL = updateURL(for: plugin) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + init(siteID: Int64, |
| 73 | + pluginName: String, |
| 74 | + storesManager: StoresManager = ServiceLocator.stores, |
| 75 | + storageManager: StorageManagerType = ServiceLocator.storageManager) { |
| 76 | + self.siteID = siteID |
| 77 | + self.pluginName = pluginName |
| 78 | + self.storesManager = storesManager |
| 79 | + self.storageManager = storageManager |
| 80 | + self.title = String(format: Localization.pluginDetailTitle, pluginName) |
| 81 | + self.plugin = nil |
| 82 | + self.updateURL = nil |
| 83 | + self.version = "" |
| 84 | + self.versionLatest = nil |
| 85 | + observePlugin { self.plugin = self.resultsController.fetchedObjects.first } |
| 86 | + } |
| 87 | + |
| 88 | + /// Start fetching and observing plugin data from local storage. |
| 89 | + /// |
| 90 | + private func observePlugin(onDataChanged: @escaping () -> Void) { |
| 91 | + resultsController.onDidChangeContent = onDataChanged |
| 92 | + } |
| 93 | + |
| 94 | + /// Used to refresh the store after the webview is used to perform an update |
| 95 | + /// |
| 96 | + func refreshPlugin() { |
| 97 | + let action = SystemStatusAction.synchronizeSystemPlugins(siteID: siteID) { _ in } |
| 98 | + storesManager.dispatch(action) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +private extension PluginDetailsViewModel { |
| 103 | + private func updateURL(for plugin: SystemPlugin?) -> URL? { |
| 104 | + guard let url = storesManager.sessionManager.defaultSite?.pluginsURL, |
| 105 | + updateAvailable(for: plugin) |
| 106 | + else { |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + return URL(string: url) |
| 111 | + } |
| 112 | + |
| 113 | + private func updateAvailable(for plugin: SystemPlugin?) -> Bool { |
| 114 | + guard let plugin = plugin else { |
| 115 | + return false |
| 116 | + } |
| 117 | + return !VersionHelpers.isVersionSupported(version: plugin.version, minimumRequired: plugin.versionLatest) |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +private enum Localization { |
| 123 | + static let pluginDetailTitle = NSLocalizedString( |
| 124 | + "%1$@ Version", |
| 125 | + comment: "Title for the plugin version detail row in settings. %1$@ is a placeholder for the plugin name. " + |
| 126 | + "This is displayed with the current version number, and whether an update is available.") |
| 127 | + |
| 128 | + static let unknownVersionValue = NSLocalizedString( |
| 129 | + "unknown", |
| 130 | + comment: "Value for the WooCommerce plugin version detail row in settings, when the version is unknown. " + |
| 131 | + "This is in place of the current version number.") |
| 132 | +} |
0 commit comments