Skip to content

Instantly share code, notes, and snippets.

View patridge's full-sized avatar

Adam Patridge patridge

View GitHub Profile
@patridge
patridge / SuperAwesomeButton.cs
Last active January 22, 2016 21:10
Attempt at making a non-generic refactor-friendly BindableProperty. (Some back-story on the deprecation and my motivation: https://twitter.com/PatridgeDev/status/690620310870106112.)
public class SomeButton : Button {
public string ExampleThing {
get { return (string)GetValue(ExampleThingBindableProperty); }
set { SetValue(ExampleThingBindableProperty, value); }
}
public static readonly BindableProperty ExampleThingBindableProperty =
BindableProperty.Create(nameof(ExampleThing), typeof(string), typeof(Button), default(string));
}
@patridge
patridge / MongoDateTimeExtensions.cs
Created December 9, 2015 16:50
Creating Mongo ObjectIds manually (e.g., for testing) from a given DateTime object.
public static class DateTimeExtensions
{
static readonly DateTime Epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static string AsObjectIdPrefix (this DateTime dateTime)
{
var deltaSinceEpoch = dateTime - Epoch;
var seconds = (int)deltaSinceEpoch.TotalSeconds;
var secondsAsHex = seconds.ToString ("X").PadLeft (8, '0').ToLowerInvariant ();
return secondsAsHex;
}
@patridge
patridge / ColorHelpers.cs
Created June 16, 2015 19:03
Xamarin Color Helper
// If you don't want to pull the whole [Splat](https://github.com/paulcbetts/splat) library over just for colors,
// here are a couple functions to help.
// Store your color values as hex-based `long`s and just convert them to native colors when you use them.
// NOTE: iOS uses RGBA floats (0..1) and Android uses ARGB ints (0..255) when converting.
// Usage example:
SomeView.SetBackgroundColor(MyAppColors.SomeGrayHighlight.AsNative());
// Core library can hold your colors:
public static class MyAppColors {
@patridge
patridge / UITableViewWithoutLayoutMargins.cs
Last active August 29, 2015 14:15
UITableView and UITableViewCell without the iOS 7/8 margins.
// LICENSE: MIT
public class UITableViewWithoutLayoutMargins : UITableView {
void InitializeWithoutMargins() {
// iOS 7
if (RespondsToSelector(new Selector("setSeparatorInset:"))) {
SeparatorInset = UIEdgeInsets.Zero;
}
// iOS 8[+?]
if (RespondsToSelector(new Selector("setLayoutMargins:"))) {
LayoutMargins = UIEdgeInsets.Zero;
@patridge
patridge / gist:2ca7e309cd53716fb384
Created February 17, 2015 15:38
Android: what ID is that thing?
// I had to use this to debug why `OnOptionsItemSelected` wasn't triggering for an ActionBar home tap.
// Turns out, `Resource.Id.home` and `Resource.Id.homeAsUp` are not related to `Android.Resource.Id.Home`.
var projectIdType = typeof(Resource.Id);
foreach (var field in projectIdType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)) {
if (item.ItemId == (int)field.GetValue(null)) {
Console.WriteLine("{0}: {1}", field.Name, item.ItemId);
}
}
var androidIdType = typeof(Android.Resource.Id);
@patridge
patridge / AppDelegate.cs
Created February 5, 2015 17:05
Sample: BTProgressHUD blocking tab bar
using System;
using Foundation;
using UIKit;
using CoreGraphics;
using BigTed;
using System.Threading.Tasks;
using System.Linq;
using ObjCRuntime;
namespace HudOverTabsSample {
public static class TaskHelper {
public static Task CompletedTask = AsCompletedTask(true);
public static Task<T> AsCompletedTask<T>(T result) {
TaskCompletionSource<T> precompletedSource = new TaskCompletionSource<T>();
precompletedSource.SetResult(result);
return precompletedSource.Task;
}
public static void TrySetResultTask<TResult>(this TaskCompletionSource<TResult> tcs, Task<TResult> task) {
if (task == null) {
throw new ArgumentNullException("task");
@patridge
patridge / MainActivity.cs
Created September 19, 2014 15:47
Core reproduction code for `NoSuchMethodError` in Google Analytics code using Google Play Services Xamarin Component (v19.0.0.1)
using System;
using Android.App;
using Android.Content;
using Android.Gms.Analytics; // via Google Play Services Xamarin Component (v19.0.0.1)
using Android.Gms.Analytics.Ecommerce;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
@patridge
patridge / gist:10499338
Last active August 23, 2018 15:52
Xamarin: get a random color
// Xamarin.iOS (using MonoTouch.UIKit;)
static Random rand = new Random();
public static UIColor GetRandomColor() {
int hue = rand.Next(255);
UIColor color = UIColor.FromHSB(
(hue / 255.0f),
1.0f,
1.0f);
return color;
}
@patridge
patridge / gist:8984934
Created February 13, 2014 22:08
One approach for handling keyboard show/hide in Xamarin.iOS.
NSObject keyboardShowObserver;
NSObject keyboardHideObserver;
public override void ViewWillAppear(bool animated) {
base.ViewWillAppear(animated);
keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, (notification) => {
NSValue nsKeyboardBounds = (NSValue)notification.UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey);
RectangleF keyboardBounds = nsKeyboardBounds.RectangleFValue;
float height = View.Bounds.Height - keyboardBounds.Height;
if (NavigationController != null && NavigationController.TabBarController != null && NavigationController.TabBarController.TabBar != null) {