LRStreakKit is a lightweight Swift library designed to integrate streak tracking in iOS applications written in SwiftUI. This library allows you to implement daily streaks in your app with an easy to use API. It has a built in view component along with customisations and different persistence mechanisms.
LRStreakKit can be seen in use in Science Makes Sense
The library can be be added to your project using Swift Package Manager.
Either add the URL to your project by going to File -> Add Package Dependencies and entering:
https://github.com/lukerobertsapps/LRStreakKit.git
or add it to your Package.swift
dependencies: [
.package(url: "https://github.com/lukerobertsapps/LRStreakKit.git", .upToNextMajor(from: "1.0.2"))
]
- iOS 15 | Xcode 13+
To get started, call setupStreak()
in the entry point of your application. This will inject an instance of StreakManager
into the SwiftUI environment:
ContentView()
.setupStreak()
When you want the user to update their streak, access the StreakManager
from the environment and call updateStreak()
. This operation may happen when the user first opens the app or when they complete a major action in the app.
@EnvironmentObject var streak: StreakManager
Button("Check in") {
streak.updateStreak()
}
Finally, display the streak using the built in StreakView
:
StreakView()
The library can be customised in a few ways to help it fit into your application.
By default, the streak data is stored in UserDefaults under the "DailyStreak"
key. To change the key, update the setupStreak()
method:
ContentView()
.setupStreak(key: "CustomPersistenceKey")
You can also change the technology if UserDefaults isn't your cup of tea:
ContentView()
.setupStreak(persistence: .documentsDirectory)
Shared UserDefaults is also supported. Just fill in your app group ID
ContentView()
.setupStreak(persistence: .sharedUserDefaults(appGroup: "group.example.com"))
If one of the pre-determined persistence options doesn't work for you, you can make your own by conforming to the StreakPersistence
protocol. Just handle saving and retrieving data:
class MyCustomPersistence: StreakPersistence {
func getData() -> Data? {
//
}
func save(data: Data) throws {
//
}
}
And pass an instance into the setup method:
ContentView()
.setupStreak(persistence: .custom(MyCustomPersistence()))
You can display the user's streak in a number of ways.
By default, you can get a great looking view of the streak by using the built in streak view:
StreakView()
You can customise it using a few different options:
StreakView(
streakColor: .white,
noStreakColor: .gray,
backgroundColor: .black,
animateOnAppear: true,
font: .system(size: 16, weight: .bold, design: .rounded),
imageHeight: 24
)
If you want something completely custom, use the StreakManager
injected into the environment along with a few different properties.
Access the streak manager:
@EnvironmentObject private var streak: StreakManager
Display the current day count:
streak.getStreakLength()
Determine whether the streak has been completed today:
streak.hasCompletedStreak()
A full example:
struct MyCustomStreakView: View {
@EnvironmentObject private var streak: StreakManager
var body: some View {
VStack {
Text("\(streak.getStreakLength())")
}
.foregroundStyle(streak.hasCompletedStreak() ? .blue : .gray)
}
}