|
| 1 | +using AsyncNavigation.Abstractions; |
| 2 | +using AsyncNavigation.Core; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | + |
| 5 | +namespace AsyncNavigation; |
| 6 | + |
| 7 | +public static class NavigationContextExtensions |
| 8 | +{ |
| 9 | + public static bool TryResolveView(this NavigationContext navigationContext, [MaybeNullWhen(false)] out IView view) |
| 10 | + { |
| 11 | + if (navigationContext.Target.IsSet) |
| 12 | + { |
| 13 | + view = navigationContext.Target.Value!; |
| 14 | + return true; |
| 15 | + } |
| 16 | + view = default; |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + public static bool TryResolveNavigationAware(this NavigationContext navigationContext, [MaybeNullWhen(false)] out INavigationAware aware) |
| 21 | + { |
| 22 | + if (navigationContext.Target.IsSet) |
| 23 | + { |
| 24 | + if(navigationContext.Target.Value is not null |
| 25 | + && navigationContext.Target.Value.DataContext is INavigationAware navAware) |
| 26 | + { |
| 27 | + aware = navAware; |
| 28 | + return true; |
| 29 | + } |
| 30 | + } |
| 31 | + aware = default; |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + public static bool TryResolveViewAndAware(this NavigationContext navigationContext, |
| 36 | + [MaybeNullWhen(false)] out IView view, |
| 37 | + [MaybeNullWhen(false)] out INavigationAware aware) |
| 38 | + { |
| 39 | + view = default; |
| 40 | + aware = default; |
| 41 | + |
| 42 | + if (!navigationContext.Target.IsSet) |
| 43 | + return false; |
| 44 | + |
| 45 | + var target = navigationContext.Target.Value; |
| 46 | + if (target is null) |
| 47 | + return false; |
| 48 | + |
| 49 | + view = target; |
| 50 | + |
| 51 | + if (target.DataContext is INavigationAware navAware) |
| 52 | + { |
| 53 | + aware = navAware; |
| 54 | + return true; |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + public static NavigationContext WithParameter(this NavigationContext navigationContext, string key, object value) |
| 59 | + { |
| 60 | + if (navigationContext.IsCompleted && !navigationContext.IsForwordNavigation && !navigationContext.IsBackNavigation) |
| 61 | + throw new InvalidOperationException("Cannot add parameters after navigation is completed."); |
| 62 | + |
| 63 | + navigationContext.Parameters ??= new NavigationParameters(); |
| 64 | + navigationContext.Parameters.Add(key, value); |
| 65 | + return navigationContext; |
| 66 | + } |
| 67 | + |
| 68 | + public static NavigationContext WithParameters(this NavigationContext navigationContext, IEnumerable<KeyValuePair<string, object>> parameters) |
| 69 | + { |
| 70 | + |
| 71 | + if (navigationContext.IsCompleted && !navigationContext.IsForwordNavigation && !navigationContext.IsBackNavigation) |
| 72 | + throw new InvalidOperationException("Cannot add parameters after navigation is completed."); |
| 73 | + |
| 74 | + navigationContext.Parameters ??= new NavigationParameters(); |
| 75 | + navigationContext.Parameters.AddRange(parameters); |
| 76 | + return navigationContext; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments