Skip to content

Commit

Permalink
feat: add logic for disabling save
Browse files Browse the repository at this point in the history
  • Loading branch information
issammani committed Jul 14, 2024
1 parent 9bc1246 commit bd1f54e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct AddressListView: View {
trailing: Button(String.Addresses.Settings.Edit.AutofillSaveButton) {
viewModel.saveAddressButtonTap()
}
.disabled(!viewModel.isSaveEnabled)
)

case .edit:
Expand All @@ -101,6 +102,7 @@ struct AddressListView: View {
Button(String.Addresses.Settings.Edit.AutofillSaveButton) {
viewModel.saveEditButtonTap()
}
.disabled(!viewModel.isSaveEnabled)
} else {
Button(String.Addresses.Settings.Edit.EditNavBarButtonLabel) {
viewModel.editButtonTap()
Expand All @@ -123,6 +125,11 @@ struct AddressListView: View {
.onDisappear {
viewModel.editAddressWebViewManager.teardownWebView()
}
.onReceive(NotificationCenter.default.publisher(for: .addressSettingsSaving)) { notification in
if let enabled = notification.userInfo?["enabled"] as? Bool {
viewModel.isSaveEnabled = enabled
}
}
}

// MARK: - Theme Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ final class AddressListViewModel: ObservableObject, FeatureFlaggable {
return value.guid
}
}

var address: Address {
switch self {
case .add(let value), .edit(let value):
return value
}
}
}

// MARK: - Properties
Expand All @@ -30,6 +37,7 @@ final class AddressListViewModel: ObservableObject, FeatureFlaggable {
@Published var showSection = false
@Published var destination: Destination?
@Published var isEditMode = false
@Published var isSaveEnabled = false

let windowUUID: WindowUUID

Expand Down Expand Up @@ -300,17 +308,12 @@ final class AddressListViewModel: ObservableObject, FeatureFlaggable {
}

do {
let address: Address =
switch destination {
case .add(let address):
address
case .edit(let address):
address
}

let address: Address = destination.address
let addressString = try jsonString(from: address)
let l10sString = try jsonString(from: EditAddressLocalization.editAddressLocalizationIDs)
let javascript = "init(\(addressString), \(l10sString), \(isDarkTheme));"
let isNewRecord = { if case .add = destination { return true } else { return false } }()

let javascript = "init(\(addressString), \(l10sString), \(isDarkTheme), \(isNewRecord));"
return javascript
} catch {
logger.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ protocol WebViewPreloadManaging {
func teardownWebView()
}

class EditAddressWebViewManager: WebViewPreloadManaging {
class EditAddressWebViewManager: NSObject, WebViewPreloadManaging, WKScriptMessageHandler {
private(set) var webView: WKWebView?

init() {
override init() {
super.init()

let webConfiguration = WKWebViewConfiguration()
let contentController = WKUserContentController()
contentController.add(self, name: "saveEnabled")
webConfiguration.userContentController = contentController

self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
self.webView?.translatesAutoresizingMaskIntoConstraints = false

Expand Down Expand Up @@ -44,4 +50,16 @@ class EditAddressWebViewManager: WebViewPreloadManaging {
webView?.removeFromSuperview()
webView = nil
}

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard message.name == "saveEnabled",
let body = message.body as? [String: Any],
let saveEnabled = body["enabled"] as? Bool else { return }

NotificationCenter.default.post(name: .addressSettingsSaving, object: nil, userInfo: ["enabled": saveEnabled])
}
}

extension Notification.Name {
static let addressSettingsSaving = Notification.Name("addressSettingsSaving")
}

0 comments on commit bd1f54e

Please sign in to comment.