Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
V1.5.4
Browse files Browse the repository at this point in the history
fix #25 - File encoding (#27)
  • Loading branch information
jibedoubleve authored Oct 30, 2019
1 parent 51cc955 commit f6a43aa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
84 changes: 45 additions & 39 deletions Probel.JsonReader.Presentation/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public class ShellViewModel : BindableBase
{
#region Fields

public readonly string DefaultDirectory;
private const string DEFAULT_DIRECTORY = @"%appdata%\Probel\Perdeval\Logs\";
private const string TITLE_PREFIX = "Log reader";
private readonly IFormatProvider _formatProvider = new CultureInfo("en-US");
private readonly ILogService _logger;
private readonly ILogRepository LogRepository;
private ObservableCollection<string> _categories = new ObservableCollection<string>();
private Encoding _fileEncoding = Encoding.UTF8;
private decimal _filterMinutes = 0;
private ObservableCollection<LogModel> _logs = new ObservableCollection<LogModel>();
private SettingsViewModel _settings;
Expand All @@ -37,8 +37,8 @@ public class ShellViewModel : BindableBase
private string _title = TITLE_PREFIX;
private string _version;
private string FilePath;

private FileSystemWatcher FileWatcher;
public readonly string DefaultDirectory;

#endregion Fields

Expand All @@ -60,12 +60,20 @@ public ShellViewModel(ILogRepository logRepository, ICommandBuilder commandBuild

#region Properties

private IEnumerable<LogModel> BufferLogs { get; set; }

public ObservableCollection<string> Categories
{
get => _categories;
set => SetProperty(ref _categories, value, nameof(Categories));
}

public Encoding FileEncoding
{
get => _fileEncoding;
set => SetProperty(ref _fileEncoding, value, nameof(FileEncoding));
}

public ICommand FilterCommand { get; }

public decimal FilterMinutes
Expand Down Expand Up @@ -113,43 +121,10 @@ public string Version
set => SetProperty(ref _version, value, nameof(Version));
}

private IEnumerable<LogModel> BufferLogs { get; set; }

#endregion Properties

#region Methods

public string GetLastFile()
{
return (Settings.FileHistory.Count > 0)
? Settings.FileHistory.OrderBy(e => e).Last()
: string.Empty;
}

public async Task Load(Encoding encoding)
{
var lastFile = GetLastFile();
if (File.Exists(lastFile))
{
_logger.Debug($"Load last opened file. Path: '{lastFile}'");
await OpenAsync(lastFile, encoding);
Title = lastFile;
}
else { _logger.Warn($"Cannot load last opened file. Path: '{(lastFile ?? "<empty>")}'"); }
}

public void RefillCategories(IEnumerable<string> categories)
{
Categories.Clear();
Categories.AddRange(categories);
}

internal void FilterCategories(IEnumerable<string> categories) => FillLogs(BufferLogs.Filter(categories, Settings));

internal IEnumerable<string> GetCategories() => BufferLogs.GetCategories();

internal async Task OpenFileAsync(string path, Encoding encoding) => await Task.Run(() => OpenAsync(path, encoding));

private void AddFileInHistory(string filePath)
{
var doubloon = (from f in Settings.FileHistory
Expand All @@ -166,7 +141,6 @@ private bool CanFilter(string arg)
return isNumber && hasLogs;
}


private void FillLogs(IEnumerable<LogModel> models)
{
Logs = new ObservableCollection<LogModel>(models);
Expand Down Expand Up @@ -208,17 +182,17 @@ private void ListenToFileChange()
private async void OnFileChanged(object sender, FileSystemEventArgs e)
{
FilterCommand.RaiseCanExecuteChanged();
BufferLogs = await Task.Run(() => LogRepository.GetAllLogs(FilePath));
BufferLogs = await Task.Run(() => LogRepository.GetAllLogs(FilePath, FileEncoding));
FillLogs(BufferLogs.Filter(FilterMinutes, Settings).Filter(Categories, Settings));
Status = Messages.Status_FileChanged + $" - [{DateTime.Now.ToLongTimeString()}]";
}

private async Task OpenAsync(string filePath, Encoding encoding)
private async Task OpenAsync(string filePath)
{
AddFileInHistory(filePath);
FilterMinutes = 0;
FilePath = filePath;
BufferLogs = await Task.Run(() => LogRepository.GetAllLogs(filePath, encoding));
BufferLogs = await Task.Run(() => LogRepository.GetAllLogs(filePath, FileEncoding));
RefillCategories(GetCategories());
ListenToFileChange();

Expand All @@ -229,6 +203,38 @@ private async Task OpenAsync(string filePath, Encoding encoding)

private void SetItemsCount() => StatusItemsCount = string.Format(Messages.Status_xxItems, Logs.Count);

internal void FilterCategories(IEnumerable<string> categories) => FillLogs(BufferLogs.Filter(categories, Settings));

internal IEnumerable<string> GetCategories() => BufferLogs.GetCategories();

internal async Task OpenFileAsync(string path) => await Task.Run(() => OpenAsync(path));

public string GetLastFile()
{
return (Settings.FileHistory.Count > 0)
? Settings.FileHistory.OrderBy(e => e).Last()
: string.Empty;
}

public async Task Load(Encoding encoding)
{
var lastFile = GetLastFile();
if (File.Exists(lastFile))
{
_logger.Debug($"Load last opened file. Path: '{lastFile}'");

await OpenAsync(lastFile);
Title = lastFile;
}
else { _logger.Warn($"Cannot load last opened file. Path: '{(lastFile ?? "<empty>")}'"); }
}

public void RefillCategories(IEnumerable<string> categories)
{
Categories.Clear();
Categories.AddRange(categories);
}

#endregion Methods
}
}
6 changes: 5 additions & 1 deletion Probel.JsonReader.Presentation/Views/ShellView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private void OnEncodingClick(object sender, RoutedEventArgs e)
if (i == sender) { i.IsChecked = !value; }
else { i.IsChecked = value; }
}

ViewModel.FileEncoding = GetEncoding();
}
}

Expand Down Expand Up @@ -183,7 +185,9 @@ private async void OpenFile(string path)
{
LastestFile = path;
ViewModel.Title = path;
await ViewModel.OpenFileAsync(path, GetEncoding());

ViewModel.FileEncoding = GetEncoding();
await ViewModel.OpenFileAsync(path);
Refresh();
}
else
Expand Down
4 changes: 2 additions & 2 deletions Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
// [assembly: AssemblyVersion("1.0.*")]
using System.Reflection;

[assembly: AssemblyVersion("1.5.3")]
[assembly: AssemblyFileVersion("1.5.3")]
[assembly: AssemblyVersion("1.5.4")]
[assembly: AssemblyFileVersion("1.5.4")]

0 comments on commit f6a43aa

Please sign in to comment.