Glimmer / app · glimmertoday.ai / native / Glimmer / SendToInkspring.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// A small button under today's quote that saves it as a post idea in Inkspring. |
| 4 | /// It only shows when Inkspring is installed, and checks again whenever Glimmer comes forward. |
| 5 | struct SendToInkspringButton: View { |
| 6 | let quote: DailyQuote |
| 7 | @Environment(\.scenePhase) private var phase |
| 8 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 9 | @State private var installed = false |
| 10 | @State private var sentQuoteID: String? |
| 11 | @State private var launches = 0 |
| 12 | |
| 13 | private var sent: Bool { sentQuoteID == quote.id } |
| 14 | |
| 15 | var body: some View { |
| 16 | Group { |
| 17 | if installed, let url = InkspringLink.newIdea(quote: quote.text, author: quote.author) { |
| 18 | Button { |
| 19 | InkspringLink.open(url) |
| 20 | launches += 1 |
| 21 | withAnimation(reduceMotion ? nil : .spring(duration: 0.35)) { sentQuoteID = quote.id } |
| 22 | } label: { |
| 23 | Label { |
| 24 | Text(sent ? "Sent to Inkspring" : "Send to Inkspring") |
| 25 | } icon: { |
| 26 | Image(systemName: sent ? "checkmark.circle.fill" : "paperplane.fill") |
| 27 | .symbolRenderingMode(.monochrome) |
| 28 | .fontWeight(.medium) |
| 29 | .foregroundStyle(GlimmerColor.accent) |
| 30 | .symbolEffect(.bounce.up, options: .nonRepeating, value: launches) |
| 31 | .contentTransition(.symbolEffect(.replace)) |
| 32 | .accessibilityHidden(true) |
| 33 | } |
| 34 | } |
| 35 | .glimmerButton() |
| 36 | .controlSize(.small) |
| 37 | .font(.callout) |
| 38 | .help("Save today’s quote as a post idea in Inkspring") |
| 39 | .accessibilityHint("Opens Inkspring with this quote as a new post idea") |
| 40 | .accessibilityIdentifier("today.quote.sendToInkspring") |
| 41 | .padding(.top, 8) |
| 42 | .transition(.opacity) |
| 43 | } |
| 44 | } |
| 45 | .onAppear(perform: refresh) |
| 46 | .onChange(of: phase) { _, newPhase in if newPhase == .active { refresh() } } |
| 47 | #if os(macOS) |
| 48 | .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in refresh() } |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | private func refresh() { installed = InkspringLink.isInstalled } |
| 53 | } |