-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from zxbmmmmmmmmm/feat/cache
天气数据缓存 Close #1
- Loading branch information
Showing
9 changed files
with
766 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.