Last active
July 31, 2018 07:41
-
-
Save gokselkoksal/4ab590f24305e072a547af46d81c056e to your computer and use it in GitHub Desktop.
Using Channels #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Theme: String { | |
case light, dark | |
} | |
class UserSettings { | |
enum Message { | |
case didUpdateTheme(Theme) | |
} | |
static let shared = UserSettings() | |
let channel = Channel<Message>() | |
var theme: Theme = .light { | |
didSet { | |
channel.broadcast(.didUpdateTheme(theme)) | |
} | |
} | |
init() { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeViewController: UIViewController { | |
let userSettings = UserSettings.shared | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
subscribe() | |
} | |
func subscribe() { | |
userSettings.channel.subscribe(self) { (message) in | |
switch message { | |
case .didUpdateTheme(let theme): | |
// Apply theme here. | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks for this.
Can you show how to unsubscribe properly? And how to use the
self
argument passed into subscribe?