Skip to content

Commit 6bf127f

Browse files
authored
Change if statements to switch-case statements (#24100)
* Change if statements to switch-case statements * Replace 'Group' with a view builder function
1 parent 50cf4e8 commit 6bf127f

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

WordPress/Classes/Plugins/Views/AddNewPluginView.swift

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct AddNewPluginView: View {
2929
}
3030
.listStyle(.grouped)
3131
.scrollContentBackground(.hidden)
32-
.navigationTitle("Add New Plugin")
32+
.navigationTitle(Strings.navigationTitle)
3333
.navigationBarTitleDisplayMode(.inline)
3434
.searchable(text: $viewModel.searchInput)
3535
.toolbar {
@@ -84,16 +84,21 @@ struct AddNewPluginView: View {
8484

8585
@ViewBuilder
8686
private func rowContent(_ row: ListRow) -> some View {
87-
if case .loading = row {
87+
switch row {
88+
case .loading:
8889
Label {
89-
Text("Loading plugins...")
90-
} icon: { ProgressView() }
90+
Text(Strings.loadingPlugins)
91+
} icon: {
92+
ProgressView()
93+
}
9194
.font(.callout)
9295
.foregroundColor(.secondary)
9396
.frame(maxWidth: .infinity, alignment: .center)
94-
} else if case .error = row {
95-
Text("Failed to load plugins. Tap here to retry")
96-
} else if case let .plugin(plugin, isInstalled) = row {
97+
98+
case .error(let errorMessage):
99+
Text(Strings.loadError)
100+
101+
case let .plugin(plugin, isInstalled):
97102
NavigationLink(destination: PluginDetailsView(plugin: plugin, service: viewModel.service)) {
98103
HStack(alignment: .center) {
99104
PluginIconView(plugin: plugin, service: viewModel.service)
@@ -130,10 +135,10 @@ struct AddNewPluginView: View {
130135
}
131136
}
132137
}
133-
} else if case .empty = row {
134-
Text("No plugins found")
135-
} else {
136-
Text("Impossible to reach here")
138+
139+
case .empty:
140+
Text(Strings.noPluginsFound)
141+
137142
}
138143
}
139144
}
@@ -165,14 +170,14 @@ private enum ListSection: Identifiable, Hashable {
165170
case let .plugins(category, _):
166171
switch category {
167172
case .featured:
168-
return "Featured"
173+
return Strings.featured
169174
case .popular:
170-
return "Popular"
175+
return Strings.popular
171176
case .recommended:
172-
return "Recommended"
177+
return Strings.recommended
173178
}
174179
case .searchResult:
175-
return "Search Results"
180+
return Strings.searchResults
176181
}
177182
}
178183
}
@@ -344,4 +349,52 @@ private enum Strings {
344349
value: "Installed",
345350
comment: "Tag shown next to plugins that are already installed"
346351
)
352+
353+
static let navigationTitle = NSLocalizedString(
354+
"site.plugins.add.title",
355+
value: "Add New Plugin",
356+
comment: "Navigation title for the add new plugin screen"
357+
)
358+
359+
static let loadingPlugins = NSLocalizedString(
360+
"site.plugins.add.loading",
361+
value: "Loading plugins...",
362+
comment: "Message shown while loading plugins in the list"
363+
)
364+
365+
static let loadError = NSLocalizedString(
366+
"site.plugins.add.loadError",
367+
value: "Failed to load plugins. Tap here to retry",
368+
comment: "Error message shown when plugins failed to load, with retry option"
369+
)
370+
371+
static let noPluginsFound = NSLocalizedString(
372+
"site.plugins.add.empty",
373+
value: "No plugins found",
374+
comment: "Message shown when no plugins are found in the list"
375+
)
376+
377+
static let featured = NSLocalizedString(
378+
"site.plugins.add.category.featured",
379+
value: "Featured",
380+
comment: "Title for the featured plugins section"
381+
)
382+
383+
static let popular = NSLocalizedString(
384+
"site.plugins.add.category.popular",
385+
value: "Popular",
386+
comment: "Title for the popular plugins section"
387+
)
388+
389+
static let recommended = NSLocalizedString(
390+
"site.plugins.add.category.recommended",
391+
value: "Recommended",
392+
comment: "Title for the recommended plugins section"
393+
)
394+
395+
static let searchResults = NSLocalizedString(
396+
"site.plugins.add.category.searchResults",
397+
value: "Search Results",
398+
comment: "Title for the search results section"
399+
)
347400
}

WordPress/Classes/Plugins/Views/PluginDetailsView.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,24 +392,27 @@ enum ActionButton {
392392
case activate(plugin: InstalledPlugin, action: () -> Void)
393393
case activated(plugin: InstalledPlugin)
394394

395+
@ViewBuilder
395396
var view: some View {
396-
let button: AnyView
397+
button
398+
.buttonStyle(.borderedProminent)
399+
.buttonBorderShape(.capsule)
400+
}
401+
402+
@ViewBuilder
403+
private var button: some View {
397404
switch self {
398405
case let .install(_, action):
399-
button = AnyView(Button(Strings.installButton, action: action)
400-
.font(.callout.bold()))
406+
Button(Strings.installButton, action: action)
407+
.font(.callout.bold())
401408
case let .activate(_, action):
402-
button = AnyView(Button(Strings.activateButton, action: action)
403-
.font(.callout.bold()))
409+
Button(Strings.activateButton, action: action)
410+
.font(.callout.bold())
404411
case .activated:
405-
button = AnyView(Button(Strings.activatedButton, action: { })
412+
Button(Strings.activatedButton, action: { })
406413
.font(.callout)
407-
.disabled(true))
414+
.disabled(true)
408415
}
409-
410-
return button
411-
.buttonStyle(.borderedProminent)
412-
.buttonBorderShape(.capsule)
413416
}
414417
}
415418

0 commit comments

Comments
 (0)