Coder Social home page Coder Social logo

dean151 / rulekit Goto Github PK

View Code? Open in Web Editor NEW
48.0 3.0 1.0 84 KB

TipKit style API to call a closure, or trigger a NSNotification based on events and condition.

License: MIT License

Swift 100.00%
swift ios ipados macos tvos watchos notificationcenter swift-package-manager xcode tipkit

rulekit's Introduction

RuleKit

TipKit style API to trigger an arbitrary closure, or a NSNotification based on events and condition.

Usecases

  • To open your Paywall from time to time
  • To prompt your user to add an App Store review
  • Watching and sending achievements to Game Center with GameKit
  • Show a shortcut available for an action that is often performed by a user
  • ...

Requirements

  • Swift 5.9+ (Xcode 15+)
  • iOS 14+, iPadOS 14+, tvOS 14+, watchOS 7+, macOS 11+

Installation

Install using Swift Package Manager

dependencies: [
    .package(url: "https://github.com/Dean151/RuleKit.git", from: "0.5.0"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "RuleKit", package: "RuleKit"),
    ]),
]

And import it:

import RuleKit

How to use?

RuleKit is about one thing: invoking a closure, or trigger a NSNotification when a set of rules are fulfilled!

  • Configure RuleKit when your application starts
try RuleKit.configure(storeLocation: .applicationDefault)
  • Create "events" to trigger RuleKit
extension RuleKit.Event {
    public static let appStarted: Self = "appStarted"
    public static let entityCreated: Self = "itemCreated"
    public static let promptAttempt: Self = "promptAttempt"
}
  • Create a custom notification to be triggered by RuleKit
import Foundation

extension Notification.Name {
    static let requestReviewPrompt = Notification.Name("RequestReviewPrompt")
}
  • Implement your logic of when your custom notification is triggered
import StoreKit
import SwiftUI

struct ContentView: View {
    @Environment(\.requestReview)
    private var requestReview

    var body: View {
        Text("Hello, World!")
            .onReceive(NotificationCenter.default.publisher(for: .requestReviewPrompt)) { _ in
                requestReview()
                RuleKit.Event.promptAttempt.sendDonation()
            }
    }
}
  • Register your business rules that should trigger your closure, or your notification
RuleKit.setRule(
    triggering: requestReviewNotification, 
    options: [.triggerFrequency(.monthly)], 
    .allOf([
        .event(.promptAttempt) {
            $0.donations.last?.version != .current
        },
        .anyOf([
            .event(.entityCreated) { _ in
                MyStore.shared.entityCount >= 5
            },
            .allOf([
                .event(.appStarted) {
                    $0.donations.count >= 3
                },
                .event(.entityCreated) { _ in
                    MyStore.shared.entityCount >= 3
                }
            ])
        ])
    ])
)
  • Donate those events at proper places in your app
// Asynchronously
RuleKit.Event.appStarted.sendDonation()
// Synchronously
await RuleKit.Event.entityCreated.donate()
  • As soon as an event is donated, if all the rules are fulfilled, the notification will be sent
  • If required, reset an event donations to zero:
// Asynchronously
RuleKit.Event.appStarted.resetDonations()
// Synchronously
await RuleKit.Event.appStarted.reset()

Available stores:

  • .applicationDefault: Will use the default Document folder of your app
  • .groupContainer(identifier: String): Will store your event donations in the shared AppGroup container
  • .url(URL): Provide your own URL. It should be a directory URL.

Available options:

  • .triggerFrequency(_): Throttle down notification donation or using given period
  • .dispatchQueue(_): Choose the DispatchQueue you want your notification to be sent from. Defaults to main queue.
  • .delay(for: _) and .delay(nanoseconds: _): Delay the trigger of a specific notification after it was fulfilled.

Event.Donations properties available in the condition closure:

  • count: the number of times an event have been donated
  • first and last: the first and last retrieved donation (date + version)

Contribute

You are encouraged to contribute to this repository, by opening issues, or pull requests for bug fixes, improvement requests, or support. Suggestions for contributing:

  • Improving documentation
  • Adding some automated tests ๐Ÿ˜œ
  • Adding some new rules, options or properties for more use cases

rulekit's People

Contributors

dean151 avatar divadretlaw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mumer92

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.