Skip to content

Commit f2d5619

Browse files
committed
Created ReactiveActionBarActivity
Added AppCompat library
1 parent 4ff011d commit f2d5619

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

NuGet/ReactiveUI-AndroidSupport/ReactiveUI-AndroidSupport.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<dependencies>
1414
<dependency id="reactiveui-core" version="[6.3.1]" />
1515
<dependency id="Xamarin.Android.Support.v4" version="20.0.0.3" />
16+
<dependency id="Xamarin.Android.Support.v7.AppCompat" version="21.0.3.0" />
1617
</dependencies>
1718
</metadata>
1819
</package>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics.Contracts;
5+
using System.Linq;
6+
using System.Reactive;
7+
using System.Reactive.Concurrency;
8+
using System.Reactive.Disposables;
9+
using System.Reactive.Linq;
10+
using System.Reactive.Subjects;
11+
using System.Reactive.Threading.Tasks;
12+
using System.Reflection;
13+
using System.Runtime.CompilerServices;
14+
using System.Runtime.Serialization;
15+
using System.Text;
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using Android.App;
19+
using Android.Content;
20+
using Android.OS;
21+
using Android.Runtime;
22+
using Android.Support.V7.App;
23+
using Android.Views;
24+
using Android.Widget;
25+
using Splat;
26+
27+
namespace ReactiveUI.AndroidSupport
28+
{
29+
/// <summary>
30+
/// This is an Activity that is both an Activity and has ReactiveObject powers
31+
/// (i.e. you can call RaiseAndSetIfChanged)
32+
/// </summary>
33+
public class ReactiveActionBarActivity<TViewModel> : ReactiveActionBarActivity, IViewFor<TViewModel>, ICanActivate
34+
where TViewModel : class
35+
{
36+
protected ReactiveActionBarActivity() { }
37+
38+
TViewModel _ViewModel;
39+
public TViewModel ViewModel
40+
{
41+
get { return _ViewModel; }
42+
set { this.RaiseAndSetIfChanged(ref _ViewModel, value); }
43+
}
44+
45+
object IViewFor.ViewModel
46+
{
47+
get { return _ViewModel; }
48+
set { _ViewModel = (TViewModel)value; }
49+
}
50+
}
51+
52+
/// <summary>
53+
/// This is an Activity that is both an Activity and has ReactiveObject powers
54+
/// (i.e. you can call RaiseAndSetIfChanged)
55+
/// </summary>
56+
public class ReactiveActionBarActivity : ActionBarActivity, IReactiveObject, IReactiveNotifyPropertyChanged<ReactiveActionBarActivity>, IHandleObservableErrors
57+
{
58+
public event PropertyChangingEventHandler PropertyChanging
59+
{
60+
add { PropertyChangingEventManager.AddHandler(this, value); }
61+
remove { PropertyChangingEventManager.RemoveHandler(this, value); }
62+
}
63+
64+
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args)
65+
{
66+
PropertyChangingEventManager.DeliverEvent(this, args);
67+
}
68+
69+
public event PropertyChangedEventHandler PropertyChanged
70+
{
71+
add { PropertyChangedEventManager.AddHandler(this, value); }
72+
remove { PropertyChangedEventManager.RemoveHandler(this, value); }
73+
}
74+
75+
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args)
76+
{
77+
PropertyChangedEventManager.DeliverEvent(this, args);
78+
}
79+
80+
/// <summary>
81+
/// Represents an Observable that fires *before* a property is about to
82+
/// be changed.
83+
/// </summary>
84+
public IObservable<IReactivePropertyChangedEventArgs<ReactiveActionBarActivity>> Changing
85+
{
86+
get { return this.getChangingObservable(); }
87+
}
88+
89+
/// <summary>
90+
/// Represents an Observable that fires *after* a property has changed.
91+
/// </summary>
92+
public IObservable<IReactivePropertyChangedEventArgs<ReactiveActionBarActivity>> Changed
93+
{
94+
get { return this.getChangedObservable(); }
95+
}
96+
97+
/// <summary>
98+
/// When this method is called, an object will not fire change
99+
/// notifications (neither traditional nor Observable notifications)
100+
/// until the return value is disposed.
101+
/// </summary>
102+
/// <returns>An object that, when disposed, reenables change
103+
/// notifications.</returns>
104+
public IDisposable SuppressChangeNotifications()
105+
{
106+
return this.suppressChangeNotifications();
107+
}
108+
109+
public IObservable<Exception> ThrownExceptions { get { return this.getThrownExceptionsObservable(); } }
110+
111+
readonly Subject<Unit> activated = new Subject<Unit>();
112+
public IObservable<Unit> Activated { get { return activated; } }
113+
114+
readonly Subject<Unit> deactivated = new Subject<Unit>();
115+
public IObservable<Unit> Deactivated { get { return deactivated; } }
116+
117+
protected override void OnPause()
118+
{
119+
base.OnPause();
120+
deactivated.OnNext(Unit.Default);
121+
}
122+
123+
protected override void OnResume()
124+
{
125+
base.OnResume();
126+
activated.OnNext(Unit.Default);
127+
}
128+
129+
readonly Subject<Tuple<int, Result, Intent>> activityResult = new Subject<Tuple<int, Result, Intent>>();
130+
public IObservable<Tuple<int, Result, Intent>> ActivityResult
131+
{
132+
get { return activityResult; }
133+
}
134+
135+
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
136+
{
137+
base.OnActivityResult(requestCode, resultCode, data);
138+
activityResult.OnNext(Tuple.Create(requestCode, resultCode, data));
139+
}
140+
141+
public Task<Tuple<Result, Intent>> StartActivityForResultAsync(Intent intent, int requestCode)
142+
{
143+
// NB: It's important that we set up the subscription *before* we
144+
// call ActivityForResult
145+
var ret = ActivityResult
146+
.Where(x => x.Item1 == requestCode)
147+
.Select(x => Tuple.Create(x.Item2, x.Item3))
148+
.FirstAsync()
149+
.ToTask();
150+
151+
StartActivityForResult(intent, requestCode);
152+
return ret;
153+
}
154+
155+
public Task<Tuple<Result, Intent>> StartActivityForResultAsync(Type type, int requestCode)
156+
{
157+
// NB: It's important that we set up the subscription *before* we
158+
// call ActivityForResult
159+
var ret = ActivityResult
160+
.Where(x => x.Item1 == requestCode)
161+
.Select(x => Tuple.Create(x.Item2, x.Item3))
162+
.FirstAsync()
163+
.ToTask();
164+
165+
StartActivityForResult(type, requestCode);
166+
return ret;
167+
}
168+
}
169+
}
170+

ReactiveUI.AndroidSupport/ReactiveUI.AndroidSupport.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
<HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\portable-windows8+net45+wp8\System.Reactive.PlatformServices.dll</HintPath>
6565
</Reference>
6666
<Reference Include="Xamarin.Android.Support.v4">
67-
<HintPath>..\packages\Xamarin.Android.Support.v4.20.0.0.2\lib\MonoAndroid10\Xamarin.Android.Support.v4.dll</HintPath>
67+
<HintPath>..\packages\Xamarin.Android.Support.v4.21.0.3.0\lib\MonoAndroid10\Xamarin.Android.Support.v4.dll</HintPath>
68+
</Reference>
69+
<Reference Include="Xamarin.Android.Support.v7.AppCompat">
70+
<HintPath>..\packages\Xamarin.Android.Support.v7.AppCompat.21.0.3.0\lib\MonoAndroid403\Xamarin.Android.Support.v7.AppCompat.dll</HintPath>
6871
</Reference>
6972
</ItemGroup>
7073
<ItemGroup>
@@ -74,6 +77,7 @@
7477
<Compile Include="ReactiveFragmentActivity.cs" />
7578
<Compile Include="ReactivePagerAdapter.cs" />
7679
<Compile Include="ReactiveFragment.cs" />
80+
<Compile Include="ReactiveActionBarActivity.cs" />
7781
</ItemGroup>
7882
<ItemGroup>
7983
<None Include="packages.config" />

ReactiveUI.AndroidSupport/packages.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<package id="Rx-Main" version="2.2.5" targetFramework="MonoAndroid403" />
77
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="MonoAndroid403" />
88
<package id="Splat" version="1.6.0" targetFramework="MonoAndroid403" />
9-
<package id="Xamarin.Android.Support.v4" version="20.0.0.2" targetFramework="MonoAndroid403" />
9+
<package id="Xamarin.Android.Support.v4" version="21.0.3.0" targetFramework="MonoAndroid403" />
10+
<package id="Xamarin.Android.Support.v7.AppCompat" version="21.0.3.0" targetFramework="MonoAndroid403" />
1011
</packages>

0 commit comments

Comments
 (0)