Add Firebase Firestore support to a Swift application through SwiftfulGamification framework.
See documentation in the parent repo: https://github.com/SwiftfulThinking/SwiftfulGamification
// Streaks
#if DEBUG
let streakManager = StreakManager(services: MockStreakServices(), configuration: StreakConfiguration.mockDefault())
#else
let streakManager = StreakManager(services: FirebaseStreakServices(), configuration: StreakConfiguration(streakKey: "daily"))
#endif
// Experience Points
#if DEBUG
let xpManager = ExperiencePointsManager(services: MockExperiencePointsServices(), configuration: ExperiencePointsConfiguration.mockDefault())
#else
let xpManager = ExperiencePointsManager(services: FirebaseExperiencePointsServices(), configuration: ExperiencePointsConfiguration(experienceKey: "general"))
#endif
// Progress
#if DEBUG
let progressManager = ProgressManager(services: MockProgressServices(), configuration: ProgressConfiguration.mockDefault())
#else
let progressManager = ProgressManager(services: FirebaseProgressServices(), configuration: ProgressConfiguration(progressKey: "general"))
#endif// Streaks
try await streakManager.logIn(userId: userId)
try await streakManager.addStreakEvent()
try await streakManager.addStreakFreeze(id: "freeze_id", expiresAt: Date())
streakManager.currentStreakData.currentStreak
streakManager.logOut()
// Experience Points
try await xpManager.logIn(userId: userId)
try await xpManager.addExperiencePoints(points: 100)
xpManager.currentExperiencePointsData.pointsAllTime
xpManager.logOut()
// Progress
try await progressManager.logIn(userId: userId)
try await progressManager.addProgress(id: "item_id", value: 0.5)
progressManager.getProgress(id: "item_id")
await progressManager.logOut()Details (Click to expand)
The ServerCalculations/ folder contains Firebase Cloud Functions for server-side streak and XP calculations.
# Copy functions to your Firebase project
cp -r ServerCalculations/calculateStreak.ts your-firebase-project/functions/src/
cp -r ServerCalculations/calculateExperiencePoints.ts your-firebase-project/functions/src/
# Deploy to Firebase
cd your-firebase-project
firebase deploy --only functions// Streaks with server calculation
let streakService = FirebaseRemoteStreakService(
rootCollectionName: "swiftful_streaks",
calculateStreakCloudFunctionName: "calculateStreak" // Add function name
)
let streakManager = StreakManager(
services: FirebaseStreakServices(remote: streakService, local: FileManagerStreakPersistence()),
configuration: StreakConfiguration(
streakKey: "daily",
eventsRequiredPerDay: 1,
useServerCalculation: true, // Enable server calculation
leewayHours: 4,
freezeBehavior: .autoConsumeFreezes
)
)
// Experience Points with server calculation
let xpService = FirebaseRemoteExperiencePointsService(
rootCollectionName: "swiftful_experience_points",
calculateExperiencePointsCloudFunctionName: "calculateExperiencePoints"
)
let xpManager = ExperiencePointsManager(
services: FirebaseExperiencePointsServices(remote: xpService, local: FileManagerExperiencePointsPersistence()),
configuration: ExperiencePointsConfiguration(
experienceKey: "general",
useServerCalculation: true // Enable server calculation
)
)Note: Server calculation is optional. By default, calculations run client-side for offline support and faster updates.
Details (Click to expand)
Firebase docs: https://firebase.google.com/docs/firestore
- Firebase Console -> Build -> Firestore Database
Parent repo: https://github.com/SwiftfulThinking/SwiftfulGamification