@@ -2,48 +2,15 @@ import Foundation
22import Yosemite
33import protocol Storage. StorageManagerType
44
5- protocol PluginDetailsViewModel {
6- var version : String { get }
7- var versionLatest : String ? { get }
8- var title : String { get }
9- var updateURL : URL ? { get }
10- func refreshPlugin( )
11- }
12-
13- final class WooCommercePluginViewModel : PluginDetailsViewModel {
14- var updateURL : URL ? {
15- guard let url = storesManager. sessionManager. defaultSite? . pluginsURL,
16- updateAvailable
17- else {
18- return nil
19- }
20-
21- return URL ( string: url)
22- }
23-
24- var updateAvailable : Bool {
25- guard let plugin = plugin else {
26- return false
27- }
28- return !VersionHelpers. isVersionSupported ( version: plugin. version, minimumRequired: plugin. versionLatest)
29- }
30-
31- var version : String {
32- plugin? . version ?? Localization . unknownVersionValue
33- }
34-
35- var versionLatest : String ? {
36- plugin? . versionLatest
37- }
38-
39- private var plugin : SystemPlugin ?
40-
41- let title : String
42-
5+ final class PluginDetailsViewModel : ObservableObject {
436 /// ID of the site to load plugins for
447 ///
458 private let siteID : Int64
469
10+ /// Name of the plugin to show details for
11+ ///
12+ private let pluginName : String
13+
4714 /// Reference to the StoresManager to dispatch Yosemite Actions.
4815 ///
4916 private let storesManager : StoresManager
@@ -55,7 +22,7 @@ final class WooCommercePluginViewModel: PluginDetailsViewModel {
5522 /// Results controller for the plugin list
5623 ///
5724 private lazy var resultsController : ResultsController < StorageSystemPlugin > = {
58- let predicate = NSPredicate ( format: " siteID = %ld AND name = %@ " , self . siteID, Constants . wooCommercePluginName )
25+ let predicate = NSPredicate ( format: " siteID = %ld AND name = %@ " , self . siteID, pluginName )
5926 let resultsController = ResultsController < StorageSystemPlugin > (
6027 storageManager: storageManager,
6128 matching: predicate,
@@ -71,38 +38,92 @@ final class WooCommercePluginViewModel: PluginDetailsViewModel {
7138 return resultsController
7239 } ( )
7340
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+
7472 init ( siteID: Int64 ,
73+ pluginName: String ,
7574 storesManager: StoresManager = ServiceLocator . stores,
76- storageManager: StorageManagerType = ServiceLocator . storageManager,
77- title: String = Localization . pluginDetailTitle) {
75+ storageManager: StorageManagerType = ServiceLocator . storageManager) {
7876 self . siteID = siteID
77+ self . pluginName = pluginName
7978 self . storesManager = storesManager
8079 self . storageManager = storageManager
81- self . title = title
82- observeWooCommercePlugin { self . plugin = self . resultsController. fetchedObjects. first }
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 }
8386 }
8487
8588 /// Start fetching and observing plugin data from local storage.
8689 ///
87- private func observeWooCommercePlugin ( onDataChanged: @escaping ( ) -> Void ) {
90+ private func observePlugin ( onDataChanged: @escaping ( ) -> Void ) {
8891 resultsController. onDidChangeContent = onDataChanged
8992 }
9093
94+ /// Used to refresh the store after the webview is used to perform an update
95+ ///
9196 func refreshPlugin( ) {
9297 let action = SystemStatusAction . synchronizeSystemPlugins ( siteID: siteID) { _ in }
9398 storesManager. dispatch ( action)
9499 }
95100}
96101
97- private enum Constants {
98- static let wooCommercePluginName = " WooCommerce "
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+
99120}
100121
101122private enum Localization {
102123 static let pluginDetailTitle = NSLocalizedString (
103- " WooCommerce Version" ,
104- comment: " Title for the WooCommerce plugin version detail row in settings. This is displayed with the " +
105- " current version number, and whether an update is available. " )
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." )
106127
107128 static let unknownVersionValue = NSLocalizedString (
108129 " unknown " ,
0 commit comments