Skip to content

Commit

Permalink
fix(SamplesApp): Favorites list loading
Browse files Browse the repository at this point in the history
- Loads and saves favorites and recents list from local storage on
non-reference .NET Standard platforms. This includes WASM and Skia
targets.
- Asynchronously loads the favorites and recents list on startup.
  • Loading branch information
trungnt2910 committed Oct 3, 2022
1 parent b872d46 commit 419483a
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public partial class SampleChooserViewModel
private List<SampleChooserCategory> _categories;

private readonly Uno.Threading.AsyncLock _fileLock = new Uno.Threading.AsyncLock();
#if !UNO_REFERENCE_API
#if !__NETSTD_REFERENCE__
private readonly string SampleChooserFileAddress = "SampleChooserFileAddress.";
#endif

Expand Down Expand Up @@ -109,6 +109,22 @@ public SampleChooserViewModel()
{
_log.Info($"Found {_categories.SelectMany(c => c.SamplesContent).Distinct().Count()} sample(s) in {_categories.Count} categories.");
}

_ = Window.Current.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
async () =>
{
// Initialize favorites and recents list as soon as possible.
if (FavoriteSamples == null || !FavoriteSamples.Any())
{
FavoriteSamples = await GetFavoriteSamples(CancellationToken.None, true);
}
if (RecentSamples == null || !RecentSamples.Any())
{
RecentSamples = await GetRecentSamples(CancellationToken.None);
}
}
);
}

public event EventHandler SampleChanging;
Expand Down Expand Up @@ -999,15 +1015,15 @@ from test in category.SamplesContent

private async Task Set<T>(string key, T value)
{
#if !UNO_REFERENCE_API
#if !__NETSTD_REFERENCE__
var json = Newtonsoft.Json.JsonConvert.SerializeObject(value);
ApplicationData.Current.LocalSettings.Values[key] = json;
#endif
}

private async Task<T> Get<T>(string key, Func<T> d = null)
{
#if !UNO_REFERENCE_API
#if !__NETSTD_REFERENCE__
var json = (string)ApplicationData.Current.LocalSettings.Values[key];
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
#else
Expand All @@ -1017,7 +1033,7 @@ private async Task<T> Get<T>(string key, Func<T> d = null)

private async Task SetFile<T>(string key, T value)
{
#if !UNO_REFERENCE_API
#if !__NETSTD_REFERENCE__
var json = Newtonsoft.Json.JsonConvert.SerializeObject(value);

using (await _fileLock.LockAsync(CancellationToken.None))
Expand All @@ -1044,20 +1060,17 @@ private async Task SetFile<T>(string key, T value)

private async Task<T> GetFile<T>(string key, Func<T> defaultValue = null)
{
#if !UNO_REFERENCE_API
#if !__NETSTD_REFERENCE__
string json = null;

using (await _fileLock.LockAsync(CancellationToken.None))
{
try
{
var folderPath = ApplicationData.Current.LocalFolder.Path;
var filePath = Path.Combine(folderPath, SampleChooserFileAddress + key);
if (File.Exists(filePath))
{
json = File.ReadAllText(filePath);
}

var folder = ApplicationData.Current.LocalFolder;
// GetFileAsync ensures the filesystem has been loaded on WASM.
var file = await folder.GetFileAsync(SampleChooserFileAddress + key);
json = await FileIO.ReadTextAsync(file);
}
catch (IOException e)
{
Expand Down

0 comments on commit 419483a

Please sign in to comment.