Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, June 2, 2016

Web Services made easy with HttpClient & JSON.NET

I have intended to write this for a while. As a Sales Engineer for Xamarin, and now Microsoft, I go through a lot of the same scenarios when working with companies to get up and running with Xamarin and mobile development. As one example, there is almost a 99% guarantee that the first apps these companies plan to build will communicate with an external data source in some way, almost always using web services.

Now, there is a high likelihood this is not a new topic to those who read this blog, but as a believer in the "Lucky 10,000" from xkcd, I figure this information is still worth sharing. To those of you who were unaware of today's content, congrats as you are one of the lucky 10,000 today! What I have to share should make your mobile journey all the easier.

Now it should be noted that I recognize web services are a broad topic, lots of solutions out there. One quick hop over to the Xamarin documentation alone lists a few:

https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/

The main three built into the Xamarin tooling are on consuming RESTful, SOAP and WCF services. For the remainder of this post I will focus on REST as I find it to be the most straightforward to consume.

Consuming REST services really falls down to two main parts. Consumption of the service itself, and then consumption of the data received. As you likely have guessed, there are two tools available to help make this process much more straightforward. For the service, Microsoft's HTTP Client libraries and for the data, JSON.NET. When combined, it can be as simple as three lines of code to communicate with an external service and acquire data. Below is a sample of what that code would look like:

var httpClient = new HttpClient ();
var json = await httpClient.GetStringAsync ($"http://mywebservice.com");
var result = JsonConvert.DeserializeObject (json);


What you see above is both parts. Line one creates the HttpClient followed by actually making a REST call to grab you data. The third line is part two, parsing the resulting json into something more C# readable. If you have not used JSON.NET before, it really is something of magic under the hood. Basically, all you need to do is provide it a valid json file, and then pass a constructed C# object for it to parse into. In the above example, it will try to parse your json into the class "MY_DATA_OBJECT". 

To use a more "practical" example, I am a big fan of Blizzard games and recently wrote an app to play with some of the community API's available, found here https://dev.battle.net/. The main goal was to just do a bit of poking and write a simple Warcraft character display app. Nothing complex, make some calls to load character data, item sets, etc. In  order to grab the basic character profile in my app, I have the following code:

var httpClient = new HttpClient (new NativeMessageHandler ());
var json = await httpClient.GetStringAsync ($"https://us.api.battle.net/wow/character/Muradin/Deaus?locale=en_US&apikey={
Constants.APIKEY}" );

var res = JsonConvert.DeserializeObject (json); 

In this case, I am pulling the data for my Paladin, Deaus, from the Muradin server. The resulting JSON looked like this:

{
     "lastModified": 1426370270000,
     "name": "Deaus",
     "realm": "Muradin",
     "battlegroup": "Vengeance",
     "class": 2,
     "race": 3,
     "gender": 0,
     "level": 100,
     "achievementPoints": 7620,
     "thumbnail": "muradin/10/119640842-avatar.jpg",
     "calcClass": "b",
     "faction": 0,
     "totalHonorableKills": 1961
}

On the C# side, I would create a class called CharData which would look something like this:

public class CharData
{
    public long lastModified { get; set; }
    public string name { get; set; }
    public string realm { get; set; }
    public string battlegroup { get; set; }
    public int class { get; set; }
    public int race { get; set; }
    public int gender { get; set; }
    public int level { get; set; }
    public int achievementPoints { get; set; }
    public string thumbnail { get; set; }
    public string calcClass { get; set; }
    public int faction { get; set; }
    public int totalHonorableKills { get; set; }
}


JSON.NET does the rest of the work, taking advantage of the json tags and matching them to the appropriate C# properties, and instantiates/populates my data for me. The best part of all of this? All of this is PCL compliant so it is located 100% inside my shared code!

One useful tool to expedite the process even more is the website  json2csharp.com. This website will take json data and auto generate the resulting C# class for you, at least at a base level.

As a note, not all REST end points are so easily configured, but HttpClient is actually quite flexible. I was also recently poking around a community made Hearthstone API, https://market.mashape.com/omgvamp/hearthstone, and their .NET API's are powered through unirest (http://unirest.io/net). To get this to work I had to add a couple of DefaultRequestHeader paramters to match the documentation there. Still, rather straight forward to communicate with.

That is about it. While this does put some requirement to set up good REST endpoints for your mobile applications, it tends to be worth the effort. Even if you have legacy WCF systems or complicated back ends, I have found that companies that have made exposed REST front ends to facilitate the communication can help streamline the entire process and, as you see above, can really make consumption on the mobile side quite easy. Until next time!

Monday, May 16, 2016

Complete Mobile Dev Ops with Visual Studio Team Services and Xamarin Test Cloud

Wow it has been over six months. Sorry! As you can imagine, things have been busy in 2016. There has been an acquisition, Xamarin tools made available for free in Visual Studio, and then Xamarin Evolve. Now that things have settled down, and integration into Microsoft underway, I wanted to take this as an opportunity to get back to making semi-regular posts once more.

When I joined Xamarin in December of 2013, it was still largely a one product company. Xamarin enabled you to develop apps for Android, iOS, or Mac using C#. (OK, technically three products but to a degree you can look at them as different facets of the same tool).

Since that point Xamarin has grown to help assist in the full mobile development lifecycle. This included the launch of Xamarin University for training, Xamarin Test Cloud for better testing and Xamarin Inisghts for crash reporting, messaging and analytics. With Xamarin now under the Microsoft umbrella, a few new tools are available to even further complete that picture. The focus of this entry will be on how to leverage Visual Studio Team Services with Xamarin, both development and Xamarin Test Cloud, to create a full Dev-Test-Publish lifecycle.

Getting setup with Visual Studio Team Services
I won't spend a whole lot of time on this. The basic checklist can be found below.

1. Create your VSTS account
2. Create your Project
3. Connect to Visual Studio
4. Set up your repo and code

The main setup guide hub can be found here: https://www.visualstudio.com/get-started/setup/set-up-vs

For my scenario, I ended up using git as my repository, which would also be my recommendation if you don't have a previous requirement or preference.

Integrating Xamarin Test Cloud
Now that you have your code integrated with VSTS, and uploaded, the next step is to create a build process that confirms your build and also calls to Xamarin Test Cloud for regression testing. That way, any time a developer on your team submits their code, you can confirm everything is still working. Please note, this will require having created a UITest project and at least one step to run. If you have not ever done this, you can find more details here:
https://developer.xamarin.com/guides/testcloud/uitest/intro-to-uitest/

Now, the final piece of this puzzle is to integrate this as part of your VSTS build chain. Microsoft actually made this process quite straight forward, once I had my head wrapped around the tooling. A full general doc, found after I got it sorted myself, can be found here:

https://msdn.microsoft.com/library/vs/alm/build/apps/xamarin

This is what I wanted to break down and guide in more detail. First, select the Build tab in the VSTS portal and hit the green "+" button:

This will create a new dialog window. This is where you will select your build type. For our example we will use Xamarin.Android template. If you need iOS, you would match this process with iOS.

So many options
From here you can use the default settings, you want to select your Repository in question so that it uses this code for the build. If you notice in my example, I have "Continuous integration" selected. I recommend this if possible as it will allow you to run these tests whenever code is submitted to the branch.

Once you hit create, you are then resented with a dialog to configure the specific build settings. There are a three steps that require input:





But it can be boiled down to
1. Your license login (user name and password) information to activate\deactivate your Xamarin credentials
2. Xamarin Test Cloud API Key, Device ID, Test Cloud email

As a pro-tip for the password, the password field defaults to a plain text entry. What I would recommend is select the variables tab, and add your password as a variable, with the lock icon selected to flag it as "Secret".
MyPassword is the variable I have created.
Then in the password field you just use that variable with the syntax $(_VARNAME_). Much more secure than plain text usage.

For the "Test" step, you need to provide a few pieces of information.

First is your Test Cloud API Key, which is found under Teams and Organizations on your Test Cloud account. You can find details on obtaining that here: https://developer.xamarin.com/guides/testcloud/organizations-and-teams/#Obtaining_the_Team_API_Key
 
If you are not currently using Xamarin Test Cloud, fear not! We have a free trial that you can try out. More details can be found here: https://testcloud.xamarin.com/register

Again, you need to provide an email, this time the email of registered/added to the Test Cloud account. This may or may not differ from your Xamarin license details.

The devices ID you can acquire when you create an upload on Xamarin Test Cloud. The final step will give you a command line prompt that can be used for Windows or OSX and included is a --devices (#########) flag. This id is the final piece of information as it tells Test Cloud what devices the test needs to be run on, and can be reused from run to run. I would recommend recording these ID's as currently there is not another way to retrieve them to my knowledge.

Once you have entered all this information you have everything you need. At this point I would recommend selecting "Queue build" to do a test run with what you have uploaded, troubleshoot issues as needed. Ideally you should see this:
Green is good!
That's it! You have made your first step into a larger world of end to end mobile development and more efficient DevOps! Now, any time you submit code from git, this build step will be executed and you are on your way to writing higher quality code. Use this to make sure you are keeping your app at a high quality, and catch bugs and regressions before your users do!

Monday, August 31, 2015

Android ListViews and Updating the Backend Data

I wanted to spend at least one more post to try to put my app in a finished state so there was a lot of time spent on both backend code and getting my skills list to display properly.

My first thought was to use a ListView to display all of my skills, it seemed logical. It was a of items, numbers and a checkbox. Time to brush off what I learned from custom spinner items. Creating adapters for your ListView is actually pretty standard as a lot of the time the default elements are going to not provide what you need.

The first step was to at least get my ListView organized and all my skills displayed. I already had all the hookups in place thanks to my work last week with implementing ViewPager. The first step was to create a new LayoutView, which housed my ListView. I then used a default ArrayAdapter to just grab a placeholder list of strings.


        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var root = inflater.Inflate (Resource.Layout.fragment_charsheet_skills, container, false);
            ListView view = root.FindViewById<ListView> (Resource.Id.skillsList);
            var skills = ((CharacterSheetActivity)this.Activity).CharacterInformation.Skills;
           view.Adapter = new ArrayAdapter<string> (this.Activity,Android.Resource.Layout.SimpleListItem1, skills);

            return root;

        }


 Pretty straight forward, and shows at least a starting point:
 

We have a successful ListView! Now to add the Custom Adapter. In this case, I want to display not only the name, but the ability mod score, whether or not the character is trained, and then the final value. (Mod + Proficiency)

The adapter is set up and in order. The first challenge I ran into was getting my items to not clump together. RelativeLayout was not really an option here, so a little searching I discovered one effective option was to use the "Weight" tag to give each item equal weight within the control. 

 





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">
    <TextView
        android:id="@+id/skillName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Placeholder"
        android:layout_weight="1" />
    <TextView
        android:id="@+id/modValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="+3" />
    <CheckBox
        android:id="@+id/isTrained"
        android:layout_width="32.0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView
        android:id="@+id/totalValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="+5" />
</LinearLayout>


In theory that worked well, but then I realized I had a problem once I got the final result running.


Looks Perfect
Maybe I need to think this through...
As the weights are row specific, it does not help align each column. Once again, for implementation and times sake I decided to call that "good enough" for now. 

The next step was to work on my back end code. As can be seen in the above, I am hard coding all of my values, and no hookup event for the proficiency checkbox. This actually required a lot of back end work, which was good to revisit and clean up the data.

 Now, I will preface by saying I do not admit this code will be the best. I'm playing more of the functional over proper card here. As a side project it will regularly be a work in progress. 

I created both a Skill and SkillsList class on my back end. The reason for the latter is it gave me a collection that I could house some of the more static concepts like what skills have what ability mods, and a fully populated list of all possible names. Again, not proud of it, but its functional. Then, at launch all possible skills are preloaded based on starting stats. This gives me all my initial conditions that I access as accessor methods. (Loading data I will revisit at a later point).

From an input standpoint the two big changes I needed to make sure happened were 

a) update final values as proficiency was enabled
b) update mods when ability scores are changed.

For the first, I did an inline set of code, pretty straight forward.


proficient.CheckedChange += (object sender, CompoundButton.CheckedChangeEventArgs e) =>  
{ 
    CharacterSheet sheet = ((CharacterSheetActivity)_context).CharacterInformation; 
    
sheet.Skills[position].IsProficient = e.IsChecked;
    skillMod.Text = sheet.Skills [position].TotalValue + ""; 
}


On the flip side, with all of my Attribute code, I added this line:

character.UpdateSkills (Attributes.AttributeName.STR);

Which is essentially calling an update method within my SkillsList:

public void UpdateSkillMod (Attributes.AttributeName type, int mod)
{
    foreach(Skill skill in _skills)
    {                
        if(skill.ModType == type)
        {
            skill.ModValue = mod;
        }
    }
}


This is simply an enumeration through all skills that updates the ModValue of the associated type. That gets us to our final version, which we will hold with for now.

On the back end I also did a lot of cleanup for better preparation for when I need to load character data. At this point I am going to call the Android version of the app in a good spot and now move to get an iOS skin running with this.

Friday, August 14, 2015

Implementing the Toolbar and Scrolling Tabs

After spending a lot of time working on what was essentially a single page view, I felt like I had enough blocked functionality to really start broadening my horizons. There is too much data on a single character sheet to warrant it all on one view, nor would that make for pretty UI Code.

As a reference, here is what we are basing everything off of, pulled straight from the Dungeons and Dragons Website
This does not include the spells page, that will be its own beast that I will tackle later, but there are some logical groupings of information that we can use. What makes the most sense is to add a Tab Control based layout to scroll through sections of information.

First step was the Toolbar. I wanted to try to use all the new latest and greatest and was previously using an ActionBar. Luckily James Montemagno gave a great overview for adding a Toolbar to your app to replace the ActionBar, I suggest starting with a read there:

https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar/

From a visual side of things everything looked roughly the same, but gives me greater flexibility in the long run. Next step was to add new menu items. I have learned that Android really likes its .xml files and menus are no different. For now, we will start with a new, save and load button. For spacial consideration I am just showing the add button for now, but it gives the app a nice look I think:



I also am going to remove the floating action button for the time being. I have some ideas for it down the line, but likely will only resurface once I get a more complete product (much later).

The next step was to add a TabLayout to the toolbar. I also wanted to include both tap and swipe navigation as it adds a logical flow to the app. This is where I also had to learn about fragments and ViewPagers. This definitely took a bit of research as there were a number of parts to get a handle on.

From a high level overview, Fragments are a way for Android to both compartmentalize UI into component pieces, to allow you to better support multiple screen sizes, and just break down your UI easier. A good overview that helped me can be found here:

http://developer.xamarin.com/guides/android/platform_features/fragments/

To create my desired setup, this guide on the Android developer docs was highly valuable:

http://developer.android.com/training/implementing-navigation/lateral.html

You break it down by having your TabLayout add 2+ ViewPager objects to represent each tab. Each ViewPager object is going to be a unique Fragment on the back end, or basically a UI view. Really cool stuff.

So the first thing I need to do is set up each of my ViewPager objects, so that they are grouped together and can be "swipable". The following code handles all of that.

var viewPager = FindViewById<ViewPager> (Resource.Id.viewPager);
SetupViewPagers (viewPager);


private void SetupViewPagers(ViewPager viewPager)
{
      var adapter = new MyAdapter (SupportFragmentManager);
      adapter.AddFragment (new CharSheetOverviewFragment (), "Overview");
      adapter.AddFragment(new CharSheetSkillsFragment(), "Skills");
      viewPager.Adapter = adapter;
}


This will create my ViewPager object, where the SetupViewPagers method creates each individual Page, and then sets the adapter to group them together. The final result is a ViewPager object, or a number of views that are side by side and have built in scrolling. Pretty neat. SupportFragmentManager is a predefined Adapter provided by the Android Support Package that we need to set all this up. the viewPager resource is simply something that I added to my main .axml file that will house the ViewPager spells.

From there you are following the same process as normal. You create a Fragment .axml file and a correspoding Activity class to load it. The difference being instead of inheriting from "Activity" you are inheriting from Fragment. Your OnCreateView method inflates the fragment UI and returns the view.
   public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
     var root = inflater.Inflate (Resource.Layout.fragment_charsheet_overview, 
container
, false);
     return root;
}


One of the cool side effects of using Fragments is they also maintain a reference to the parent Activity which loaded them. So in my case, the parent Activity, a CharacterSheetView has a loaded set of character Data. Each of my fragments can pull from that data by using this.Activity.

A view of what we see now can be found below, I have been updating some of the code to modify how my attributes are being displayed, moving from a TableLayout to GridLayout



I am going to spend a little more time polishing the attribute list and then actually look to start replicating this in iOS to help broaden my skillset. I do know that I will likely use a ListView for the skills so I may just tackle that next. We will see.


Friday, July 31, 2015

Data Validation of Numeric Edit Text in Android

After spending some time to learn Material design, I felt it would be worth my time to take a break and focus on actually getting data onto my app and start progressing, then do a design pass later. I was churning my wheels trying to get all the character attributes set up and decided it was best to just get something on paper. I settled on a TableLayout to organize the labels from the character attributes and modifiers. I also did a bit of playing with design and cleanup and had something at least organized:

Overall this is rather respectable, it has a semi clean look and gives logical organization of the data. Am I in love with it? No, but it is something. The biggest challenge was getting the labels to stretch across the entirety of the view, instead of getting clumped. It took a bit of searching, but I found this attribute:

android:stretchColumns

Which made all the difference. It allowed me to get the desired spread. I likely should revisit the spacing size of the central Numeric EditText but since this is more placeholder I say sufficient for now.

The next big step was setting it so that when the user sets his Attribute value, it would auto update the modifier on the back end. The initial setup was not terrible, you hook up a "TextChanged" event handler, grab the values, update accordingly.

        void OnStrengthChanged (object sender, Android.Text.TextChangedEventArgs e)
{ 

     var numField = (EditText)sender;
     var value = int.Parse (numField.Text);
     characterData.SetSingleAttribute (new Attributes{ Strength = value });

     var mod = FindViewById<TextView> (Resource.Id.strengthMod);
     mod.Text = testCharacter.FinalAttributes.StrengthModifier.ToString ();

}

Overall, rather straight forward. I currently store Attributes as a single object that contains the individual properties for both Values and modifiers, basically a special array container with some validation inside. "characterData" is my storage for all applicable data. All of that is defined in my shared PCL. I think this is an area that could use a refactor. Currently my SetSingleAttribute is something like this:

        public void SetSingleAttribute(Attributes attributes)
        {
            baseAttributes.Strength = attributes.Strength == 0 ? baseAttributes.Strength : attributes.Strength;
            baseAttributes.Dexterity = attributes.Dexterity == 0 ? baseAttributes.Dexterity : attributes.Dexterity;
            baseAttributes.Constitution = attributes.Constitution == 0 ? baseAttributes.Constitution : attributes.Constitution;
            baseAttributes.Intelligence = attributes.Intelligence == 0 ? baseAttributes.Intelligence : attributes.Intelligence;
            baseAttributes.Wisdom = attributes.Wisdom == 0 ? baseAttributes.Wisdom : attributes.Wisdom;
            baseAttributes.Charisma = attributes.Charisma == 0 ? baseAttributes.Charisma : attributes.Charisma;

            baseAttributes = baseAttributes - Race.BonusAttributes;
        }







Basically I find the attribute that has been modified as all the others have been nulled out. I need to spend some more time on this, especially once I implement racial and feat bonuses. For now, it gets things moving forward while I block out all the data.

One challenge that I ran into was editing attribute values. The default behavior when clicking these fields (as they are just text fields) is to allow the user to edit the entry. To handle the scenario where a user deletes the existing values, I was running into an awkward case of trying to convert an empty string to a number, which does not work very well, and left an unintuitive experience. What I did to solve it was set the fields "Hint" parameter:

strValue.Hint = "8"; 

Then you get a nice greyed out sample field, without disrupting your flow:
As you can see, there is a nice greyed entry, more intuitive.
This is definitely coming along well. For some preliminary data validation I also set maxLength to 2 to prevent numbers larger than 2 digits. Ideally I  should limit to something like 6-20 as per the rules, but it is a start. As a note, a more complete complete implementation would use either a custom class of EditText or InputFilters.