Skip to content

Instantly share code, notes, and snippets.

View mr5z's full-sized avatar
🎯
Focusing

mark mr5z

🎯
Focusing
View GitHub Profile
@mr5z
mr5z / ObservableCollectionAndItems.cs
Created December 26, 2021 14:16
Observes both collection changed and items changed.
internal class ObservableCollectionAndItems<TModel> where TModel : INotifyPropertyChanged
{
private readonly ObservableCollection<TModel> collection = new();
public event EventHandler<EventArgs>? ItemChanged;
public event EventHandler<NotifyCollectionChangedEventArgs>? CollectionChanged;
public ObservableCollectionAndItems()
{
collection.CollectionChanged += Collection_CollectionChanged;
@mr5z
mr5z / user-agent-parser.regex
Last active September 6, 2021 16:23
Group User-Agent by platforms
[a-zA-Z]+\/[0-9.]+\s?([(][a-zA-Z ;0-9.,]+[)])?
@mr5z
mr5z / date_format.cpp
Last active July 1, 2021 09:15
Various ways to print date time
static std::string date_format(boost::posix_time::ptime const& datetime) {
using namespace boost;
std::ostringstream ss;
auto output_facet = new boost::posix_time::time_facet();
ss.imbue(std::locale(std::locale::classic(), output_facet));
output_facet->format("%Y-%m-%d %H:%M:%s%Q");
ss.str("");
@mr5z
mr5z / RaisedTabbedPageRenderer.cs
Last active May 13, 2021 07:11
Raised Middle Button TabbedPage implementation for Xamarin.Android. Just replace the `Replace_With_Your_Custom_BottomNavigationView` with your own implementation of BNV!
using System;
using System.Reflection;
using Android.Content;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using AWidget = Android.Widget;
using ATabbedRenderer = Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer;
using System.Collections.Specialized;
@mr5z
mr5z / MonitorController.cs
Created May 10, 2021 08:30
GET api/monitor
[Route("api/[controller]")]
[ApiController]
public class MonitorController : Controller
{
private readonly IActionDescriptorCollectionProvider _provider;
public MonitorController(IActionDescriptorCollectionProvider provider)
{
_provider = provider;
}
@mr5z
mr5z / Observable.cs
Last active May 9, 2021 13:25
Property event to observable.
class Observable
{
private object actionCollection = null!;
private MethodInfo? methodInfo;
public ActionCollection<TModel> From<TModel>(TModel target) where TModel : INotifyPropertyChanged
{
RegisterPropertyChanged(target);
return BuildActionCollection<TModel>();
}
class LoggingInterceptor : HttpClientHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.Log("Request {0}", request.RequestUri.PathAndQuery);
if (request.Content != null)
{
var contentRequest = await request.Content.ReadAsStringAsync();
}
@mr5z
mr5z / Configuration.cs
Last active April 26, 2021 14:05
Scheduled closing of config file
public class Configuration : IConfiguration
{
private readonly string assemblyResourceFile;
private AutoDisposeJsonDocument? smartJsonDocument;
public Configuration(string assemblyResourceFile)
{
this.assemblyResourceFile = assemblyResourceFile;
}
@mr5z
mr5z / AutoDisposeJsonDocument.cs
Created April 26, 2021 11:18
Scheduled disposable objects to be registered in an IoC container where IDisposable is disallowed.
private void ScheduleDisposeResourceStreams()
{
_ = Task.Factory.StartNew(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(1));
// `stream` creation happens right after the invocation of this
// They are in the same thread so no lock needed
jsonStream?.Dispose();
jsonDocument?.Dispose();
jsonStream = null;
@mr5z
mr5z / PleaseHelp.cs
Created April 24, 2021 11:50
JSON Path to Object
public async Task<T?> GetValue<T>(string path, JsonSerializerOptions? options, CancellationToken cancellationToken)
{
using var stream = GetStream();
using var jsonDocument = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
var root = jsonDocument.RootElement;
var jsonPath = JsonPath.Parse(path);
var pathResult = jsonPath.Evaluate(root);
if (pathResult.Error != null)
throw new InvalidOperationException(pathResult.Error);