mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
create newsletter email publisher for apple apps
This commit is contained in:
parent
e27c2bdd8b
commit
0f55e1ec14
2 changed files with 84 additions and 0 deletions
21
apple/OmnivoreKit/Sources/Models/NewsletterEmails.swift
Normal file
21
apple/OmnivoreKit/Sources/Models/NewsletterEmails.swift
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Foundation
|
||||
|
||||
public class NewsletterEmails {
|
||||
public let newsletterEmails: [NewsletterEmail]
|
||||
|
||||
public init(newsletterEmails: [NewsletterEmail]) {
|
||||
self.newsletterEmails = newsletterEmails
|
||||
}
|
||||
}
|
||||
|
||||
public struct NewsletterEmail {
|
||||
public let id: String
|
||||
public let email: String
|
||||
public let confirmationCode: String?
|
||||
|
||||
public init(id: String, email: String, confirmationCode: String?) {
|
||||
self.id = id
|
||||
self.email = email
|
||||
self.confirmationCode = confirmationCode
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
import Combine
|
||||
import Foundation
|
||||
import Models
|
||||
import SwiftGraphQL
|
||||
|
||||
public extension DataService {
|
||||
func newsletterEmailsPublisher() -> AnyPublisher<NewsletterEmails, ServerError> {
|
||||
enum QueryResult {
|
||||
case success(result: NewsletterEmails)
|
||||
case error(error: String)
|
||||
}
|
||||
|
||||
let newsletterEmailSelection = Selection.NewsletterEmail {
|
||||
NewsletterEmail(
|
||||
id: try $0.id(),
|
||||
email: try $0.address(),
|
||||
confirmationCode: try $0.confirmationCode()
|
||||
)
|
||||
}
|
||||
|
||||
let selection = Selection<QueryResult, Unions.NewsletterEmailsResult> {
|
||||
try $0.on(
|
||||
newsletterEmailsSuccess: .init {
|
||||
QueryResult.success(result:
|
||||
NewsletterEmails(
|
||||
newsletterEmails: try $0.newsletterEmails(selection: newsletterEmailSelection.list)
|
||||
)
|
||||
)
|
||||
},
|
||||
newsletterEmailsError: .init {
|
||||
QueryResult.error(error: try $0.errorCodes().description)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
let query = Selection.Query {
|
||||
try $0.newsletterEmails(selection: selection)
|
||||
}
|
||||
|
||||
let path = appEnvironment.graphqlPath
|
||||
let headers = networker.defaultHeaders
|
||||
|
||||
return Deferred {
|
||||
Future { promise in
|
||||
send(query, to: path, headers: headers) { result in
|
||||
switch result {
|
||||
case let .success(payload):
|
||||
switch payload.data {
|
||||
case let .success(result: result):
|
||||
promise(.success(result))
|
||||
case .error:
|
||||
promise(.failure(.unknown))
|
||||
}
|
||||
case .failure:
|
||||
promise(.failure(.unknown))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.receive(on: DispatchQueue.main)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue