Skip to content
Browse files

Try to wire up AndroidPlayground to use auto-suspend and activity-bas…

…ed routing
  • Loading branch information...
1 parent 5d2573e commit 6f133ff127f60604c2c14ce67d95efc52db49f4f @paulcbetts paulcbetts committed
View
5 AndroidPlayground/AndroidPlayground.csproj
@@ -73,6 +73,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SecondaryActivity.cs" />
<Compile Include="AppBootstrapper.cs" />
+ <Compile Include="App.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
@@ -103,5 +104,9 @@
<Project>{F92A477B-BB94-43C1-984E-E177EF9FEDB7}</Project>
<Name>ReactiveUI_Monodroid</Name>
</ProjectReference>
+ <ProjectReference Include="..\ReactiveUI.Mobile\ReactiveUI.Mobile_Monodroid.csproj">
+ <Project>{A92E477B-BB94-43C1-984E-E177EF9FEDB1}</Project>
+ <Name>ReactiveUI.Mobile_Monodroid</Name>
+ </ProjectReference>
</ItemGroup>
</Project>
View
67 AndroidPlayground/App.cs
@@ -0,0 +1,67 @@
+using System;
+using ReactiveUI;
+using System.Collections.Generic;
+using System.Linq;
+using ReactiveUI.Routing;
+
+namespace AndroidPlayground
+{
+ public class App
+ {
+ static App _Current;
+ public static App Current {
+ get { return (_Current = _Current ?? new App()); }
+ }
+
+ public FuncServiceLocator Locator { get; protected set; }
+
+ protected App()
+ {
+ var locator = new FuncServiceLocator();
+
+ RxApp.ConfigureServiceLocator(
+ (t,s) => locator.GetAllServices(t,s).FirstOrDefault(),
+ (t,s) => locator.GetAllServices(t,s).ToArray(),
+ (c,t,s) => locator.Register(() => Activator.CreateInstance(c), t, s));
+
+ locator.Register(() => typeof(MainView), typeof(IViewFor<MainViewModel>));
+ locator.Register(() => typeof(SecondaryView), typeof(IViewFor<SecondaryViewModel>));
+
+ RxApp.Register(typeof(AppBootstrapper), typeof(IApplicationRootState));
+
+ Locator = locator;
+ }
+ }
+
+ public class FuncServiceLocator
+ {
+ readonly Dictionary<Tuple<Type, string>, List<Func<object>>> _registry = new Dictionary<Tuple<Type, string>, List<Func<object>>>();
+
+ public void Register(Func<object> factory, Type type, string contract = null)
+ {
+ var pair = Tuple.Create(type, contract ?? "");
+ if (!_registry.ContainsKey(pair)) _registry[pair] = new List<Func<object>>();
+
+ _registry[pair].Add(factory);
+ }
+
+ public IEnumerable<object> GetAllServices(Type type, string contract = null)
+ {
+ var pair = Tuple.Create(type, contract ?? "");
+ if (!_registry.ContainsKey(pair)) {
+ return Enumerable.Empty<object>();
+ }
+
+ return _registry[pair].Select(x => x());
+ }
+
+ public void ClearRegistration(Type type, string contract = null)
+ {
+ var pair = Tuple.Create(type, contract ?? "");
+ if (_registry.ContainsKey(pair)) {
+ _registry.Remove(pair);
+ }
+ }
+ }
+}
+
View
13 AndroidPlayground/AppBootstrapper.cs
@@ -1,11 +1,20 @@
using System;
+using ReactiveUI.Routing;
+using ReactiveUI.Mobile;
namespace AndroidPlayground
{
- public class AppBootstrapper
+ public class AppBootstrapper : IApplicationRootState
{
- protected AppBootstrapper()
+ public IRoutingState Router { get; protected set; }
+
+ public AppBootstrapper()
{
+ Router = new RoutingState();
+ Router.Navigate.Execute(new MainViewModel(this));
+
+ App.Current.Locator.Register(() => this, typeof(IScreen));
+ App.Current.Locator.Register(() => this, typeof(IApplicationRootState));
}
}
}
View
85 AndroidPlayground/MainActivity.cs
@@ -7,17 +7,56 @@
using Android.Widget;
using Android.OS;
using ActionbarSherlock.App;
+using ReactiveUI;
+using ReactiveUI.Routing;
+using System.ComponentModel;
+using ReactiveUI.Android;
+using ReactiveUI.Mobile;
namespace AndroidPlayground
{
[Activity (Label = "AndroidPlayground", MainLauncher = true)]
- public class Activity1 : SherlockActivity
+ public class MainView : SherlockActivity, IViewFor<MainViewModel>, INotifyPropertyChanged
{
int count = 1;
+ readonly ActivityRoutedViewHost routeHelper;
+ readonly AutoSuspendActivityHelper suspendHelper;
+
+ #region Boring copy-paste code I want to die
+ MainViewModel _ViewModel;
+ public MainViewModel ViewModel {
+ get { return _ViewModel; }
+ set {
+ if (_ViewModel == value) return;
+ _ViewModel = value;
+ if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ViewModel"));
+ }
+ }
+
+ object IViewFor.ViewModel {
+ get { return ViewModel; }
+ set { ViewModel = (MainViewModel)value; }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+ #endregion
+
+ public MainView()
+ {
+ // NB: This is dumb.
+ Console.WriteLine(App.Current);
+ RxApp.DeferredScheduler = new AndroidUIScheduler(this);
+
+ suspendHelper = new AutoSuspendActivityHelper(this);
+ suspendHelper.SuspensionHost.SetupDefaultSuspendResume();
+
+ routeHelper = new ActivityRoutedViewHost(this);
+ }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
+ suspendHelper.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
@@ -26,14 +65,48 @@ protected override void OnCreate(Bundle bundle)
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.myButton);
- button.Click += (o,e) =>
- {
-
+ button.Click += (o,e) => {
+ ViewModel.HostScreen.Router.Navigate.Execute(new SecondaryViewModel(ViewModel.HostScreen));
};
- ActionBar.Title = "Bar";
+ this.OneWayBind(ViewModel, x => x.UrlPathSegment, x => x.ActionBar.Title);
+ }
+
+ public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
+ {
+ return routeHelper.OnKeyUp(keyCode, e);
+ }
+
+ protected override void OnResume()
+ {
+ base.OnResume();
+ suspendHelper.OnResume();
+ }
+
+ protected override void OnPause()
+ {
+ base.OnPause();
+ suspendHelper.OnPause();
+ }
+
+ protected override void OnSaveInstanceState(Bundle outState)
+ {
+ base.OnSaveInstanceState(outState);
+ suspendHelper.OnSaveInstanceState(outState);
}
}
-}
+ public class MainViewModel : ReactiveObject, IRoutableViewModel
+ {
+ public string UrlPathSegment {
+ get { return "Main!"; }
+ }
+
+ public IScreen HostScreen { get; protected set; }
+ public MainViewModel(IScreen hostScreen)
+ {
+ HostScreen = hostScreen ?? RxApp.GetService<IScreen>();
+ }
+ }
+}
View
78 AndroidPlayground/SecondaryActivity.cs
@@ -11,17 +11,93 @@
using Android.Views;
using Android.Widget;
using ActionbarSherlock.App;
+using ReactiveUI;
+using ReactiveUI.Routing;
+using ReactiveUI.Android;
+using ReactiveUI.Mobile;
+using System.ComponentModel;
namespace AndroidPlayground
{
[Activity (Label = "SecondaryActivity")]
- public class SecondaryActivity : SherlockActivity
+ public class SecondaryView : SherlockActivity, IViewFor<SecondaryViewModel>
{
+ readonly ActivityRoutedViewHost routeHelper;
+ readonly AutoSuspendActivityHelper suspendHelper;
+
+ #region Boring copy-paste code I want to die
+ SecondaryViewModel _ViewModel;
+ public SecondaryViewModel ViewModel {
+ get { return _ViewModel; }
+ set {
+ if (_ViewModel == value) return;
+ _ViewModel = value;
+ if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ViewModel"));
+ }
+ }
+
+ object IViewFor.ViewModel {
+ get { return ViewModel; }
+ set { ViewModel = (SecondaryViewModel)value; }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+ #endregion
+
+ public SecondaryView()
+ {
+ // NB: Super Dumb
+ Console.WriteLine(App.Current);
+ RxApp.DeferredScheduler = new AndroidUIScheduler(this);
+
+ routeHelper = new ActivityRoutedViewHost(this);
+ suspendHelper = new AutoSuspendActivityHelper(this);
+ }
+
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
+ suspendHelper.OnCreate(bundle);
+
SetContentView(Resource.Layout.Secondary);
}
+
+ public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
+ {
+ return routeHelper.OnKeyUp(keyCode, e);
+ }
+
+ protected override void OnResume()
+ {
+ base.OnResume();
+ suspendHelper.OnResume();
+ }
+
+ protected override void OnPause()
+ {
+ base.OnPause();
+ suspendHelper.OnPause();
+ }
+
+ protected override void OnSaveInstanceState(Bundle outState)
+ {
+ base.OnSaveInstanceState(outState);
+ suspendHelper.OnSaveInstanceState(outState);
+ }
+ }
+
+ public class SecondaryViewModel : ReactiveObject, IRoutableViewModel
+ {
+ public string UrlPathSegment {
+ get { return "Secondary!"; }
+ }
+
+ public IScreen HostScreen { get; protected set; }
+
+ public SecondaryViewModel(IScreen hostScreen)
+ {
+ HostScreen = hostScreen ?? RxApp.GetService<IScreen>();
+ }
}
}

0 comments on commit 6f133ff

Please sign in to comment.
Something went wrong with that request. Please try again.