Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube/Classes/HistorySet.cs
T
Michael Gordeev 6a8896d804 Refinements
2020-07-06 15:07:23 +03:00

107 lines
3.4 KiB
C#

using Google.Apis.YouTube.v3.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.UserActivities;
using Windows.Storage;
namespace FoxTube
{
public class HistoryItem
{
public string Id { get; set; }
public TimeSpan LeftOn { get; set; } = TimeSpan.FromSeconds(0);
}
public static class HistorySet
{
public static List<HistoryItem> Items { get; set; } = new List<HistoryItem>();
public static async void Update(HistoryItem item, Video video)
{
if (!SecretsVault.IsAuthorized)
return;
Items.RemoveAll(i => i.Id == item.Id);
Items.Insert(0, item);
if (Items.Count > 200)
Items.RemoveRange(200, Items.Count - 200);
Save();
UserActivitySession _currentActivity = null;
// Get the default UserActivityChannel and query it for our UserActivity. If the activity doesn't exist, one is created.
UserActivityChannel channel = UserActivityChannel.GetDefault();
UserActivity userActivity = await channel.GetOrCreateUserActivityAsync(item.Id);
// Populate required properties
userActivity.VisualElements.DisplayText = video.Snippet.Title;
userActivity.VisualElements.Description = video.Snippet.ChannelTitle;
userActivity.ActivationUri = new Uri("my-app://page2?action=edit");
//Save
await userActivity.SaveAsync(); //save the new metadata
// Dispose of any current UserActivitySession, and create a new one.
_currentActivity?.Dispose();
_currentActivity = userActivity.CreateSession();
}
public static void Delete(HistoryItem item)
{
if (!SecretsVault.IsAuthorized)
return;
Items.Remove(item);
Save();
}
public static void Clear()
{
if (!SecretsVault.IsAuthorized)
return;
Items.Clear();
Save();
}
public static void Save()
{
List<HistoryItem>[] parts = new List<HistoryItem>[4]
{
new List<HistoryItem>(), new List<HistoryItem>(),
new List<HistoryItem>(), new List<HistoryItem>()
};
foreach(HistoryItem i in Items)
if (parts[0].Count < 50)
parts[0].Add(i);
else
{
if (parts[1].Count < 50)
parts[1].Add(i);
else
{
if (parts[2].Count < 50)
parts[2].Add(i);
else
parts[3].Add(i);
}
}
for(int k = 0; k < parts.Length; k++)
ApplicationData.Current.RoamingSettings.Values[$"history-{SecretsVault.AccountId}-level{k}"] = JsonConvert.SerializeObject(parts[k]);
}
public static void Load()
{
try
{
for (int k = 0; k < 4; k++)
Items.AddRange(JsonConvert.DeserializeObject<List<HistoryItem>>(ApplicationData.Current.RoamingSettings.Values[$"history-{SecretsVault.AccountId}-level{k}"] as string));
}
catch { }
}
}
}