Vera - @joshvera Josh Abernathy - @joshaber 3 Dienstag, 16. April 13 I work on GitHub for Mac. I primarily write Objective-C code, but I also regularly code in Haskell as well. Paul works on GitHub for Windows (written in C#), Josh Vera works on an internal talks app written in Objective-C, and Josh Abernathy also works on GitHub for Mac. There’s quite a large diversity of experience between us, and I’ve included these guys on here because they all had something do with this presentation today.
Dienstag, 16. April 13 As recommended by Apple, Cocoa applications are typically designed using Model-View-Controller, shown here. The solid lines represent direct references; the dashed lines represent indirect references (like observation).
Dienstag, 16. April 13 At GitHub, we much prefer to use Model-View-ViewModel, shown here. If you haven’t been introduced to MVVM, here’s a quick explanation: The ViewModel replaces the role of the (View) Controller, but the VM doesn’t have a direct reference to the view like a controller would. Instead, the VM communicates to the V with a system of bindings. … For example, If you want to show a loading spinner, the view model might have a boolean property which indicates whether to show it. The view would observe that property for changes, and hide/show the spinner in response.
automation required ✓ View models can do model-like things (e.g., serialization) 9 Dienstag, 16. April 13 Traditionally, view controllers rarely get unit tested in Cocoa, simply because it’s such a pain to write a controller that doesn’t depend on having a view (or, alternatively, to set up a valid view in unit tests). Since the VM doesn’t even know about view objects, they can be tested without a GUI at all! Serialization: for example, to save and restore the state of your UI, you can just save and restore your VM hierarchy. Doing this in MVC would require a separate set of “view state” objects – which are basically view models anyways!
– no Qt, GTK+, or Java To do this, we need to create views specific to each platform 13 Dienstag, 16. April 13 This makes sense and is perfectly appropriate. Sharing view code leads to lower-quality apps which cater to the lowest common denominator and ignore each platform’s individual UI conventions.
API UI behaviors (e.g., how to populate lists, or when to show spinners) 14 Dienstag, 16. April 13 If we follow MVC, we’re also rewriting this logic for each platform (as part of our controller layer), even though it’s not platform- speciﬁc. This is code that _should_ be shared, but isn’t. Now, let’s contrast that with MVVM.
because the VM doesn’t reference the view (or any UI) directly, it becomes reusable across platforms. The VM describes only how the UI should update and respond to user actions – not how it should look. Multiple types of view can be created for one view model, and each can look completely different, but most of the underlying logic will remain the same. If we’re using Xamarin, we can now write most of our model _and view model_ code just once. The VM implements most of our UI behavior, like…
showing content Date, number, and string formatting Responding to the user 16 Dienstag, 16. April 13 There are just some typical use cases, not a complete list. Loading: note that the view model is not actually responsible for the details of persistence, networking, etc. It’s only responsible for communicating with whatever that layer is, _based on_ what the UI needs to show at any point in time.
that we use for user support. It lets us triage our users’ emails and get them to the right people as quickly as possible. Supportocats and developers can reply to messages, bring other people into the discussion, cross-link to other internal resources, etc. Here, we’re looking at a discussion thread in the Technical inbox.
Francisco, but about half of GitHub works remotely on a regular basis (I myself work from Seattle). Twice every year, all of the company meets in SF for GitHub Summit. Our last summit was earlier this year, and a few of us wanted to spend our Hack Day working on a native client for Halp. We decided to use Xamarin to share code between our different desired platforms, and reduce the development and maintenance effort that would otherwise be involved in each one. We started the iOS client that day, and a Mac client since.
Display a message count in the menu bar View the messages in any inbox (but especially the watched one) 20 Dienstag, 16. April 13 This is what we want to do for our Mac client. We’ve started on a prototype. It’s still very premature, so it doesn’t do much yet.
any message Triage messages by moving to another inbox 21 Dienstag, 16. April 13 And this is what we want to do for our iPhone client. (An iPad client would be very similar as well.) This one’s a bit further along, but still pretty rough around the edges. All the data here is loaded from the API and cached locally by the app.
Showing loading indicators 23 Dienstag, 16. April 13 By no coincidence, these are the behaviors implemented by our cross-platform view models. Let’s take a look at the code. (ViewModels, MenubarController?, PopoverController?, TableSources)
view controllers are actually part of the view layer! 26 Dienstag, 16. April 13 OS X and iOS both have view (or window) controllers, which can make MVVM confusing at ﬁrst glance. Once you look deeper, though, it’s not much of a problem at all.
Between views and view controllers, use the easiest one 27 Dienstag, 16. April 13 Basically, use the class that will make implementing your view layer easiest. On OS X, you’ll probably just want NSView, since NSViewController is relatively useless. On iOS, you’ll probably want UIViewController, so you can handle rotation, navigation, etc. No matter what you decide to use for your UI, you’ll still have a ViewModel.
Key-Value Observing is difficult to use and comes with boilerplate 28 Dienstag, 16. April 13 It’s hard to write the indirect relationship from the ViewModel to the View without a powerful system of bindings. Cocoa (and, by extension, Xamarin.Mac and Xamarin.iOS) offers a couple solutions, but they’re woefully inadequate. In addition to these individual problems, neither supports automatic transformation or ﬁltering of bound values. Worse, both are speciﬁc to Cocoa, so our V <> VM bindings will look quite different from our VM <> M bindings (which should be cross- platform).
In C#, we have Reactive Extensions and our ReactiveUI framework 29 Dienstag, 16. April 13 Reactive Extensions (or Rx) is an implementation of Functional Reactive Programming, which is unfortunately beyond the scope of this talk, but there are lots of great resources for learning more about it. ReactiveUI is an MVVM framework for .NET. One of its major features is an API for declarative data bindings, built on top of Rx.