Skip to content

Commit

Permalink
Visually indicate to users that they should click Refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jul 29, 2024
1 parent 2561753 commit b3ed9b6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 23 deletions.
29 changes: 9 additions & 20 deletions ConsoleUI/ModListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,16 @@ public ModListScreen(GameInstanceManager mgr, RepositoryDataManager repoData, Re
AddObject(new ConsoleLabel(
-searchWidth, -1, -2,
() => {
int days = daysSinceUpdated(registryFilePath());
var days = Math.Round(timeSinceUpdate.TotalDays);
return days < 1 ? ""
: days == 1 ? string.Format(Properties.Resources.ModListUpdatedDayAgo, days)
: string.Format(Properties.Resources.ModListUpdatedDaysAgo, days);
},
null,
(ConsoleTheme th) => {
int daysSince = daysSinceUpdated(registryFilePath());
return daysSince < daysTillStale ? th.RegistryUpToDate
: daysSince < daystillVeryStale ? th.RegistryStale
: th.RegistryVeryStale;
return timeSinceUpdate < RepositoryDataManager.TimeTillStale ? th.RegistryUpToDate
: timeSinceUpdate < RepositoryDataManager.TimeTillVeryStale ? th.RegistryStale
: th.RegistryVeryStale;
}
));

Expand Down Expand Up @@ -442,16 +441,6 @@ private bool ViewSuggestions(ConsoleTheme theme)
return true;
}

private string registryFilePath()
{
return Path.Combine(manager.CurrentInstance.CkanDir(), "registry.json");
}

private int daysSinceUpdated(string filename)
{
return (DateTime.Now - File.GetLastWriteTime(filename)).Days;
}

private bool PlayGame()
=> PlayGame(manager.CurrentInstance
.game
Expand Down Expand Up @@ -591,6 +580,7 @@ private void RefreshList(ConsoleTheme theme)

private List<CkanModule> GetAllMods(ConsoleTheme theme, bool force = false)
{
timeSinceUpdate = repoData.LastUpdate(registry.Repositories.Values);
ScanForMods();
if (allMods == null || force) {
if (!registry?.HasAnyAvailable() ?? false)
Expand Down Expand Up @@ -722,12 +712,13 @@ private long totalInstalledDownloadSize()
return total;
}

private readonly GameInstanceManager manager;
private RegistryManager regMgr;
private Registry registry;
private readonly GameInstanceManager manager;
private RegistryManager regMgr;
private Registry registry;
private readonly RepositoryDataManager repoData;
private Dictionary<bool, List<CkanModule>> upgradeableGroups;
private readonly bool debug;
private TimeSpan timeSinceUpdate = TimeSpan.Zero;

private readonly ConsoleField searchBox;
private readonly ConsoleListBox<CkanModule> moduleList;
Expand All @@ -739,8 +730,6 @@ private long totalInstalledDownloadSize()
Properties.Resources.ModListSearchFocusedGhostText.Length,
Properties.Resources.ModListSearchUnfocusedGhostText.Length
));
private const int daysTillStale = 7;
private const int daystillVeryStale = 30;

private static readonly string unavailable = "!";
private static readonly string notinstalled = " ";
Expand Down
4 changes: 2 additions & 2 deletions ConsoleUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ If you uninstall it, CKAN will not be able to re-install it.</value></data>
<data name="ModListAutoInstTip" xml:space="preserve"><value>Mark auto-installed</value></data>
<data name="ModListUserSelectedTip" xml:space="preserve"><value>Mark user-selected</value></data>
<data name="ModListApplyChangesTip" xml:space="preserve"><value>Apply changes</value></data>
<data name="ModListUpdatedDayAgo" xml:space="preserve"><value>Updated at least {0} day ago</value></data>
<data name="ModListUpdatedDaysAgo" xml:space="preserve"><value>Updated at least {0} days ago</value></data>
<data name="ModListUpdatedDayAgo" xml:space="preserve"><value>Updated {0} day ago</value></data>
<data name="ModListUpdatedDaysAgo" xml:space="preserve"><value>Updated {0} days ago</value></data>
<data name="ModListPlayMenu" xml:space="preserve"><value>Play game</value></data>
<data name="ModListPlayMenuTip" xml:space="preserve"><value>Launch the game</value></data>
<data name="ModListSortMenu" xml:space="preserve"><value>Sort...</value></data>
Expand Down
23 changes: 23 additions & 0 deletions Core/Repositories/RepositoryDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ public void Prepopulate(List<Repository> repos, IProgress<int> percentProgress)
}
}

public TimeSpan LastUpdate(IEnumerable<Repository> repos)
=> repos.Distinct().Where(r => repoDataStale(r))
.Select(r => RepoUpdateTimestamp(r))
.OfType<DateTime>()
.Select(dt => DateTime.Now - dt)
.DefaultIfEmpty(TimeSpan.Zero)
.Min();

public static readonly TimeSpan TimeTillStale = TimeSpan.FromDays(3);
public static readonly TimeSpan TimeTillVeryStale = TimeSpan.FromDays(14);

/// <summary>
/// Values to describe the result of an attempted repository update.
/// Failure is actually handled by throwing exceptions, so I'm not sure we need that.
Expand Down Expand Up @@ -150,6 +161,11 @@ public UpdateResult Update(Repository[] repos,
.ToArray();
if (toUpdate.Length < 1)
{
// Update timestamp for already up to date repos
foreach (var f in repos.Select(GetRepoDataPath))
{
File.SetLastWriteTimeUtc(f, DateTime.UtcNow);
}
user.RaiseProgress(Properties.Resources.NetRepoAlreadyUpToDate, 100);
user.RaiseMessage(Properties.Resources.NetRepoNoChanges);
return UpdateResult.NoChanges;
Expand Down Expand Up @@ -307,6 +323,13 @@ private IEnumerable<RepositoryData> GetRepoDatas(IEnumerable<Repository> repos)
.Where(data => data != null)
?? Enumerable.Empty<RepositoryData>();

private DateTime? RepoUpdateTimestamp(Repository repo)
=> FileTimestamp(GetRepoDataPath(repo));

private static DateTime? FileTimestamp(string path)
=> File.Exists(path) ? (DateTime?)File.GetLastWriteTime(path)
: null;

private string etagsPath => Path.Combine(reposDir, "etags.json");
private Dictionary<Uri, string> etags = new Dictionary<Uri, string>();

Expand Down
24 changes: 24 additions & 0 deletions GUI/Controls/ManageMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,30 @@ private bool _UpdateModsList(Dictionary<string, bool> old_modules = null)

UpdateHiddenTagsAndLabels();

var timeSinceUpdate = guiConfig.RefreshOnStartup ? TimeSpan.Zero
: repoData.LastUpdate(registry.Repositories.Values);
Util.Invoke(this, () =>
{
if (timeSinceUpdate < RepositoryDataManager.TimeTillStale)
{
RefreshToolButton.Image = EmbeddedImages.refresh;
RefreshToolButton.ToolTipText = new SingleAssemblyComponentResourceManager(typeof(ManageMods))
.GetString($"{RefreshToolButton.Name}.ToolTipText");
}
else if (timeSinceUpdate < RepositoryDataManager.TimeTillVeryStale)
{
RefreshToolButton.Image = EmbeddedImages.refreshStale;
RefreshToolButton.ToolTipText = string.Format(Properties.Resources.ManageModsRefreshStaleToolTip,
Math.Round(timeSinceUpdate.TotalDays));
}
else
{
RefreshToolButton.Image = EmbeddedImages.refreshVeryStale;
RefreshToolButton.ToolTipText = string.Format(Properties.Resources.ManageModsRefreshVeryStaleToolTip,
Math.Round(timeSinceUpdate.TotalDays));
}
});

ClearStatusBar?.Invoke();
Util.Invoke(this, () => ModGrid.Focus());
return true;
Expand Down
4 changes: 3 additions & 1 deletion GUI/Properties/EmbeddedImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CKAN.GUI
#if NET5_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
public class EmbeddedImages
public static class EmbeddedImages
{
public static readonly Icon AppIcon = new Icon(
Assembly.GetExecutingAssembly()
Expand All @@ -36,6 +36,8 @@ public class EmbeddedImages
public static Bitmap info => get("info");
public static Bitmap ksp => get("ksp");
public static Bitmap refresh => get("refresh");
public static Bitmap refreshStale => get("refreshStale");
public static Bitmap refreshVeryStale => get("refreshVeryStale");
public static Bitmap resetCollapse => get("resetCollapse");
public static Bitmap search => get("search");
public static Bitmap settings => get("settings");
Expand Down
6 changes: 6 additions & 0 deletions GUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ Are you sure you want to skip this change?</value></data>
<data name="ManageModsHiddenLabels" xml:space="preserve"><value>Hidden labels:</value></data>
<data name="ManageModsHiddenTags" xml:space="preserve"><value>Hidden tags:</value></data>
<data name="ManageModsHiddenLabelsAndTags" xml:space="preserve"><value>Hidden labels and tags:</value></data>
<data name="ManageModsRefreshStaleToolTip" xml:space="preserve"><value>Your mod list was last updated {0} days ago.
Click Refresh regularly to keep it up to date.
Or choose "Update repositories on launch" in the settings.</value></data>
<data name="ManageModsRefreshVeryStaleToolTip" xml:space="preserve"><value>Your mod list was last updated {0} days ago!
Click Refresh to update!
Or choose "Update repositories on launch" in the settings.</value></data>
<data name="TotalPlayTime" xml:space="preserve"><value>Total play time: {0} hours</value></data>
<data name="ChangeTypeNone" xml:space="preserve"><value>None</value></data>
<data name="ChangeTypeInstall" xml:space="preserve"><value>Install</value></data>
Expand Down
Binary file added GUI/Resources/refreshStale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added GUI/Resources/refreshVeryStale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b3ed9b6

Please sign in to comment.