Skip to content

Wuchiwei/LKBroadcaster

Repository files navigation

LKBroadcaster

Swift Generic Observer Pattern inspired by iOS Notification Center API.

Design Explaination

You can review design thinking in Design Explaination Document.

Installation

Supprot Swift Package Manager installation.

Usage

Step 1. Create your custom notification through LKAnyNotification.

struct Movie {
    let name: String
}

struct LKNotificationFactory {
    static let addLongMovieToFavorate = LKAnyNotification<Movie>(name: "addLongMovieToFavorate")
}

Step 2: Observe to LKBroadcaster and store the return AnyCancellable object in Set or Array.

import UIKit
import Combine
import LKBroadcaster

class ViewController: UIViewController {

    var cancelled: Set<AnyCancellable> = Set()

    var movie: Movie?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        LKBroadcaster.observe(
            notification: LKNotificationFactory.addLongMovieToFavorate,
            queue: DispatchQueue.global(),
            { [weak self] movie in
                guard let self = self else { return }
                self.movie = movie
                print(self.movie!, Thread.current)
            }
        ).store(in: &cancelled)
    }
}

Step 3: Post Notification through LKBroadscater

LKBroadcaster.post(
    notification: LKNotificationFactory.addLongMovieToFavorate, 
    content: Movie(name: "Nice Movie")
)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages