-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Site Settings in SwiftUI: Initial #20838
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import SwiftUI | ||
|
||
struct Backport<T: View> { | ||
let view: T | ||
} | ||
|
||
extension View { | ||
var backport: Backport<Self> { Backport(view: self) } | ||
} | ||
|
||
extension Backport { | ||
@ViewBuilder | ||
func refreshable(action: @Sendable @escaping () async -> Void) -> some View { | ||
if #available(iOS 15, *) { | ||
view.refreshable(action: action) | ||
} else { | ||
view | ||
} | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
WordPress/Classes/ViewRelated/Blog/Settings/SiteSettingsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import Foundation | ||
import SwiftUI | ||
import Combine | ||
import SVProgressHUD | ||
|
||
struct SiteSettingsView: View { | ||
@ObservedObject private var blog: Blog | ||
@ObservedObject private var settings: BlogSettings | ||
|
||
@StateObject private var viewModel: SiteSettingsViewModel | ||
|
||
@SwiftUI.Environment(\.presentationMode) private var presentationMode | ||
|
||
init(blog: Blog) { | ||
self.blog = blog | ||
self.settings = blog.settings ?? BlogSettings(context: ContextManager.shared.mainContext) // Right-side should never happen | ||
self._viewModel = StateObject(wrappedValue: SiteSettingsViewModel(blog: blog)) | ||
} | ||
|
||
var body: some View { | ||
List { | ||
sections | ||
} | ||
.listStyle(.insetGrouped) | ||
.onReceive(viewModel.onDismissableError) { | ||
SVProgressHUD.showDismissibleError(withStatus: $0) | ||
} | ||
.backport.refreshable { | ||
await viewModel.refresh() | ||
} | ||
.onAppear { | ||
Task { await viewModel.refresh() } | ||
} | ||
.navigationTitle(Strings.title) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { toolbar } | ||
} | ||
|
||
private var toolbar: some ToolbarContent { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
if presentationMode.wrappedValue.isPresented { | ||
closeButton | ||
} | ||
} | ||
} | ||
|
||
private var sections: some View { | ||
kean marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Section(header: Text(Strings.Sections.general)) { | ||
siteTitleRow | ||
} | ||
} | ||
|
||
// MARK: - General | ||
|
||
private var siteTitleRow: some View { | ||
withAdminNavigationLink(destination: { | ||
SettingsTextEditView( | ||
value: settings.name, | ||
placeholder: Strings.General.siteTitlePlaceholder, | ||
onCommit: viewModel.updateSiteTitle | ||
) | ||
.navigationTitle(Strings.General.siteTitle) | ||
}) { | ||
SettingsCell(title: Strings.General.siteTitle, value: settings.name ?? Strings.General.siteTitlePlaceholder) | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
@ViewBuilder | ||
private func withAdminNavigationLink<T: View, U: View>( | ||
@ViewBuilder destination: () -> T, | ||
@ViewBuilder content: () -> U | ||
) -> some View { | ||
if blog.isAdmin { | ||
NavigationLink(destination: destination(), label: content) | ||
} else { | ||
content() | ||
} | ||
} | ||
|
||
private var closeButton: some View { | ||
Button(action: { presentationMode.wrappedValue.dismiss() }) { | ||
Text(Strings.done) | ||
.font(.body.weight(.medium)) | ||
.foregroundColor(Color.primary) | ||
} | ||
} | ||
} | ||
|
||
private struct SettingsCell: View { | ||
let title: String | ||
let value: String | ||
|
||
var body: some View { | ||
HStack { | ||
Text(title) | ||
.layoutPriority(1) | ||
Spacer() | ||
Text(value) | ||
.foregroundColor(.secondary) | ||
} | ||
.lineLimit(1) | ||
} | ||
} | ||
|
||
private struct SettingsTextEditView: UIViewControllerRepresentable { | ||
let value: String? | ||
let placeholder: String | ||
var hint: String? | ||
let onCommit: ((String)) -> Void | ||
|
||
func makeUIViewController(context: Context) -> SettingsTextViewController { | ||
let viewController = SettingsTextViewController(text: value ?? "", placeholder: placeholder, hint: hint ?? "") | ||
viewController.onValueChanged = onCommit | ||
return viewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: SettingsTextViewController, context: Context) { | ||
// Do nothing | ||
} | ||
} | ||
kean marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private extension SiteSettingsView { | ||
enum Strings { | ||
static let title = NSLocalizedString("siteSettings.title", value: "Settings", comment: "Title for screen that allows configuration of your blog/site settings.") | ||
static let done = NSLocalizedString("siteSettings.done", value: "Done", comment: "Label for Done button") | ||
|
||
enum Sections { | ||
static let general = NSLocalizedString("siteSettings.general.title", value: "General", comment: "Title for the general section in site settings screen") | ||
} | ||
|
||
enum General { | ||
static let siteTitle = NSLocalizedString("siteSettings.general.siteTitle", value: "Site Title", comment: "Label for site title blog setting") | ||
static let siteTitlePlaceholder = NSLocalizedString("siteSettings.general.siteTitlePlaceholder", value: "A title for the site", comment: "Placeholder text for the title of a site") | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
WordPress/Classes/ViewRelated/Blog/Settings/SiteSettingsViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
import SwiftUI | ||
import Combine | ||
|
||
final class SiteSettingsViewModel: ObservableObject { | ||
private let blog: Blog | ||
private let service: BlogService | ||
|
||
let onDismissableError = PassthroughSubject<String, Never>() | ||
|
||
init(blog: Blog, | ||
service: BlogService = BlogService(coreDataStack: ContextManager.shared)) { | ||
self.blog = blog | ||
self.service = service | ||
} | ||
|
||
func refresh() async -> Void { | ||
await withUnsafeContinuation { continuation in | ||
service.syncSettings(for: blog, success: { | ||
continuation.resume() | ||
}, failure: { error in | ||
continuation.resume() | ||
DDLogError("Error while refreshing blog settings: \(error)") | ||
}) | ||
} | ||
} | ||
|
||
func updateSiteTitle(_ value: String) { | ||
guard value != blog.settings?.name else { return } | ||
blog.settings?.name = value | ||
save() | ||
trackSettingsChange(fieldName: "site_title") | ||
} | ||
|
||
private func save() { | ||
service.updateSettings(for: blog, success: { | ||
NotificationCenter.default.post(name: .WPBlogSettingsUpdated, object: nil) | ||
}, failure: { [weak self] error in | ||
self?.onDismissableError.send(Strings.saveFailed) | ||
DDLogError("Error while trying to update BlogSettings: \(error)") | ||
}) | ||
} | ||
|
||
private func trackSettingsChange(fieldName: String, value: Any? = nil) { | ||
WPAnalytics.trackSettingsChange("site_settings", fieldName: fieldName, value: value) | ||
} | ||
} | ||
|
||
private extension SiteSettingsViewModel { | ||
enum Strings { | ||
static let saveFailed = NSLocalizedString("siteSettings.updateFailedMessage", value: "Settings update failed", comment: "Message to show when setting save failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove it once iOS 14 support is dropped.