|
| 1 | +// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using Android.App; |
| 7 | +using Android.Content; |
| 8 | +using Android.Runtime; |
| 9 | +using AndroidX.AppCompat.App; |
| 10 | + |
| 11 | +namespace ReactiveUI.AndroidX; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// This is an Activity that is both an Activity and has ReactiveObject powers |
| 15 | +/// (i.e. you can call RaiseAndSetIfChanged). |
| 16 | +/// </summary> |
| 17 | +public class ReactiveAppCompatActivity : AppCompatActivity, IReactiveObject, IReactiveNotifyPropertyChanged<ReactiveAppCompatActivity>, IHandleObservableErrors |
| 18 | +{ |
| 19 | + private readonly Subject<Unit> _activated = new(); |
| 20 | + private readonly Subject<Unit> _deactivated = new(); |
| 21 | + private readonly Subject<(int requestCode, Result result, Intent? intent)> _activityResult = new(); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="ReactiveAppCompatActivity" /> class. |
| 25 | + /// </summary> |
| 26 | + protected ReactiveAppCompatActivity() |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="ReactiveAppCompatActivity" /> class. |
| 32 | + /// </summary> |
| 33 | + /// <param name="handle">The handle.</param> |
| 34 | + /// <param name="ownership">The ownership.</param> |
| 35 | + protected ReactiveAppCompatActivity(in IntPtr handle, JniHandleOwnership ownership) |
| 36 | + : base(handle, ownership) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public event PropertyChangingEventHandler? PropertyChanging; |
| 42 | + |
| 43 | + /// <inheritdoc/> |
| 44 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 45 | + |
| 46 | + /// <inheritdoc/> |
| 47 | + public IObservable<IReactivePropertyChangedEventArgs<ReactiveAppCompatActivity>> Changing => this.GetChangingObservable(); |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + public IObservable<IReactivePropertyChangedEventArgs<ReactiveAppCompatActivity>> Changed => this.GetChangedObservable(); |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + public IObservable<Exception> ThrownExceptions => this.GetThrownExceptionsObservable(); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets a signal when activated. |
| 57 | + /// </summary> |
| 58 | + /// <value> |
| 59 | + /// The activated. |
| 60 | + /// </value> |
| 61 | + public IObservable<Unit> Activated => _activated.AsObservable(); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Gets a signal when deactivated. |
| 65 | + /// </summary> |
| 66 | + /// <value> |
| 67 | + /// The deactivated. |
| 68 | + /// </value> |
| 69 | + public IObservable<Unit> Deactivated => _deactivated.AsObservable(); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets the activity result. |
| 73 | + /// </summary> |
| 74 | + /// <value> |
| 75 | + /// The activity result. |
| 76 | + /// </value> |
| 77 | + public IObservable<(int requestCode, Result result, Intent? intent)> ActivityResult => _activityResult.AsObservable(); |
| 78 | + |
| 79 | + /// <inheritdoc/> |
| 80 | + void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args) => PropertyChanging?.Invoke(this, args); |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args); |
| 84 | + |
| 85 | + /// <inheritdoc/> |
| 86 | + public IDisposable SuppressChangeNotifications() => IReactiveObjectExtensions.SuppressChangeNotifications(this); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Starts the activity for result asynchronously. |
| 90 | + /// </summary> |
| 91 | + /// <param name="intent">The intent.</param> |
| 92 | + /// <param name="requestCode">The request code.</param> |
| 93 | + /// <returns>A task with the result and intent.</returns> |
| 94 | + public Task<(Result result, Intent? intent)> StartActivityForResultAsync(Intent intent, int requestCode) |
| 95 | + { |
| 96 | + // NB: It's important that we set up the subscription *before* we |
| 97 | + // call ActivityForResult |
| 98 | + var ret = ActivityResult |
| 99 | + .Where(x => x.requestCode == requestCode) |
| 100 | + .Select(x => (x.result, x.intent)) |
| 101 | + .FirstAsync() |
| 102 | + .ToTask(); |
| 103 | + |
| 104 | + StartActivityForResult(intent, requestCode); |
| 105 | + return ret; |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Starts the activity for result asynchronously. |
| 110 | + /// </summary> |
| 111 | + /// <param name="type">The type.</param> |
| 112 | + /// <param name="requestCode">The request code.</param> |
| 113 | + /// <returns>A task with the result and intent.</returns> |
| 114 | + public Task<(Result result, Intent? intent)> StartActivityForResultAsync(Type type, int requestCode) |
| 115 | + { |
| 116 | + // NB: It's important that we set up the subscription *before* we |
| 117 | + // call ActivityForResult |
| 118 | + var ret = ActivityResult |
| 119 | + .Where(x => x.requestCode == requestCode) |
| 120 | + .Select(x => (x.result, x.intent)) |
| 121 | + .FirstAsync() |
| 122 | + .ToTask(); |
| 123 | + |
| 124 | + StartActivityForResult(type, requestCode); |
| 125 | + return ret; |
| 126 | + } |
| 127 | + |
| 128 | + /// <inheritdoc/> |
| 129 | + protected override void OnPause() |
| 130 | + { |
| 131 | + base.OnPause(); |
| 132 | + _deactivated.OnNext(Unit.Default); |
| 133 | + } |
| 134 | + |
| 135 | + /// <inheritdoc/> |
| 136 | + protected override void OnResume() |
| 137 | + { |
| 138 | + base.OnResume(); |
| 139 | + _activated.OnNext(Unit.Default); |
| 140 | + } |
| 141 | + |
| 142 | + /// <inheritdoc/> |
| 143 | + protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data) |
| 144 | + { |
| 145 | + base.OnActivityResult(requestCode, resultCode, data); |
| 146 | + _activityResult.OnNext((requestCode, resultCode, data)); |
| 147 | + } |
| 148 | + |
| 149 | + /// <inheritdoc/> |
| 150 | + protected override void Dispose(bool disposing) |
| 151 | + { |
| 152 | + if (disposing) |
| 153 | + { |
| 154 | + _activated.Dispose(); |
| 155 | + _deactivated.Dispose(); |
| 156 | + _activityResult.Dispose(); |
| 157 | + } |
| 158 | + |
| 159 | + base.Dispose(disposing); |
| 160 | + } |
| 161 | +} |
0 commit comments