Skip to content

Commit 3f283bc

Browse files
committed
After session
1 parent a578339 commit 3f283bc

File tree

7 files changed

+136
-34
lines changed

7 files changed

+136
-34
lines changed

sessions/Season-06/0604/src/Directory.build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-ios'">14.2</SupportedOSPlatformVersion>
44
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-maccatalyst'">14.0</SupportedOSPlatformVersion>
5-
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
5+
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">33.0</SupportedOSPlatformVersion>
66
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
77
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
88
</PropertyGroup>

sessions/Season-06/0604/src/MonkeyFinder.sln

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ VisualStudioVersion = 17.0.31611.283
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonkeyFinder", "MonkeyFinder\MonkeyFinder.csproj", "{C60F2B1C-790E-4432-A719-52182A333ADE}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{393B8F30-F402-46A9-B209-8C71897A3416}"
9-
ProjectSection(SolutionItems) = preProject
10-
..\Directory.build.props = ..\Directory.build.props
11-
..\Directory.build.targets = ..\Directory.build.targets
12-
README.md = README.md
13-
EndProjectSection
14-
EndProject
158
Global
169
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1710
Debug|Any CPU = Debug|Any CPU

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
23
using MonkeyFinder.Services;
34
using MonkeyFinder.View;
45

@@ -16,6 +17,10 @@ public static MauiApp CreateMauiApp()
1617
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
1718
});
1819

20+
builder.Services.AddSingleton<IConnectivity>(Connectivity.Current);
21+
builder.Services.AddSingleton<IGeolocation>(Geolocation.Default);
22+
builder.Services.AddSingleton<IMap>(Map.Default);
23+
1924
builder.Services.AddSingleton<MonkeyService>();
2025
builder.Services.AddSingleton<MonkeysViewModel>();
2126
builder.Services.AddSingleton<MainPage>();

sessions/Season-06/0604/src/MonkeyFinder/View/DetailsPage.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
Margin="0,0,0,8"/>
4343

4444
<VerticalStackLayout Grid.Row="2" Padding="10" Spacing="10">
45+
<Button Text="Show on Map"
46+
Command="{Binding OpenMapCommand}"
47+
HorizontalOptions="Center"
48+
WidthRequest="200"
49+
Margin="8"
50+
Style="{StaticResource ButtonOutline}"/>
4551
<Label Style="{StaticResource MediumLabel}" Text="{Binding Monkey.Details}" />
4652
<Label Style="{StaticResource SmallLabel}" Text="{Binding Monkey.Location, StringFormat='Location: {0}'}" />
4753
<Label Style="{StaticResource SmallLabel}" Text="{Binding Monkey.Population, StringFormat='Population: {0}'}" />

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,68 @@
33
x:Class="MonkeyFinder.View.MainPage"
44
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
55
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
67
xmlns:model="clr-namespace:MonkeyFinder.Model"
78
xmlns:viewmodel="clr-namespace:MonkeyFinder.ViewModel"
89
x:DataType="viewmodel:MonkeysViewModel"
9-
Title="{Binding Title}">
10+
Title="{Binding Title}"
11+
ios:Page.UseSafeArea="True">
1012
<Grid
1113
ColumnDefinitions="*,*"
1214
ColumnSpacing="5"
1315
RowDefinitions="*,Auto"
1416
RowSpacing="0">
15-
<CollectionView
17+
<RefreshView
18+
Grid.ColumnSpan="2"
19+
Command="{Binding GetMonkeysCommand}"
20+
IsRefreshing="{Binding IsRefreshing}">
21+
<CollectionView
1622
Grid.ColumnSpan="2"
1723
ItemsSource="{Binding Monkeys}"
1824
SelectionMode="None">
19-
<CollectionView.ItemTemplate>
20-
<DataTemplate x:DataType="model:Monkey">
21-
<Grid Padding="10">
22-
<Frame HeightRequest="125" Style="{StaticResource CardView}">
2325

24-
<Frame.GestureRecognizers>
25-
<TapGestureRecognizer
26+
<CollectionView.EmptyView>
27+
<StackLayout Padding="100">
28+
<Image
29+
HorizontalOptions="Center"
30+
Source="nodata.png"
31+
HeightRequest="160"
32+
WidthRequest="160"
33+
VerticalOptions="Center" />
34+
</StackLayout>
35+
</CollectionView.EmptyView>
36+
37+
<CollectionView.ItemTemplate>
38+
<DataTemplate x:DataType="model:Monkey">
39+
<Grid Padding="10">
40+
<Frame HeightRequest="125" Style="{StaticResource CardView}">
41+
42+
<Frame.GestureRecognizers>
43+
<TapGestureRecognizer
2644
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MonkeysViewModel}}, Path=GoToDetailsCommand}"
2745
CommandParameter="{Binding .}"/>
28-
</Frame.GestureRecognizers>
46+
</Frame.GestureRecognizers>
2947

3048

31-
<Grid Padding="0" ColumnDefinitions="125,*">
32-
<Image Aspect="AspectFill" Source="{Binding Image}"
49+
<Grid Padding="0" ColumnDefinitions="125,*">
50+
<Image Aspect="AspectFill" Source="{Binding Image}"
3351
WidthRequest="125"
3452
HeightRequest="125"/>
35-
<VerticalStackLayout
53+
<VerticalStackLayout
3654
VerticalOptions="Center"
3755
Grid.Column="1"
3856
Padding="10">
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>
57+
<Label Style="{StaticResource LargeLabel}" Text="{Binding Name}" />
58+
<Label Style="{StaticResource MediumLabel}" Text="{Binding Location}" />
59+
</VerticalStackLayout>
60+
</Grid>
61+
</Frame>
62+
</Grid>
63+
</DataTemplate>
64+
</CollectionView.ItemTemplate>
65+
</CollectionView>
66+
67+
</RefreshView>
4868

4969
<Button Text="Get Monkeys"
5070
Command="{Binding GetMonkeysCommand}"
@@ -54,6 +74,13 @@
5474
Style="{StaticResource ButtonOutline}"
5575
Margin="8"/>
5676

77+
<Button Text="Find Closest"
78+
Command="{Binding GetClosestMonkeyCommand}"
79+
IsEnabled="{Binding IsNotBusy}"
80+
Grid.Row="1"
81+
Grid.Column="1"
82+
Style="{StaticResource ButtonOutline}"
83+
Margin="8"/>
5784

5885
<ActivityIndicator IsVisible="{Binding IsBusy}"
5986
IsRunning="{Binding IsBusy}"
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
namespace MonkeyFinder.ViewModel;
1+
using Microsoft.Maui.ApplicationModel;
2+
3+
namespace MonkeyFinder.ViewModel;
24

35
//Add QueryProperty
46
[QueryProperty(nameof(Monkey), "Monkey")]
57
public partial class MonkeyDetailsViewModel : BaseViewModel {
6-
public MonkeyDetailsViewModel() {
8+
9+
private readonly IMap _Map;
10+
11+
public MonkeyDetailsViewModel(IMap map) {
12+
_Map = map;
713
}
814

915
[ObservableProperty]
1016
Monkey monkey;
17+
18+
[RelayCommand]
19+
async Task OpenMap() {
20+
try {
21+
await _Map.OpenAsync(Monkey.Latitude, Monkey.Longitude, new MapLaunchOptions {
22+
Name = Monkey.Name,
23+
NavigationMode = NavigationMode.None
24+
});
25+
}
26+
catch (Exception ex) {
27+
Debug.WriteLine($"Unable to launch maps: {ex.Message}");
28+
await Shell.Current.DisplayAlert("Error, no Maps app!", ex.Message, "OK");
29+
}
30+
}
31+
1132
}

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
using MonkeyFinder.Services;
1+
using Microsoft.Maui.Devices.Sensors;
2+
using Microsoft.Maui.Networking;
3+
using MonkeyFinder.Services;
24

35
namespace MonkeyFinder.ViewModel;
46

57
public partial class MonkeysViewModel : BaseViewModel {
8+
69
public ObservableCollection<Monkey> Monkeys { get; } = new();
10+
711
MonkeyService monkeyService;
8-
public MonkeysViewModel(MonkeyService monkeyService) {
12+
private readonly IConnectivity _Connectivity;
13+
private readonly IGeolocation _Geolocation;
14+
15+
public MonkeysViewModel(MonkeyService monkeyService, IConnectivity connectivity, IGeolocation geolocation) {
916
Title = "Monkey Finder";
1017
this.monkeyService = monkeyService;
18+
_Connectivity = connectivity;
19+
_Geolocation = geolocation;
1120
}
1221

22+
[ObservableProperty]
23+
bool isRefreshing;
24+
1325
[RelayCommand]
1426
async Task GetMonkeysAsync() {
1527
if (IsBusy)
1628
return;
1729

30+
if (_Connectivity.NetworkAccess != NetworkAccess.Internet) {
31+
await Shell.Current.DisplayAlert("No connectivity!",
32+
$"Please check internet and try again.", "OK");
33+
return;
34+
}
35+
36+
1837
try {
1938
IsBusy = true;
2039
var monkeys = await monkeyService.GetMonkeys();
@@ -32,6 +51,7 @@ async Task GetMonkeysAsync() {
3251
}
3352
finally {
3453
IsBusy = false;
54+
IsRefreshing = false;
3555
}
3656

3757
}
@@ -47,5 +67,35 @@ async Task GoToDetails(Monkey monkey) {
4767
});
4868
}
4969

70+
[RelayCommand]
71+
async Task GetClosestMonkey() {
72+
if (IsBusy || Monkeys.Count == 0)
73+
return;
74+
75+
try {
76+
// Get cached ___location, else get real ___location.
77+
//var ___location = await _Geolocation.GetLastKnownLocationAsync();
78+
//if (___location == null) {
79+
var ___location = await _Geolocation.GetLocationAsync(new GeolocationRequest {
80+
DesiredAccuracy = GeolocationAccuracy.Lowest,
81+
Timeout = TimeSpan.FromSeconds(30)
82+
});
83+
//}
84+
85+
// Find closest monkey to us
86+
var first = Monkeys.OrderBy(m => ___location.CalculateDistance(
87+
new Location(m.Latitude, m.Longitude), DistanceUnits.Miles))
88+
.FirstOrDefault();
89+
90+
await Shell.Current.DisplayAlert("", first.Name + " " +
91+
first.Location, "OK");
92+
93+
}
94+
catch (Exception ex) {
95+
Debug.WriteLine($"Unable to query ___location: {ex.Message}");
96+
await Shell.Current.DisplayAlert("Error!", ex.Message, "OK");
97+
}
98+
}
99+
50100
}
51101

0 commit comments

Comments
 (0)