|
| 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 | + |
0 commit comments