Facebook Photo Sync

The social media giant’s latest offering of background photo uploads had me slightly worried.

I’m working on an app to do effectively just that. However, on further reading, I’m not so worried.

Firstly, the official app uploads every single photo from your phone automatically. Don’t worry about picking out the keepers or my mobile data limit – just upload everything.

This is not the workflow that I’m targeting. It shouldn’t really be anyone’s work flow!

My app assumes the following workflow.

First, you to choose the photos you want to upload. This is usually the post processed version – even the best shots usually benefit from a little crop, some tone curves applied, a touch of sharpening.

Next, I would like to arrange the photos for my album, add descriptions and tag people – all offline.

Finally, I’d like it to upload in the background at some point. Perhaps I’ll only want to transfer over wifi.

Thankfully, Facebook must cater to the lowest common denominator. Whilst its brute force method may suit some, I hope to find a more select audience. At least before they improve the app too much!

Advertisements

Observable Cache

Source Code: https://gist.github.com/4115023

Given: An async operation that returns a value e.g web/service call or complex calculation
When: I request the value multiple times
Then: I only perform the operation once and cache the value

When: I haven’t requested the value
Then: Don’t perform the operation

When: I clear the cached value
Then: requesting the value again performs the async operation again

My current solution:


    public class Cache<T> : IDisposable
    {
        private readonly IConnectableObservable<T> _source;
        private IDisposable _connection;

        public Cache(IObservable<T> source)
        {
            _source = Observable.Create<T>(o => source.Subscribe(o))
                                .Replay(1, Scheduler.Immediate);
        }

        public IObservable<T> GetValue()
        {
            return Observable.Create<T>(o =>
                                         {
                                             var subscription = _source.Subscribe(o);
                                             _connection = _source.Connect();
                                             return subscription;
                                         });
        }

        public void Clear()
        {
            if (_connection != null)
            {
                _connection.Dispose();
                _connection = null;
            }
        }

        public void Dispose()
        {
            Clear();
        }
    }

DateTime.MinValue – be careful!

We’ve just had a support call at work where the user was running our application in Singapore.

Our investigation uncovered an interesting “feature” of using DateTime.MinValue.

Have a look at the following test:

namespace Stuff
{
    using System;
    using NUnit.Framework;

    [TestFixture]
    public class MinDateTimeOffset
    {
        [Test]
        public void Testing_MinValue_in_DateTime()
        {
            Action<string> p = Console.WriteLine;

            // This is Now in the current timezone
            var nowOffset = new DateTimeOffset(DateTime.Now);
            p("Current time:");
            p("- current timezone: " + nowOffset);
            p("- UTC equivilent time: " + nowOffset.ToUniversalTime());
            p("");

            // This is the minimum time allowed in the current timezone
            var minOffset = DateTimeOffset.MinValue;
            p("DateTimeOffset.MinValue:");
            p("- current timezone: " + minOffset);
            p("- UTC equivilent time: " + minOffset.ToUniversalTime());
            p("");

            // This is UTC minimum time
            var minValue = DateTime.MinValue;
            p("DateTime.MinValue:");
            p("- current timezone: " + minValue);
            p("- UTC equivilent time: " + minValue.ToUniversalTime());
            p("");

            // Converted to the current timezone
            try
            {
                p("Trying to cast DateTime.Min to DateTimeOffset:");

                var minDateTimeAsDateTimeOffset = (DateTimeOffset)minValue;
                p("- current timezone: " + minDateTimeAsDateTimeOffset);
                p("- UTC equivilent time: " + minDateTimeAsDateTimeOffset.ToUniversalTime());
            }
            catch (Exception ex)
            {
                p("Error: " + ex.Message);
            }
        }
    }
}

We are based in London, and when we run it, we see this output:


Current time:
- current timezone: 31/10/2012 11:46:35 +00:00
- UTC equivilent time: 31/10/2012 11:46:35 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 00:00:00

Trying to cast DateTime.Min to DateTimeOffset:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

However, when run in Singapore, you get this:

Current time:
- current timezone: 31/10/2012 19:47:38 +08:00
- UTC equivilent time: 31/10/2012 11:47:38 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 00:00:00

Trying to cast DateTime.Min to DateTimeOffset:
Error: The UTC time represented when the offset is applied must be between year 0 and 10,000.
Parameter name: offset

In New York, you get this :

Current time:
- current timezone: 31/10/2012 07:48:50 -04:00
- UTC equivilent time: 31/10/2012 11:48:50 +00:00

DateTimeOffset.MinValue:
- current timezone: 01/01/0001 00:00:00 +00:00
- UTC equivilent time: 01/01/0001 00:00:00 +00:00

DateTime.MinValue:
- current timezone: 01/01/0001 00:00:00
- UTC equivilent time: 01/01/0001 05:00:00

Trying to cast DateTime.Min to DateTimeOffset:
- current timezone: 01/01/0001 00:00:00 -05:00
- UTC equivilent time: 01/01/0001 05:00:00 +00:00

The value of DateTime.MinValue can not be cast to a DateTimeOffset if you are east of London!

MonoDroid Unit Tests

A well known approach to unit testing class libraries that target Silverlight, Mono for Android, MonoTouch etc is to make a “real” .net  class library project which contains all of your non-ui code, and link those files through to a project that targets the runtime. This way, we can use our full unit test tools such as nUnit, nCrunch and Resharper.

The main drawback of it though is that if your runtime environment has some peculiar difference, then tests that pass in full .net go on to fail in the target run time.

I encountered a problem in using TinyIOC. I needed to resolve the same instance of Mono.Facebook in several places. Despite registering it as a Singleton, every time my different components resolved it, they got a new instance.

This was a tricky problem to diagnose without unit tests. Continually running the app, setting breakpoints in the constructors and then looking at facebook.GetHashCode().

Some unit tests written in full .net all passed using  dummy objects registered in the container. Mono.Facebook could not be referenced in the full .net unit tests though because it is a Mono for Android library.

What I needed was a way to run unit tests within Android. Enter monodroid-unittest!

This allowed me to create a unit test project native to Mono for Android, and to run the unit tests in my target run time.

I soon had unit test for my real library up and running, and the problem resolved!

TinyIOC

As my Mono for Android app continues, I’ve been introducing more and more libraries to try to sort out its structure.

One of the largest changes to my development many moons ago was the use of a dependency injection container. There is only so far that I can go in any project before needing one – even if it’s just a test project.

Greg Shackles wrote about using either TinyIoC or Func with Mono for Android back in Feb 2011.

So I’ve added TinyIoC  to my app and have been registering the classes, adding interfaces to separate component dependencies etc.

Now in WPF / Silverlight, we’ve been spoilt by the powerful AutoFac. It seems a little heavyweight for a phone app though, but that doesn’t stop me wanting some of its power.

I chose to try TinyIoC because:

  • it’s just a single cs file that you include in your project – simples!
  • it had support for automatic factories – so you register T and can then inject Func<T>

What I’ve found that is that many of my components have a mixture of dependencies and parameters in the constructor. AutoFac just copes with this automagically, but I’ve been having to register custom factories for everything in TinyIoC. It’s ok for now but will get tedious quickly. It also uses a slightly cumbersome string object dictionary for custom parameters.

If you’re doing IoC correctly, then none of the components should be affected by which container you’re using – so I might try a different container shortly.

C# to Javascript?

In a recent conversation with friends at work, we were talking about the recent emergence of TypeScript.

We’re all primarily C# .Net developers and we were saying that instead of having to learn a new language, wouldn’t it be neat if there was a way to compile C# to Javascript or run .Net IL on a Javascript implementation of the .Net VM?

Well, would you believe it? Miguel de Icaza investigated this only last month:

Miguel de Icaza: 2012 Update: Running C# on the Browser

Acer Aspire S7

With Windows 8 on the verge of release this month, there are several new pieces of hardware waiting to be unleashed on the poor defenceless consumers.

Damn, these Acer Aspire S7 ultrabooks look so very nice and shiny.

Acer Aspire S7s - photo from arstechnica

The gorgeous 13″ and 11″ Acer Aspire S7 Windows 8 Ultrabooks

CNet brief look: http://asia.cnet.com/countdown-to-windows-8-acers-lineup-62218999.htm

I’m finding it difficult to think clearly about whether I really need one. It’s clear to me that I want one!