I've written a very simple ReactiveCocoa test application to try my hand at coding in RAC (rather than just reading about it endlessly). It's on Github, and I wanted to get some specific questions about it answered. I'll link to the code components as I go along.
First, a brief explanation of the application: it's a timer-driven iteration counter that can be paused by the user. (Its purpose is to count how many seconds have elapsed, eliding the ones where the user paused it.) Once a second, a timer increments a variable iff the user hasn't paused the incrementing behaviour.
There are three classes I'm concerned about hearing feedback for:
- MPSTicker (.m), which performs "accumulate since initialization unless paused" and provides that result on a signal. It has a public
BOOL
property to control whether or not accumulation is running. - MPSViewModel (.m), which provides a ViewModel wrapping from
MPSTicker
to the view controller. It provides read-only strings which show the number of ticks and show the text for the action which, if taken, "pauses" or "resumes" ticks. It also has a read-writeBOOL
for pausing/unpausing ticks. - MPSViewController (.m), which consumes strings from
MPSViewModel
by binding a label to the ViewModel's tick string, binding a button's text to the "tick action" string, and mapping a button's press into the ViewModel's paused property.
My questions:
- I don't like the
BOOL
property onMPSTicker
for enabling/disabling its accumulation, but I didn't know how to do it more Reactive-ly. (This also runs downstream to the ViewModel and ViewController: how can I run a string through all three of these to control whether or not the ticker is running?) - The ViewModel exposes
tickString
andtickStateString
as very traditional properties, but the ViewController which consumes these immediately maps them back into text on a label and button text withRACObserve
. This feels wrong, but I don't know how to expose a signal from the ViewModel that's easy for the ViewController to consume for these two attributes. - The ViewController suffers an indignity when flipping the
paused
BOOL
on the ViewModel. I think this is another downstream effect of #1, "This shouldn't be aBOOL
property", but I'm not sure
(Notes: I think I shied away from a signal for the BOOL
of paused
on MPSTicker
because I didn't know how to consume it in the ViewModel to derive two strings (one for the current tick count, and one for the action text), nor how to push UI-driven value changes when the user pushes the "pause" or "resume" button. This is my core concern in questions 1 and 3.)
Some screenshots to help you visualize this gorgeous design:
Ticking:
Paused: