Skip to content

Commit

Permalink
Merge pull request #50 from zxbmmmmmmmmm/feat/fahrenheit
Browse files Browse the repository at this point in the history
Feat/fahrenheit
  • Loading branch information
zxbmmmmmmmmm authored Jun 1, 2024
2 parents ff8bb34 + bcf0a65 commit f33e580
Show file tree
Hide file tree
Showing 32 changed files with 593 additions and 151 deletions.
24 changes: 17 additions & 7 deletions FluentWeather.Abstraction/Helpers/UnitConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FluentWeather.Abstraction.Models;
using FluentWeather.Abstraction.Strings;

namespace FluentWeather.Abstraction.Helpers;

public class UnitConverter
public static class UnitConverter
{
/// <summary>
/// 将风速(KM/H)转换为风力等级
Expand Down Expand Up @@ -65,11 +66,20 @@ public static WindDirection GetWindDirectionFromAngle(int angle)
/// <returns></returns>
public static string GetAqiCategoryUS(int aqi)
{
if (0 <= aqi && aqi <= 50) return "Good";
if (51 <= aqi && aqi <= 100) return "Moderate";
if (101 <= aqi && aqi <= 150) return "Unhealthy for sensitive groups";
if (151 <= aqi && aqi <= 200) return "Unhealthy";
if (201 <= aqi && aqi <= 300) return "Very unhealthy";
return "Hazardous";
if (0 <= aqi && aqi <= 50) return Resources.ResourceManager.GetString("USAQI_Good")!;
if (51 <= aqi && aqi <= 100) return Resources.ResourceManager.GetString("USAQI_Moderate")!;
if (101 <= aqi && aqi <= 150) return Resources.ResourceManager.GetString("USAQI_UnhealthyForSensitiveGroups")!;
if (151 <= aqi && aqi <= 200) return Resources.ResourceManager.GetString("USAQI_Unhealthy")!;
if (201 <= aqi && aqi <= 300) return Resources.ResourceManager.GetString("USAQI_VeryUnhealthy")!;
return Resources.ResourceManager.GetString("USAQI_Hazardous")!;
}

public static double ToFahrenheit(this double celsiusTemperature)
{
return celsiusTemperature * 9 / 5 + 32;
}
public static int ToFahrenheit(this int celsiusTemperature)
{
return celsiusTemperature * 9 / 5 + 32;
}
}
18 changes: 18 additions & 0 deletions FluentWeather.Abstraction/Strings/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="USAQI_Good" xml:space="preserve">
<value>Good</value>
</data>
<data name="USAQI_Hazardous" xml:space="preserve">
<value>Hazardous</value>
</data>
<data name="USAQI_Moderate" xml:space="preserve">
<value>Moderate</value>
</data>
<data name="USAQI_Unhealthy" xml:space="preserve">
<value>Unhealthy</value>
</data>
<data name="USAQI_UnhealthyForSensitiveGroups" xml:space="preserve">
<value>Unhealthy for sensitive groups</value>
</data>
<data name="USAQI_VeryUnhealthy" xml:space="preserve">
<value>Very unhealthy</value>
</data>
<data name="WeatherCode_0" xml:space="preserve">
<value>Clear</value>
</data>
Expand Down
18 changes: 18 additions & 0 deletions FluentWeather.Abstraction/Strings/Resources.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="USAQI_Good" xml:space="preserve">
<value>好</value>
</data>
<data name="USAQI_Hazardous" xml:space="preserve">
<value>危险</value>
</data>
<data name="USAQI_Moderate" xml:space="preserve">
<value>中等</value>
</data>
<data name="USAQI_Unhealthy" xml:space="preserve">
<value>不健康</value>
</data>
<data name="USAQI_UnhealthyForSensitiveGroups" xml:space="preserve">
<value>不适于敏感人群</value>
</data>
<data name="USAQI_VeryUnhealthy" xml:space="preserve">
<value>非常不健康</value>
</data>
<data name="WeatherCode_0" xml:space="preserve">
<value>晴</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions FluentWeather.Uwp.Shared/FluentWeather.Uwp.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
<Compile Include="Constants.Secret.cs" />
<Compile Include="Helpers\DataProviderHelper.cs" />
<Compile Include="DialogManager.cs" />
<Compile Include="Helpers\ValueConverters\AutoSuggestSuggestionParameterConverter.cs" />
<Compile Include="Helpers\ValueConverters\ConverterMethods.cs" />
<Compile Include="Helpers\ValueConverters\DataConverters.cs" />
<Compile Include="Helpers\ValueConverters\DateConverter.cs" />
<Compile Include="Helpers\ValueConverters\ListPropertyNullToVisibilityConverter.cs" />
<Compile Include="Helpers\ValueConverters\VisibilityInverter.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Helpers\ThemeHelper.cs" />
<Compile Include="Helpers\TileHelper.cs" />
Expand Down
11 changes: 6 additions & 5 deletions FluentWeather.Uwp.Shared/Helpers/TileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using FluentWeather.Abstraction.Models;
using FluentWeather.Uwp.Shared.Helpers.ValueConverters;
using Microsoft.Toolkit.Uwp.Notifications;

namespace FluentWeather.Uwp.Shared.Helpers
Expand Down Expand Up @@ -107,7 +108,7 @@ public static TileContent GenerateForecastTileContent(List<WeatherDailyBase> dai
// Wide Tile
builder.AddTile(TileSize.Wide)
.AddText($"{daily[0].Description}", TileSize.Wide, AdaptiveTextStyle.Title)
.AddText($"{daily[0].MaxTemperature}° / {daily[0].MinTemperature}° {daily[0].WindDirectionDescription} {daily[0].WindScale}{ResourceLoader.GetForViewIndependentUse().GetString("Level")}", TileSize.Wide, AdaptiveTextStyle.Body)
.AddText($"{daily[0].MaxTemperature.ConvertTemperatureUnit()}° / {daily[0].MinTemperature.ConvertTemperatureUnit()}° {daily[0].WindDirectionDescription} {daily[0].WindScale}{ResourceLoader.GetForViewIndependentUse().GetString("Level")}", TileSize.Wide, AdaptiveTextStyle.Body)
.AddText($"{ResourceLoader.GetForViewIndependentUse().GetString("Humidity")}:{daily[0].Humidity}% {ResourceLoader.GetForViewIndependentUse().GetString("Pressure")}:{daily[0].Pressure}hPa", TileSize.Wide, AdaptiveTextStyle.CaptionSubtle)
.SetBranding(TileBranding.Auto, TileSize.Wide)
.SetTextStacking(TileTextStacking.Center);
Expand All @@ -129,7 +130,7 @@ public static TileContent GenerateForecastTileContent(List<WeatherDailyBase> dai
//builder.Content.Visual.BaseUri = new Uri("Assets/", UriKind.Relative);

builder.Content.Visual.LockDetailedStatus1 = daily[0].Description;
builder.Content.Visual.LockDetailedStatus2 = $"{daily[0].MaxTemperature}° / {daily[0].MinTemperature}° {daily[0].WindDirectionDescription} {daily[0].WindScale}{ResourceLoader.GetForViewIndependentUse().GetString("Level")}";
builder.Content.Visual.LockDetailedStatus2 = $"{daily[0].MaxTemperature.ConvertTemperatureUnit()}° / {daily[0].MinTemperature.ConvertTemperatureUnit()}° {daily[0].WindDirectionDescription} {daily[0].WindScale}{ResourceLoader.GetForViewIndependentUse().GetString("Level")}";


return builder.Content;
Expand Down Expand Up @@ -223,7 +224,7 @@ public static void GetGroupChildren(AdaptiveGroup group, List<WeatherDailyBase>
{
foreach (var item in daily)
{
group.Children.Add(GenerateSubgroup(GetWeek(item.Time), "ms-appx:///Assets/Weather/Resized/32" + AssetsHelper.GetWeatherIconName(item.WeatherType), item.MaxTemperature, item.MinTemperature));
group.Children.Add(GenerateSubgroup(GetWeek(item.Time), "ms-appx:///Assets/Weather/Resized/32" + AssetsHelper.GetWeatherIconName(item.WeatherType), item.MaxTemperature.ConvertTemperatureUnit(), item.MinTemperature.ConvertTemperatureUnit()));
}
}

Expand Down Expand Up @@ -260,7 +261,7 @@ public static TileBindingContentAdaptive GenerateLargeTileContent(List<WeatherDa

new AdaptiveText()
{
Text = (daily[0]).MaxTemperature + "° / " +daily[0].MinTemperature + "° " +(daily[0]).WindDirectionDescription + (daily[0]).WindScale + ResourceLoader.GetForViewIndependentUse().GetString("Level")
Text = (daily[0]).MaxTemperature + "° / " +daily[0].MinTemperature.ConvertTemperatureUnit() + "° " +(daily[0]).WindDirectionDescription + (daily[0]).WindScale + ResourceLoader.GetForViewIndependentUse().GetString("Level")
},
},
HintTextStacking = AdaptiveSubgroupTextStacking.Center,
Expand All @@ -277,7 +278,7 @@ public static TileBindingContentAdaptive GenerateLargeTileContent(List<WeatherDa
var largeGroup = new AdaptiveGroup();
foreach (var item in daily.GetRange(1,5))
{
largeGroup.Children.Add(GenerateTileSubgroup(GetWeek(item.Time), $"Assets/Weather/Resized/32/{AssetsHelper.GetWeatherIconName(item.WeatherType)}", item.MaxTemperature, item.MinTemperature));
largeGroup.Children.Add(GenerateTileSubgroup(GetWeek(item.Time), $"Assets/Weather/Resized/32/{AssetsHelper.GetWeatherIconName(item.WeatherType)}", item.MaxTemperature.ConvertTemperatureUnit(), item.MinTemperature.ConvertTemperatureUnit()));
}
content.Children.Add(largeGroup);
return content;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using FluentWeather.Abstraction.Models;

namespace FluentWeather.Uwp.Shared.Helpers.ValueConverters;

public sealed class AutoSuggestSuggestionParameterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
// cast value to whatever EventArgs class you are expecting here
var args = (AutoSuggestBoxSuggestionChosenEventArgs)value;
// return what you need from the args
return (GeolocationBase)args.SelectedItem;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
148 changes: 148 additions & 0 deletions FluentWeather.Uwp.Shared/Helpers/ValueConverters/ConverterMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using Windows.ApplicationModel.Resources;
using Windows.UI;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using FluentWeather.Abstraction.Helpers;
using FluentWeather.Abstraction.Models;
using static FluentWeather.Abstraction.Models.WeatherCode;

namespace FluentWeather.Uwp.Shared.Helpers.ValueConverters;

public static class ConverterMethods
{

public static string GetWindScaleDescription(string scale)
{
if (scale.Contains("-"))
{
var s = scale.Split("-");
scale = s[1];
}
return ResourceLoader.GetForCurrentView().GetString("WindScaleDescription_" + scale);
}
public static Brush SeverityColorToColor(SeverityColor? color)
{
return color switch
{
SeverityColor.Red => new SolidColorBrush(Colors.Red),
SeverityColor.Green => new SolidColorBrush(Colors.Green),
SeverityColor.Blue => new SolidColorBrush(Colors.DeepSkyBlue),
SeverityColor.Orange => new SolidColorBrush(Colors.Orange),
SeverityColor.White => new SolidColorBrush(Colors.White),
SeverityColor.Yellow => new SolidColorBrush(Colors.Gold),
SeverityColor.Black => new SolidColorBrush(Colors.Black),
_ => new SolidColorBrush(Colors.Red)
};
}

/// <summary>
/// 根据应用设置自动转换温度
/// 此转换仅在UI层进行
/// </summary>
/// <param name="temp">摄氏温度</param>
/// <param name="disableRound">关闭取整</param>
/// <returns></returns>
public static double? ConvertTemperatureUnit(this double? temp,bool disableRound = false)
{
if (temp is null) return null;
var result = Common.Settings.TemperatureUnit is TemperatureUnit.Fahrenheit ? temp.Value.ToFahrenheit():temp.Value;
return disableRound ? result : Math.Round(result);
}

/// <summary>
/// 根据应用设置自动转换温度
/// 此转换仅在UI层进行
/// </summary>
/// <param name="temp">摄氏温度</param>
/// <param name="disableRound">关闭取整</param>
/// <returns></returns>
public static int ConvertTemperatureUnit(this int temp)
{
var result = Common.Settings.TemperatureUnit is TemperatureUnit.Fahrenheit ? temp.ToFahrenheit() : temp;
return result;
}

public static ImageSource GetIconByWeather(this WeatherCode code)
{
return new BitmapImage(GetIconUriByWeather(code));
}

public static Uri GetIconUriByWeather(this WeatherCode code)
{
var name = GetImageNameDay(code);
name = name is "" ? GetImageName(code) : name;
return new Uri("ms-appx:///Assets/Weather/" + name);
}
public static Uri GetIconUriByWeather(this WeatherCode weather, DateTime time)
{
var imageName = "";
if (time.TimeOfDay >= TimeSpan.FromHours(6) && time.TimeOfDay <= TimeSpan.FromHours(18))
{
imageName = GetImageNameDay(weather);
}
else
{
imageName = GetImageNameNight(weather);
}
imageName = imageName is "" ? GetImageName(weather) : imageName;

return new Uri("ms-appx:///Assets/Weather/" + imageName);
}

public static ImageSource GetIconByWeather(this WeatherCode weather, DateTime time)
{
return new BitmapImage(GetIconUriByWeather(weather, time));
}

public static string GetImageName(this WeatherCode weatherType)
{
return weatherType switch
{
SlightHail or ModerateOrHeavyHail => "BlowingHail.png",
HeavyRain => "HeavyRain.png",
ModerateRain => "LightRain.png",
SlightRain => "LightRain.png",
ModerateRainShowers => "LightRain.png",
PartlyCloudy => "VeryCloudy.png",
Overcast => "Cloudy.png",
SlightSnowFall or ModerateSnowFall => "LightSnow.png",
HeavySnowFall => "HeavySnow.png",
Fog => "Fog.png",
DenseFreezingDrizzle or LightFreezingDrizzle or LightFreezingRain or HeavyFreezingRain => "FreezingRain.png",
SlightSleet or ModerateOrHeavySleet => "RainSnow.png",
ThunderstormWithHeavyHail or SlightOrModerateThunderstorm or ThunderstormWithSlightHail or HeavyThunderStorm => "Thunder.png",
_ => "Cloudy.png",
};
}

public static string GetImageNameDay(this WeatherCode weatherType)
{
return weatherType switch
{
Clear => "SunnyDay.png",
Haze or Mist => "HazeSmokeDay.png",
MainlyClear => "MostlySunnyDay.png",
SlightHail or ModerateOrHeavyHail => "HailDay.png",
PartlyCloudy => "PartlyCloudyDay.png",
SlightRainShowers or ModerateRainShowers or ViolentRainShowers => "RainShowersDay.png",
SlightSnowShowers or HeavySnowShowers => "SnowShowersDay.png",
_ => "",
};
}
public static string GetImageNameNight(this WeatherCode weatherType)
{
return weatherType switch
{
Clear => "ClearNight.png",
Haze => "HazeSmokeNight.png",
MainlyClear => "MostlyClearNight.png",
SlightHail or ModerateOrHeavyHail => "HailNight.png",
PartlyCloudy => "PartlyCloudyNight.png",
SlightRainShowers or ModerateRainShowers or ViolentRainShowers => "RainShowersNight.png",
SlightSnowShowers or HeavySnowShowers => "SnowShowersNight.png",
_ => "",
};
}
}

Loading

0 comments on commit f33e580

Please sign in to comment.