Skip to content

Commit

Permalink
Refactor logging and UI functionality in PreviewerViewModel
Browse files Browse the repository at this point in the history
Improved modularization and readability by separating out UI functions into a separate UiUtils class. Added extensive logging capabilities using the NLog library in PreviewerViewModel, particularly in the handling of image selection and dialog interactions.
  • Loading branch information
DineshSolanki committed Dec 12, 2023
1 parent 5346615 commit 334ac44
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
7 changes: 5 additions & 2 deletions FoliCon/Modules/utils/DialogUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public static VistaFolderBrowserDialog NewFolderBrowserDialog(string description
};
return folderBrowser;
}

public static VistaOpenFileDialog NewOpenFileDialog(string description, string filter= "All files (*.*)|*.*")
public static VistaOpenFileDialog NewOpenFileDialog(string description)
{
return NewOpenFileDialog(description, "All files (*.*)|*.*");
}
public static VistaOpenFileDialog NewOpenFileDialog(string description, string filter)
{
Logger.Debug("Creating New Open File Dialog");
var openFileDialog = new VistaOpenFileDialog
Expand Down
5 changes: 5 additions & 0 deletions FoliCon/Modules/utils/UiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public static void SetColumnWidth(ListView listView)
column.Width = double.NaN;
}
}

public static Visibility BooleanToVisibility(bool value)
{
return value ? Visibility.Visible : Visibility.Hidden;
}
}
64 changes: 39 additions & 25 deletions FoliCon/ViewModels/PreviewerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using FoliCon.Modules.utils;
using FoliCon.Modules.utils;
using NLog;

namespace FoliCon.ViewModels
{
public class PreviewerViewModel : BindableBase, IDialogAware
{
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();

public PreviewerViewModel()
{
Logger.Debug("PosterIconConfigViewModel created");
PosterIconInstance = new FoliCon.Models.Data.PosterIcon
{
Rating = Rating
Expand Down Expand Up @@ -59,7 +56,7 @@ public bool RatingVisibility
set
{
SetProperty(ref _ratingVisibility, value);
PosterIconInstance.RatingVisibility = BooleanToVisibility(value).ToString();
PosterIconInstance.RatingVisibility = UiUtils.BooleanToVisibility(value).ToString();
}
}

Expand All @@ -69,46 +66,63 @@ public bool OverlayVisibility
set
{
SetProperty(ref _overlayVisibility, value);
PosterIconInstance.MockupVisibility = BooleanToVisibility(value).ToString();
PosterIconInstance.MockupVisibility = UiUtils.BooleanToVisibility(value).ToString();
}
}

public event Action<IDialogResult> RequestClose;
public DelegateCommand SelectImageCommand { get; set; }


private void SelectImage()
{
var fileDialog = DialogUtils.NewOpenFileDialog("Select Image", "Image files (*.png, *.jpg, *.gif, *.bmp, *.ico)|*.png;*.jpg;*.gif;*.bmp;*.ico");
Logger.Debug("Opening image selection dialog");
var fileDialog = DialogUtils.NewOpenFileDialog(LangProvider.GetLang("ChooseAnImage"), "Image files (*.png, *.jpg, *.gif, *.bmp, *.ico)|*.png;*.jpg;*.gif;*.bmp;*.ico");
var selected = fileDialog.ShowDialog();

if (selected != null && (bool)!selected) return;
if (selected != null && (bool)!selected)
{
Logger.Warn("No image selected");
return;
}

var thisMemoryStream = new MemoryStream(File.ReadAllBytes(fileDialog.FileName));
var rt= new FoliCon.Models.Data.PosterIcon
{
FolderJpg = (ImageSource)new ImageSourceConverter().ConvertFrom(thisMemoryStream)
};
PosterIconInstance = rt;
Logger.Info("Image selected: {FileName}", fileDialog.FileName);
}

public bool CanCloseDialog()


#region DialogMethods
public event Action<IDialogResult> RequestClose;
protected virtual void CloseDialog(string parameter)
{
return true;
}
Logger.Info("CloseDialog called with parameter {Parameter}", parameter);
var result = parameter?.ToLower(CultureInfo.InvariantCulture) switch
{
"true" => ButtonResult.OK,
"false" => ButtonResult.Cancel,
_ => ButtonResult.None
};

public void OnDialogClosed()
{

RaiseRequestClose(new DialogResult(result));
}

public void OnDialogOpened(IDialogParameters parameters)
public virtual void RaiseRequestClose(IDialogResult dialogResult) =>
RequestClose?.Invoke(dialogResult);

public virtual bool CanCloseDialog() => true;

public virtual void OnDialogClosed()
{

}
public Visibility BooleanToVisibility(bool value)

public virtual void OnDialogOpened(IDialogParameters parameters)
{
return value ? Visibility.Visible : Visibility.Hidden;
}

#endregion DialogMethods
}
}

0 comments on commit 334ac44

Please sign in to comment.