Skip to content

Commit

Permalink
updates and fixes for Carousels and registration
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 18, 2020
1 parent bc2fadb commit e995276
Show file tree
Hide file tree
Showing 26 changed files with 390 additions and 304 deletions.
4 changes: 3 additions & 1 deletion e2e/Forms/src/HelloRegions/RegionDemoModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using HelloRegions.ViewModels;
using HelloRegions.Views;
using Prism.Ioc;
Expand All @@ -15,6 +15,8 @@ public void OnInitialized(IContainerProvider containerProvider)

public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterRegionServices();

containerRegistry.RegisterForNavigation<CarouselDemoRegion, CarouselDemoRegionViewModel>();
containerRegistry.RegisterForNavigation<CollectionViewDemoRegion, CollectionViewDemoRegionViewModel>();
containerRegistry.RegisterForNavigation<ContentViewDemoRegion, ContentViewDemoRegionViewModel>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Prism.Regions;
using Xamarin.Forms;
using Prism.Regions;

namespace HelloRegions.ViewModels
{
Expand All @@ -15,14 +11,4 @@ public FrameDemoRegionViewModel(IRegionManager regionManager)

protected override string RegionName => "FrameRegion";
}

public class ScrollViewDemoRegionViewModel : RegionDemoBase
{
public ScrollViewDemoRegionViewModel(IRegionManager regionManager)
: base(regionManager)
{
}

protected override string RegionName => "ScrollRegion";
}
}
13 changes: 13 additions & 0 deletions e2e/Forms/src/HelloRegions/ViewModels/IRefreshable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace HelloRegions.ViewModels
{
public interface IRefreshable
{
ICommand RefreshCommand { get; }
bool IsRefreshing { get; }
}
}
49 changes: 43 additions & 6 deletions e2e/Forms/src/HelloRegions/ViewModels/RegionAViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
using Prism.Navigation;
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Commands;
using Prism.Navigation;
using Prism.Regions.Navigation;

namespace HelloRegions.ViewModels
{
public class RegionAViewModel : ViewModelBase
public class RegionAViewModel : ViewModelBase, IRefreshable
{
public RegionAViewModel(INavigationService navigationService, IRegionNavigationService regionNavigation)
: base(regionNavigation)
public RegionAViewModel(INavigationService navigationService)
{
Title = "Hello from Region A";
NavigationService = navigationService;
RegionNavigation = regionNavigation;

RefreshCommand = new DelegateCommand(OnRefresh);
}

private string _refreshedMessage;
public string RefreshedMessage
{
get => _refreshedMessage;
set => SetProperty(ref _refreshedMessage, value);
}

public INavigationService NavigationService { get; set; }

public IRegionNavigationService RegionNavigation { get; set; }
public ICommand RefreshCommand { get; }

private bool _isRefreshing;
public bool IsRefreshing
{
get => _isRefreshing;
set => SetProperty(ref _isRefreshing, value);
}

private async void OnRefresh()
{
IsRefreshing = true;

try
{
await Task.Delay(TimeSpan.FromSeconds(3));
RefreshedMessage = $"Last Refreshed: {DateTime.Now:T}";
}
catch (System.Exception)
{
// You should catch exceptions and handle them here...
}
finally
{
IsRefreshing = false;
}
}
}
}
3 changes: 1 addition & 2 deletions e2e/Forms/src/HelloRegions/ViewModels/RegionBViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace HelloRegions.ViewModels
{
public class RegionBViewModel : ViewModelBase
{
public RegionBViewModel(IRegionNavigationService regionNavigationService)
: base(regionNavigationService)
public RegionBViewModel()
{
Title = "Hello from Region B";
}
Expand Down
3 changes: 1 addition & 2 deletions e2e/Forms/src/HelloRegions/ViewModels/RegionCViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace HelloRegions.ViewModels
{
public class RegionCViewModel : ViewModelBase
{
public RegionCViewModel(IRegionNavigationService regionNavigationService)
: base(regionNavigationService)
public RegionCViewModel()
{
Title = "Hello from Region C";
}
Expand Down
7 changes: 6 additions & 1 deletion e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace HelloRegions.ViewModels
{
public abstract class RegionDemoBase : BindableBase, IDestructible
public abstract class RegionDemoBase : BindableBase, IDestructible, IInitialize
{
private IRegionManager _regionManager { get; }

Expand Down Expand Up @@ -35,5 +35,10 @@ public void Destroy()
{
_regionManager.Regions.Remove(RegionName);
}

public void Initialize(INavigationParameters parameters)
{
_regionManager.RequestNavigate(RegionName, "RegionA", NavigationCallback);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Prism.Regions;

namespace HelloRegions.ViewModels
{
public class ScrollViewDemoRegionViewModel : RegionDemoBase
{
public ScrollViewDemoRegionViewModel(IRegionManager regionManager)
: base(regionManager)
{
}

protected override string RegionName => "ScrollRegion";
}
}
59 changes: 46 additions & 13 deletions e2e/Forms/src/HelloRegions/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using Prism.Commands;
using System;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions.Navigation;

namespace HelloRegions.ViewModels
{
public abstract class ViewModelBase : BindableBase, IRegionAware
public abstract class ViewModelBase : BindableBase, IRegionAware, Prism.IActiveAware
{
private IRegionNavigationService _navigationService { get; }

protected ViewModelBase(IRegionNavigationService regionNavigationService)
protected ViewModelBase()
{
_navigationService = regionNavigationService;
GoBackCommand = new DelegateCommand(GoBack);
GoForwardCommand = new DelegateCommand(GoForward);
GoBackCommand = new DelegateCommand(GoBack, () => Context?.NavigationService?.Journal?.CanGoBack ?? false)
.ObservesProperty(() => Context);
GoForwardCommand = new DelegateCommand(GoForward, () => Context?.NavigationService?.Journal?.CanGoForward ?? false)
.ObservesProperty(() => Context);
}

public string Title { get; set; }

private string _message;

public event EventHandler IsActiveChanged;

public string Message
{
get => _message;
Expand All @@ -28,30 +31,60 @@ public string Message

public DelegateCommand GoForwardCommand { get; }

public bool IsNavigationTarget(INavigationContext navigationContext) =>
false;
private bool _isActive;
public bool IsActive
{
get => _isActive;
set => SetProperty(ref _isActive, value, OnIsActiveChanged);
}

public void OnNavigatedFrom(INavigationContext navigationContext)
private INavigationContext _context;
private INavigationContext Context
{
get => _context;
set => SetProperty(ref _context, value);
}

public bool IsNavigationTarget(INavigationContext navigationContext)
{
Console.WriteLine($"{GetType().Name} IsNavigationTarget called");
var lookingFor = navigationContext.NavigatedName();
return _contextName == lookingFor;
}

public void OnNavigatedFrom(INavigationContext navigationContext)
{
Context = navigationContext;
Console.WriteLine($"{GetType().Name} NavigatedFrom");
}

private string _contextName;
public void OnNavigatedTo(INavigationContext navigationContext)
{
Context = navigationContext;
_contextName = navigationContext.NavigatedName();
if (navigationContext.Parameters.ContainsKey("message"))
Message = navigationContext.Parameters.GetValue<string>("message");
else
Message = "No Message provided...";

Console.WriteLine($"{GetType().Name} NavigatedTo");
}

private void GoBack()
{
_navigationService.Journal.GoBack();
Context.NavigationService.Journal.GoBack();
}

private void GoForward()
{
_navigationService.Journal.GoForward();
Context.NavigationService.Journal.GoForward();
}

private void OnIsActiveChanged()
{
IsActiveChanged?.Invoke(this, EventArgs.Empty);
Console.WriteLine($"{GetType().Name} is Active: {IsActive}");
}
}
}
44 changes: 12 additions & 32 deletions e2e/Forms/src/HelloRegions/Views/CarouselDemoRegion.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,23 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
xmlns:local="clr-namespace:HelloRegions.Views"
Title="Carousel - Demo"
x:Class="HelloRegions.Views.CarouselDemoRegion">
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Button" x:Key="SelectorButton">
<Setter Property="BorderColor" Value="Blue" />
<Setter Property="BorderWidth" Value="1" />
<Setter Property="Padding" Value="20,0" />
<Setter Property="BackgroundColor" Value="#666666" />
<Setter Property="TextColor" Value="White" />
<Setter Property="VerticalOptions" Value="Center" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid RowDefinitions="Auto,*,40,40"
Padding="20,40,20,20">

<Grid Padding="20,40,20,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Label Text="CarouselView - Demo Region"
HorizontalTextAlignment="Center" />
<CarouselView prism:RegionManager.RegionName="CarouselRegion"
Grid.Row="1" />

<StackLayout Orientation="Horizontal"
Padding="20"
HorizontalOptions="Center"
Grid.Row="2">
<Button Text="Region A"
Command="{Binding NavigateCommand}"
CommandParameter="RegionA?message=This%20is%20awesome!"
Style="{StaticResource SelectorButton}" />
<Button Text="Region B"
Command="{Binding NavigateCommand}"
CommandParameter="RegionB"
Style="{StaticResource SelectorButton}" />
<Button Text="Region C"
Command="{Binding NavigateCommand}"
CommandParameter="RegionC"
Style="{StaticResource SelectorButton}" />
</StackLayout>
<local:NavigationView Grid.Row="2" />
</Grid>
</ContentPage>

</ContentPage>
44 changes: 12 additions & 32 deletions e2e/Forms/src/HelloRegions/Views/CollectionViewDemoRegion.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,23 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
xmlns:local="clr-namespace:HelloRegions.Views"
Title="CollectionView - Demo"
x:Class="HelloRegions.Views.CollectionViewDemoRegion">
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="Button" x:Key="SelectorButton">
<Setter Property="BorderColor" Value="Blue" />
<Setter Property="BorderWidth" Value="1" />
<Setter Property="Padding" Value="20,0" />
<Setter Property="BackgroundColor" Value="#666666" />
<Setter Property="TextColor" Value="White" />
<Setter Property="VerticalOptions" Value="Center" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid RowDefinitions="Auto,*,40,40"
Padding="20,40,20,20">

<Grid Padding="20,40,20,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Label Text="CollectionView - Demo Region"
HorizontalTextAlignment="Center" />
<CollectionView prism:RegionManager.RegionName="CollectionRegion"
Grid.Row="1" />

<StackLayout Orientation="Horizontal"
Padding="20"
HorizontalOptions="Center"
Grid.Row="2">
<Button Text="Region A"
Command="{Binding NavigateCommand}"
CommandParameter="RegionA?message=This%20is%20awesome!"
Style="{StaticResource SelectorButton}" />
<Button Text="Region B"
Command="{Binding NavigateCommand}"
CommandParameter="RegionB"
Style="{StaticResource SelectorButton}" />
<Button Text="Region C"
Command="{Binding NavigateCommand}"
CommandParameter="RegionC"
Style="{StaticResource SelectorButton}" />
</StackLayout>
<local:NavigationView Grid.Row="2" />
</Grid>
</ContentPage>

</ContentPage>
Loading

0 comments on commit e995276

Please sign in to comment.