Skip to content

Seamlessly implement daily streak tracking into SwiftUI apps

License

Notifications You must be signed in to change notification settings

lukerobertsapps/LRStreakKit

Repository files navigation

🔥 LRStreakKit

Swift Package Manager

LRStreakKit

Table of Contents

Overview

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

Installation

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"))
]

Requirements

  • iOS 15 | Xcode 13+

Basic Usage

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()

Customisation

The library can be customised in a few ways to help it fit into your application.

Persistence

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"))

Custom Persistence

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()))

Appearance

You can display the user's streak in a number of ways.

StreakView

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
)

Make Your Own

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)
    }
}

About

Seamlessly implement daily streak tracking into SwiftUI apps

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages