Skip to content

Commit

Permalink
Merge pull request #17 from zxbmmmmmmmmm/feat/cache
Browse files Browse the repository at this point in the history
天气数据缓存
Close #1
  • Loading branch information
zxbmmmmmmmmm authored Nov 5, 2023
2 parents cd1c1a9 + b384551 commit 7eda635
Show file tree
Hide file tree
Showing 9 changed files with 766 additions and 593 deletions.
2 changes: 1 addition & 1 deletion FluentWeather.Abstraction/Models/WeatherBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentWeather.Abstraction.Models;

public abstract class WeatherBase
public class WeatherBase
{
public virtual WeatherType WeatherType { get; set; }
public string Description { get; set; }
Expand Down
20 changes: 20 additions & 0 deletions FluentWeather.Abstraction/Models/WeatherCacheBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentWeather.Abstraction.Interfaces.Weather;
using System;
using System.Collections.Generic;
using System.Text;
namespace FluentWeather.Abstraction.Models;
public class WeatherCacheBase
{
public virtual List<WeatherBase> DailyForecasts { get; set; } = new();
public virtual List<WeatherBase> HourlyForecasts { get; set; } = new();
public virtual List<WeatherWarningBase> Warnings { get; set; }
public virtual WeatherBase WeatherNow { get; set; }
public virtual string WeatherDescription { get; set; }
public virtual DateTime SunRise { get; set; }
public virtual DateTime SunSet { get; set; }
public virtual GeolocationBase Location { get; set; }
public virtual List<IndicesBase> Indices { get; set; }
public virtual PrecipitationBase Precipitation { get; set; }
public virtual AirConditionBase AirCondition { get; set; }
public virtual DateTime UpdatedTime { get; set; }
}
2 changes: 1 addition & 1 deletion FluentWeather.Abstraction/Models/WeatherWarningBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace FluentWeather.Abstraction.Models;

public abstract class WeatherWarningBase
public class WeatherWarningBase
{
public string Id{ get; set; }
public string Sender { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions FluentWeather.QWeatherProvider/Models/QWeatherCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using FluentWeather.Abstraction.Models;

namespace FluentWeather.QWeatherProvider.Models;

public class QWeatherCache:WeatherCacheBase
{
public new List<QWeatherDailyForecast> DailyForecasts { get; set; } = new();
public new List<QWeatherHourlyForecast> HourlyForecasts { get; set; } = new();
public new List<QWeatherWarning> Warnings { get; set; }
public new QWeatherNow WeatherNow { get; set; }
public new QWeatherPrecipitation Precipitation { get; set; }
public new QAirCondition AirCondition { get; set; }
}
1 change: 1 addition & 0 deletions FluentWeather.Uwp.Shared/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using MetroLog;
using MetroLog.Targets;
using System.Collections.ObjectModel;
using FluentWeather.QWeatherProvider.Models;

namespace FluentWeather.Uwp.Shared;
#nullable enable
Expand Down
4 changes: 4 additions & 0 deletions FluentWeather.Uwp/FluentWeather.Uwp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
<Compile Include="Controls\WeatherHourlyForecastItem.xaml.cs">
<DependentUpon>WeatherHourlyForecastItem.xaml</DependentUpon>
</Compile>
<Compile Include="Helpers\CacheHelper.cs" />
<Compile Include="Helpers\DIFactory.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="Helpers\LocationHelper.cs" />
Expand Down Expand Up @@ -527,6 +528,9 @@
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>7.0.3</Version>
</PackageReference>
<PackageReference Include="Telerik.UI.for.UniversalWindowsPlatform">
<Version>1.0.2.11</Version>
</PackageReference>
Expand Down
76 changes: 76 additions & 0 deletions FluentWeather.Uwp/Helpers/CacheHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using FluentWeather.Abstraction.Models;
using FluentWeather.QWeatherProvider.Models;
using FluentWeather.Uwp.Shared;
using FluentWeather.Uwp.ViewModels;
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Windows.Storage;

namespace FluentWeather.Uwp.Helpers;

public class CacheHelper
{
public static async Task<QWeatherCache> GetWeatherCache(GeolocationBase location)
{
var item = (await ApplicationData.Current.LocalCacheFolder.TryGetItemAsync("WeatherCache.txt") )as IStorageFile;
if (item is null)
{
item = await CreateCacheFile();
return null;
}
try
{
//读取文件
var text = await FileIO.ReadTextAsync(item);
var data = JsonSerializer.Deserialize<List<QWeatherCache>>(text);
data.RemoveAll(p => DateTime.Now - p.UpdatedTime > TimeSpan.FromMinutes(10));//删除过期的数据
return data.Find(p => p.Location.Name == location.Name);
}
catch
{
return null;
}

}
public static async void Cache(MainPageViewModel viewModel)
{
var item = (await ApplicationData.Current.LocalCacheFolder.TryGetItemAsync("WeatherCache.txt")) as IStorageFile;
var text = await FileIO.ReadTextAsync(item);
List<JsonNode> cacheData;
try
{
cacheData = JsonSerializer.Deserialize<List<JsonNode>>(text);
}
catch
{
cacheData = new();
}
var cache = new QWeatherCache
{
DailyForecasts = viewModel.DailyForecasts.ConvertAll(p => p as QWeatherDailyForecast),
SunRise = viewModel.SunRise,
SunSet = viewModel.SunSet,
AirCondition = viewModel.AirCondition as QAirCondition,
Location = viewModel.CurrentLocation,
HourlyForecasts = viewModel.HourlyForecasts.ConvertAll(p => p as QWeatherHourlyForecast),
Indices = viewModel.Indices,
Precipitation = viewModel.Precipitation as QWeatherPrecipitation,
UpdatedTime = DateTime.Now,
Warnings = viewModel.Warnings.ConvertAll(p => p as QWeatherWarning),
WeatherDescription = viewModel.WeatherDescription,
WeatherNow = viewModel.WeatherNow as QWeatherNow,
};
cacheData.RemoveAll(p => DateTime.Now - p["UpdatedTime"].GetValue<DateTime>() > TimeSpan.FromMinutes(10));//删除过期的数据
cacheData.Add(JsonSerializer.SerializeToNode(cache));
var json = JsonSerializer.Serialize(cacheData);
await FileIO.WriteTextAsync(item,json);
}
public static async Task<IStorageFile> CreateCacheFile()
{
return await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("WeatherCache.txt");
}
}
Loading

0 comments on commit 7eda635

Please sign in to comment.