Archive

Monthly Archives: September 2016

In this video, I discuss how I wire-up an Android activity to a viewmodel written in F#.

Advertisements

Here’s a view-model class written in F#:

type ClaimsSummaryViewModel(memberId , dispatcher:Dispatcher , repository:IClaimsRepository) = 

    member val FamilySummary =      None   with get,set
    member val DependentSummaries = seq [] with get,set
    member val DependentSummary =   None   with get,set

    member this.Load() =
        this.FamilySummary      <- repository.GetFamilySummary      memberId
        this.DependentSummaries <- repository.GetDependentSummaries memberId

    member this.SetDependentSummary summary =
        this.DependentSummary <- Some summary

    member this.ViewClaims =

        DelegateCommand( (fun _ -> match this.DependentSummary with
                                   | Some v -> dispatcher.ViewMemberClaims v.Member.MemberId
                                   | None   -> ()), 
                         fun _ -> true ) :> ICommand

The following enables C# to access an Option type hosted by the FSharp.Core library:

using Microsoft.FSharp.Core;
using static Claims;

namespace Healthcare.Android
{
    static class Utility
    {
        public static bool IsSome<T>(this FSharpOption<T> option) => FSharpOption<T>.get_IsSome(option);
        public static bool IsNone<T>(this FSharpOption<T> option) => FSharpOption<T>.get_IsNone(option);
    }
}

Here’s an example of how to access an option type from a Xamarin.Android application:

            var familySummary = FindViewById<TextView>(Resource.Id.FamilySummaryValue);
            familySummary.Text = _viewModel.FamilySummary.IsSome()
                ? _viewModel.FamilySummary.Value.Claims.Count().ToString()
                : string.Empty;