-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
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 { | ||
Section(header: Text(Strings.Sections.general), content: { | ||
siteTitleRow | ||
}) | ||
} | ||
.listStyle(.insetGrouped) | ||
.navigationTitle(Strings.title) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
if presentationMode.wrappedValue.isPresented { | ||
closeButton | ||
} | ||
} | ||
} | ||
.onReceive(viewModel.onDismissableError) { | ||
SVProgressHUD.showDismissibleError(withStatus: $0) | ||
} | ||
} | ||
|
||
// 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 | ||
} | ||
} | ||
|
||
private extension SiteSettingsView { | ||
enum Strings { | ||
static let title = NSLocalizedString("Settings", comment: "Title for screen that allows configuration of your blog/site settings.") | ||
static let done = NSLocalizedString("Done", comment: "Label for Done button") | ||
|
||
enum Sections { | ||
static let general = NSLocalizedString("General", comment: "Title for the general section in site settings screen") | ||
} | ||
|
||
enum General { | ||
static let siteTitle = NSLocalizedString("Site Title", comment: "Label for site title blog setting") | ||
static let siteTitlePlaceholder = NSLocalizedString("A title for the site", comment: "Placeholder text for the title of a site") | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
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) { | ||
self.blog = blog | ||
self.service = BlogService(coreDataStack: ContextManager.shared) | ||
} | ||
|
||
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("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