Skip to content

Instantly share code, notes, and snippets.

@rdavisau
rdavisau / CocosExtensions.cs
Last active August 29, 2015 14:15
CocosSharp Extension Methods - Helpers for actions, layout, sizing, drawing
/*
Snippet Name: CocosSharp Extension Methods
Platform: All
Function: CocosSharp helpers for actions, layout, sizing, drawing
Includes:
- Awaitable CCNode.RunActionsWithTask - allows you to await the running of a set of actions (for example, in order to not proceed with subsequent code till an animation completes), without having to explicitly calculate the duration of the actions or use a callback.
- Easy relative positioning with CCNode.PlaceAt - allows you to position a CCNode relative to the boundaries of a parent CCNode. Fluent style, so can be used at initialisation time.
- Easy filled colours with CCDrawNode.FillWith and quick sprite initialisation with CCNode.WithSprite, also fluent.
- Quick insetting or outsetting with CCSize.MultiplyBy.
@rdavisau
rdavisau / InlineTableViewSource
Last active August 29, 2015 14:15
InlineTableViewSource
/*
Snippet Name: InlineTableViewSource
Platform: iOS
Function: A subclass of UITableViewSource that allows you to define UITableViewDataSource and UITableViewDelegate methods inline, rather than subclassing. Lets you take advantage of closures and use tableviews where you can't create subclasses, like in Xamarin Sketches (compile and reference).
Funcs/Actions are prefixed with "_", feel free to alter if this disagrees with your styling guidelines.
Usage:
var cellId = new NSString("cell");
@rdavisau
rdavisau / TaskTimeout.cs
Created April 17, 2015 00:30
Timeout extension method for Task<T>
// I don't remember where this is taken/adapted from.
public static class AsyncExtensions
{
public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout, CancellationTokenSource cancellationTokenSource = null)
{
if (task == await Task.WhenAny(task, Task.Delay(timeout)))
return await task;
else
{
if (cancellationTokenSource != null)
@rdavisau
rdavisau / BetterTcpSocketClient
Last active August 29, 2015 14:19
ITcpSocketClient implementation with disconnected detection
public class BetterTcpSocketClient : ITcpSocketClient
{
private TcpSocketClient _backingTcpSocketClient = new TcpSocketClient();
private readonly Stream _readBuffer = new BlockingMemoryStream();
private CancellationTokenSource _canceller;
public EventHandler<TcpSocketConnectedEventArgs> Connected { get; set; }
public EventHandler<TcpSocketDisconnectedEventArgs> Disconnected { get; set; }
public EventHandler<ErrorEventArgs> Errored { get; set; }
public Stream ReadStream
@rdavisau
rdavisau / BetterUdpSocket
Created April 20, 2015 10:15
Wrapper for sockets-for-pcl UdpSocketReceiver that provides a blocking `Receive` call
public class BetterUdpSocket : IDisposable
{
private BlockingCollection<byte[]> _readBuffer;
private readonly UdpSocketReceiver _backingUdpSocketReceiver = new UdpSocketReceiver();
public BetterUdpSocket()
{
_backingUdpSocketReceiver.MessageReceived += OnMessageReceived;
}
@rdavisau
rdavisau / SocketHelpersDemo.linq
Last active August 29, 2015 14:21
LINQPad script demonstrating how to use service discovery and the JsonProtocolMessenger
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference Prerelease="true">rda.SocketHelpers</NuGetReference>
<Namespace>SocketHelpers.Discovery</Namespace>
<Namespace>SocketHelpers.Messaging</Namespace>
<Namespace>Sockets.Plugin</Namespace>
<Namespace>Splat</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
@rdavisau
rdavisau / DU.fsx
Created August 10, 2015 07:10
Akka.Cluster + Discriminated Union test
open System
open System.IO
open System.Net
open Akka
open Akka.Actor
open Akka.FSharp
open Akka.Remote
open Akka.Actor
open Akka.Cluster
@rdavisau
rdavisau / AcceptSelfSignedCertificateWebView.cs
Created December 3, 2015 00:34
A UIWebView subclass for Xamarin iOS that accepts self-signed certificates. Note the caveats at https://ryandavis.io/allowing-uiwebview-to-accept-self-signed-certificates
public class AcceptSelfSignedCertificateWebView : UIWebView
{
private NSUrlRequest _failedRequest;
private bool _authenticated;
private bool OnShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
var result = _authenticated;
if (!_authenticated)
{
@rdavisau
rdavisau / GetXamarinApps.cs
Created November 5, 2015 04:31
Checks your iTunes folder for any .ipas that look like Xamarin apps
void Main()
{
var myXamarinApps =
Directory
.EnumerateFiles(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), @"Music\iTunes\iTunes Media\Mobile Applications"), "*.ipa")
.Where(f=> ZipFile.Open(f, ZipArchiveMode.Read)
.GetRawEntries()
.Any(e=> e.FullName.Contains(".monotouch-")));
foreach (var app in myXamarinApps)
@rdavisau
rdavisau / 💣.cs
Created October 24, 2016 20:13
HockeyApp UnobservedTaskException
using System;
using System.Threading.Tasks;
using Foundation;
using HockeyApp.iOS;
using UIKit;
namespace HockeyCrash
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate