Permalink
Browse files

Create default suspension drivers for iOS and Android

  • Loading branch information...
1 parent cad179f commit 5306726755fb0368c9c4101cde0b69fa805d4845 @paulcbetts paulcbetts committed Mar 30, 2013
@@ -0,0 +1,42 @@
+using System;
+using System.Reactive;
+using System.Reactive.Linq;
+using Newtonsoft.Json;
+
+namespace ReactiveUI.Mobile
+{
+ public class BundleSuspensionDriver : ISuspensionDriver
+ {
+ public IObservable<T> LoadState<T>() where T : class, IApplicationRootState
+ {
+ try {
+ return Observable.Return(JsonConvert.DeserializeObject<T>(
+ AndroidSuspensionHost.latestBundle.GetString("__state")));
+ } catch (Exception ex) {
+ return Observable.Throw<T>(ex);
+ }
+ }
+
+ public IObservable<Unit> SaveState<T>(T state) where T : class, IApplicationRootState
+ {
+ try {
+ AndroidSuspensionHost.latestBundle.PutString("__state", JsonConvert.SerializeObject(state));
+ return Observable.Return(Unit.Default);
+
+ } catch(Exception ex) {
+ return Observable.Throw<Unit>(ex);
+ }
+ }
+
+ public IObservable<Unit> InvalidateState()
+ {
+ try {
+ AndroidSuspensionHost.latestBundle.PutString("__state", "");
+ return Observable.Return(Unit.Default);
+
+ } catch(Exception ex) {
+ return Observable.Throw<Unit>(ex);
+ }
+ }
+ }
+}
@@ -0,0 +1,62 @@
+using System;
+using System.Reactive.Linq;
+using Newtonsoft.Json;
+using MonoTouch.Foundation;
+using System.Reactive;
+using System.IO;
+using System.Text;
+
+namespace ReactiveUI.Mobile
+{
+ public class AppSupportJsonSuspensionDriver : ISuspensionDriver
+ {
+ public IObservable<T> LoadState<T>() where T : class, IApplicationRootState
+ {
+ try {
+ var target = Path.Combine(CreateAppDirectory(NSSearchPathDirectory.ApplicationSupportDirectory), "state.json");
+ return Observable.Return(JsonConvert.DeserializeObject<T>(File.ReadAllText(target, Encoding.UTF8)));
+ } catch (Exception ex) {
+ return Observable.Throw<T>(ex);
+ }
+ }
+
+ public IObservable<Unit> SaveState<T>(T state) where T : class, IApplicationRootState
+ {
+ try {
+ var target = Path.Combine(CreateAppDirectory(NSSearchPathDirectory.ApplicationSupportDirectory), "state.json");
+ File.WriteAllText(target, JsonConvert.SerializeObject(state));
+
+ return Observable.Return(Unit.Default);
+
+ } catch(Exception ex) {
+ return Observable.Throw<Unit>(ex);
+ }
+ }
+
+ public IObservable<Unit> InvalidateState()
+ {
+ try {
+ var target = Path.Combine(CreateAppDirectory(NSSearchPathDirectory.ApplicationSupportDirectory), "state.json");
+ File.Delete(target);
+
+ return Observable.Return(Unit.Default);
+
+ } catch(Exception ex) {
+ return Observable.Throw<Unit>(ex);
+ }
+ }
+
+ string CreateAppDirectory(NSSearchPathDirectory targetDir, string subDir = "Data")
+ {
+ NSError err;
+
+ var fm = new NSFileManager();
+ var url = fm.GetUrl(targetDir, NSSearchPathDomain.All, null, true, out err);
+ var ret = Path.Combine(url.RelativePath, NSBundle.MainBundle.BundleIdentifier, subDir);
+ if (!Directory.Exists(ret)) Directory.CreateDirectory(ret);
+
+ return ret;
+ }
+ }
+}
+
@@ -118,6 +118,9 @@
</Reference>
<Reference Include="Mono.Android" />
<Reference Include="Mono.Android.Support.v4" />
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\ext\Newtonsoft.Json.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -126,6 +129,7 @@
<Compile Include="SuspensionHost.cs" />
<Compile Include="AutoSuspendActivityHelper.cs" />
<Compile Include="Geolocation.cs" />
+ <Compile Include="BundleSuspensionDriver.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
@@ -52,6 +52,9 @@
<Reference Include="Xamarin.Mobile">
<HintPath>..\ext\ios\Xamarin.Mobile.dll</HintPath>
</Reference>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\ext\Newtonsoft.Json.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
@@ -67,6 +70,7 @@
<Compile Include="Interfaces.cs" />
<Compile Include="SuspensionHost.cs" />
<Compile Include="AutoSuspendAppDelegate.cs" />
+ <Compile Include="JsonSuspensionDriver.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReactiveUI\ReactiveUI_Monotouch.csproj">
@@ -18,8 +18,10 @@ public void Register()
RxApp.Register(typeof(WinRTAppDataDriver), typeof (ISuspensionDriver));
#elif UIKIT
RxApp.Register(typeof(CocoaSuspensionHost), typeof(ISuspensionHost));
+ RxApp.Register(typeof(AppSupportJsonSuspensionDriver), typeof(ISuspensionDriver));
#elif ANDROID
RxApp.Register(typeof(AndroidSuspensionHost), typeof(ISuspensionHost));
+ RxApp.Register(typeof(BundleSuspensionDriver), typeof(ISuspensionDriver));
#endif
}
}

0 comments on commit 5306726

Please sign in to comment.