Skip to content

Commit

Permalink
Fix theme shuffler sometimes picking same theme as last
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Oct 26, 2024
1 parent 2321cd8 commit d546b33
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/ThemeShuffler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,10 @@ public static ToolStripItem[] GetMenuItems()
};
}

public static void AddThemeToHistory(string themeId, bool clearHistory = false)
public static void AddThemeToHistory(string themeId)
{
List<string> shuffleHistory;

if (!clearHistory)
{
shuffleHistory = JsonConfig.settings.shuffleHistory?.ToList() ?? new List<string>();
shuffleHistory.Remove(themeId);
}
else
{
shuffleHistory = new List<string>();
}

List<string> shuffleHistory = JsonConfig.settings.shuffleHistory?.ToList() ?? new List<string>();
shuffleHistory.Remove(themeId);
shuffleHistory.Add(themeId);
JsonConfig.settings.shuffleHistory = shuffleHistory.ToArray();
JsonConfig.settings.lastShuffleTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -147,7 +137,7 @@ public static void AddThemeToHistory(string themeId, bool clearHistory = false)
return nextUpdateTime?.AddTicks(1);
}

private static ThemeConfig GetNextTheme()
private static ThemeConfig GetNextTheme(string lastThemeId)
{
List<string> shuffleHistory = JsonConfig.settings.shuffleHistory?.ToList() ?? new List<string>();
ThemeConfig[] themeChoices = GetThemeChoices().Where(
Expand All @@ -162,15 +152,16 @@ private static ThemeConfig GetNextTheme()
{
themeChoices = GetThemeChoices().ToArray();
nextTheme = themeChoices[rng.Next(themeChoices.Length)];
string lastThemeId = shuffleHistory.LastOrDefault();
lastThemeId = lastThemeId ?? shuffleHistory.LastOrDefault();
Array.Clear(JsonConfig.settings.shuffleHistory);

while ((themeChoices.Length > 1) && (nextTheme.themeId == lastThemeId))
{
nextTheme = themeChoices[rng.Next(themeChoices.Length)];
}
}

AddThemeToHistory(nextTheme.themeId, themeChoices.Length == 0);
AddThemeToHistory(nextTheme.themeId);
return nextTheme;
}

Expand All @@ -196,19 +187,19 @@ private static void ShuffleWallpaper()
{
if (JsonConfig.settings.activeThemes[0] != null)
{
JsonConfig.settings.activeThemes[0] = GetNextTheme().themeId;
JsonConfig.settings.activeThemes[0] = GetNextTheme(JsonConfig.settings.activeThemes[0]).themeId;
}
else
{
for (int i = 1; i < JsonConfig.settings.activeThemes.Length; i++)
{
JsonConfig.settings.activeThemes[i] = GetNextTheme().themeId;
JsonConfig.settings.activeThemes[i] = GetNextTheme(JsonConfig.settings.activeThemes[i]).themeId;
}
}

if (JsonConfig.settings.lockScreenTheme != null)
{
JsonConfig.settings.lockScreenTheme = GetNextTheme().themeId;
JsonConfig.settings.lockScreenTheme = GetNextTheme(JsonConfig.settings.lockScreenTheme).themeId;
}
}

Expand Down

0 comments on commit d546b33

Please sign in to comment.