Updated Program.

Removed all code from console testing, and moved into App.xaml.cs for
handling of the Syncing of things in co-op with the UI Thread.
This commit is contained in:
Mario Steele 2025-07-31 03:35:26 -05:00
parent b0ac6c1b7b
commit 6fb47905c0

View file

@ -18,100 +18,7 @@ class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
GlobalJsonOptions.Options.Converters.Add(new StringToLongJsonConverter(false));
var paths = new string[]
{
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config/FreeTube/"),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".var/app/io.freetubeapp.FreeTube/config/FreeTube")
};
var path = "";
foreach (var tpath in paths)
{
if (!Directory.Exists(tpath)) continue;
path = tpath;
break;
}
var dbWatcher = new DBSyncWatcher(path);
var historySyncer = new Syncer<History>(dbWatcher, Path.Join(path, "history.db"), "history.db", "/history");
var playlistSyncer = new Syncer<Playlist>(dbWatcher, Path.Join(path, "playlists.db"), "playlists.db", "/playlist");
var profileSyncer = new Syncer<Profile>(dbWatcher, Path.Join(path, "profiles.db"), "profiles.db", "/profile");
var searchHistorySyncer = new Syncer<SearchHistory>(dbWatcher, Path.Join(path, "search-history.db"), "search-history.db", "/searchHistory");
var settingsSyncer = new Syncer<Setting>(dbWatcher, Path.Join(path, "settings.db"), "settings.db", "/settings");
Task.Run(() => CheckCanSync(historySyncer, playlistSyncer, profileSyncer, searchHistorySyncer, settingsSyncer));
Console.WriteLine("Watching databases... Press return to exit...");
Console.ReadLine();
}
private static void CheckCanSync(Syncer<History>? historySyncer = null, Syncer<Playlist>? playlistSyncer = null,
Syncer<Profile>? profileSyncer = null, Syncer<SearchHistory>? searchHistorySyncer = null,
Syncer<Setting>? settingsSyncer = null)
{
var syncers = new List<ISyncer>()
{
historySyncer,
playlistSyncer,
profileSyncer,
searchHistorySyncer,
settingsSyncer
};
var lastTime = DateTime.Now;
var checkInterval = TimeSpan.FromSeconds(30);
while (true)
{
if (syncers.Any(x => x != null && x.IsDirty() ))
{
Thread.Sleep(100);
var lastCheck = DateTime.Now - lastTime;
if (lastCheck > checkInterval)
{
var start = DateTime.Now;
Console.WriteLine("Checking for updates...");
foreach (var syncer in syncers)
syncer.FetchDatabase().Wait();
lastTime = DateTime.Now;
var end = DateTime.Now - start;
Console.WriteLine($"Check completed. Total Time: {end}");
}
var procs = Process.GetProcessesByName("FreeTube");
if (procs.Length > 0) continue;
Console.WriteLine("FreeTube has closed and we have updates, we're going to try and update.");
Thread.Sleep(1500);
if (historySyncer != null && historySyncer.IsDirty())
historySyncer.Sync();
if (playlistSyncer != null && playlistSyncer.IsDirty())
playlistSyncer.Sync();
if (profileSyncer != null && profileSyncer.IsDirty())
profileSyncer.Sync();
if (searchHistorySyncer != null && searchHistorySyncer.IsDirty())
searchHistorySyncer.Sync();
if (settingsSyncer != null && settingsSyncer.IsDirty())
settingsSyncer.Sync();
}
else
{
Thread.Sleep(100);
var lastCheck = DateTime.Now - lastTime;
if (lastCheck < checkInterval) continue;
var start = DateTime.Now;
Console.WriteLine("Checking for updates...");
foreach (var syncer in syncers)
syncer.FetchDatabase().Wait();
lastTime = DateTime.Now;
var end = DateTime.Now - start;
Console.WriteLine($"Check completed. Total Time: {end}");
}
}
}
// public static void Main(string[] args) => BuildAvaloniaApp()
// .StartWithClassicDesktopLifetime(args);
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()