diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/EmailsView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/EmailsView.swift deleted file mode 100644 index 4036e1706..000000000 --- a/apple/OmnivoreKit/Sources/App/Views/Profile/EmailsView.swift +++ /dev/null @@ -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") - } -} diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift new file mode 100644 index 000000000..3f326d949 --- /dev/null +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/NewsletterEmailsView.swift @@ -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() + + 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") + } +} diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift index b81554cb9..332e732b4 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift @@ -55,7 +55,7 @@ struct ProfileView: View { } Section { - NavigationLink(destination: EmailsView()) { + NavigationLink(destination: NewsletterEmailsView()) { Text("Emails") } } diff --git a/apple/OmnivoreKit/Sources/Models/NewsletterEmail.swift b/apple/OmnivoreKit/Sources/Models/NewsletterEmail.swift index 8bd467055..ee4270668 100644 --- a/apple/OmnivoreKit/Sources/Models/NewsletterEmail.swift +++ b/apple/OmnivoreKit/Sources/Models/NewsletterEmail.swift @@ -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 } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift index af59ef863..25ccea69f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/NewsletterEmailsQuery.swift @@ -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() )