Skip to content

Commit

Permalink
lifecycle improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Oct 23, 2022
1 parent 7226471 commit 5d8ff0c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/MAUI/TinyMvvm.Maui/ITinyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ITinyViewModel
/// This method runs when navigation parameters are set.
/// </summary>
/// <returns></returns>
Task ParameterSet();
Task OnParameterSet();

/// <summary>
/// This method will run every time the view is apperaing.
Expand Down
29 changes: 20 additions & 9 deletions src/MAUI/TinyMvvm.Maui/TinyView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,40 @@ public abstract class TinyView : ContentPage
{
internal SemaphoreSlim InternalLock { get; private set; } = new SemaphoreSlim(1, 1);

protected override void OnBindingContextChanged()
protected override async void OnBindingContextChanged()
{
base.OnBindingContextChanged();

if (BindingContext is ITinyViewModel viewModel)
{
try
if (!viewModel.IsInitialized)
{
if (!viewModel.IsInitialized)

async Task InternalInitialize()
{
MainThread.BeginInvokeOnMainThread(async () =>
try
{
await InternalLock.WaitAsync();
await viewModel.Initialize();

viewModel.IsInitialized = true;
});
}
finally
{
InternalLock.Release();
}
}

if (MainThread.IsMainThread)
{
await InternalInitialize();
}
else
{
MainThread.BeginInvokeOnMainThread(async () => await InternalInitialize());
}
}
finally
{
InternalLock.Release();
}

}
}

Expand Down
17 changes: 6 additions & 11 deletions src/MAUI/TinyMvvm.Maui/TinyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public TinyViewModel()


/// <inheritdoc />
public virtual Task ParameterSet() => Task.CompletedTask;
public virtual Task OnParameterSet() => Task.CompletedTask;

/// <inheritdoc />
public virtual Task OnAppearing()
Expand Down Expand Up @@ -90,7 +90,7 @@ public INavigation Navigation
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsNotBusy))]
private bool _isBusy;


/// <inheritdoc />
public bool IsNotBusy
Expand All @@ -104,7 +104,7 @@ public bool IsNotBusy
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsNotInitialized))]
private bool _isInitialized;


/// <inheritdoc />
public bool IsNotInitialized
Expand All @@ -121,7 +121,7 @@ public bool IsNotInitialized
/// <inheritdoc />
public async void ApplyQueryAttributes(IDictionary<string, object> query)
{
if(query == null || query.Count == 0)
if (query == null || query.Count == 0)
{
return;
}
Expand Down Expand Up @@ -152,20 +152,15 @@ public async void ApplyQueryAttributes(IDictionary<string, object> query)
QueryParameters = query;
}

async Task RunParameterSet()
{
await ParameterSet();
}

if (MainThread.IsMainThread)
{
await RunParameterSet();
await OnParameterSet();
}
else
{
MainThread.BeginInvokeOnMainThread(async () =>
{
await RunParameterSet();
await OnParameterSet();
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MAUI/TinyMvvm.Sample/ViewModels/DetailsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public DetailsViewModel(ICityService cityService)
this.cityService = cityService;
}

public override async Task ParameterSet()
public override async Task OnParameterSet()
{
IsBusy = true;

Expand Down
2 changes: 1 addition & 1 deletion src/MAUI/TinyMvvm.Sample/ViewModels/ListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async Task Initialize()
IsBusy = true;

var result = await cityService.GetAll();
Cities = new ObservableCollection<City>(result);
Cities = new ObservableCollection<City>(result.Take(100));

IsBusy = false;
}
Expand Down

0 comments on commit 5d8ff0c

Please sign in to comment.