-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReactiveAppCompatActivity.cs
194 lines (165 loc) · 6.43 KB
/
ReactiveAppCompatActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Support.V7.App;
namespace ReactiveUI.AndroidSupport;
/// <summary>
/// This is an Activity that is both an Activity and has ReactiveObject powers
/// (i.e. you can call RaiseAndSetIfChanged).
/// </summary>
/// <typeparam name="TViewModel">The view model type.</typeparam>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "Classes with the same class names within.")]
public class ReactiveAppCompatActivity<TViewModel> : ReactiveAppCompatActivity, IViewFor<TViewModel>, ICanActivate
where TViewModel : class
{
private TViewModel? _viewModel;
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveAppCompatActivity{TViewModel}"/> class.
/// </summary>
protected ReactiveAppCompatActivity()
{
}
/// <inheritdoc/>
public TViewModel? ViewModel
{
get => _viewModel;
set => this.RaiseAndSetIfChanged(ref _viewModel, value);
}
/// <inheritdoc/>
object? IViewFor.ViewModel
{
get => _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 ReactiveAppCompatActivity : AppCompatActivity, IReactiveObject, IReactiveNotifyPropertyChanged<ReactiveAppCompatActivity>, IHandleObservableErrors
{
private readonly Subject<Unit> _activated = new();
private readonly Subject<Unit> _deactivated = new();
private readonly Subject<(int requestCode, Result result, Intent? intent)> _activityResult = new();
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveAppCompatActivity" /> class.
/// </summary>
protected ReactiveAppCompatActivity()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveAppCompatActivity" /> class.
/// </summary>
/// <param name="handle">The handle.</param>
/// <param name="ownership">The ownership.</param>
protected ReactiveAppCompatActivity(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}
/// <inheritdoc/>
public event PropertyChangingEventHandler? PropertyChanging;
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <inheritdoc/>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveAppCompatActivity>> Changing => this.GetChangingObservable();
/// <inheritdoc/>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveAppCompatActivity>> Changed => this.GetChangedObservable();
/// <inheritdoc/>
public IObservable<Exception> ThrownExceptions => this.GetThrownExceptionsObservable();
/// <summary>
/// Gets a signal when activated.
/// </summary>
/// <value>
/// The activated.
/// </value>
public IObservable<Unit> Activated => _activated.AsObservable();
/// <summary>
/// Gets a signal when deactivated.
/// </summary>
/// <value>
/// The deactivated.
/// </value>
public IObservable<Unit> Deactivated => _deactivated.AsObservable();
/// <summary>
/// Gets the activity result.
/// </summary>
/// <value>
/// The activity result.
/// </value>
public IObservable<(int requestCode, Result result, Intent? intent)> ActivityResult => _activityResult.AsObservable();
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args) => PropertyChanging?.Invoke(this, args);
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
/// <inheritdoc/>
public IDisposable SuppressChangeNotifications() => IReactiveObjectExtensions.SuppressChangeNotifications(this);
/// <summary>
/// Starts the activity for result asynchronously.
/// </summary>
/// <param name="intent">The intent.</param>
/// <param name="requestCode">The request code.</param>
/// <returns>A task with the result and intent.</returns>
public Task<(Result result, Intent? 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.requestCode == requestCode)
.Select(x => (x.result, x.intent))
.FirstAsync()
.ToTask();
StartActivityForResult(intent, requestCode);
return ret;
}
/// <summary>
/// Starts the activity for result asynchronously.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="requestCode">The request code.</param>
/// <returns>A task with the result and intent.</returns>
public Task<(Result result, Intent? 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.requestCode == requestCode)
.Select(x => (x.result, x.intent))
.FirstAsync()
.ToTask();
StartActivityForResult(type, requestCode);
return ret;
}
/// <inheritdoc/>
protected override void OnPause()
{
base.OnPause();
_deactivated.OnNext(Unit.Default);
}
/// <inheritdoc/>
protected override void OnResume()
{
base.OnResume();
_activated.OnNext(Unit.Default);
}
/// <inheritdoc/>
protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
{
base.OnActivityResult(requestCode, resultCode, data);
_activityResult.OnNext((requestCode, resultCode, data));
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
_activated.Dispose();
_deactivated.Dispose();
_activityResult.Dispose();
}
base.Dispose(disposing);
}
}