Skip to content

Commit

Permalink
better state management with thread locking
Browse files Browse the repository at this point in the history
  • Loading branch information
wiegell committed Nov 16, 2022
1 parent 6652123 commit 08df4bf
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 81 deletions.
5 changes: 3 additions & 2 deletions AstroWall/ApplicationLayer/Helpers/Screen.Model.Macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public static Screen Main()
private Screen(NSScreen nSscreen)
{
Id = nSscreen.LocalizedName;
xRes = (int)nSscreen.Frame.Size.Width * 2;
yRes = (int)nSscreen.Frame.Size.Height * 2;
// Always assume HDPI (x2)
xRes = (int)(nSscreen.Frame.Size.Width * 2);
yRes = (int)(nSscreen.Frame.Size.Height * 2);
isMainScreen = NSScreen.MainScreen == nSscreen;
}

Expand Down
8 changes: 4 additions & 4 deletions AstroWall/ApplicationLayer/View/AppDelegate.Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public void hideSubTitle()
});
}

public void changeIconTo(string iconName, bool doubleCheckState = false, BusinessLayer.stateEnum doubleCheckStateShouldHaveThisValue = BusinessLayer.stateEnum.BrowsingWallpapers)
public void changeIconTo(string iconName, bool doubleCheckState = false)
{
Action ac = () =>
{
if (doubleCheckState && appHandler.State.state != doubleCheckStateShouldHaveThisValue) return;
//if (doubleCheckState && appHandler.State.state != doubleCheckStateShouldHaveThisValue) return;
var image = NSImage.ImageNamed(iconName);
image.Template = true;
statusBarItem.Button.Image = image;
Expand Down Expand Up @@ -136,9 +136,9 @@ Action onclickCallBack
{
if (e.Description == "Mouse Entered" && previewIsLoaded)
{
if (stateRef.state != BusinessLayer.stateEnum.BrowsingWallpapers)
if (!stateRef.isBrowsingWallpapers)
{
stateRef.setStateBrowsing();
stateRef.SetStateBrowsingWallpapers();
}
cancelEndBrowsingStateWithDelayCallback();
setPreviewWallpaperCallback();
Expand Down
1 change: 1 addition & 0 deletions AstroWall/ApplicationLayer/View/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public AppDelegate()
#region Override Methods
public async override void DidFinishLaunching(NSNotification notification)
{
Console.WriteLine("UI thread: " + Thread.CurrentThread.ManagedThreadId);
await appHandler.Init();
}

Expand Down
15 changes: 8 additions & 7 deletions AstroWall/BusinessLayer/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ private async Task secondaryInit(Preferences.Preferences prefsFromPostInstallPro
// Populate submenu
MenuHandler.PopulateSubmenuLatestPictures(db.getPresentableImages(), State);

// Check for new pics
// Check for new pics, don't wait up
if (Prefs.DailyCheck == DailyCheckEnum.Newest)
{
await checkForNewPics();
Wallpaper.RunPostProcessAndSetWallpaperAllScreens(db.ImgWrapList[0]);
}
Task.Run(async () =>
{
await checkForNewPics();
await Wallpaper.RunPostProcessAndSetWallpaperAllScreens(db.ImgWrapList[0]);
});

State.SetStateIdle();

// Check for updates
Updates.ConsiderCheckingForUpdates();
Expand All @@ -117,7 +117,7 @@ private async Task secondaryInit(Preferences.Preferences prefsFromPostInstallPro
break;
}
}

State.UnsetStateInitializing();
}

public async Task checkForNewPics()
Expand All @@ -139,6 +139,7 @@ public async Task checkForNewPics()

// Update submenu
MenuHandler.PopulateSubmenuLatestPictures(db.getPresentableImages(), State);
State.UnsetStateDownloading();
}
}

Expand Down
3 changes: 2 additions & 1 deletion AstroWall/BusinessLayer/ImgWrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using AstroWall.ApplicationLayer.Helpers;
using System.Threading;

namespace AstroWall.BusinessLayer
{
Expand Down Expand Up @@ -178,7 +179,7 @@ private async Task createPreviewFromFullSize(int width = 250, int height = 180)

public async Task createPostProcessedImages(Dictionary<string, Screen> screens, Dictionary<Preferences.PostProcessType, Preferences.PostProcess> postProcessPrefsDictionary)
{
Console.WriteLine("creating postprocessed images");
Console.WriteLine("creating postprocessed images on thread: " + Thread.CurrentThread.ManagedThreadId);

// Load full res image
SKBitmap image;
Expand Down
18 changes: 11 additions & 7 deletions AstroWall/BusinessLayer/MenuHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,13 @@ public void PopulateSubmenuLatestPictures(List<ImgWrap> imgWrapList, State state
() => setEndBrowsingStateWithDelay(),
() =>
{
appHandler.Prefs.CurrentAstroWallpaper = iw;
appHandler.Wallpaper.RunPostProcessAndSetWallpaperAllScreens(iw);
// Task wrap to run un non-UI thread
Task.Run(async () =>
{
appHandler.State.UnsetStateBrowsingWallpapers();
appHandler.Prefs.CurrentAstroWallpaper = iw;
await appHandler.Wallpaper.RunPostProcessAndSetWallpaperAllScreens(iw);
});
}


Expand Down Expand Up @@ -181,6 +186,7 @@ public void changedInMenuCheckUpdatesAtStartup(bool newState)
}
appDelegate.updateMenuCheckMarks(appHandler.Prefs);
}

public void changedInMenuDailyCheckNewest(bool newState)
{
Console.WriteLine("Daily check changed to newest: " + newState);
Expand All @@ -202,28 +208,26 @@ private void renewCancellationSource()

private void OnTimedEventDownloadAnimation(Object stateInfo)
{

int flipCounter1based = flipCounter + 1;
string iconName = "download" + flipCounter1based;
appDelegate.changeIconTo("download" + flipCounter1based, true, stateEnum.Downloading);
appDelegate.changeIconTo("download" + flipCounter1based, true);
flipCounter = (flipCounter + 1) % 3;

}

private void OnTimedEventSpinnerAnimation(Object stateInfo)
{
int iconRotationDeg = (flipCounter * 15);
string iconName = "MainIcon_rot_" + iconRotationDeg;
Console.WriteLine("iconname: " + iconName);
appDelegate.changeIconTo(iconName, true, BusinessLayer.stateEnum.Initializing);
appDelegate.changeIconTo(iconName, true);
flipCounter = (flipCounter + 1) % 6;
}

private async Task setEndBrowsingStateWithDelay()
{
await Task.Delay(100, this.cancellationToken);
appHandler.Wallpaper.ResetWallpaper();
appHandler.State.SetStateIdle();
appHandler.State.UnsetStateBrowsingWallpapers();
}

private void cancelEndBrowsingStateWithDelay()
Expand Down
156 changes: 121 additions & 35 deletions AstroWall/BusinessLayer/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@

namespace AstroWall.BusinessLayer
{
public enum stateEnum
{
Initializing,
Idle,
Downloading,
PostProcessing,
SettingWallpaper,
BrowsingWallpapers
}

public class State
{
// Application state
public readonly object _lock_ = new Object();
// All of below should only be accessed if locked
public bool isInitializing;
public bool isPostProcessing;
public bool isIdle;
public bool isDownloading;
public bool isSettingWallpaper;
public bool isBrowsingWallpapers;
public bool isChoosingPrefs;

// Refs
ApplicationHandler applicationHandler;

// Overall state
public stateEnum state { get; private set; }

// currentVersionString is the long tag from git including commit hash
public State(ApplicationHandler applicationHandlerArg, string currentVersionString)
{
Expand All @@ -34,44 +33,131 @@ public State(ApplicationHandler applicationHandlerArg, string currentVersionStri

public void SetStateInitializing()
{
Console.WriteLine("State: Initializing");
state = stateEnum.Initializing;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.DisableAllItems();
applicationHandler.MenuHandler.SetTitleInitialising();
applicationHandler.MenuHandler.RunSpinnerIconAnimation();
lock (_lock_)
{
Console.WriteLine("State: Initializing");
isIdle = false;
isInitializing = true;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.DisableAllItems();
applicationHandler.MenuHandler.SetTitleInitialising();
applicationHandler.MenuHandler.RunSpinnerIconAnimation();
}
}
public void UnsetStateInitializing()
{
lock (_lock_)
{
Console.WriteLine("State unset: Initializing");
isInitializing = false;
trySetStateIdle();
}
}

public void SetStateDownloading(string downloadingWhat)
{
Console.WriteLine("State: Downloading");
state = stateEnum.Downloading;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.DisableAllItems();
applicationHandler.MenuHandler.SetTitleDownloading(downloadingWhat);
applicationHandler.MenuHandler.RunDownloadIconAnimation();
lock (_lock_)
{
Console.WriteLine("State: Downloading");
isDownloading = true;
isIdle = false;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.DisableAllItems();
applicationHandler.MenuHandler.SetTitleDownloading(downloadingWhat);
applicationHandler.MenuHandler.RunDownloadIconAnimation();
}
}
public void UnsetStateDownloading()
{
lock (_lock_)
{
Console.WriteLine("State unset: Downloading");
isDownloading = false;
trySetStateIdle();
}
}

public void SetStateChoosePrefs()
{
Console.WriteLine("State: Choose prefs");
applicationHandler.MenuHandler.disableStatusIcon();
lock (_lock_)
{
Console.WriteLine("State: Choose prefs");
isDownloading = true;
isIdle = false;
applicationHandler.MenuHandler.disableStatusIcon();
}
}
public void UnsetStateChoosePrefs()
{
lock (_lock_)
{
Console.WriteLine("State unset: ChoosePrefs");
isChoosingPrefs = false;
trySetStateIdle();
}
}

public void SetStatePostProcessing()
{
lock (_lock_)
{
Console.WriteLine("State: PostProcessing");
isPostProcessing = true;
isIdle = false;
// Check to see if animation already running
if (!isInitializing) applicationHandler.MenuHandler.RunSpinnerIconAnimation();
}
}
public void UnsetStatePostProcessing()
{
lock (_lock_)
{
Console.WriteLine("State unset: PostProcessing");
isPostProcessing = false;
trySetStateIdle();
}
}

public void SetStateIdle()
public void SetStateBrowsingWallpapers()
{
lock (_lock_)
{
Console.WriteLine("State: browsing wallpapers");
isBrowsingWallpapers = true;
}
}
public void UnsetStateBrowsingWallpapers()
{
Console.WriteLine("Setting state to idle:");
state = stateEnum.Idle;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.SetIconToDefault();
applicationHandler.MenuHandler.HideState();
lock (_lock_)
{
Console.WriteLine("State unset: browsing wallpapers");
isBrowsingWallpapers = false;
trySetStateIdle();
}
}

public void setStateBrowsing()
private void trySetStateIdle()
{
Console.WriteLine("Setting state browsing");
state = BusinessLayer.stateEnum.BrowsingWallpapers;
lock (_lock_)
{
Console.WriteLine("Trying to set state idle: {0},{1},{2},{3},{4},{5}", isBrowsingWallpapers, isChoosingPrefs, isDownloading, isInitializing, isPostProcessing, isSettingWallpaper);
if (
!(
isBrowsingWallpapers ||
isChoosingPrefs ||
isDownloading ||
isInitializing ||
isPostProcessing ||
isSettingWallpaper
))
{
Console.WriteLine("Setting state to idle:");
isIdle = true;
applicationHandler.MenuHandler.EnableStatusIcon();
applicationHandler.MenuHandler.SetIconToDefault();
applicationHandler.MenuHandler.HideState();
}
}
}

public void SetLaunchAgentToReflectPrefs()
Expand Down
2 changes: 2 additions & 0 deletions AstroWall/BusinessLayer/Updates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public async void ConsiderCheckingForUpdates()
{
if (applicationHandler.Prefs.CheckUpdatesOnStartup)
{
applicationHandler.State.SetStateDownloading("Checking updates");
await CheckForUpdates();
applicationHandler.State.UnsetStateDownloading();
}
}

Expand Down
Loading

0 comments on commit 08df4bf

Please sign in to comment.