Skip to content

Commit

Permalink
refactor: Simplify MainWindowViewModel constructor
Browse files Browse the repository at this point in the history
Refactored MainWindowViewModel constructor by extracting property tracking, culture setup, and command line processing into separate methods for better readability and maintainability. Removed redundant initialization code and reorganized method calls for logical flow.
  • Loading branch information
DineshSolanki committed Jul 27, 2024
1 parent 9e8efd5 commit bc33309
Showing 1 changed file with 56 additions and 47 deletions.
103 changes: 56 additions & 47 deletions FoliCon/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,57 +233,16 @@ public FoliconThemes Theme

public MainWindowViewModel(IDialogService dialogService)
{
ProcessUtils.CheckWebView2();
ShowPreviewer = new DelegateCommand(() =>
{
_dialogService.ShowPreviewer(_ => { });
});
Logger.Info("Application Started, Initializing MainWindowViewModel.");
_dialogService = dialogService;
Services.Tracker.Configure<MainWindowViewModel>()
.Property(p => p.IsRatingVisible, false)
.Property(p => p.IsPosterMockupUsed, true)
.Property(p => p.IsPosterWindowShown, false)
.Property(p => p.AppLanguage, Languages.English)
.Property(p => p.Theme, FoliconThemes.System)
.Property(p => p.EnableErrorReporting, false)
.PersistOn(nameof(PropertyChanged));
Services.Tracker.Track(this);
var selectedLanguage = AppLanguage;
var cultureInfo = CultureUtils.GetCultureInfoByLanguage(selectedLanguage);
LangProvider.Culture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Kernel32.SetThreadUILanguage((ushort)Thread.CurrentThread.CurrentUICulture.LCID);
InitializeProperties();
Logger.Info("Application Started, Initializing MainWindowViewModel.");
ProcessUtils.CheckWebView2();
TrackProperties();
SetCultureInfo();
InitializeDelegates();
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;


var cmdArgs = ProcessUtils.GetCmdArgs();
if (!cmdArgs.TryGetValue("path", out var arg))
{
return;
}

SelectedFolder = arg;
var mode = cmdArgs["mode"];
if (mode != "Professional" &&
new List<string> { MediaTypes.Mtv, MediaTypes.Tv, MediaTypes.Movie, MediaTypes.Game }.Contains(mode))
{
IconMode = "Poster";
SearchMode = mode;
}
else
{
IconMode = "Professional";
}
Logger.Info("Command Line argument initialized, selected folder: {SelectedFolder}, mode: {IconMode}",
SelectedFolder, IconMode);
SearchAndMakeMethod();

ProcessCommandLineArgs();
}

private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
Logger.Debug("Network Availability Changed, Updating StatusBar.");
Expand Down Expand Up @@ -738,6 +697,7 @@ private void InitializeDelegates()
ProcessUtils.StartProcess(SelectedFolder + Path.DirectorySeparatorChar);
}
});
ShowPreviewer = new DelegateCommand(() => { _dialogService.ShowPreviewer(_ => { }); });
Logger.ForDebugEvent().Message("Delegates Initialized for MainWindow").Log();
}

Expand Down Expand Up @@ -1009,6 +969,55 @@ private async Task InitializeClientObjects()
Logger.Debug("Client Objects Initialized.");
}

private void TrackProperties()
{
Services.Tracker.Configure<MainWindowViewModel>()
.Property(p => p.IsRatingVisible, false)
.Property(p => p.IsPosterMockupUsed, true)
.Property(p => p.IsPosterWindowShown, false)
.Property(p => p.AppLanguage, Languages.English)
.Property(p => p.Theme, FoliconThemes.System)
.Property(p => p.EnableErrorReporting, false)
.PersistOn(nameof(PropertyChanged));
Services.Tracker.Track(this);
}

private void SetCultureInfo()
{
var selectedLanguage = AppLanguage;
var cultureInfo = CultureUtils.GetCultureInfoByLanguage(selectedLanguage);
LangProvider.Culture = cultureInfo;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Kernel32.SetThreadUILanguage((ushort)Thread.CurrentThread.CurrentUICulture.LCID);
}

private void ProcessCommandLineArgs()
{
var cmdArgs = ProcessUtils.GetCmdArgs();
if (!cmdArgs.TryGetValue("path", out var selectedFolder))
{
return;
}

SelectedFolder = selectedFolder;
var mode = cmdArgs["mode"];
if (mode != "Professional" &&
new List<string> { MediaTypes.Mtv, MediaTypes.Tv, MediaTypes.Movie, MediaTypes.Game }.Contains(mode))
{
IconMode = "Poster";
SearchMode = mode;
}
else
{
IconMode = "Professional";
}

Logger.Info("Command Line argument initialized, selected folder: {SelectedFolder}, mode: {IconMode}",
SelectedFolder, IconMode);
SearchAndMakeMethod();
}

private void AboutMethod()
{
_dialogService.ShowAboutBox(_ => { });
Expand Down

0 comments on commit bc33309

Please sign in to comment.