@@ -39,11 +39,11 @@ struct PluginDetailsView: View {
3939 return plugin. isActive
4040 ? . activated( plugin: plugin)
4141 : . activate( plugin: plugin) {
42- // TODO
42+ Task { await viewModel . updatePluginStatus ( plugin , activated : true ) }
4343 }
4444 } else {
4545 return . install( slug: slug) {
46- // TODO
46+ Task { await viewModel . install ( ) }
4747 }
4848 }
4949 }
@@ -87,7 +87,7 @@ struct PluginDetailsView: View {
8787
8888 actionButton
8989 . view
90- . disabled ( viewModel. isLoading || viewModel. isActivating || viewModel. isUninstalling)
90+ . disabled ( viewModel. isLoading || viewModel. isActivating || viewModel. isDeactivating || viewModel . isUninstalling || viewModel . isInstalling )
9191 }
9292 . listRowSeparator ( . hidden)
9393
@@ -98,7 +98,13 @@ struct PluginDetailsView: View {
9898 . listSectionSeparator ( . hidden)
9999
100100 if viewModel. isUninstalling {
101- uninstallingView ( )
101+ inlineProgressView ( title: Strings . uninstallingTitle, message: Strings . uninstallingMessage)
102+ } else if viewModel. isInstalling {
103+ inlineProgressView ( title: Strings . installingTitle, message: Strings . installingMessage)
104+ } else if viewModel. isActivating {
105+ inlineProgressView ( title: Strings . activatingTitle, message: Strings . activatingMessage)
106+ } else if viewModel. isDeactivating {
107+ inlineProgressView ( title: Strings . deactivatingTitle, message: Strings . deactivatingMessage)
102108 } else if let newVersion {
103109 updateAvailableView ( newVersion)
104110 }
@@ -134,6 +140,14 @@ struct PluginDetailsView: View {
134140 }
135141 }
136142
143+ if let installed = viewModel. installed, installed. isActive {
144+ Button {
145+ Task { await viewModel. updatePluginStatus ( installed, activated: false ) }
146+ } label: {
147+ Label ( Strings . deactivateButton, systemImage: " circle.slash " )
148+ }
149+ }
150+
137151 if let url = wpOrgURL {
138152 Section {
139153 ShareLink ( item: url)
@@ -214,14 +228,14 @@ struct PluginDetailsView: View {
214228 }
215229
216230 @ViewBuilder
217- private func uninstallingView ( ) -> some View {
231+ private func inlineProgressView ( title : String , message : String ) -> some View {
218232 HStack {
219233 ProgressView ( )
220234
221235 VStack ( alignment: . leading) {
222- Text ( Strings . uninstallingTitle )
236+ Text ( title )
223237 . font ( . headline)
224- Text ( Strings . uninstallingMessage )
238+ Text ( message )
225239 . font ( . subheadline)
226240 . foregroundStyle ( . secondary)
227241 }
@@ -383,10 +397,12 @@ final class WordPressPluginDetailViewModel: ObservableObject {
383397
384398 @Published private( set) var isLoading = false
385399 @Published private( set) var isUninstalling = false
400+ @Published private( set) var isInstalling = false
386401 @Published private( set) var plugin : PluginInformation ?
387402 @Published private( set) var installed : InstalledPlugin ?
388403 @Published private( set) var error : String ?
389404 @Published private( set) var isActivating = false
405+ @Published private( set) var isDeactivating = false
390406
391407 private var initialLoad = false
392408
@@ -428,14 +444,15 @@ final class WordPressPluginDetailViewModel: ObservableObject {
428444 }
429445 }
430446
431- func activate( _ plugin: InstalledPlugin ) async {
432- isActivating = true
447+ func updatePluginStatus( _ plugin: InstalledPlugin , activated: Bool ) async {
448+ let keyPath : ReferenceWritableKeyPath < WordPressPluginDetailViewModel , Bool > = activated ? \. isActivating : \. isDeactivating
449+ self [ keyPath: keyPath] = true
433450 defer {
434- isActivating = false
451+ self [ keyPath : keyPath ] = false
435452 }
436453
437454 do {
438- try await service. togglePluginActivation ( slug : plugin. slug )
455+ self . installed = try await service. updatePluginStatus ( plugin : plugin, activated : false )
439456 } catch {
440457 // TODO: Show an error notice
441458 }
@@ -453,6 +470,19 @@ final class WordPressPluginDetailViewModel: ObservableObject {
453470 // TODO: Show an error notice
454471 }
455472 }
473+
474+ func install( ) async {
475+ isInstalling = true
476+ defer {
477+ isInstalling = false
478+ }
479+
480+ do {
481+ self . installed = try await service. installPlugin ( slug: slug)
482+ } catch {
483+ // TODO: Show an error notice
484+ }
485+ }
456486}
457487
458488private extension PluginInformation {
@@ -619,6 +649,12 @@ private enum Strings {
619649 comment: " Button label to activate a plugin "
620650 )
621651
652+ static let deactivateButton = NSLocalizedString (
653+ " pluginDetails.deactivate.button " ,
654+ value: " Deactivate " ,
655+ comment: " Button label to deactivate a plugin "
656+ )
657+
622658 static let activatedButton = NSLocalizedString (
623659 " pluginDetails.activated.button " ,
624660 value: " Activated " ,
@@ -636,4 +672,40 @@ private enum Strings {
636672 value: " Please wait while the plugin is being removed... " ,
637673 comment: " Message shown while a plugin is being uninstalled "
638674 )
675+
676+ static let installingTitle = NSLocalizedString (
677+ " pluginDetails.install.title " ,
678+ value: " Install Plugin " ,
679+ comment: " Title shown while a plugin is being installed "
680+ )
681+
682+ static let installingMessage = NSLocalizedString (
683+ " pluginDetails.install.message " ,
684+ value: " Please wait while the plugin is being installed... " ,
685+ comment: " Message shown while a plugin is being installed "
686+ )
687+
688+ static let activatingTitle = NSLocalizedString (
689+ " pluginDetails.activating.title " ,
690+ value: " Activating Plugin " ,
691+ comment: " Title shown while a plugin is being activated "
692+ )
693+
694+ static let activatingMessage = NSLocalizedString (
695+ " pluginDetails.activating.message " ,
696+ value: " Please wait while the plugin is being activated... " ,
697+ comment: " Message shown while a plugin is being activated "
698+ )
699+
700+ static let deactivatingTitle = NSLocalizedString (
701+ " pluginDetails.deactivating.title " ,
702+ value: " Deactivating Plugin " ,
703+ comment: " Title shown while a plugin is being deactivated "
704+ )
705+
706+ static let deactivatingMessage = NSLocalizedString (
707+ " pluginDetails.deactivating.message " ,
708+ value: " Please wait while the plugin is being deactivated... " ,
709+ comment: " Message shown while a plugin is being deactivated "
710+ )
639711}
0 commit comments