Skip to content

Commit

Permalink
Switched to using source generators for ViewModels, added library ref…
Browse files Browse the repository at this point in the history
…resh, and fixed issue with hidden achievements not showing when "Show Hidden" was toggled.
  • Loading branch information
syntax-tm committed Nov 25, 2023
1 parent 43fb1c3 commit 05cb697
Show file tree
Hide file tree
Showing 33 changed files with 444 additions and 506 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.2.0

- Added new release workflow
- Added ability to Refresh Steam library
- Switched ViewModels to source generators
- [BUG] Fixed issue with the "Show Hidden" button not working for Achievements

## 0.1.0

Expand Down
2 changes: 0 additions & 2 deletions src/SAM.Core/API/Steamworks/SteamLibraryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace SAM.Core
{
public class SteamLibraryManager
{

private static readonly ILog log = LogManager.GetLogger(nameof(SteamLibraryManager));

private static readonly object syncLock = new ();
Expand Down Expand Up @@ -54,6 +53,5 @@ public static void Init()
throw new SAMInitializationException(message, e);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class MultiBoolToVisibilityConverter : IMultiValueConverter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var boolValues = values.Cast<bool>();
var boolValues = values.Where(v => v is bool).Cast<bool>();

var result = Mode switch
{
MultiBoolEvaluationMode.All => boolValues.All(b => b),
MultiBoolEvaluationMode.Any => boolValues.Any(b => b),
MultiBoolEvaluationMode.None => !boolValues.All(b => b),
_ => throw new ArgumentOutOfRangeException()
_ => throw new ArgumentOutOfRangeException(nameof(Mode))
};

if (Inverse)
Expand Down
29 changes: 10 additions & 19 deletions src/SAM.Core/Common/Mvvm/ObservableHandlerT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace SAM.Core
{
// ReSharper disable InconsistentNaming
public class ObservableHandler<T> : IWeakEventListener
where T : class, INotifyPropertyChanged
{
Expand All @@ -23,7 +24,7 @@ public ObservableHandler([NotNull] T source)

_source = new (source);
}

[NotNull]
public ObservableHandler<T> Add([NotNull] Expression<Func<T, object>> expression, [NotNull] Action handler)
{
Expand All @@ -32,11 +33,7 @@ public ObservableHandler<T> Add([NotNull] Expression<Func<T, object>> expression
throw new ArgumentNullException(nameof(handler));
}

var source = GetSource();
if (source == null)
{
throw new InvalidOperationException("Source has been garbage collected.");
}
var source = GetSource() ?? throw new InvalidOperationException("Source has been garbage collected.");

var propertyName = ReflectionHelper.GetPropertyNameFromLambda(expression);

Expand All @@ -54,11 +51,7 @@ public ObservableHandler<T> Add([NotNull] Expression<Func<T, object>> expression
throw new ArgumentNullException(nameof(handler));
}

var source = GetSource();
if (source == null)
{
throw new InvalidOperationException("Source has been garbage collected.");
}
var source = GetSource() ?? throw new InvalidOperationException("Source has been garbage collected.");

var propertyName = ReflectionHelper.GetPropertyNameFromLambda(expression);

Expand All @@ -67,7 +60,7 @@ public ObservableHandler<T> Add([NotNull] Expression<Func<T, object>> expression

return this;
}

[NotNull]
public ObservableHandler<T> AddAndInvoke([NotNull] Expression<Func<T, object>> expression, [NotNull] Action handler)
{
Expand All @@ -83,7 +76,7 @@ public ObservableHandler<T> AddAndInvoke([NotNull] Expression<Func<T, object>> e
handler(GetSource());
return this;
}

private T GetSource()
{
if (_source.TryGetTarget(out var source)) return source;
Expand All @@ -103,26 +96,23 @@ public virtual bool OnReceiveWeakEvent(Type managerType, object sender, EventArg
return false;
}

var propertyName = ((PropertyChangedEventArgs)e).PropertyName;
var propertyName = ((PropertyChangedEventArgs) e).PropertyName;
Notify(propertyName);

return true;
}

protected void Notify(string propertyName)
{
var source = GetSource();
if (source == null)
{
throw new InvalidOperationException("Confused, received a PropertyChanged event from a source that has been garbage collected.");
}
var source = GetSource() ?? throw new InvalidOperationException("Source has been garbage collected.");

if (string.IsNullOrEmpty(propertyName))
{
foreach (var handler in _handlers.Values)
{
handler();
}

foreach (var handler in _handlersT.Values)
{
handler(source);
Expand All @@ -137,5 +127,6 @@ protected void Notify(string propertyName)
handlerT(source);
}
}

}
}
5 changes: 4 additions & 1 deletion src/SAM.Core/Models/SteamLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ public void Refresh(bool loadCache = false)

CancelRefresh();

_libraryWorker.RunWorkerAsync();
if (!_libraryWorker.IsBusy)
{
_libraryWorker.RunWorkerAsync();
}
}

public void CancelRefresh()
Expand Down
52 changes: 24 additions & 28 deletions src/SAM.Core/Models/SteamUser.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
using System.Windows.Media;
using System.Xml;
using DevExpress.Mvvm.POCO;
using DevExpress.Mvvm.CodeGenerators;
using log4net;
using SAM.Core.Extensions;

namespace SAM.Core
{
// TODO: add support for any steam user (currently only supports the current steam user)
public class SteamUser
[GenerateViewModel]
public partial class SteamUser
{
private const string PROFILE_URL_FORMAT = @"https://steamcommunity.com/profiles/{0}";
private const string PROFILE_XML_URL_FORMAT = @"https://steamcommunity.com/profiles/{0}?xml=1";

private static readonly ILog log = LogManager.GetLogger(nameof(SteamUser));

public virtual ulong SteamId64 { get; set; }
public virtual string SteamId { get; set; }
public virtual int PlayerLevel { get; set; }
public virtual string CustomUrl { get; set; }
public virtual decimal RecentHoursPlayed { get; set; }
public virtual string Headline { get; set; }
public virtual string Location { get; set; }
public virtual string DisplayLocation { get; set; }
public virtual string MemberSince { get; set; }
public virtual string RealName { get; set; }
public virtual string ProfileUrl { get; set; }
public virtual string AvatarIcon { get; set; }
public virtual string AvatarMedium { get; set; }
public virtual string AvatarFull { get; set; }
public virtual bool VACBanned { get; set; }
public virtual bool IsLimitedAccount { get; set; }

public virtual ImageSource Avatar { get; set; }

protected SteamUser()
[GenerateProperty] private ulong steamId64;
[GenerateProperty] private string steamId;
[GenerateProperty] private int playerLevel;
[GenerateProperty] private string customUrl;
[GenerateProperty] private decimal recentHoursPlayed;
[GenerateProperty] private string headline;
[GenerateProperty] private string location;
[GenerateProperty] private string displayLocation;
[GenerateProperty] private string memberSince;
[GenerateProperty] private string realName;
[GenerateProperty] private string profileUrl;
[GenerateProperty] private string avatarIcon;
[GenerateProperty] private string avatarMedium;
[GenerateProperty] private string avatarFull;
[GenerateProperty] private bool vacBanned;
[GenerateProperty] private bool isLimitedAccount;

[GenerateProperty] private ImageSource avatar;

public SteamUser()
{
RefreshProfile();
}

public static SteamUser Create()
{
return ViewModelSource.Create(() => new SteamUser());
}

private void RefreshProfile()
{
var client = SteamClientManager.Default;
Expand Down Expand Up @@ -80,7 +76,7 @@ private void RefreshProfile()
// TODO: no idea what this is for since mine and everyone i checked was empty
//var steamRating = GetValue(doc, @"//steamRating");

VACBanned = doc.GetValueAsBool(@"//vacBanned");
VacBanned = doc.GetValueAsBool(@"//vacBanned");
IsLimitedAccount = doc.GetValueAsBool(@"//isLimitedAccount");
RecentHoursPlayed = doc.GetValueAsDecimal(@"//hoursPlayed2Wk");

Expand Down
1 change: 1 addition & 0 deletions src/SAM.Core/SAM.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

<ItemGroup>
<PackageReference Include="Autofac" Version="7.1.0" />
<PackageReference Include="DevExpress.Mvvm.CodeGenerators" Version="22.1.1" />
<PackageReference Include="DevExpressMvvm" Version="22.1.3" />
<PackageReference Include="gong-wpf-dragdrop" Version="3.2.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
Expand Down
19 changes: 18 additions & 1 deletion src/SAM.Core/Stats/SteamAchievement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public class SteamAchievement : BindableBase
public string IconLockedName => AchievementDefinition.IconLocked;
public string IconNormalName => AchievementDefinition.IconNormal;
public string Name => AchievementDefinition.Name;
public string Description => AchievementDefinition.IsHidden && !IsAchieved ? @"Hidden" : AchievementDefinition.Description;
public string Description
{
get => GetProperty(() => Description);
set => SetProperty(() => Description, value);
}
public string FullDescription => AchievementDefinition.Description;
public int Permission => AchievementDefinition.Permission;
public bool OriginalLockState
Expand Down Expand Up @@ -77,9 +81,22 @@ public SteamAchievement(uint gameId, AchievementInfo definition)

DownloadIcons();

RefreshDescription();

_isLoading = false;
}

public void RefreshDescription(bool forceShow = false)
{
if (forceShow)
{
Description = AchievementDefinition.Description;
return;
}

Description = AchievementDefinition.IsHidden && !IsAchieved ? @"Hidden" : AchievementDefinition.Description;
}

public void Unlock()
{
if (IsAchieved) return;
Expand Down
55 changes: 26 additions & 29 deletions src/SAM.Core/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
using System;
using System.ComponentModel;
using System.Windows.Data;
using DevExpress.Mvvm.POCO;
using DevExpress.Mvvm.CodeGenerators;
using log4net;
using SAM.Core.Converters;
using SAM.Core.Extensions;

namespace SAM.Core.ViewModels
{
public class HomeViewModel
[GenerateViewModel]
public partial class HomeViewModel
{
// ReSharper disable once InconsistentNaming
protected readonly ILog log = LogManager.GetLogger(nameof(HomeViewModel));

private CollectionViewSource _itemsViewSource;
private bool _loading = true;

public virtual bool EnableGrouping { get; set; }
public virtual string FilterText { get; set; }
public virtual string FilterNormal { get; set; }
public virtual string FilterDemos { get; set; }
public virtual string FilterMods { get; set; }
public virtual bool FilterJunk { get; set; }
public virtual bool ShowHidden { get; set; }
public virtual bool FilterFavorites { get; set; }
public virtual string FilterTool { get; set; }
public virtual int TileWidth { get; set; } = 100;
public virtual ICollectionView ItemsView { get; set; }
[GenerateProperty] private bool enableGrouping;
[GenerateProperty] private string filterText;
[GenerateProperty] private string filterNormal;
[GenerateProperty] private string filterDemos;
[GenerateProperty] private string filterMods;
[GenerateProperty] private bool filterJunk;
[GenerateProperty] private bool showHidden;
[GenerateProperty] private bool filterFavorites;
[GenerateProperty] private string filterTool;
[GenerateProperty] private int tileWidth = 100;
[GenerateProperty] private ICollectionView itemsView;

public SteamApp SelectedItem
{
get => (SteamApp) ItemsView.CurrentItem;
set => ItemsView.MoveCurrentTo(value);
get => (SteamApp) ItemsView!.CurrentItem;
set => ItemsView!.MoveCurrentTo(value);
}
public virtual SteamLibrary Library { get; set; }
[GenerateProperty] private SteamLibrary library;

protected HomeViewModel()
public HomeViewModel()
{
Refresh();
}

public static HomeViewModel Create()
{
return ViewModelSource.Create(() => new HomeViewModel());
}

private void ItemsViewSourceOnFilter(object sender, FilterEventArgs e)
{
if (e.Item is not SteamApp app) throw new ArgumentException(nameof(e.Item));
Expand All @@ -56,18 +53,20 @@ private void ItemsViewSourceOnFilter(object sender, FilterEventArgs e)

e.Accepted = isNameMatch && isJunkFiltered && isHiddenFiltered && isNonFavoriteFiltered;
}


[GenerateCommand]
public void Loaded()
{
}

public void Refresh(bool force = true)
[GenerateCommand]
public void Refresh(bool force = false)
{
_loading = true;

if (force)
{
//SteamLibraryManager.Refresh();
SteamLibraryManager.DefaultLibrary.Refresh();
}

Library = SteamLibraryManager.DefaultLibrary;
Expand All @@ -88,8 +87,6 @@ public void Refresh(bool force = true)
_itemsViewSource.LiveFilteringProperties.Add(nameof(SteamApp.IsHidden));
_itemsViewSource.LiveFilteringProperties.Add(nameof(SteamApp.IsFavorite));

//_itemsViewSource.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SteamApp.Name), new StringToGroupConverter()));

_itemsViewSource.IsLiveFilteringRequested = true;
_itemsViewSource.IsLiveSortingRequested = true;
_itemsViewSource.IsLiveGroupingRequested = false;
Expand Down Expand Up @@ -119,9 +116,9 @@ protected void OnEnableGroupingChanged()

if (EnableGrouping)
{
ItemsView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SteamApp.Name), new StringToGroupConverter()));
ItemsView!.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SteamApp.Name), new StringToGroupConverter()));
}
}
}
}
}
}
Loading

0 comments on commit 05cb697

Please sign in to comment.