Skip to content

Commit

Permalink
fix: Fetching past and future weather
Browse files Browse the repository at this point in the history
feat: Add month display to weathercard
  • Loading branch information
Indeedornot committed Jan 29, 2023
1 parent 4e1779a commit 2cc4626
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
17 changes: 13 additions & 4 deletions WPFWeather/UserControls/WeatherCard.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@
<Grid
d:Visibility="Visible"
Visibility="{Binding Loading, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Top"
Text="{Binding Time}" />
<StackPanel VerticalAlignment="Top">
<TextBlock
HorizontalAlignment="Center"
FontSize="12"
Text="{Binding TimeString}" />
<TextBlock
Margin="-2"
Padding="0"
HorizontalAlignment="Center"
FontSize="6"
Text="{Binding DateString}" />
</StackPanel>

<local:WeatherIconDisplay
Height="32"
HorizontalAlignment="Stretch"
Expand Down
33 changes: 17 additions & 16 deletions WPFWeather/UserControls/WeatherCard.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -13,39 +14,39 @@ public WeatherCard() {
InitializeComponent();
}

public string Time {
get => (string)GetValue(TimeProperty);
set => SetValue(TimeProperty, value);
}
public string TimeString => Time.ToString("HH:mm");
public string DateString => Time.ToString("dd-MM");

public string Temperature {
get => (string)GetValue(TemperatureProperty);
set => SetValue(TemperatureProperty, value);
}

public static readonly DependencyProperty TimeProperty =
DependencyProperty.Register(nameof(Time), typeof(string), typeof(WeatherCard), new PropertyMetadata(default(string)));

public static readonly DependencyProperty TemperatureProperty =
DependencyProperty.Register(nameof(Temperature), typeof(string), typeof(WeatherCard), new PropertyMetadata(default(string)));


public DateTime Time {
get => (DateTime)GetValue(TimeProperty);
set => SetValue(TimeProperty, value);
}

public bool Loading {
get => (bool)GetValue(LoadingProperty);
set => SetValue(LoadingProperty, value);
}

// Using a DependencyProperty as the backing store for Loading. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LoadingProperty =
DependencyProperty.Register(nameof(Loading), typeof(bool), typeof(WeatherCard), new PropertyMetadata(default(bool)));

public WeatherType Description {
get => (WeatherType)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}

// Using a DependencyProperty as the backing store for WeatherType. This enables animation, styling, binding, etc...

public static readonly DependencyProperty TimeProperty =
DependencyProperty.Register(nameof(Time), typeof(DateTime), typeof(WeatherCard), new PropertyMetadata(default(DateTime)));

public static readonly DependencyProperty TemperatureProperty =
DependencyProperty.Register(nameof(Temperature), typeof(string), typeof(WeatherCard), new PropertyMetadata(default(string)));

public static readonly DependencyProperty LoadingProperty =
DependencyProperty.Register(nameof(Loading), typeof(bool), typeof(WeatherCard), new PropertyMetadata(default(bool)));

public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.Register(nameof(Description), typeof(WeatherType), typeof(WeatherCard), new PropertyMetadata(WeatherType.Clear));

Expand Down
5 changes: 2 additions & 3 deletions WPFWeather/ViewModels/WeatherHomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ public WeatherHomeViewModel(AppStore appStore, IWeatherProvider weatherProvider,
FetchPastWeatherCommand = new RelayCommand((_) => FetchPastWeather());
}


public async Task FetchFutureWeather() {
Location? location = _appStore.Location;
DateTime fetchFrom = _appStore.LatestFetched;

if (location == null) return;
if (IsFetching || IsLoading) return;

DateTime fetchTo = DateTime.Now.AddDays(3);
DateTime fetchTo = fetchFrom.AddDays(3);
if (fetchTo > DateTime.Now.AddDays(12)) return;
IsFetching = true;

Expand All @@ -134,7 +133,7 @@ public async Task FetchPastWeather() {
if (location == null) return;
if (IsFetching || IsLoading) return;

DateTime fetchFrom = DateTime.Now.Subtract(TimeSpan.FromDays(3));
DateTime fetchFrom = fetchTo.Subtract(TimeSpan.FromDays(3));
if (fetchFrom < DateTime.Now.Subtract(TimeSpan.FromDays(12))) return;
IsFetching = true;

Expand Down
2 changes: 1 addition & 1 deletion WPFWeather/Views/WeatherHomeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
d:Time="12:00"
Description="{Binding Description}"
Temperature="{Binding Temperature}"
Time="{Binding Time, StringFormat={}{0:HH:mm}}" />
Time="{Binding Time}" />
</DataTemplate>
</ListView.ItemTemplate>

Expand Down

0 comments on commit 2cc4626

Please sign in to comment.