Commit fe6eca0a authored by Petr Nahodil's avatar Petr Nahodil
Browse files

DU1 fix

parent 881e2555
......@@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DU1_Naive_Implementation"
mc:Ignorable="d"
Title="Current Weather" Height="240" Width="413">
Title="Current Weather" Height="240" Width="600">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="30*" />
......@@ -45,9 +45,6 @@
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem x:Name="TabItem_Current" Header="Current"/>
<TabItem x:Name="TabItem_Tomorrow" Header="Tomorrow"/>
<TabItem x:Name="TabItem_Thursday" Header=" thursday " ToolTip="Always represents next Thursday" />
</TabControl>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
......@@ -21,7 +22,7 @@ namespace DU1_Naive_Implementation
/// </summary>
public partial class MainWindow : Window
{
private readonly Dictionary<TabItem, int> tabItemToDays;
private readonly Dictionary<TabItem, DateTime> tabItemToDays;
private Label Label_Humidity, Label_Temperature;
......@@ -31,27 +32,26 @@ namespace DU1_Naive_Implementation
var today = DateTime.Today;
var daysUntilNextThursday = today.DayOfWeek switch
{
DayOfWeek.Monday => 3,
DayOfWeek.Tuesday => 2,
DayOfWeek.Wednesday => 1,
DayOfWeek.Thursday => 7,
DayOfWeek.Friday => 6,
DayOfWeek.Saturday => 5,
DayOfWeek.Sunday => 4,
_ => throw new NotImplementedException()
};
var nextThursday = today.AddDays(daysUntilNextThursday);
TabItem_Thursday.Header = $"Thursday {nextThursday:dd.MM.yyyy}";
tabItemToDays = new()
tabItemToDays = new();
for (int i = 0; i < 5; i++)
{
[TabItem_Current] = 0,
[TabItem_Tomorrow] = 1,
[TabItem_Thursday] = daysUntilNextThursday
};
var anotherDay = today.AddDays(i);
TabItem tab = new()
{
Header = i switch
{
0 => "Current",
1 => "Tomorrow",
_ => anotherDay.ToString("dddd dd.MM.yyyy", CultureInfo.InvariantCulture)
}
};
TabControl_Days.Items.Add(tab);
tabItemToDays[tab] = anotherDay;
}
}
......
......@@ -28,46 +28,48 @@ namespace DU1_Naive_Implementation
WeatherNet.ClientSettings.SetApiUrl(Properties.Resources.WeatherApiUrl);
}
internal static Task<(double temperature, double humidity)> CzechWeatherForecastAsync(string city, int daysFromToday, CancellationToken token = default)
internal static Task<(double temperature, double humidity)> CzechWeatherForecastAsync(string city, DateTime day, CancellationToken token = default)
{
if (string.IsNullOrWhiteSpace(city))
{
throw new ArgumentNullException(city);
}
if (daysFromToday is not (>= 0 and <= 15))
var today = DateTime.Today;
if (day < today || day > today.AddDays(5))
{
throw new ArgumentException($"{nameof(daysFromToday)} has to lie between 0 and 15");
throw new ArgumentException($"{nameof(day)} has to lie between today and five days later");
}
return Task.Run(() =>
{
WeatherResult weather;
bool success;
if (daysFromToday == 0)
var date = day.Date;
if (date == today)
{
token.ThrowIfCancellationRequested();
var result = CurrentWeather.GetByCityName(city, "Czech Republic", "en", "metric");
success = result.Success;
weather = result.Item;
}
else if (daysFromToday <= 5)
{
token.ThrowIfCancellationRequested();
var result = FiveDaysForecast.GetByCityName(city, "Czech Republic", "en", "metric");
success = result.Success;
weather = result.Items?[daysFromToday];
if (!result.Success)
{
throw new WeatherAPIException("The weather API returned success=false");
}
return (result.Item.Temp, result.Item.Humidity);
}
else
{
token.ThrowIfCancellationRequested();
var result = SixteenDaysForecast.GetByCityName(city, "Czech Republic", daysFromToday, "en", "metric");
success = result.Success;
weather = result.Items?[daysFromToday];
}
token.ThrowIfCancellationRequested();
var result = FiveDaysForecast.GetByCityName(city, "Czech Republic", "en", "metric");
if (!result.Success)
{
throw new WeatherAPIException("The weather API returned success=false");
}
var dayForecasts =
result.Items
.SkipWhile(forecast => forecast.Date.Date != date)
.TakeWhile(forecast => forecast.Date.Date == date)
.ToList();
return success ? (weather.Temp, weather.Humidity) : throw new WeatherAPIException("The weather API returned success=false");
return (dayForecasts.Average(forecast => forecast.Temp), dayForecasts.Average(forecast => forecast.Humidity));
}
}, token);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment