SlideShare a Scribd company logo
1 of 31
Download to read offline
( And Halp clients )
Code Reuse
with MVVM
1Dienstag, 16. April 13
High-level talk, mostly focused on Apple (because that’s my forté), but most of the concepts here are generally applicable to all
platforms (and that’s exactly the point).
2Dienstag, 16. April 13
But first, let me introduce myself and the other guys who worked on this app.
Who
Justin Spahr-Summers - @jspahrsummers
Paul Betts - @xpaulbettsx
Josh Vera - @joshvera
Josh Abernathy - @joshaber
3Dienstag, 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.
What
Write unit tests for UI behavior
Example: our native Halp apps
Maximize code reuse across platforms,
but keep 100% native UI
4Dienstag, 16. April 13
Halp is an internal app that we use at GitHub for user support (but more on that later).
Why
“Build software better, together.”
At GitHub, we ask: “What is the best way to build software?”
Philosophies, tools, practices
5Dienstag, 16. April 13
One of the questions _we_ work on every day is, “What’s the best way to build native user interfaces?”
View Model
Controller
How
( According to  )
6Dienstag, 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).
View ViewModel Model
How
( According to us )
7Dienstag, 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.
Meh. So what?
8Dienstag, 16. April 13
This might just seem like a way to restate the MVC pattern, but the reversed relationship between the View and the ViewModel
offers huge benefits.
Benefits of MVVM
✓ View models are testable, no UI
automation required
✓ View models can do model-like
things (e.g., serialization)
9Dienstag, 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!
Also, portability!
10Dienstag, 16. April 13
I mentioned code sharing between platforms, so let’s take a look at how that works in MVC and MVVM. Naturally, we’ll assume
the use of Xamarin for both.
Model-View-Controller
View Model
Controller
11Dienstag, 16. April 13
The blue circle here is the code we can share across platforms.
What’s Shared?
Xamarin means we only have to write
our models once, in .NET
Any networking and domain logic
is trivially cross-platform
12Dienstag, 16. April 13
What’s Unique?
We want 100% native UI on each
platform – no Qt, GTK+, or Java
To do this, we need to create views
specific to each platform
13Dienstag, 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.
What’s Unshared?
Logic for when to fetch
resources from the API
UI behaviors
(e.g., how to populate lists, or when to show spinners)
14Dienstag, 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-
specific. This is code that _should_ be shared, but isn’t.
Now, let’s contrast that with MVVM.
Model-View-ViewModel
View ViewModel Model
15Dienstag, 16. April 13
Interestingly, 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…
View Models Handle…
Loading content the UI needs
Hiding and showing content
Date, number, and string formatting
Responding to the user
16Dienstag, 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.
Halp!
17Dienstag, 16. April 13
That’s most of the abstract stuff. I want to switch gears for a moment here and talk about our support tool, and the native clients
we’re implementing using MVVM and Xamarin.
18Dienstag, 16. April 13
This is the web app 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.
19Dienstag, 16. April 13
GitHub is based in San 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.
Mac App Goals
Watch a specific inbox for new messages
Display a message count in the menu bar
View the messages in any inbox
(but especially the watched one)
20Dienstag, 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.
iPhone App Goals
View the messages in any inbox
Read any message
Triage messages by moving to another inbox
21Dienstag, 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.
(Demo)
22Dienstag, 16. April 13
Shared Behaviors
Showing inboxes and messages
Requesting and caching data
Showing loading indicators
23Dienstag, 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)
(Code)
24Dienstag, 16. April 13
Let’s get real.
25Dienstag, 16. April 13
Cocoa wasn’t really designed with MVVM in mind. Here are some minor obstacles you may encounter.
View Controllers
Layout, animations, device rotation,
view transitions
Seems like view controllers are
actually part of the view layer!
26Dienstag, 16. April 13
OS X and iOS both have view (or window) controllers, which can make MVVM confusing at first glance. Once you look deeper,
though, it’s not much of a problem at all.
View Controllers
NSViewController doesn’t do much
UIViewController is quite powerful
Between views and view controllers,
use the easiest one
27Dienstag, 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.
Data Binding
Notifications are too general,
and have global scope
Key-Value Observing is difficult
to use and comes with boilerplate
28Dienstag, 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 filtering of bound values. Worse, both are
specific to Cocoa, so our V <> VM bindings will look quite different from our VM <> M bindings (which should be cross-
platform).
Data Binding
In Objective-C, we wrote a
framework called ReactiveCocoa
In C#, we have Reactive Extensions
and our ReactiveUI framework
29Dienstag, 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.
30Dienstag, 16. April 13
GitHub for Mac uses ReactiveCocoa to implement MVVM at a large scale. The app itself is written in Objective-C, but the lessons
we’ve learned about MVVM are just as applicable to Xamarin.Mac and Xamarin.iOS.
Linkage
Rx – introtorx.com
ReactiveUI – reactiveui.net
github:mac – mac.github.com
github:windows – windows.github.com
31Dienstag, 16. April 13

More Related Content

What's hot

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 

What's hot (19)

Cordova: APIs and instruments
Cordova: APIs and instrumentsCordova: APIs and instruments
Cordova: APIs and instruments
 
C# Security Testing and Debugging
C# Security Testing and DebuggingC# Security Testing and Debugging
C# Security Testing and Debugging
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
 
Android programming-basics
Android programming-basicsAndroid programming-basics
Android programming-basics
 
Embedding Web UIs in your Eclipse application
Embedding Web UIs in your Eclipse applicationEmbedding Web UIs in your Eclipse application
Embedding Web UIs in your Eclipse application
 
Cordova + Ionic + MobileFirst
Cordova + Ionic + MobileFirstCordova + Ionic + MobileFirst
Cordova + Ionic + MobileFirst
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
 
Xamarin.iOS introduction
Xamarin.iOS introductionXamarin.iOS introduction
Xamarin.iOS introduction
 
Apache cordova
Apache cordovaApache cordova
Apache cordova
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Eclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And TricksEclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And Tricks
 
Entity frameworks101
Entity frameworks101Entity frameworks101
Entity frameworks101
 
C#/.NET Little Wonders
C#/.NET Little WondersC#/.NET Little Wonders
C#/.NET Little Wonders
 
Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)
 
Apache ant
Apache antApache ant
Apache ant
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
iPhone Coding For Web Developers
iPhone Coding For Web DevelopersiPhone Coding For Web Developers
iPhone Coding For Web Developers
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 

Viewers also liked

Manual tecnico primer parcial
Manual tecnico primer parcialManual tecnico primer parcial
Manual tecnico primer parcial
Damaris Raquel
 
Planeacion quimicayentorno
Planeacion quimicayentornoPlaneacion quimicayentorno
Planeacion quimicayentorno
blognms
 

Viewers also liked (20)

RubyからC#を扱う
RubyからC#を扱うRubyからC#を扱う
RubyからC#を扱う
 
Interaction channel
Interaction channelInteraction channel
Interaction channel
 
日々の面倒をプログラミングで解決!【入門編】
日々の面倒をプログラミングで解決!【入門編】日々の面倒をプログラミングで解決!【入門編】
日々の面倒をプログラミングで解決!【入門編】
 
広がる .Net
広がる .Net広がる .Net
広がる .Net
 
Keep yourself up to date
Keep yourself up to dateKeep yourself up to date
Keep yourself up to date
 
Manual tecnico primer parcial
Manual tecnico primer parcialManual tecnico primer parcial
Manual tecnico primer parcial
 
Bs25999 2 advisory board
Bs25999 2 advisory boardBs25999 2 advisory board
Bs25999 2 advisory board
 
Planeacion quimicayentorno
Planeacion quimicayentornoPlaneacion quimicayentorno
Planeacion quimicayentorno
 
Danteyladivinacomedia 120702121455-phpapp01
Danteyladivinacomedia 120702121455-phpapp01Danteyladivinacomedia 120702121455-phpapp01
Danteyladivinacomedia 120702121455-phpapp01
 
Your Marketing Toolkit:Low-Cost/No Cost Business Tools That Will Make You Mor...
Your Marketing Toolkit:Low-Cost/No Cost Business Tools That Will Make You Mor...Your Marketing Toolkit:Low-Cost/No Cost Business Tools That Will Make You Mor...
Your Marketing Toolkit:Low-Cost/No Cost Business Tools That Will Make You Mor...
 
Ancianos
AncianosAncianos
Ancianos
 
Instituto Universitario de Posgrado
Instituto Universitario de PosgradoInstituto Universitario de Posgrado
Instituto Universitario de Posgrado
 
HOW CAN I SPY ON SOMEONES FACEBOOK
HOW CAN I SPY ON SOMEONES FACEBOOK HOW CAN I SPY ON SOMEONES FACEBOOK
HOW CAN I SPY ON SOMEONES FACEBOOK
 
Ayudas para la mejora de la producción y comercialización de la miel en la Co...
Ayudas para la mejora de la producción y comercialización de la miel en la Co...Ayudas para la mejora de la producción y comercialización de la miel en la Co...
Ayudas para la mejora de la producción y comercialización de la miel en la Co...
 
Revista Regio nr.30 iulie 2014
Revista Regio nr.30 iulie 2014Revista Regio nr.30 iulie 2014
Revista Regio nr.30 iulie 2014
 
Progettare antifurto a norme Cei 79 3 Diakron
Progettare antifurto a norme Cei 79 3 DiakronProgettare antifurto a norme Cei 79 3 Diakron
Progettare antifurto a norme Cei 79 3 Diakron
 
Catálogo Productos-piscina.com
Catálogo Productos-piscina.comCatálogo Productos-piscina.com
Catálogo Productos-piscina.com
 
Decreto de consagracion del ecuador
Decreto de consagracion del ecuadorDecreto de consagracion del ecuador
Decreto de consagracion del ecuador
 
OAuth 2.0 und OpenId Connect
OAuth 2.0 und OpenId ConnectOAuth 2.0 und OpenId Connect
OAuth 2.0 und OpenId Connect
 
Descartes
DescartesDescartes
Descartes
 

Similar to GitHub halp app - Minimizing platform-specific code with MVVM - Justin Spahr-Summers

Man in-the-browser-in-depth-report
Man in-the-browser-in-depth-reportMan in-the-browser-in-depth-report
Man in-the-browser-in-depth-report
Hai Nguyen
 
A Brief Note On Asp.Net And Cloud Computing Essay
A Brief Note On Asp.Net And Cloud Computing EssayA Brief Note On Asp.Net And Cloud Computing Essay
A Brief Note On Asp.Net And Cloud Computing Essay
Lanate Drummond
 

Similar to GitHub halp app - Minimizing platform-specific code with MVVM - Justin Spahr-Summers (20)

What is Codename One - Transcript.pdf
What is Codename One - Transcript.pdfWhat is Codename One - Transcript.pdf
What is Codename One - Transcript.pdf
 
9 reasons why programmers should learn react native
9 reasons why programmers should learn react native9 reasons why programmers should learn react native
9 reasons why programmers should learn react native
 
Common Missteps in Cross-Platform Development.pdf
Common Missteps in Cross-Platform Development.pdfCommon Missteps in Cross-Platform Development.pdf
Common Missteps in Cross-Platform Development.pdf
 
Introduction to Docker and Containers- Learning Simple
Introduction to Docker and Containers- Learning SimpleIntroduction to Docker and Containers- Learning Simple
Introduction to Docker and Containers- Learning Simple
 
Basic iOS Training with SWIFT - Part 1
Basic iOS Training with SWIFT - Part 1Basic iOS Training with SWIFT - Part 1
Basic iOS Training with SWIFT - Part 1
 
Man in-the-browser-in-depth-report
Man in-the-browser-in-depth-reportMan in-the-browser-in-depth-report
Man in-the-browser-in-depth-report
 
Microservice pitfalls
Microservice pitfalls Microservice pitfalls
Microservice pitfalls
 
Viacheslav Eremin interview about DOT NET (eng lang)
Viacheslav Eremin interview about DOT NET (eng lang)Viacheslav Eremin interview about DOT NET (eng lang)
Viacheslav Eremin interview about DOT NET (eng lang)
 
MyReplayInZen
MyReplayInZenMyReplayInZen
MyReplayInZen
 
Developers survival-guide
Developers survival-guideDevelopers survival-guide
Developers survival-guide
 
Why You Should Use MERN Stack for Startup Apps?
Why You Should Use MERN Stack for Startup Apps?Why You Should Use MERN Stack for Startup Apps?
Why You Should Use MERN Stack for Startup Apps?
 
Microservices and functional programming
Microservices and functional programmingMicroservices and functional programming
Microservices and functional programming
 
Enterprise ipad Development with notes
Enterprise ipad Development with notesEnterprise ipad Development with notes
Enterprise ipad Development with notes
 
Elm Detroit 9/7/17 - Planting Seeds with Elm
Elm Detroit 9/7/17 - Planting Seeds with ElmElm Detroit 9/7/17 - Planting Seeds with Elm
Elm Detroit 9/7/17 - Planting Seeds with Elm
 
How, When, and Why to Patch a Module
How, When, and Why to Patch a Module How, When, and Why to Patch a Module
How, When, and Why to Patch a Module
 
Reactive design: languages, and paradigms
Reactive design: languages, and paradigmsReactive design: languages, and paradigms
Reactive design: languages, and paradigms
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
A Brief Note On Asp.Net And Cloud Computing Essay
A Brief Note On Asp.Net And Cloud Computing EssayA Brief Note On Asp.Net And Cloud Computing Essay
A Brief Note On Asp.Net And Cloud Computing Essay
 
Introduction to meteor
Introduction to meteorIntroduction to meteor
Introduction to meteor
 
Codename one
Codename oneCodename one
Codename one
 

More from Xamarin

More from Xamarin (20)

Xamarin University Presents: Building Your First Intelligent App with Xamarin...
Xamarin University Presents: Building Your First Intelligent App with Xamarin...Xamarin University Presents: Building Your First Intelligent App with Xamarin...
Xamarin University Presents: Building Your First Intelligent App with Xamarin...
 
Xamarin University Presents: Ship Better Apps with Visual Studio App Center
Xamarin University Presents: Ship Better Apps with Visual Studio App CenterXamarin University Presents: Ship Better Apps with Visual Studio App Center
Xamarin University Presents: Ship Better Apps with Visual Studio App Center
 
Get the Most Out of iOS 11 with Visual Studio Tools for Xamarin
Get the Most Out of iOS 11 with Visual Studio Tools for XamarinGet the Most Out of iOS 11 with Visual Studio Tools for Xamarin
Get the Most Out of iOS 11 with Visual Studio Tools for Xamarin
 
Get the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
Get the Most out of Android 8 Oreo with Visual Studio Tools for XamarinGet the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
Get the Most out of Android 8 Oreo with Visual Studio Tools for Xamarin
 
Creative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePushCreative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePush
 
Build Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft AzureBuild Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft Azure
 
Exploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksExploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin Workbooks
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
 
Developer’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningDeveloper’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine Learning
 
Customizing Xamarin.Forms UI
Customizing Xamarin.Forms UICustomizing Xamarin.Forms UI
Customizing Xamarin.Forms UI
 
Session 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesSession 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and Resources
 
Session 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilitySession 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and Profitability
 
Session 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeSession 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile Practice
 
Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud
 
SkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsSkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.Forms
 
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureBuilding Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
 
Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017
 
Connected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureConnected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft Azure
 
Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017
 
Building Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioBuilding Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual Studio
 

Recently uploaded

Recently uploaded (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

GitHub halp app - Minimizing platform-specific code with MVVM - Justin Spahr-Summers

  • 1. ( And Halp clients ) Code Reuse with MVVM 1Dienstag, 16. April 13 High-level talk, mostly focused on Apple (because that’s my forté), but most of the concepts here are generally applicable to all platforms (and that’s exactly the point).
  • 2. 2Dienstag, 16. April 13 But first, let me introduce myself and the other guys who worked on this app.
  • 3. Who Justin Spahr-Summers - @jspahrsummers Paul Betts - @xpaulbettsx Josh Vera - @joshvera Josh Abernathy - @joshaber 3Dienstag, 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.
  • 4. What Write unit tests for UI behavior Example: our native Halp apps Maximize code reuse across platforms, but keep 100% native UI 4Dienstag, 16. April 13 Halp is an internal app that we use at GitHub for user support (but more on that later).
  • 5. Why “Build software better, together.” At GitHub, we ask: “What is the best way to build software?” Philosophies, tools, practices 5Dienstag, 16. April 13 One of the questions _we_ work on every day is, “What’s the best way to build native user interfaces?”
  • 6. View Model Controller How ( According to  ) 6Dienstag, 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).
  • 7. View ViewModel Model How ( According to us ) 7Dienstag, 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.
  • 8. Meh. So what? 8Dienstag, 16. April 13 This might just seem like a way to restate the MVC pattern, but the reversed relationship between the View and the ViewModel offers huge benefits.
  • 9. Benefits of MVVM ✓ View models are testable, no UI automation required ✓ View models can do model-like things (e.g., serialization) 9Dienstag, 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!
  • 10. Also, portability! 10Dienstag, 16. April 13 I mentioned code sharing between platforms, so let’s take a look at how that works in MVC and MVVM. Naturally, we’ll assume the use of Xamarin for both.
  • 11. Model-View-Controller View Model Controller 11Dienstag, 16. April 13 The blue circle here is the code we can share across platforms.
  • 12. What’s Shared? Xamarin means we only have to write our models once, in .NET Any networking and domain logic is trivially cross-platform 12Dienstag, 16. April 13
  • 13. What’s Unique? We want 100% native UI on each platform – no Qt, GTK+, or Java To do this, we need to create views specific to each platform 13Dienstag, 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.
  • 14. What’s Unshared? Logic for when to fetch resources from the API UI behaviors (e.g., how to populate lists, or when to show spinners) 14Dienstag, 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- specific. This is code that _should_ be shared, but isn’t. Now, let’s contrast that with MVVM.
  • 15. Model-View-ViewModel View ViewModel Model 15Dienstag, 16. April 13 Interestingly, 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…
  • 16. View Models Handle… Loading content the UI needs Hiding and showing content Date, number, and string formatting Responding to the user 16Dienstag, 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.
  • 17. Halp! 17Dienstag, 16. April 13 That’s most of the abstract stuff. I want to switch gears for a moment here and talk about our support tool, and the native clients we’re implementing using MVVM and Xamarin.
  • 18. 18Dienstag, 16. April 13 This is the web app 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.
  • 19. 19Dienstag, 16. April 13 GitHub is based in San 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.
  • 20. Mac App Goals Watch a specific inbox for new messages Display a message count in the menu bar View the messages in any inbox (but especially the watched one) 20Dienstag, 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.
  • 21. iPhone App Goals View the messages in any inbox Read any message Triage messages by moving to another inbox 21Dienstag, 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.
  • 23. Shared Behaviors Showing inboxes and messages Requesting and caching data Showing loading indicators 23Dienstag, 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)
  • 25. Let’s get real. 25Dienstag, 16. April 13 Cocoa wasn’t really designed with MVVM in mind. Here are some minor obstacles you may encounter.
  • 26. View Controllers Layout, animations, device rotation, view transitions Seems like view controllers are actually part of the view layer! 26Dienstag, 16. April 13 OS X and iOS both have view (or window) controllers, which can make MVVM confusing at first glance. Once you look deeper, though, it’s not much of a problem at all.
  • 27. View Controllers NSViewController doesn’t do much UIViewController is quite powerful Between views and view controllers, use the easiest one 27Dienstag, 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.
  • 28. Data Binding Notifications are too general, and have global scope Key-Value Observing is difficult to use and comes with boilerplate 28Dienstag, 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 filtering of bound values. Worse, both are specific to Cocoa, so our V <> VM bindings will look quite different from our VM <> M bindings (which should be cross- platform).
  • 29. Data Binding In Objective-C, we wrote a framework called ReactiveCocoa In C#, we have Reactive Extensions and our ReactiveUI framework 29Dienstag, 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.
  • 30. 30Dienstag, 16. April 13 GitHub for Mac uses ReactiveCocoa to implement MVVM at a large scale. The app itself is written in Objective-C, but the lessons we’ve learned about MVVM are just as applicable to Xamarin.Mac and Xamarin.iOS.
  • 31. Linkage Rx – introtorx.com ReactiveUI – reactiveui.net github:mac – mac.github.com github:windows – windows.github.com 31Dienstag, 16. April 13