-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
b1b504d
commit e2afa49
Showing
6 changed files
with
209 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2021 .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 System; | ||
using System.Reactive; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Subjects; | ||
|
||
namespace ReactiveUI.DI.Tests.Mocks | ||
{ | ||
/// <summary> | ||
/// Activating View. | ||
/// </summary> | ||
/// <seealso cref="ReactiveUI.ReactiveObject" /> | ||
/// <seealso cref="ReactiveUI.IActivatableView" /> | ||
public sealed class ActivatingView : ReactiveObject, IViewFor<ActivatingViewModel>, IDisposable | ||
{ | ||
private int _count; | ||
private ActivatingViewModel _viewModel; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ActivatingView"/> class. | ||
/// </summary> | ||
public ActivatingView() | ||
{ | ||
this.WhenActivated(d => | ||
{ | ||
_count++; | ||
d(Disposable.Create(() => _count--)); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the count. | ||
/// </summary> | ||
/// <value> | ||
/// The count. | ||
/// </value> | ||
public int IsActiveCount => _count; | ||
|
||
/// <summary> | ||
/// Gets the loaded. | ||
/// </summary> | ||
public Subject<Unit> Loaded { get; } = new(); | ||
|
||
/// <summary> | ||
/// Gets the unloaded. | ||
/// </summary> | ||
public Subject<Unit> Unloaded { get; } = new(); | ||
|
||
/// <summary> | ||
/// Gets or sets the view model. | ||
/// </summary> | ||
public ActivatingViewModel ViewModel | ||
{ | ||
get => _viewModel; | ||
set => this.RaiseAndSetIfChanged(ref _viewModel, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the view model. | ||
/// </summary> | ||
object IViewFor.ViewModel | ||
{ | ||
get => ViewModel; | ||
set => ViewModel = (ActivatingViewModel)value; | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Loaded.Dispose(); | ||
Unloaded.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2021 .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 System; | ||
using System.Reactive.Linq; | ||
|
||
namespace ReactiveUI.DI.Tests.Mocks | ||
{ | ||
/// <summary> | ||
/// Simulates a activating view fetcher. | ||
/// </summary> | ||
public class ActivatingViewFetcher : IActivationForViewFetcher | ||
{ | ||
/// <summary> | ||
/// Determines the priority that the Activation View Fetcher | ||
/// will be able to process the view type. | ||
/// 0 means it cannot activate the View, value larger than 0 | ||
/// indicates it can activate the View. | ||
/// The class derived off IActivationForViewFetcher which returns | ||
/// the highest affinity value will be used to activate the View. | ||
/// </summary> | ||
/// <param name="view">The type for the View.</param> | ||
/// <returns> | ||
/// The affinity value which is equal to 0 or above. | ||
/// </returns> | ||
public int GetAffinityForView(Type view) => view == typeof(ActivatingView) ? 100 : 0; | ||
|
||
/// <summary> | ||
/// Gets a Observable which will activate the View. | ||
/// This is called after the GetAffinityForView method. | ||
/// </summary> | ||
/// <param name="view">The view to get the activation observable for.</param> | ||
/// <returns> | ||
/// A Observable which will returns if Activation was successful. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException">The view is null.</exception> | ||
public IObservable<bool> GetActivationForView(IActivatableView view) | ||
{ | ||
if (view is not ActivatingView av) | ||
{ | ||
throw new ArgumentNullException(nameof(view)); | ||
} | ||
|
||
return av.Loaded.Select(_ => true).Merge(av.Unloaded.Select(_ => false)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2021 .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 System.Reactive.Disposables; | ||
|
||
namespace ReactiveUI.DI.Tests.Mocks | ||
{ | ||
/// <summary> | ||
/// ActivatingViewModel. | ||
/// </summary> | ||
/// <seealso cref="ReactiveUI.ReactiveObject" /> | ||
public class ActivatingViewModel : ReactiveObject, IActivatableViewModel | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ActivatingViewModel"/> class. | ||
/// </summary> | ||
public ActivatingViewModel() | ||
{ | ||
Activator = new ViewModelActivator(); | ||
|
||
this.WhenActivated(d => | ||
{ | ||
IsActiveCount++; | ||
d(Disposable.Create(() => IsActiveCount--)); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the Activator which will be used by the View when Activation/Deactivation occurs. | ||
/// </summary> | ||
public ViewModelActivator Activator { get; protected set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the active count. | ||
/// </summary> | ||
public int IsActiveCount { get; protected set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters