fetch email list when loading email view

This commit is contained in:
Satindar Dhillon
2022-02-25 13:10:59 -08:00
parent a8ca27993f
commit 666da89c98
5 changed files with 89 additions and 59 deletions

View File

@ -1,53 +0,0 @@
import SwiftUI
struct EmailsView: View {
let footerText = "Add PDFs to your library, or subscribe to emails using an Omnivore email address."
@State var emails = [String]()
var body: some View {
#if os(iOS)
Form {
innerBody
}
#elseif os(macOS)
List {
innerBody
}
.listStyle(InsetListStyle())
#endif
}
private var innerBody: some View {
Group {
Section(footer: Text(footerText)) {
Button(
action: {
withAnimation {
emails.insert("newemail@omnivore-relay.app\(emails.count)", at: 0)
}
},
label: {
HStack {
Image(systemName: "plus.circle.fill").foregroundColor(.green)
Text("Create a new email address")
Spacer()
}
}
)
}
if !emails.isEmpty {
Section(header: Text("Existing Emails (Tap to copy)")) {
ForEach(emails, id: \.self) { email in
Button(
action: {},
label: { Text(email) }
)
}
}
}
}
.navigationTitle("Emails")
}
}

View File

@ -0,0 +1,82 @@
import Combine
import Models
import Services
import SwiftUI
final class NewsletterEmailsViewModel: ObservableObject {
private var hasLoadedInitialEmails = false
@Published var isLoading = false
@Published var emails = [NewsletterEmail]()
var subscriptions = Set<AnyCancellable>()
func loadEmails(dataService: DataService) {
isLoading = true
dataService.newsletterEmailsPublisher().sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] result in
self?.isLoading = false
self?.emails = result
self?.hasLoadedInitialEmails = true
}
)
.store(in: &subscriptions)
}
}
struct NewsletterEmailsView: View {
@EnvironmentObject var dataService: DataService
@ObservedObject var viewModel = NewsletterEmailsViewModel()
let footerText = "Add PDFs to your library, or subscribe to emails using an Omnivore email address."
var body: some View {
Group {
#if os(iOS)
Form {
innerBody
}
#elseif os(macOS)
List {
innerBody
}
.listStyle(InsetListStyle())
#endif
}
.onAppear { viewModel.loadEmails(dataService: dataService) }
}
private var innerBody: some View {
Group {
Section(footer: Text(footerText)) {
Button(
action: {
withAnimation {
print("create email")
}
},
label: {
HStack {
Image(systemName: "plus.circle.fill").foregroundColor(.green)
Text("Create a new email address")
Spacer()
}
}
)
.disabled(viewModel.isLoading)
}
if !viewModel.emails.isEmpty {
Section(header: Text("Existing Emails (Tap to copy)")) {
ForEach(viewModel.emails) { newsletterEmail in
Button(
action: {},
label: { Text(newsletterEmail.email) }
)
}
}
}
}
.navigationTitle("Emails")
}
}

View File

@ -55,7 +55,7 @@ struct ProfileView: View {
}
Section {
NavigationLink(destination: EmailsView()) {
NavigationLink(destination: NewsletterEmailsView()) {
Text("Emails")
}
}

View File

@ -1,12 +1,13 @@
import Foundation
public struct NewsletterEmail {
public let id: String
public struct NewsletterEmail: Identifiable {
public let id = UUID()
public let emailId: String
public let email: String
public let confirmationCode: String?
public init(id: String, email: String, confirmationCode: String?) {
self.id = id
public init(emailId: String, email: String, confirmationCode: String?) {
self.emailId = emailId
self.email = email
self.confirmationCode = confirmationCode
}

View File

@ -12,7 +12,7 @@ public extension DataService {
let newsletterEmailSelection = Selection.NewsletterEmail {
NewsletterEmail(
id: try $0.id(),
emailId: try $0.id(),
email: try $0.address(),
confirmationCode: try $0.confirmationCode()
)