Skip to content

anaisbetts/starter-sample

Repository files navigation

RxUISample Project for Mobile Applications

This project is a base project for cross-platform mobile applications that should help you get started quicker without hours of right-clicking.

How do I get started?

git clone https://github.com/paulcbetts/starter-mobile
script/create NameOfMyNewProject
git remote set-url origin https://github.com/MyUserName/NameOfMyNewProject
git push -u origin master

NOTE: Don't use a '.' in any of your project names, iOS gets hella upset. Yes, I think it'd look way better than '-' too.

How do I build the project

script/cibuild

What's the project structure here?

  1. RxUISample-Android: The main Android app project.
  2. RxUISample-iOS: The main iOS (both iPhone and iPad) application project.
  3. RxUISample-Core: Code that is shared between the two projects. Note that at the moment, these are two separate projects, but they'll be one single csproj soon once Xamarin releases proper Portable Library support.

Philosophy

How does this MVVM Thing Work™?

The idea is, that we want to create our models (i.e. stuff we save to disk and stuff we send over the network) and ViewModels (a class that represents the behavior of an app screen, by describing how properties are related to each other) in the RxUISample-Core library.

Ideally, the vast majority of our app will live in this library, because everything we don't put in here we have to write twice, and is super hard to test.

In this app, what we mean by "View" is, on iOS is a UIViewController and friends, and on Android is usually an Activity. Both of these classes are hard to test, so we want them to be as dumb as possible.

Here are some things that belong in RxUISample-Core:

  1. Anything related to sending stuff to the network
  2. Anything related to loading/saving user's data
  3. All of our ViewModel classes (i.e. for every screen in the app, we'll have a ViewModel class)

So, it's important that RxUISample-Core not do platform-specific things - if you're talking about CGRects in a RxUISample-Core class, you're Doing It Wrong™.

Here are some things that will end up in RxUISample-{Platform}

  1. The app startup code (i.e. AppDelegate)
  2. A ViewController or Activity for every screen in the app.

"How Do I?"

How do I add a new screen to the app?

  1. Create a new ViewModel class in RxUISample-Core and derive it from ReactiveObject. Put all of the interesting code in here - calling REST APIs, validating stuff, you name it!
  2. Walk over to the iOS project, create a new "Universal View Controller", copy paste some of the stuff from TestViewController (like the base class ReactiveViewController, and that boilerplate ViewModel property at the bottom)
  3. In the Android project, create a new Activity, same deal around the boilerplate there too.

How do I do interesting stuff in the core library when I can't access UI elements or other stuff??

You should define an Interface, then let the platforms implement. Here's how you could do this with an example UI action (note that this isn't the best way to do this, it's just an example):

First, define an interface in the Core project:

public interface IAlertHelper
{
    void ShowAlert(string message);
}

Then, in your ViewModel, you should make it a constructor parameter - we write it this way so that if we want to replace it with a dummy version in a test runner, it's easy to do:

public TestViewModel(IAlertHelper alertHelper = null)
{
    alertHelper = alertHelper ??  RxApp.DependencyResolver.GetService<IAlertHelper>();
    alertHelper.ShowAlert("Wat it do.");
}

Now, in the iOS and Android projects, you can register an implementation of this interface:

public class AppleAlertHelper : IAlertHelper
{
    public void ShowAlert(string message)
    {
        /* ... */
    }
}

and in your AppDelegate, you can register it:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    RxApp.MutableResolver.RegisterConstant(new AppleAlertHelper(), typeof(IAlertHelper));
}

About

ReactiveUI + Akavache + Splat Sample

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published