|
1 |
| -using Android.Content; |
2 |
| -using System.Reflection; |
| 1 | +using Android.App; |
| 2 | +using Android.Content; |
| 3 | +using System.Collections.Concurrent; |
3 | 4 |
|
4 |
| -#nullable disable |
5 | 5 | namespace MauiBlazorToolkit.Platform
|
6 | 6 | {
|
7 | 7 | public class IntermediateActivity
|
8 | 8 | {
|
9 |
| - static readonly Type intermediateActivityType = typeof(FileSystem).Assembly.GetType("Microsoft.Maui.ApplicationModel.IntermediateActivity") ?? throw new Exception("IntermediateActivity type not found."); |
10 |
| - static readonly MethodInfo startAsync = intermediateActivityType.GetMethod("StartAsync", BindingFlags.Public | BindingFlags.Static) ?? throw new Exception("StartAsync method not found."); |
| 9 | + static readonly string guidExtra = Guid.NewGuid().ToString(); |
11 | 10 |
|
12 |
| - public static Task<Intent> StartAsync(Intent intent, int requestCode, Action<Intent>? onCreate = null, Action<Intent>? onResult = null) |
| 11 | + static readonly ConcurrentDictionary<string, IntermediateTask> pendingTasks = new(); |
| 12 | + |
| 13 | + static Activity? Activity => Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; |
| 14 | + |
| 15 | + internal static void OnActivityResult(int requestCode, Result resultCode, Intent? data) |
| 16 | + { |
| 17 | + string? guid = Activity?.Intent?.GetStringExtra(guidExtra); |
| 18 | + var task = GetIntermediateTask(guid, true); |
| 19 | + |
| 20 | + if (task is null) |
| 21 | + return; |
| 22 | + |
| 23 | + if (resultCode == Result.Canceled) |
| 24 | + { |
| 25 | + task.TaskCompletionSource.TrySetCanceled(); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + try |
| 30 | + { |
| 31 | + data ??= new Intent(); |
| 32 | + |
| 33 | + task.OnResult?.Invoke(data); |
| 34 | + |
| 35 | + task.TaskCompletionSource.TrySetResult(data); |
| 36 | + } |
| 37 | + catch (Exception ex) |
| 38 | + { |
| 39 | + task.TaskCompletionSource.TrySetException(ex); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static Task<Intent> StartAsync(Intent intent, int requestCode, Action<Intent>? onResult = null) |
| 45 | + { |
| 46 | + var data = new IntermediateTask(onResult); |
| 47 | + pendingTasks[data.Id] = data; |
| 48 | + |
| 49 | + Activity?.Intent?.PutExtra(guidExtra, data.Id); |
| 50 | + |
| 51 | + Activity?.StartActivityForResult(intent, requestCode); |
| 52 | + |
| 53 | + return data.TaskCompletionSource.Task; |
| 54 | + } |
| 55 | + |
| 56 | + static IntermediateTask? GetIntermediateTask(string? guid, bool remove = false) |
| 57 | + { |
| 58 | + if (string.IsNullOrEmpty(guid)) |
| 59 | + return null; |
| 60 | + |
| 61 | + if (remove) |
| 62 | + { |
| 63 | + pendingTasks.TryRemove(guid, out var removedTask); |
| 64 | + return removedTask; |
| 65 | + } |
| 66 | + |
| 67 | + pendingTasks.TryGetValue(guid, out var task); |
| 68 | + return task; |
| 69 | + } |
| 70 | + |
| 71 | + class IntermediateTask |
13 | 72 | {
|
14 |
| - return startAsync.Invoke(null, [intent, requestCode, onCreate, onResult]) as Task<Intent>; |
| 73 | + public IntermediateTask(Action<Intent>? onResult) |
| 74 | + { |
| 75 | + Id = Guid.NewGuid().ToString(); |
| 76 | + TaskCompletionSource = new TaskCompletionSource<Intent>(); |
| 77 | + |
| 78 | + OnResult = onResult; |
| 79 | + } |
| 80 | + |
| 81 | + public string Id { get; } |
| 82 | + |
| 83 | + public TaskCompletionSource<Intent> TaskCompletionSource { get; } |
| 84 | + |
| 85 | + public Action<Intent>? OnResult { get; } |
15 | 86 | }
|
16 | 87 | }
|
17 | 88 | }
|
0 commit comments