Parts one and two have covered the InterceptKeys class which exposes a feed of raw keyboard events and the MessageProvider which turns key presses into messages to display on the screen. There is a class between those two, the KeyProvider which I am not going to actually blog about as it doesn’t introduce any new concepts which I have not covered in parts 1 and 2 of this blog series.
Instead we will dive into the MessageController class which brings the streams from the KeyProvider and MessageProvider together then maintains the list of Messages displayed on the screen. The requirements are:
- Add any new messages into the messages collection
- After 5 seconds set a flag on the message which triggers an animation
- 1 second after that the message is removed from the messages collection
- If a message is updated the 5 second countdown should start again.
The previous implementation was quite simple but it was hard to test. Also Rx can solve this problem very nicely, so why not.
- New messages were added to the messages list
- A timer fired once a second, the callback did:
- A foreach loop over every item which had a LastUpdated property more than 5 seconds go, then set the IsDeleting flag to true
- A foreach loop over every item which had a LastUpdated property more than 6 seconds go, then removed it from the list
Lets rewrite this into Rx