Xamarin.Forms Plugin – Unobtrusive FluentValidation

Xamarin.Forms Plugin – Unobtrusive FluentValidation

Background

One of my all time favorite libraries is FluentValidation, Jeremy Skinner has done a great job at producing a library that is easy to use and just works.  Whether it is trivial validation or complicated databases access validation, FluentValidation has the means to get it done; while keeping your validation away from your view.  I won’t go into depth on why one would need/want to separate validation from a view in this post, but I might make another post in future describing in detail why that is important.

This post will assume a basic understanding of MVVM and ASP.NET’s MVC Framework with a little common knowledge of javascript.

The Problem

During my expedition as a Xamarin developer, I’ve seen and tried many different forms of validation.  Some were great and some were just good, but they all seemed have different approaches on when or where the validation gets triggered.

From my experience the best user-interface one can offer when validation is a small and effective means of communication.  Not multiple popups with fancy emoticons of sad faces.  People already don’t like to click on their phones (just look at the most popular apps, they just scroll), so why force them to click an ‘X’ or an ‘okay’ button?  Just tell the user what they should do to correct the form and move on.

The Solution

So one night I decided to blend what FluentValidation.MVCX has to offer with Xamarin.Forms.

Enter the first Unobtrusive FluentValidation Plugin for Xamarin.Forms!

Now this plugin is built on top of the latest FluentValidation NuGet published version.  There isn’t anything that is specific with this version, just needed a starting place. If you need an older version, please let me know and I’ll do my best to support older versions.

Check out the code on GitHub and get the latest version from NuGet!

 

Take care and have a great day!

A Few Years With Xamarin: The Good, The Bad, The Great

People jump to conclusions. A lot.  It’s human nature and we somehow forget that and we always focus on the negative rather than the positive of most things.   This is apparent when you search for “mobile cross-platform frameworks”.  Yes you’ll find frameworks like Xamarin, Appcelerator, Apache Cordova, Sencha, etc.; And the list keeps growing everyday.

One of the most intriguing platforms, for C# developers, is Xamarin.  A platform that allows you to write C# code and it compiles down into Natvie code for Android, iOS, and Windows Phones.  This is possible by the original project called “mono-project“, this is where it all started some years ago as an open source project that quickly grew over night. Now Xamarin has employees and clients around the world building great mobile apps and solving unique problems in less time than ever before.  Lets be honest C# (.NET) isn’t on as many devices as Java, yet. Xamarin is helping make that transition more quickly with giving Microsoft’s .NET Framework the ability to go across platform.  I’m not sure if Microsoft ever thought that they’d be running, compiling, and deploying products on the Linux Platform, but they are and I believe the push from Xamarin has something to do with that.  Enough rambling, lets get into the technical aspect of Xamarin!

Continue reading “A Few Years With Xamarin: The Good, The Bad, The Great”

Hooked On Twitch Live!! [Alpha]

So a while back in 2015 I was just getting into Twitch.tv and watching everything League of Legends related and eSports in general. But I found myself in the car more than anything and I was destroying my data plan by having the video play for 2-3 hours everyday in the car. So I began to look for an alternative app or anything to save my data plan. That’s when I found out that Twitch.tv has a public API that they offer for FREE! I was very intrigued by this and began digging into their decently documented API. At the time they were just releasing V3 and were quietly building support HTML5 streaming (m3u8 format) and in that format they offered and “audio_only” option; but that wasn’t an option inside their mobile app under quality. Seemed odd, but was a window for opportunity!

Digging (debugging) around their API I quickly learned their authentication process and began to develop an app using native Java. After about two weeks of on-and-off work I decided to port over to Xamarin.Forms in case I ever decide to go to the App Store with minimum effort. So I began writing an app, but unfortunately Twitch beat me to the punch and pushed out the option to use “audio_only” in their app. Feeling defeated and being extremely busy with work I ended up not furthering development for a month or so, but then some life changes happened and I had the entire month of December (2015) off and I decided I was going to finish the app!

Continue reading “Hooked On Twitch Live!! [Alpha]”

Xamarin.Forms Carousel

Xamarin.Forms is a very powerful product Xamarin has produced, some what control lacking because they want us to develop the controls.  This would anger some and throw people off the product completely, but not us that like a challenge.

In my quest to develop a streaming app (un-named at the moment) I decided i that it would be a great idea to have a carousel like control for some popular streams.  Digging around the pre built controls and sample codes Xamarin offers I couldn’t find exactly what I wanted; however, I did find a “Plugin” on GitHub that someone else created.  It seemed to be exactly what I wanted, I was stoked!  So I forked the repo and began implementation into my current solution. After a few hours I realized I wanted a few features that were not available via this Plugin. So I began to modify every aspect of it and ended up with something similar on screen but different in the back end and something that was more flexible.  So I present to you Xamarin.Plugins.GenericCarousel [GitHub].

Currently there is only a renderer for Android, but iOS is soon to come.  The only thing that needs to be implemented is the gesture listener.

Continue reading “Xamarin.Forms Carousel”

Xamarin MVC Project Separation & Fluent Validation – Part III

Continuing from Part II:

Finally, I’ll show you the actual validator implementation and that BaseType<T> you saw earlier.

Validator 

 

public class UserProfileValidator : AbstractValidator&lt;UserProfile&gt;
{
	public UserProfileValidator()
	{
		RuleFor(a =&gt; a.FirstName)
		.NotEmpty()
		.WithMessage("First Name required.");

		RuleFor(a =&gt; a.LastName)
		.NotEmpty()
		.WithMessage("Last name required.");
	}
}

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part III”

Xamarin MVC Project Separation & Fluent Validation – Part II

Continuing from Part I:

Fluent Validation 

So now we’ve got a project setup that shared our models and services, it’d be great to same the same validation for inputs or fields with minimum effort across multi-platforms.  Meaning if a “FirstName” is required, it would be great to not code that three separate times (Web, Android, and iOS).  With Fluent Validation we can achieve this with a little bit of setup on the mobile side.  On an .NET MVC site integrating FluentValidation is as easy as installing the correct NuGet package for your version of MVC.  It will integrate with un-obtrusive jQuery Validation automatically.  With Xamarin.Forms currently there is no solution to integrate so nicely.  However, I’ve created a framework that allows you to do something similar and have it work across the three platforms.

So one of the things you need in order to show validation is a Label, which will serve as our “Html.ValidationMessageFor” control in the web world and that label needs to be associated with some type of input.  Within the .NET MVC Platform this is achieved by a naming convention, however, we can’t necessarily achieve the same thing in Xamarin.Forms when we create our views via code, not XAML; but we can create our own naming convention!

Before we dive into the code, you should know that under normal circumstances you don’t new up a “Validator” (term for class that extends AbstractValidator) but you can! When you do there is a method you’d call called “Validate()”.  This method returns a “ValidationResult” with two properties: “IsValid” and “Errors” and “Errors” is a collection of “ValidationFailure”, which contains the Name and ErrorMessage for a particular property.  Knowing that we can create our own “unobtrusive” validation setup.  Lets begin!

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part II”

Xamarin MVC Project Separation & Fluent Validation – Part I

Recently at my job, we’ve decided to make a new project base for our company. We currently have an MVC 3 base, but considering MVC 5 is the new norm, it is time we get caught up with the latest and “greatest” (greatest used loosely). We decided that we were going to change our strategy with communicating with the database. Currently we use an ORM provided by Microsoft called Entity Framework. Entity Framework is a great ORM, however, when dealing with multi-million(s) set of records we’ve found it to be sub-par. I leave that for another topic at a later date.

The goal of this semi tutorial is to outline a project setup that has a model, service, and data layer, with the model and service layer being reusable from a MVC site to an Android and iOS app. However, with this project setup you could easily implement a WPF application, Silverlight, or even console apps. Resulting in a true “N-tier” architecture.

Below are some of the technologies that are going to be talked about and I’m going to assume some knowledge on them:
1.) AutoFac
2.) FluentValidation
3.) SqLite.NET
4.) Xamarin
5.) NHibernate

Project Setup

First thing we need to do is create a new solution and add two Solution Folders.  Create one for “Web” and one for “Mobile”, we separate these just for our convenience.  Then we need to start adding projects to the new solution.  Using Visual Studio 2015 you should have everything you need built in to your templates if not you may need to download Xamarin from here and of course make sure you have NuGet installed in Visual Studio.  Next you’re going to add four Portable Class Libraries and suffix them with “*.Configuration”, “*.Models”, “*.Services”, “*.Validation”. It is imperative that these be a “Portable Class Library” in order to get this to work correctly.  Below are the options you will want to set for all four PCLs.

PCL Settings

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part I”

Xamarin Android Button Styles

One of the best UX designs you can do for a user is give them feedback, especially when clicking on a button.  In mobile development, this no exception!  In Android this is relatively easy by setting the text color to a drawable resource.  For example lets say you have the following drawable xml:

buttonText.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
  <item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
  <item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
  <item android:color="#ffffff" />
</selector>

What we’re trying to achieve is changing the text color of the button throughout the different states the button may go through (i.e. when you click on the button).  The following next examples show you how to apply the Resource using Java then in C# using Xamarin.Android.

Java

Button button = (Button) findViewById(R.Id.Button1);

button.setTextColor(getColorStateList().getColor(R.drawable.buttonText));

C#

Button button = new Button(this.Context);

var buttonDrawable = Resources.GetLayout(Resource.Drawable.buttonText);

button.SetTextColor(Android.Content.Res.ColorStateList.CreateFromXml(Android.Content.Res.Resoures.System, buttonText));

There you have it! Setting the text color of a button using Xamarin Android. I wasn’t able to find an exact description on how to do this using c#, however, using Java’s implementation I was able to port it over into the C# world.

Until next time, Happy coding!

Employee Directory Xamarin.Forms

Let me preface this the fact  that when building a mobile app for a large company one of the first things that comes up is the ability to view all their employees in their current intranet.  Especially if they already take pictures of each employee for the intranet, it tends to give them a proxy “Facebook”  feeling.

My instance for this particular client was slightly different, the project manager/lead developer and I had to convince the client this was the best way to step into the mobile app world for his client.  Since he had no idea what we could do or what he wanted to see.  So I set out to begin the mobile app: extending the existing MVC 4 project by adding a new project to the solution strictly for the Web API and creating a new Mobile App project with Xamarin.Forms.

Continue reading “Employee Directory Xamarin.Forms”