Skip to content

Commit a578339

Browse files
committed
Merge branch 'main' into 0604
2 parents 8317630 + 80a61d8 commit a578339

File tree

9 files changed

+160
-71
lines changed

9 files changed

+160
-71
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
<PropertyGroup>
3+
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-ios'">14.2</SupportedOSPlatformVersion>
4+
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-maccatalyst'">14.0</SupportedOSPlatformVersion>
5+
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
6+
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
7+
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<ItemGroup>
3+
<PackageReference Include="System.Text.Json" Version="6.0.0" />
4+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
5+
</ItemGroup>
6+
7+
<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
8+
<!-- Required - WinUI does not yet have buildTransitive for everything -->
9+
</ItemGroup>
10+
</Project>

sessions/Season-06/0603/src/MonkeyFinder/AppShell.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public partial class AppShell : Shell
55
public AppShell()
66
{
77
InitializeComponent();
8+
9+
Routing.RegisterRoute(nameof(DetailsPage), typeof(DetailsPage));
810
}
911
}

sessions/Season-06/0603/src/MonkeyFinder/MauiProgram.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ public static MauiApp CreateMauiApp()
1616
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
1717
});
1818

19-
#if DEBUG
20-
builder.Logging.AddDebug();
21-
#endif
22-
2319
builder.Services.AddSingleton<MonkeyService>();
2420
builder.Services.AddSingleton<MonkeysViewModel>();
2521
builder.Services.AddSingleton<MainPage>();
2622

23+
builder.Services.AddTransient<MonkeyDetailsViewModel>();
24+
builder.Services.AddTransient<DetailsPage>();
25+
2726
return builder.Build();
2827
}
2928
}
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="MonkeyFinder.DetailsPage">
2+
<ContentPage
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
x:Class="MonkeyFinder.DetailsPage"
6+
xmlns:viewmodel="clr-namespace:MonkeyFinder.ViewModel"
7+
x:DataType="viewmodel:MonkeyDetailsViewModel"
8+
Title="{Binding Monkey.Name}">
9+
10+
11+
<ScrollView>
12+
<Grid RowDefinitions="Auto,Auto,*">
13+
14+
<BoxView
15+
BackgroundColor="{StaticResource Primary}"
16+
Grid.RowSpan="2"
17+
HorizontalOptions="Fill"
18+
VerticalOptions="Fill"/>
19+
20+
<Border StrokeShape="RoundRectangle 80"
21+
Stroke="White"
22+
StrokeThickness="6"
23+
HeightRequest="160"
24+
WidthRequest="160"
25+
Margin="0,8,0,0"
26+
HorizontalOptions="Center"
27+
VerticalOptions="Center">
28+
<Image Aspect="AspectFill"
29+
HeightRequest="160"
30+
HorizontalOptions="Center"
31+
VerticalOptions="Center"
32+
Source="{Binding Monkey.Image}"
33+
WidthRequest="160"/>
34+
</Border>
35+
36+
<Label Style="{StaticResource LargeLabel}"
37+
Grid.Row="1"
38+
TextColor="White"
39+
FontAttributes="Bold"
40+
Text="{Binding Monkey.Name}"
41+
HorizontalOptions="Center"
42+
Margin="0,0,0,8"/>
43+
44+
<VerticalStackLayout Grid.Row="2" Padding="10" Spacing="10">
45+
<Label Style="{StaticResource MediumLabel}" Text="{Binding Monkey.Details}" />
46+
<Label Style="{StaticResource SmallLabel}" Text="{Binding Monkey.Location, StringFormat='Location: {0}'}" />
47+
<Label Style="{StaticResource SmallLabel}" Text="{Binding Monkey.Population, StringFormat='Population: {0}'}" />
48+
</VerticalStackLayout>
49+
50+
</Grid>
51+
</ScrollView>
552
</ContentPage>

sessions/Season-06/0603/src/MonkeyFinder/View/DetailsPage.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ namespace MonkeyFinder;
22

33
public partial class DetailsPage : ContentPage
44
{
5-
public DetailsPage()
5+
public DetailsPage(MonkeyDetailsViewModel viewModel)
66
{
77
InitializeComponent();
8+
BindingContext = viewModel;
89
}
910
}

sessions/Season-06/0603/src/MonkeyFinder/View/MainPage.xaml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,46 @@
77
xmlns:viewmodel="clr-namespace:MonkeyFinder.ViewModel"
88
x:DataType="viewmodel:MonkeysViewModel"
99
Title="{Binding Title}">
10-
<Grid
10+
<Grid
1111
ColumnDefinitions="*,*"
1212
ColumnSpacing="5"
1313
RowDefinitions="*,Auto"
1414
RowSpacing="0">
15-
<CollectionView
15+
<CollectionView
1616
Grid.ColumnSpan="2"
1717
ItemsSource="{Binding Monkeys}"
1818
SelectionMode="None">
19-
<CollectionView.ItemTemplate>
20-
<DataTemplate x:DataType="model:Monkey">
21-
<Grid Padding="10">
22-
<Frame HeightRequest="125" Style="{StaticResource CardView}">
23-
<Grid Padding="0" ColumnDefinitions="125,*">
24-
<Image Aspect="AspectFill" Source="{Binding Image}"
19+
<CollectionView.ItemTemplate>
20+
<DataTemplate x:DataType="model:Monkey">
21+
<Grid Padding="10">
22+
<Frame HeightRequest="125" Style="{StaticResource CardView}">
23+
24+
<Frame.GestureRecognizers>
25+
<TapGestureRecognizer
26+
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MonkeysViewModel}}, Path=GoToDetailsCommand}"
27+
CommandParameter="{Binding .}"/>
28+
</Frame.GestureRecognizers>
29+
30+
31+
<Grid Padding="0" ColumnDefinitions="125,*">
32+
<Image Aspect="AspectFill" Source="{Binding Image}"
2533
WidthRequest="125"
2634
HeightRequest="125"/>
27-
<VerticalStackLayout
35+
<VerticalStackLayout
2836
VerticalOptions="Center"
2937
Grid.Column="1"
3038
Padding="10">
31-
<Label Style="{StaticResource LargeLabel}" Text="{Binding Name}" />
32-
<Label Style="{StaticResource MediumLabel}" Text="{Binding Location}" />
33-
</VerticalStackLayout>
34-
</Grid>
35-
</Frame>
36-
</Grid>
37-
</DataTemplate>
38-
</CollectionView.ItemTemplate>
39-
</CollectionView>
39+
<Label Style="{StaticResource LargeLabel}" Text="{Binding Name}" />
40+
<Label Style="{StaticResource MediumLabel}" Text="{Binding Location}" />
41+
</VerticalStackLayout>
42+
</Grid>
43+
</Frame>
44+
</Grid>
45+
</DataTemplate>
46+
</CollectionView.ItemTemplate>
47+
</CollectionView>
4048

41-
<Button Text="Get Monkeys"
49+
<Button Text="Get Monkeys"
4250
Command="{Binding GetMonkeysCommand}"
4351
IsEnabled="{Binding IsNotBusy}"
4452
Grid.Row="1"
@@ -47,12 +55,12 @@
4755
Margin="8"/>
4856

4957

50-
<ActivityIndicator IsVisible="{Binding IsBusy}"
58+
<ActivityIndicator IsVisible="{Binding IsBusy}"
5159
IsRunning="{Binding IsBusy}"
5260
HorizontalOptions="Fill"
5361
VerticalOptions="Center"
5462
Color="{StaticResource Primary}"
5563
Grid.RowSpan="2"
5664
Grid.ColumnSpan="2"/>
57-
</Grid>
65+
</Grid>
5866
</ContentPage>
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
namespace MonkeyFinder.ViewModel;
22

3-
public class MonkeyDetailsViewModel : BaseViewModel
4-
{
5-
}
3+
//Add QueryProperty
4+
[QueryProperty(nameof(Monkey), "Monkey")]
5+
public partial class MonkeyDetailsViewModel : BaseViewModel {
6+
public MonkeyDetailsViewModel() {
7+
}
8+
9+
[ObservableProperty]
10+
Monkey monkey;
11+
}

sessions/Season-06/0603/src/MonkeyFinder/ViewModel/MonkeysViewModel.cs

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,50 @@
22

33
namespace MonkeyFinder.ViewModel;
44

5-
public partial class MonkeysViewModel : BaseViewModel
6-
{
7-
public ObservableCollection<Monkey> Monkeys { get; } = new();
8-
MonkeyService monkeyService;
9-
public MonkeysViewModel(MonkeyService monkeyService)
10-
{
11-
Title = "Monkey Finder";
12-
this.monkeyService = monkeyService;
13-
}
14-
15-
[RelayCommand]
16-
async Task GetMonkeysAsync()
17-
{
18-
if (IsBusy)
19-
return;
20-
21-
try
22-
{
23-
IsBusy = true;
24-
var monkeys = await monkeyService.GetMonkeys();
25-
26-
if(Monkeys.Count != 0)
27-
Monkeys.Clear();
28-
29-
foreach(var monkey in monkeys)
30-
Monkeys.Add(monkey);
31-
32-
}
33-
catch (Exception ex)
34-
{
35-
Debug.WriteLine($"Unable to get monkeys: {ex.Message}");
36-
await Shell.Current.DisplayAlert("Error!", ex.Message, "OK");
37-
}
38-
finally
39-
{
40-
IsBusy = false;
41-
}
42-
43-
}
5+
public partial class MonkeysViewModel : BaseViewModel {
6+
public ObservableCollection<Monkey> Monkeys { get; } = new();
7+
MonkeyService monkeyService;
8+
public MonkeysViewModel(MonkeyService monkeyService) {
9+
Title = "Monkey Finder";
10+
this.monkeyService = monkeyService;
11+
}
12+
13+
[RelayCommand]
14+
async Task GetMonkeysAsync() {
15+
if (IsBusy)
16+
return;
17+
18+
try {
19+
IsBusy = true;
20+
var monkeys = await monkeyService.GetMonkeys();
21+
22+
if (Monkeys.Count != 0)
23+
Monkeys.Clear();
24+
25+
foreach (var monkey in monkeys)
26+
Monkeys.Add(monkey);
27+
28+
}
29+
catch (Exception ex) {
30+
Debug.WriteLine($"Unable to get monkeys: {ex.Message}");
31+
await Shell.Current.DisplayAlert("Error!", ex.Message, "OK");
32+
}
33+
finally {
34+
IsBusy = false;
35+
}
36+
37+
}
38+
39+
[RelayCommand]
40+
async Task GoToDetails(Monkey monkey) {
41+
if (monkey == null)
42+
return;
43+
44+
await Shell.Current.GoToAsync(nameof(DetailsPage), true, new Dictionary<string, object>
45+
{
46+
{"Monkey", monkey }
47+
});
48+
}
49+
4450
}
51+

0 commit comments

Comments
 (0)