diff --git a/PortfolioJournal/Services/ShareService.swift b/PortfolioJournal/Services/ShareService.swift index 6671c40..5859f3e 100644 --- a/PortfolioJournal/Services/ShareService.swift +++ b/PortfolioJournal/Services/ShareService.swift @@ -6,6 +6,29 @@ class ShareService { private init() {} + func shareText(_ content: String) { + guard let viewController = ShareService.topViewController() else { return } + + let activityVC = UIActivityViewController( + activityItems: [content], + applicationActivities: nil + ) + + if let popover = activityVC.popoverPresentationController { + popover.sourceView = viewController.view + popover.sourceRect = CGRect( + x: viewController.view.bounds.midX, + y: viewController.view.bounds.midY, + width: 0, + height: 0 + ) + } + + DispatchQueue.main.async { + viewController.present(activityVC, animated: true) + } + } + func shareTextFile(content: String, fileName: String) { guard let viewController = ShareService.topViewController() else { return } diff --git a/PortfolioJournal/Views/Dashboard/MonthlyCheckInView.swift b/PortfolioJournal/Views/Dashboard/MonthlyCheckInView.swift index d4db3a5..9b2755c 100644 --- a/PortfolioJournal/Views/Dashboard/MonthlyCheckInView.swift +++ b/PortfolioJournal/Views/Dashboard/MonthlyCheckInView.swift @@ -73,6 +73,15 @@ struct MonthlyCheckInView: View { } .navigationTitle("Monthly Check-in") .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + Button { + shareMonthlyCheckIn() + } label: { + Image(systemName: "square.and.arrow.up") + } + } + } .onAppear { viewModel.selectedAccount = accountStore.selectedAccount viewModel.showAllAccounts = accountStore.showAllAccounts @@ -444,6 +453,30 @@ struct MonthlyCheckInView: View { .cornerRadius(AppConstants.UI.cornerRadius) .shadow(color: .black.opacity(0.05), radius: 8, y: 2) } + + private func shareMonthlyCheckIn() { + let summary = viewModel.monthlySummary + let text = """ + \(summary.periodLabel) Check-in + Starting: \(summary.formattedStartingValue) + Ending: \(summary.formattedEndingValue) + Contributions: \(summary.formattedContributions) + Net performance: \(summary.formattedNetPerformance) (\(summary.formattedNetPerformancePercentage)) + + Shared from \(appDisplayName) + """ + ShareService.shared.shareText(text) + } + + private var appDisplayName: String { + if let name = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String { + return name + } + if let name = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String { + return name + } + return "Portfolio Journal" + } } struct AchievementsView: View {