Skip to content

Commit b73682c

Browse files
committed
Add view model to coupon allowed emails view
1 parent 2ce1e2e commit b73682c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

WooCommerce/Classes/ViewRelated/Coupons/Add and Edit Coupons/UsageDetails/CouponAllowedEmails.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import SwiftUI
33
/// View to input allowed email formats for coupons
44
///
55
struct CouponAllowedEmails: View {
6-
@Binding var emailFormats: String
6+
@ObservedObject private var viewModel: CouponAllowedEmailsViewModel
7+
8+
init(viewModel: CouponAllowedEmailsViewModel) {
9+
self.viewModel = viewModel
10+
}
711

812
var body: some View {
913
GeometryReader { geometry in
1014
VStack(alignment: .leading) {
11-
TextField("", text: $emailFormats)
15+
TextField("", text: $viewModel.emailPatterns)
1216
.labelsHidden()
1317
.padding(.horizontal, Constants.margin)
1418
.padding(.horizontal, insets: geometry.safeAreaInsets)
@@ -28,7 +32,7 @@ struct CouponAllowedEmails: View {
2832
.toolbar {
2933
ToolbarItem(placement: .navigationBarTrailing) {
3034
Button(action: {
31-
// TODO: validate
35+
viewModel.validateEmails()
3236
}, label: Localization.done)
3337
}
3438
}
@@ -54,6 +58,7 @@ private extension CouponAllowedEmails {
5458

5559
struct CouponAllowedEmails_Previews: PreviewProvider {
5660
static var previews: some View {
57-
CouponAllowedEmails(emailFormats: .constant("*gmail.com, *@me.com"))
61+
let viewModel = CouponAllowedEmailsViewModel(allowedEmails: "*gmail.com, *@me.com") { _ in }
62+
CouponAllowedEmails(viewModel: viewModel)
5863
}
5964
}

WooCommerce/Classes/ViewRelated/Coupons/Add and Edit Coupons/UsageDetails/CouponAllowedEmailsViewModel.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import Foundation
44
///
55
final class CouponAllowedEmailsViewModel: ObservableObject {
66

7-
@Published var allowedEmails: String
7+
@Published var emailPatterns: String
8+
@Published var foundInvalidPatterns: Bool = false
89

9-
@Published private(set) var foundInvalidPatterns: Bool = false
10+
private let onCompletion: (String) -> Void
1011

11-
init(allowedEmails: String) {
12-
self.allowedEmails = allowedEmails
12+
init(allowedEmails: String, onCompletion: @escaping (String) -> Void) {
13+
self.emailPatterns = allowedEmails
14+
self.onCompletion = onCompletion
1315
}
1416

1517
/// Validate the input
1618
///
17-
func validateEmails() {
18-
19+
func validateEmails() -> Bool {
20+
// TODO: implement this
21+
return true
1922
}
2023
}

0 commit comments

Comments
 (0)