diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift
index 5f171b349..da89450fa 100644
--- a/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift
+++ b/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift
@@ -97,7 +97,7 @@ struct NewsletterEmailsView: View {
Snackbar.show(message: "Email copied")
},
- label: { Text(newsletterEmail.email) }
+ label: { Text(newsletterEmail.unwrappedEmail) }
)
}
}
diff --git a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents
index 655f7476c..2a18d1709 100644
--- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents
+++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents
@@ -1,5 +1,15 @@
+
+
+
+
+
+
+
+
+
+
@@ -81,5 +91,6 @@
+
\ No newline at end of file
diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/NewsletterEmail.swift b/apple/OmnivoreKit/Sources/Models/DataModels/NewsletterEmail.swift
index ee4270668..5a7f540ba 100644
--- a/apple/OmnivoreKit/Sources/Models/DataModels/NewsletterEmail.swift
+++ b/apple/OmnivoreKit/Sources/Models/DataModels/NewsletterEmail.swift
@@ -1,14 +1,11 @@
import Foundation
-public struct NewsletterEmail: Identifiable {
- public let id = UUID()
- public let emailId: String
- public let email: String
- public let confirmationCode: String?
+public extension NewsletterEmail {
+ var unwrappedEmailId: String {
+ emailId ?? ""
+ }
- public init(emailId: String, email: String, confirmationCode: String?) {
- self.emailId = emailId
- self.email = email
- self.confirmationCode = confirmationCode
+ var unwrappedEmail: String {
+ email ?? ""
}
}
diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateNewsletterEmailMutation.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateNewsletterEmailMutation.swift
index b8028e47e..38b9cd6de 100644
--- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateNewsletterEmailMutation.swift
+++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateNewsletterEmailMutation.swift
@@ -1,4 +1,5 @@
import Combine
+import CoreData
import Foundation
import Models
import SwiftGraphQL
@@ -6,7 +7,7 @@ import SwiftGraphQL
public extension DataService {
func createNewsletterEmailPublisher() -> AnyPublisher {
enum MutationResult {
- case saved(newsletterEmail: NewsletterEmail)
+ case saved(newsletterEmail: InternalNewsletterEmail)
case error(errorCode: Enums.CreateNewsletterEmailErrorCode)
}
@@ -14,7 +15,7 @@ public extension DataService {
try $0.on(
createNewsletterEmailSuccess: .init {
.saved(newsletterEmail: try $0.newsletterEmail(selection: Selection.NewsletterEmail {
- NewsletterEmail(
+ InternalNewsletterEmail(
emailId: try $0.id(),
email: try $0.address(),
confirmationCode: try $0.confirmationCode()
@@ -44,7 +45,11 @@ public extension DataService {
switch payload.data {
case let .saved(newsletterEmail: newsletterEmail):
- promise(.success(newsletterEmail))
+ if let newsletterEmailObject = newsletterEmail.persist(context: self.persistentContainer.viewContext) {
+ promise(.success(newsletterEmailObject))
+ } else {
+ promise(.failure(.message(messageText: "coredata error")))
+ }
case let .error(errorCode: errorCode):
promise(.failure(.message(messageText: errorCode.rawValue)))
}
diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift
index 25ccea69f..47afa350c 100644
--- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift
+++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift
@@ -6,12 +6,12 @@ import SwiftGraphQL
public extension DataService {
func newsletterEmailsPublisher() -> AnyPublisher<[NewsletterEmail], ServerError> {
enum QueryResult {
- case success(result: [NewsletterEmail])
+ case success(result: [InternalNewsletterEmail])
case error(error: String)
}
let newsletterEmailSelection = Selection.NewsletterEmail {
- NewsletterEmail(
+ InternalNewsletterEmail(
emailId: try $0.id(),
email: try $0.address(),
confirmationCode: try $0.confirmationCode()
@@ -43,7 +43,11 @@ public extension DataService {
case let .success(payload):
switch payload.data {
case let .success(result: result):
- promise(.success(result))
+ if let newsletterEmailObject = result.persist(context: self.persistentContainer.viewContext) {
+ promise(.success(newsletterEmailObject))
+ } else {
+ promise(.failure(.unknown))
+ }
case .error:
promise(.failure(.unknown))
}
diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift
new file mode 100644
index 000000000..cd5dd3329
--- /dev/null
+++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift
@@ -0,0 +1,47 @@
+import CoreData
+import Foundation
+import Models
+
+struct InternalNewsletterEmail {
+ let emailId: String
+ let email: String
+ let confirmationCode: String?
+
+ func persist(context: NSManagedObjectContext) -> NewsletterEmail? {
+ let newsletterEmail = asManagedObject(inContext: context)
+
+ do {
+ try context.save()
+ DataService.logger.debug("NewsletterEmail saved succesfully")
+ return newsletterEmail
+ } catch {
+ context.rollback()
+ DataService.logger.debug("Failed to save NewsletterEmail: \(error.localizedDescription)")
+ return nil
+ }
+ }
+
+ func asManagedObject(inContext context: NSManagedObjectContext) -> NewsletterEmail {
+ let newsletterEmail = NewsletterEmail(context: context)
+ newsletterEmail.emailId = emailId
+ newsletterEmail.email = email
+ newsletterEmail.confirmationCode = confirmationCode
+ return newsletterEmail
+ }
+}
+
+extension Sequence where Element == InternalNewsletterEmail {
+ func persist(context: NSManagedObjectContext) -> [NewsletterEmail]? {
+ let newsletterEmails = map { $0.asManagedObject(inContext: context) }
+
+ do {
+ try context.save()
+ DataService.logger.debug("NewsletterEmail saved succesfully")
+ return newsletterEmails
+ } catch {
+ context.rollback()
+ DataService.logger.debug("Failed to save NewsletterEmail: \(error.localizedDescription)")
+ return nil
+ }
+ }
+}