church-core/swift_usage_example.swift
RTSDA 4d6b23beb3
Some checks are pending
iOS UniFFI Build / build-ios (push) Waiting to run
Initial commit: Church Core Rust library
Add church management API library with cross-platform support for iOS, Android, and WASM.
Features include event management, bulletin handling, contact forms, and authentication.
2025-08-16 19:25:01 -04:00

138 lines
4.6 KiB
Swift

// Swift Usage Example for New Config Functions
// This shows how to use the new RTSDA-compliant config functions in iOS
import Foundation
// WRONG - Old way (violates RTSDA architecture)
/*
private func loadChurchConfig() {
let configJson = fetchConfigJson() // Gets raw JSON from Rust
// THIS IS BUSINESS LOGIC AND SHOULD BE IN RUST!
if let configData = configJson.data(using: .utf8),
let config = try? JSONDecoder().decode(ChurchConfig.self, from: configData) {
self.churchConfig = config
} else {
print("Failed to load church config")
}
}
*/
// RIGHT - New way (follows RTSDA architecture)
class ChurchConfigManager: ObservableObject {
@Published var churchName: String = ""
@Published var contactPhone: String = ""
@Published var contactEmail: String = ""
@Published var brandColor: String = ""
@Published var aboutText: String = ""
@Published var donationUrl: String = ""
@Published var churchAddress: String = ""
@Published var coordinates: [Double] = [0.0, 0.0]
@Published var websiteUrl: String = ""
@Published var facebookUrl: String = ""
@Published var youtubeUrl: String = ""
@Published var instagramUrl: String = ""
@Published var missionStatement: String = ""
func loadConfig() {
// ALL business logic (JSON parsing, error handling, fallbacks) is in Rust
// Swift just calls Rust functions and gets parsed values directly
self.churchName = getChurchName()
self.contactPhone = getContactPhone()
self.contactEmail = getContactEmail()
self.brandColor = getBrandColor()
self.aboutText = getAboutText()
self.donationUrl = getDonationUrl()
self.churchAddress = getChurchAddress()
self.coordinates = getCoordinates()
self.websiteUrl = getWebsiteUrl()
self.facebookUrl = getFacebookUrl()
self.youtubeUrl = getYoutubeUrl()
self.instagramUrl = getInstagramUrl()
self.missionStatement = getMissionStatement()
}
}
// Example SwiftUI View
struct ConfigDisplayView: View {
@StateObject private var configManager = ChurchConfigManager()
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(configManager.churchName)
.font(.title)
.foregroundColor(Color(hex: configManager.brandColor))
Text(configManager.aboutText)
.font(.body)
Text("Contact: \(configManager.contactPhone)")
Text("Email: \(configManager.contactEmail)")
Text("Address: \(configManager.churchAddress)")
if !configManager.donationUrl.isEmpty {
Link("Donate", destination: URL(string: configManager.donationUrl)!)
.foregroundColor(Color(hex: configManager.brandColor))
}
}
.padding()
.onAppear {
configManager.loadConfig()
}
}
}
extension Color {
init(hex: String) {
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int: UInt64 = 0
Scanner(string: hex).scanHexInt64(&int)
let a, r, g, b: UInt64
switch hex.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (1, 1, 1, 0)
}
self.init(
.sRGB,
red: Double(r) / 255,
green: Double(g) / 255,
blue: Double(b) / 255,
opacity: Double(a) / 255
)
}
}
/*
Benefits of this approach:
1. RTSDA Compliant - ALL business logic in Rust
2. No JSON parsing in Swift - Rust handles it all
3. Proper error handling with fallbacks in Rust
4. Consistent behavior across iOS and Android
5. Easy to test - just call Rust functions
6. No model synchronization issues
7. Automatic fallback values if config fails to load
8. Cleaner Swift code - just UI logic
Functions available:
- getChurchName() -> String
- getContactPhone() -> String
- getContactEmail() -> String
- getBrandColor() -> String
- getAboutText() -> String
- getDonationUrl() -> String
- getChurchAddress() -> String
- getCoordinates() -> [Double] // [latitude, longitude]
- getWebsiteUrl() -> String
- getFacebookUrl() -> String
- getYoutubeUrl() -> String
- getInstagramUrl() -> String
- getMissionStatement() -> String
*/