Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<dependencies>
<dependency id="reactiveui-core" version="[6.3.1]" />
<dependency id="Xamarin.Android.Support.v4" version="20.0.0.3" />
<dependency id="Xamarin.Android.Support.v7.AppCompat" version="21.0.3.0" />
</dependencies>
</metadata>
</package>
170 changes: 170 additions & 0 deletions ReactiveUI.AndroidSupport/ReactiveActionBarActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Splat;

namespace ReactiveUI.AndroidSupport
{
/// <summary>
/// This is an Activity that is both an Activity and has ReactiveObject powers
/// (i.e. you can call RaiseAndSetIfChanged)
/// </summary>
public class ReactiveActionBarActivity<TViewModel> : ReactiveActionBarActivity, IViewFor<TViewModel>, ICanActivate
where TViewModel : class
{
protected ReactiveActionBarActivity() { }

TViewModel _ViewModel;
public TViewModel ViewModel
{
get { return _ViewModel; }
set { this.RaiseAndSetIfChanged(ref _ViewModel, value); }
}

object IViewFor.ViewModel
{
get { return _ViewModel; }
set { _ViewModel = (TViewModel)value; }
}
}

/// <summary>
/// This is an Activity that is both an Activity and has ReactiveObject powers
/// (i.e. you can call RaiseAndSetIfChanged)
/// </summary>
public class ReactiveActionBarActivity : ActionBarActivity, IReactiveObject, IReactiveNotifyPropertyChanged<ReactiveActionBarActivity>, IHandleObservableErrors
{
public event PropertyChangingEventHandler PropertyChanging
{
add { PropertyChangingEventManager.AddHandler(this, value); }
remove { PropertyChangingEventManager.RemoveHandler(this, value); }
}

void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args)
{
PropertyChangingEventManager.DeliverEvent(this, args);
}

public event PropertyChangedEventHandler PropertyChanged
{
add { PropertyChangedEventManager.AddHandler(this, value); }
remove { PropertyChangedEventManager.RemoveHandler(this, value); }
}

void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args)
{
PropertyChangedEventManager.DeliverEvent(this, args);
}

/// <summary>
/// Represents an Observable that fires *before* a property is about to
/// be changed.
/// </summary>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveActionBarActivity>> Changing
{
get { return this.getChangingObservable(); }
}

/// <summary>
/// Represents an Observable that fires *after* a property has changed.
/// </summary>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveActionBarActivity>> Changed
{
get { return this.getChangedObservable(); }
}

/// <summary>
/// When this method is called, an object will not fire change
/// notifications (neither traditional nor Observable notifications)
/// until the return value is disposed.
/// </summary>
/// <returns>An object that, when disposed, reenables change
/// notifications.</returns>
public IDisposable SuppressChangeNotifications()
{
return this.suppressChangeNotifications();
}

public IObservable<Exception> ThrownExceptions { get { return this.getThrownExceptionsObservable(); } }

readonly Subject<Unit> activated = new Subject<Unit>();
public IObservable<Unit> Activated { get { return activated; } }

readonly Subject<Unit> deactivated = new Subject<Unit>();
public IObservable<Unit> Deactivated { get { return deactivated; } }

protected override void OnPause()
{
base.OnPause();
deactivated.OnNext(Unit.Default);
}

protected override void OnResume()
{
base.OnResume();
activated.OnNext(Unit.Default);
}

readonly Subject<Tuple<int, Result, Intent>> activityResult = new Subject<Tuple<int, Result, Intent>>();
public IObservable<Tuple<int, Result, Intent>> ActivityResult
{
get { return activityResult; }
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
activityResult.OnNext(Tuple.Create(requestCode, resultCode, data));
}

public Task<Tuple<Result, Intent>> StartActivityForResultAsync(Intent intent, int requestCode)
{
// NB: It's important that we set up the subscription *before* we
// call ActivityForResult
var ret = ActivityResult
.Where(x => x.Item1 == requestCode)
.Select(x => Tuple.Create(x.Item2, x.Item3))
.FirstAsync()
.ToTask();

StartActivityForResult(intent, requestCode);
return ret;
}

public Task<Tuple<Result, Intent>> StartActivityForResultAsync(Type type, int requestCode)
{
// NB: It's important that we set up the subscription *before* we
// call ActivityForResult
var ret = ActivityResult
.Where(x => x.Item1 == requestCode)
.Select(x => Tuple.Create(x.Item2, x.Item3))
.FirstAsync()
.ToTask();

StartActivityForResult(type, requestCode);
return ret;
}
}
}

6 changes: 5 additions & 1 deletion ReactiveUI.AndroidSupport/ReactiveUI.AndroidSupport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
<HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\portable-windows8+net45+wp8\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Android.Support.v4">
<HintPath>..\packages\Xamarin.Android.Support.v4.20.0.0.2\lib\MonoAndroid10\Xamarin.Android.Support.v4.dll</HintPath>
<HintPath>..\packages\Xamarin.Android.Support.v4.21.0.3.0\lib\MonoAndroid10\Xamarin.Android.Support.v4.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Android.Support.v7.AppCompat">
<HintPath>..\packages\Xamarin.Android.Support.v7.AppCompat.21.0.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.AppCompat.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -74,6 +77,7 @@
<Compile Include="ReactiveFragmentActivity.cs" />
<Compile Include="ReactivePagerAdapter.cs" />
<Compile Include="ReactiveFragment.cs" />
<Compile Include="ReactiveActionBarActivity.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
3 changes: 2 additions & 1 deletion ReactiveUI.AndroidSupport/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<package id="Rx-Main" version="2.2.5" targetFramework="MonoAndroid403" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="MonoAndroid403" />
<package id="Splat" version="1.6.0" targetFramework="MonoAndroid403" />
<package id="Xamarin.Android.Support.v4" version="20.0.0.2" targetFramework="MonoAndroid403" />
<package id="Xamarin.Android.Support.v4" version="21.0.3.0" targetFramework="MonoAndroid403" />
<package id="Xamarin.Android.Support.v7.AppCompat" version="21.0.3.0" targetFramework="MonoAndroid403" />
</packages>