Skip to content

eriforce/PureLib

Repository files navigation

PureLib

Build status License MIT

A C# utility library.

Download

The library is available on nuget.org.

To install PureLib, run the following command in the Package Manager Console

PM> Install-Package PureLib
PM> Install-Package PureLib.WPF
PM> Install-Package PureLib.MediaInfo
PM> Install-Package PureLib.Generators.DicToInstGenerator

More information about NuGet package avaliable at:

Package Version
PureLib NuGet Version
PureLib.WPF NuGet Version
PureLib.MediaInfo NuGet Version
PureLib.Generators.DicToInstGenerator NuGet Version

Features

Utility

Convert a wildcard to a regular expression:

string regex = "*.txt".WildcardToRegex();

Convert a string of enum list to an enum array:

DayOfWeek[] days = "Sunday,Saturday".ToEnum<DayOfWeek>();

Conversions between binary data and base64url string:

byte[] data = Encoding.UTF8.GetBytes("test");
string result = Base64Url.Encode(data);
byte[] bin = Base64Url.Decode(result);

Dictionary to Instance Generator

[FromDictionary]
public class Payload {
    public int Id { get; set; }
    public string Name { get; init; }
    [Ignore]
    public string Value { get; set; }
}

will generate

public static class DictionaryToPayloadExtensions {
    public static Payload ToPayload(this Dictionary<string, object> dic) {
        ref var refOfId = ref CollectionsMarshal.GetValueRefOrNullRef(dic, "Id");
        ref var refOfName = ref CollectionsMarshal.GetValueRefOrNullRef(dic, "Name");

        return new Payload {
            Id = Unsafe.IsNullRef(ref refOfId) ? default : (Int32)refOfId,
            Name = Unsafe.IsNullRef(ref refOfName) ? default : (String)refOfName,
        };
    }
}

Notify Object

NotifyObject implements INotifyPropertyChanged, which enables you to raise changes of properties.

public string StatusBarText {
    get { return _statusBarText; }
    set {
        _statusBarText = value;
        RaiseChange(() => StatusBarText); // or RaiseChange("StatusBarText");
    }
}

View Model Base

ViewModelBase inherits NotifyObject, which is designed to be the base class of ViewModels in MVVM pattern.

    public class MainWindowViewModel : ViewModelBase {
        public ObservableCollection<string> Files { get; set; }
        
        private void OnTaskStarted(object sender, TaskStartedEventArgs e) {
            RunOnUIThread(() => {
                if (!Files.Contains(e.File))
                    Files.Add(e.File);
            });
        }
    }

Relay Command

RelayCommand implements ICommand, which could be bound to UI controls.

private ICommand _openDescriptionCommand;
public ICommand OpenDescriptionCommand {
    get {
        if (_openDescriptionCommand == null)
            _openDescriptionCommand = new RelayCommand(p => {
                OpeningDescription(this, new EventArgs<string>(((WatFile)p).Description));
            }, p => !((WatFile)p).Description.IsNullOrEmpty());
        return _openDescriptionCommand;
    }
}

Singleton App

SingletonApp inherits Application. The application inherits SingletonApp will not be able to run multiple instances.

public partial class App : SingletonApp { 
}

Converters

PureLib provides commonly used converters for UI bindings.

  • BooleanToVisibilityConverter
  • InverseBooleanConverter

Web Downloader

WebDownloader contains essential functions of a download manager. It can dispatch any number of threads to download concurrently.

WebDownloader downloader = new WebDownloader(Global.Config.ThreadCount, null, false);
downloader.DownloadCompleting  = OnDownloadCompleting;
downloader.AddItems(_itemPostMaps.Keys.ToList());

License

MIT

About

A C# utility library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages