Skip to content

Commit d3131fb

Browse files
committed
- Added ability to import barchart file from Histogram page.
- Reduced number of elements inside the histogram CellTemplates. - Experimenting with Task Async Patterns for better performance - Small fixes
1 parent 5b29f9a commit d3131fb

File tree

7 files changed

+116
-183
lines changed

7 files changed

+116
-183
lines changed

eBirdDataVisualizer.Core/Models/MonthData.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public ICollection<double> SampleSizes
3434
get; set;
3535
}
3636

37-
public string Q1String => $"{Month} Q1: {SampleSizes.ElementAt(0)}";
38-
public string Q2String => $"{Month} Q2: {SampleSizes.ElementAt(1)}";
39-
public string Q3String => $"{Month} Q3: {SampleSizes.ElementAt(2)}";
40-
public string Q4String => $"{Month} Q4: {SampleSizes.ElementAt(3)}";
37+
public string Q1String => (SampleSizes.Count > 0) ? $"{Month} Q1: {SampleSizes.ElementAt(0)}" : $"{Month} Q1";
38+
public string Q2String => (SampleSizes.Count > 1) ? $"{Month} Q2: {SampleSizes.ElementAt(1)}" : $"{Month} Q2";
39+
public string Q3String => (SampleSizes.Count > 2) ? $"{Month} Q3: {SampleSizes.ElementAt(2)}" : $"{Month} Q3";
40+
public string Q4String => (SampleSizes.Count > 3) ? $"{Month} Q4: {SampleSizes.ElementAt(3)}" : $"{Month} Q4";
4141

4242
public string Q1Tag => $"{Month}Q1";
4343
public string Q2Tag => $"{Month}Q2";

eBirdDataVisualizer.Core/Services/BirdDataService.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ public async Task<bool> ParseData(string data)
270270
return importResult;
271271
}
272272

273-
public async Task ParseMetadata(string name)
273+
public async Task ParseMetadata(string filename)
274274
{
275275
var yearStart = 0;
276276
var yearEnd = 0;
277277
var monthStart = Models.Month.January;
278278
var monthEnd = Models.Month.January;
279-
var location = name.Split("__").First().Split('_').Last();
280-
var timeInfo = name.Split("__").Last().Split("barchart.txt").First().Split('_').Where(x => x != String.Empty);
281-
if (timeInfo.Count() == 4)
279+
var location = filename.Split("__").First().Split('_').Last();
280+
var timeInfo = filename.Split("__").Last().Split('_').Where(x => x != String.Empty);
281+
if (timeInfo.Count() >= 4)
282282
{
283283
try
284284
{
@@ -291,7 +291,7 @@ public async Task ParseMetadata(string name)
291291
}
292292

293293
Metadata.Rows.Clear();
294-
Metadata.Rows.Add(name, location, yearStart, yearEnd, monthStart, monthEnd);
294+
Metadata.Rows.Add(filename, location, yearStart, yearEnd, monthStart, monthEnd);
295295
await Task.CompletedTask;
296296
return;
297297
}

eBirdDataVisualizer/ViewModels/DataGridViewModel.cs

+24-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using eBirdDataVisualizer.Core.Services;
1414
using Microsoft.UI.Xaml;
1515
using Microsoft.UI.Xaml.Data;
16+
using static System.Net.WebRequestMethods;
1617

1718
namespace eBirdDataVisualizer.ViewModels;
1819

@@ -346,6 +347,11 @@ public ICommand ToggleDataGridBarChartMode
346347
get;
347348
}
348349

350+
public ICommand ImportFile
351+
{
352+
get;
353+
}
354+
349355
private static CollectionViewSource groupedItems;
350356

351357
public bool MonthsAllVisible()
@@ -505,6 +511,18 @@ public DataGridViewModel(IBirdDataService birdDataService)
505511
DataGridBarChartMode = param;
506512
//ShowProgress = false;
507513
});
514+
515+
ImportFile = new RelayCommand(async () =>
516+
{
517+
ShowProgress = true;
518+
var mvm = App.GetService<MainViewModel>();
519+
var importResult = await mvm.spawnFilePicker();
520+
if (importResult.Value == true)
521+
{
522+
LoadData();
523+
}
524+
ShowProgress = false;
525+
});
508526
}
509527

510528
private bool showProgress = false;
@@ -539,6 +557,11 @@ public string CachedGroupQuery
539557
public bool IsGroupedByCommonName => (CachedGroupQuery == nameof(KeySelectorCommonName)) ? true : false;
540558

541559
public async void OnNavigatedTo(object parameter)
560+
{
561+
LoadData();
562+
}
563+
564+
public async void LoadData()
542565
{
543566
BirdsCollection.Clear();
544567

@@ -551,7 +574,7 @@ public async void OnNavigatedTo(object parameter)
551574
BirdsCollectionViewSource = new CollectionViewSource() { Source = BirdsCollection };
552575
ItemsSource = BirdsCollectionViewSource.View;
553576

554-
MonthsCollection.Clear();
577+
MonthsCollection = new ObservableCollection<MonthData>();
555578

556579
var monthData = await _birdDataService.GetMonthDataAsync();
557580

@@ -604,19 +627,6 @@ public CollectionViewSource SortData(string sortBy, bool ascending)
604627
return collectionViewSource;
605628
}
606629

607-
//if (CachedGroupQuery != string.Empty)
608-
//{
609-
// switch (CachedGroupQuery)
610-
// {
611-
// case nameof(KeySelectorGenus):
612-
// return GroupDataByGenus();
613-
// case nameof(KeySelectorCommonName):
614-
// return GroupDataByCommonName();
615-
// default:
616-
// break;
617-
// }
618-
//}
619-
620630
if (ascending)
621631
collectionViewSource.Source = new ObservableCollection<Bird>(BirdsCollection.OrderBy(bird => propertyInfo.GetValue(bird, null)));
622632
else

eBirdDataVisualizer/ViewModels/MainViewModel.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async void LoadSampleData()
5151
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/sample_US-HI__1900_2022_1_12_barchart.txt"));
5252
if (file is not null)
5353
{
54-
parseFileMetadata(file);
54+
await _birdDataService.ParseMetadata(file.Name);
5555
var text = System.IO.File.ReadAllText(file.Path);
5656
await _birdDataService.ParseData(text);
5757
}
@@ -74,12 +74,7 @@ public MainViewModel(IBirdDataService birdDataService)
7474
});
7575
}
7676

77-
public void parseFileMetadata(StorageFile file)
78-
{
79-
_birdDataService.ParseMetadata(file.Name);
80-
}
81-
82-
public async Task spawnFilePicker()
77+
public async Task<bool?> spawnFilePicker()
8378
{
8479
FileOpenPicker fileOpenPicker = new();
8580
fileOpenPicker.FileTypeFilter.Add("*");
@@ -89,15 +84,19 @@ public async Task spawnFilePicker()
8984

9085
var file = await fileOpenPicker.PickSingleFileAsync();
9186

87+
bool? result = false;
88+
9289
if (file is not null)
9390
{
9491
var importResult = false;
9592
try
9693
{
97-
parseFileMetadata(file);
94+
await _birdDataService.ParseMetadata(file.Name);
9895

9996
var text = await Windows.Storage.FileIO.ReadTextAsync(file);
10097
importResult = await _birdDataService.ParseData(text);
98+
result = importResult;
99+
101100
if (importResult)
102101
ImportResultText = $"{file.Name} successfully imported!";
103102
else
@@ -116,5 +115,6 @@ public async Task spawnFilePicker()
116115

117116
ShowImportResult = true;
118117
}
118+
return result;
119119
}
120120
}

0 commit comments

Comments
 (0)