Code snippets for the blog post "Let me tell you a storey" at http://www.apokrupto.com/blog-1/2016/10/22/mvc-rrosrws
Last active
November 5, 2016 15:59
-
-
Save warren-gavin/3750ff851844812ee02b4ad4a9b40a89 to your computer and use it in GitHub Desktop.
Code snippets for the blog post on MVC-RS
This file contains hidden or 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
| protocol HeartRateStoring { | |
| var source: HeartRateDataSource? { get } | |
| var sink: HeartRateDataSink? { get } | |
| } | |
| struct HeartRateStore: HeartRateStoring { | |
| let source: HeartRateDataSource? | |
| let sink: HeartRateDataSink? | |
| } |
This file contains hidden or 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
| let backend = HeartRateBackend() | |
| let rwStore = HeartRateStore(source: backend, sink: backend) |
This file contains hidden or 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
| struct HeartRateSensor: HeartRateDataSource { | |
| func read(at: Int) -> HeartRate { | |
| // ... | |
| } | |
| } | |
| struct HeartRateBackend: HeartRateDataSource, HeartRateDataSink { | |
| func read(at: Int) -> HeartRate { | |
| // ... | |
| } | |
| func write(heartRate: HeartRate) { | |
| } | |
| } |
This file contains hidden or 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
| let sensor = HeartRateSensor() | |
| let backend = HeartRateBackend() | |
| let liveDataViewModel = ViewModel(store: HeartRateStore(source: sensor, sink: backend)) | |
| let historyViewModel = ViewModel(store: HeartRateStore(source: backend, sink: nil)) |
This file contains hidden or 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
| protocol HeartRateDataSource { | |
| func read(at: Int) -> HeartRate | |
| } | |
| protocol HeartRateDataSink { | |
| func write(heartRate: HeartRate) | |
| } |
This file contains hidden or 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
| struct HeartRate { | |
| var value: Int | |
| } | |
| protocol HeartRateStoring { | |
| func read(at: Int) -> HeartRate | |
| func write(heartRate: HeartRate) | |
| } | |
| struct HeartRateStore: HeartRateStoring { | |
| func read(at: Int) -> HeartRate { | |
| // returns a HeartRate | |
| } | |
| func write(heartRate: HeartRate) { | |
| //... | |
| } | |
| } |
This file contains hidden or 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 ViewController: UIViewController { | |
| private let viewModel: HeartRateViewModelling | |
| init(viewModel: HeartRateViewModelling) { | |
| self.viewModel = viewModel | |
| } | |
| } | |
| // Somewhere else i.e. in the Router/Coordinator | |
| let vc = ViewController(viewModel: ViewModel(store: HeartRateStore())) |
This file contains hidden or 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 ViewController: UIViewController { | |
| var viewModel: HeartRateViewModelling! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| viewMode = ViewModel(store: HeartRateStore()) | |
| } | |
| } |
This file contains hidden or 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
| protocol HeartRateViewModelling { | |
| var store: HeartRateStoring { get } | |
| } | |
| struct ViewModel: HeartRateViewModelling { | |
| let store: HeartRateStoring | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment