Working efficiently (Part 1: communication)

Hello again!

Ever stumbled upon a problem that will probably take quite some time to solve?

Ever thought that a person can do something just by himself or herself?

The only exception to this rule if the project only solely belongs to one person and that this person truly understands everything about it. But this will also take some time.

How about if one were working within a company with several other people?

This means the person will have to understand how the infrastructure works, and the time factor to understand it takes some time depending on the size or domain of what the person needs to know.

And depending on the person, he or she might have to go through a series of weeks to figure something out.

There’s a better way. Ready for it? It is called: communication.

Now this person may call himself or herself an introvert, but good companies will always try and help each employee with their personal and professional growth.

Even when trying to debug a piece of code that can’t be solved solely yourself, always ask the person who committed the code to find out what exactly is going on. Pairing up for 10 minutes can just work wonders without headaches.

This is a reason why we call it team work. Of course if a person has to do something solely, then making a pull request to another member of the team will give an idea of what needs to be done. Once they review it, they’ll provide their feedback, or ask questions along the way.

Going out to lunch from time to time and communicating about what you can improve on will also give some clues. Seeing as a first-person is not the same perspective from a third-person.

Therefore, supporting one-another is important and will serve you well. Eventually, all the good habits will come out naturally given a problem, and then finding the best way to solve it.

So next time if it’s something related to your task that another person has done, communicate with that person to make the process easier.

As an added bonus, even senior-level people in their careers will always communicate when they have to. Therefore, there is absolutely no reason to be shy since others will find out about someone one way or another.

Until next time!
Brian.

Less is more!

Hey everyone,

Ever tried adding a library component that you’ve found through either GitHub or the Xamarin store? How many are there also to begin with?

Also, how portable will the component be? Will it work on just one platform or will it work on all?

Let’s take image library components for example. Fresco is quite a nice image library that immensely helps manage images in our memory buffer, so that there is no need to do a lot of processing on the client-side developer’s end. However, this is only for Android, and Xamarin does not yet have this component in their store 🙁

But no worries. SkiaSharp is here to the rescue! It is cross-platform and has been implemented by Google that Xamarin has ported over!

There is a nice introduction on how to get started. It is quite comprehensive in each of its functionality, but to get started is not as smooth as how Fresco’s documentation, which is to go from adding the gradle dependency down towards the implementation, would be. Its guide is okay-ish if one knows where to look, so that’s not too much of a problem. There’s an exception to one thing however.

What is it?!

The image management on where to load its buffers in memory! What if we had a large image and tried to put it on a canvas with all of the information? Our application will crash with an OutOfMemoryException! 🙁

How do we rectify this?

The easiest solution would be to call the .Resize(SKImageInfo, SKBitmapResizeMethod) method on a SKBitmap object. To do this, we’ll also need an SKImageInfo providing the desired width and height that we want to display on our device in the app.

Let’s say that if we want our image to be 1/2 the width of the screen, we can make our width to be context.Resources.DisplayMetrics.WidthPixels / 2. If we want our height to match the dimensions of how the image should look like, then it should be image.Width * width / image.Height, where image is our SKBitmap object.

Therefore, our code should look like this:

SKBitmap resizedSkBitmap;
using (var largeSkBitmap = SKBitmap.Create("ourLargeImage.png"))
{
    var width = context.Resources.DisplayMetrics.WidthPixels / 2;
    var height = largeSkBitmap.Width * width / largeSkBitmap.Height;
    var wantedImageInfo = new SKImageInfo(width, height);
    resizedSkBitmap = largeSkBitmap.Resize(wantedImageInfo, SKBitmapResizeMethod.Lanczos3);
}


Use the resize method that would suit your needs best 😉 I’ll provide the link here.

That’s pretty much it!

Also, don’t forget to call resizedSkBitmap.Dispose(); when finished with the image to avoid any memory leaks 😉

Until next time!

Brian.

Flexibility for your app

Hey everyone!

Have you ever worked on a very nicely knit portion of an app? That is, an element that is being used over and over so that you don’t have to reinvent the wheel every time?

Try this with a singleton class or type. This design pattern is really useful for symmetrical data that can be passed in multiple times from a JSON element. Therefore, singleton-models from here are very nice, no problem!

Now, try this with UI elements. Let’s take a ViewHolder from the Android SDK for example.

We have a base item element that our CustomViewHolder gets where it takes some fields like name, address, and profile picture. From there, it’s also possible to add in some nice helper methods to do some extra work like getting data from the server. However for simplicity, we’ll just focus on our CustomViewHolder. Therefore, we’ll have something like this:

public class CustomViewHolder : RecyclerView.ViewHolder
{
    public ImageView IvProfilePicture;
    public TextView TvName, TvAddress;

    public CustomViewHolder(View itemView)
    {
        IvProfilePicture = itemView.FindViewById<ImageView>(Resource.Id.ivProfilePicture);
        TvName = itemView.FindViewById<TextView>(Resource.Id.tvName);
        TvAddress = itemView.FindViewById<TextView>(Resource.Id.tvAddress);
    }
}

// ... we'll assume that the XML layout already has these fields

Cool! 🙂

Now, we’ll consolidate our CustomViewHolder into our CustomRecyclerViewAdapter, and then just populate along with the data we will need along with the constructor. Again, cool, it works as we will want it to! 🙂

Now let’s say that our design changed in one of the pages that uses this CustomRecyclerViewAdapter. There are still remaining view elements, with just one additional view element to the list item.

Therefore, depending on the screen, we’ll just toggle this view element’s visibility to Visibility.Gone or Visibility.Visible, right?

If it is a simple dumb view element, then that will work. However, if there is a control element involved, then it’s more complicated than that. 🙁

We can’t just use viewElement.Click += (objectSender, eventArg) => { // code for what should be done after user clicks }; Why? A RecyclerView will reuse elements as we move along, and therefore, we are incrementing click events instead of replacing them every single time.

Also, our constructor can’t support a ClickEventArgs type as a parameter since we will have different views all the time. Therefore, what is the solution?

Being quite familiar just with Android development, we can go back to our old View’s setOnClickListener(View.OnClickListener listener) method!

Well, almost 😉

In Xamarin.Android, we can’t just override the methods that the anonymous classes or anonymous interfaces give us in plain Android development.

Rather, we will need to make our own extension classes in C# that will support the same functionalities that is already provided in plain Android. So, let’s do that!

public CustomOnClick : Java.Lang.Object, View.IOnClickListener
{
    readonly Action action;

    public CustomOnClick(Action action)
    {
        this.action = action;
    }

    protected override void OnClick()
    {
        action.Invoke();
    }
}

Now in our CustomOnClick class, we have just made an Action that work like anonymous methods in Java. Therefore, we can now do this:

viewElement.SetOnClickListener(new CustomOnClick(() => { // code for what should be done after user clicks }));

Now every time we scroll our RecyclerView, we are sure that our clicks get reset each time with the specific functionality we want exclusive to the item! Now, we have a flexible RecyclerView.Adapter! 🙂

In this case, we have inherited from Java’s Object class to avoid IntPtr handles and Dispose since Android should handle all of this on its own regardless. In the Android SDK, this is mainly with interfaces described here, but there should be no need to do this for classes.

By the way, this works well with other portions where by default Xamarin.Android doesn’t provide their own interfaces for any other Android anonymous classes. 😉

Until next time!

Brian.

Introduction

Welcome to Improvised Code!

Our goal is to keep doing our best in our software development skills! We may know how to write out our functions/methods, structs, or classes, but some of those times what we write may not be the best.

Even some frameworks or libraries we use out there can be really great, however, there can also sometimes be bugs as well! That’s why there’s not really any best framework. They can be the best for a period of time, but maintaining that best figure is quite the challenge.

Anyhow, almost any software developer knows that as long as a person knows how to code, then the languages, frameworks or libraries that he or she is using will not matter much since there are similarities. For example, a person that knows Java will not have too much of a problem with C# since they both share very similar syntax.

Therefore, learning the technical stuff isn’t too much of a problem. However, learning the best practices of code takes a bit of time (especially if a person is just starting out). You might even be asking yourself “Wow, this looks really rough! Can I make this even better?!”

Of course! We just need to figure out what we want our software to do by first mapping it out with chicken scratch, and then code it in the simplest way to achieve what we have mapped out. When that’s done, then anybody who reviews our code will be much happier along the way!

Although the bad news is that learning the bits and pieces to make really nice and interesting code can take a bit of time, the good news is it can be fun and rewarding once a person him/herself experiences this. So with that being said, let’s motivate ourselves to make better software!

Improvised Code’s starting choice of platform will be Android, on top of using the Xamarin.Android framework. But don’t fret too much since there may be similarities with just Android under Google! Also, unless an object or type is platform specific, just because there is one focus of platform doesn’t mean that the same algorithm cannot be used in another.

And, my final words in this post are: code happy, live well.

Yours truly,

Brian.