import SwiftUI import StoreKit // Docley for iPhone and iPad. Search and doctor pages are native, built on the site's JSON API. // Signing in, rating, claiming and replying open the website in an in-app browser, so there is one // set of rules and one account system. Mac users use the website. @main struct DocleyApp: App { var body: some Scene { WindowGroup { TabView { NavigationStack { SearchView() } .tabItem { Label("Search", systemImage: "magnifyingglass") } NavigationStack { SettingsView() } .tabItem { Label("Settings", systemImage: "gearshape") } } .tint(Theme.accent) .preferredColorScheme(.dark) .onAppear { RatingPrompt.countLaunch() } } } } enum Theme { static let accent = Color(red: 0.96, green: 0.50, blue: 0.15) // #F58025 static let panel = Color(white: 0.07) } enum Site { static let base = URL(string: "https://doctors.projectatom.ai")! static func url(_ path: String) -> URL { URL(string: path, relativeTo: base)!.absoluteURL } } // MARK: - API struct DoctorSummary: Codable, Identifiable, Hashable { let npi: String let name: String let credential: String? let specialty: String? let city: String? let state: String? var id: String { npi } var title: String { credential.map { "\(name), \($0)" } ?? name } } struct PublicRecord: Codable, Identifiable, Hashable { let id: String let source: String let kind: String let case_status: String? let action_date: String? let summary: String let url: String let ended: String? let reply: String? } struct Score: Codable, Hashable { let count: Int; let score: Double? } struct Comment: Codable, Identifiable, Hashable { let id: String let kind: String let alias: String? let comment: String let updated: String? let avg: Double let reply: String? } struct Doctor: Codable, Hashable { let npi: String let name: String let credential: String? let specialty: String? let address: String? let city: String? let state: String? let zip: String? let phone: String? let records: [PublicRecord] let scores: [String: Score] let comments: [Comment] } struct SearchResponse: Codable { let page: Int; let more: Bool; let results: [DoctorSummary] } enum API { static func search(q: String, state: String, page: Int) async throws -> SearchResponse { var c = URLComponents(url: Site.url("/api/search"), resolvingAgainstBaseURL: false)! c.queryItems = [.init(name: "q", value: q), .init(name: "state", value: state), .init(name: "page", value: String(page))] let (data, _) = try await URLSession.shared.data(from: c.url!) return try JSONDecoder().decode(SearchResponse.self, from: data) } static func doctor(_ npi: String) async throws -> Doctor { let (data, _) = try await URLSession.shared.data(from: Site.url("/api/doctor/\(npi)")) return try JSONDecoder().decode(Doctor.self, from: data) } } // MARK: - Search struct SearchView: View { @State private var query = "" @State private var state = "" @State private var results: [DoctorSummary] = [] @State private var page = 1 @State private var more = false @State private var loading = false @State private var error: String? var body: some View { List { Section { Picker("State", selection: $state) { Text("All states").tag("") ForEach(States.all, id: \.0) { Text($0.1).tag($0.0) } } .onChange(of: state) { Task { await run(reset: true) } } } if let error { Text(error).foregroundStyle(.secondary) } ForEach(results) { d in NavigationLink(value: d) { VStack(alignment: .leading, spacing: 2) { Text(d.title).font(.headline) Text("\(d.specialty ?? "Physician") · \(d.city ?? ""), \(d.state ?? "")") .font(.subheadline).foregroundStyle(.secondary) } .padding(.vertical, 2) } } if more { Button(loading ? "Loading…" : "Show more") { Task { await run(reset: false) } }.disabled(loading) } if results.isEmpty && error == nil && !loading { Text("Search every licensed doctor in America by name, specialty or city.") .foregroundStyle(.secondary) } } .navigationTitle("Docley") .navigationDestination(for: DoctorSummary.self) { DoctorView(npi: $0.npi, title: $0.title) } .searchable(text: $query, prompt: "Name, specialty or city") .onSubmit(of: .search) { Task { await run(reset: true) } } } func run(reset: Bool) async { guard !query.isEmpty || !state.isEmpty else { results = []; more = false; return } loading = true; defer { loading = false } let next = reset ? 1 : page + 1 do { let r = try await API.search(q: query, state: state, page: next) results = reset ? r.results : results + r.results page = next; more = r.more; error = r.results.isEmpty && reset ? "No doctors found. Try fewer words or another state." : nil } catch { self.error = "We couldn't reach Docley. Check your connection and try again." } } } // MARK: - Doctor struct DoctorView: View { let npi: String let title: String @State private var doctor: Doctor? @State private var web: URL? @State private var failed = false var body: some View { List { if let d = doctor { Section { VStack(alignment: .leading, spacing: 4) { Text(title).font(.title2.bold()) Text("\(d.specialty ?? "Physician") · \(d.city ?? ""), \(d.state ?? "")").foregroundStyle(.secondary) } } Section("Public record") { if d.records.isEmpty { Text("No federal exclusions found in the HHS Office of Inspector General list. State board and malpractice court records are being added state by state.") .font(.subheadline).foregroundStyle(.secondary) } ForEach(d.records) { r in RecordRow(record: r) } } Section("Ratings") { ScoreRow(label: "Patients", score: d.scores["patient"]) ScoreRow(label: "Colleagues", score: d.scores["colleague"]) Button { web = Site.url("/doctor/\(d.npi)/rate") } label: { Text("Rate this doctor").frame(maxWidth: .infinity).fontWeight(.semibold) } .buttonStyle(.borderedProminent).buttonBorderShape(.capsule) .listRowBackground(Color.clear) } if !d.comments.isEmpty { Section("What people say") { ForEach(d.comments) { c in VStack(alignment: .leading, spacing: 6) { Text("\(c.alias ?? "Verified person") · \(c.kind == "colleague" ? "Colleague" : "Patient") · \(String(format: "%.1f", c.avg)) of 5") .font(.caption).foregroundStyle(.secondary) Text(c.comment) if let reply = c.reply { Text("The doctor's reply: \(reply)").font(.subheadline) .padding(.leading, 10).overlay(alignment: .leading) { Rectangle().fill(Theme.accent).frame(width: 2) } } } } } } Section("Practice") { if let a = d.address { LabeledContent("Address", value: "\(a), \(d.city ?? ""), \(d.state ?? "") \(d.zip ?? "")") } if let p = d.phone, let tel = URL(string: "tel:" + p.filter(\.isNumber)) { Link(p, destination: tel) } LabeledContent("NPI", value: d.npi) } Section { Button("Are you this doctor? Claim your page") { web = Site.url("/doctor/\(d.npi)/claim") } Link("Open on the web", destination: Site.url("/doctor/\(d.npi)")) } } else if failed { Text("We couldn't load this doctor. Pull to try again.").foregroundStyle(.secondary) } else { ProgressView().frame(maxWidth: .infinity) } } .navigationTitle(doctor == nil ? "" : "Doctor") .navigationBarTitleDisplayMode(.inline) .task { await load() } .refreshable { await load() } .sheet(item: $web, onDismiss: { Task { await load() } }) { url in WebSheet(url: url) } } func load() async { do { doctor = try await API.doctor(npi); failed = false } catch { failed = true } } } struct RecordRow: View { let record: PublicRecord static let kinds = ["exclusion": "Federal exclusion", "license_action": "License action", "malpractice_case": "Malpractice case"] static let statuses = ["filed": "Filed, not yet decided", "settled": "Settled", "dismissed": "Dismissed", "judgment_for_patient": "Judgment for the patient", "judgment_for_doctor": "Judgment for the doctor"] var body: some View { VStack(alignment: .leading, spacing: 6) { Text([Self.kinds[record.kind] ?? record.kind, record.case_status.flatMap { Self.statuses[$0] }, record.action_date].compactMap { $0 }.joined(separator: " · ")) .font(.headline).foregroundStyle(Theme.accent) Text(record.summary) if let u = URL(string: record.url) { Link("Source: \(record.source)", destination: u).font(.subheadline) } if let reply = record.reply { Text("The doctor's reply: \(reply)").font(.subheadline).foregroundStyle(.secondary) } } .padding(.vertical, 4) } } struct ScoreRow: View { let label: String let score: Score? var body: some View { LabeledContent(label) { if let s = score?.score { Text(String(format: "%.1f of 5 · %d ratings", s, score?.count ?? 0)).bold() } else if let n = score?.count, n > 0 { Text("\(n) of 5 ratings needed") } else { Text("No ratings yet") } } } } extension URL: @retroactive Identifiable { public var id: String { absoluteString } } // MARK: - Settings (house standard: rating row, free support, no tips, no nag screens) struct SettingsView: View { @Environment(\.requestReview) private var requestReview var body: some View { Form { Section { Button("Rate Docley") { requestReview() } Link("Send feedback", destination: URL(string: "https://projectatom.ai/apps?app=docley#feedback")!) Link("Ask ATOM", destination: URL(string: "https://projectatom.ai")!) ShareLink("Share Docley", item: Site.base) } header: { Text("Support Docley") } footer: { Text("Docley is free for everyone. Support is free too.") } Section("About") { LabeledContent("Version", value: "\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "") (\(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? ""))") Link("Rating rules", destination: Site.url("/rules")) Link("Privacy", destination: URL(string: "https://projectatom.ai/privacy")!) Link("Source code (public domain, Unlicense)", destination: URL(string: "https://github.com/projectatomai/docley")!) } Section { Link(destination: URL(string: "https://projectatom.ai")!) { Text("A Project ATOM project").font(.subheadline.weight(.semibold)) .padding(.horizontal, 18).padding(.vertical, 10) .overlay(Capsule().stroke(Theme.accent)) } .frame(maxWidth: .infinity) } .listRowBackground(Color.clear) } .navigationTitle("Settings") } } /// The only automatic prompt: Apple's rating request, after a few launches. enum RatingPrompt { static func countLaunch() { let n = UserDefaults.standard.integer(forKey: "launches") + 1 UserDefaults.standard.set(n, forKey: "launches") guard n == 5, let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return } AppStore.requestReview(in: scene) } }