Posts

How costly are your `Option`s?

Image
Code which is optimised and performant (for a majority of use cases) doesn't have to be ugly and unreadable, for there's a lot of performance gains to be made by simply following good design and clean coding principles. Scala's Option is a very idiomatic way of representing optional values. Now, I'm not against the use of Option , however, more often than I'd like to, have I come across it being misused/abused. For example, when defining generic types: case class Asset(name: String , shareQuantity: Option[ Int ] , sharePrice: Option[ BigDecimal ] , bondYield: Option[ BigDecimal ] , cashValue: Option[ Amount ]) which should've been modelled as specialised types: sealed abstract class Asset(name: String ) case class Stock(name: String , shareQuantity: Int, sharePrice: BigDecimal ) extends Asset(name) case class Bond(name: String , bondYield: BigDecimal ) extends Asset(name) case class C...

Partial HTML5 support in IE8 without shims or javascript

Image
Today, I stumbled across something strange in IE8 where it was able to identify and render HTML5 elements like <section> and <article> without the use of the HTML5-shiv or even without having to use document.createElement() . I figured it had to do with an "xmlns" attributed which accidently landed up on one of the <section> tags in my html. To illustrate my point, I'll use variations of the following HTML: <!DOCTYPE html> <html>   <head>     <style>       .outer,.middle,.inner {         padding: 20px;       }       .outer {         border: 1px solid black;         background: lightblue;       }       .middle {         border: 1px solid red;         background: yellow;       }       .inner {         border...

ListView/ExpandableListView randomly changes background colour on scroll/click/expand/collapse

Image
Working on the same android app which I posted about a couple of times earlier, I had this one feature I wanted to implement where I show a list of persons and highlight those from that list who have a birthday today. I tried a couple of different approaches to to the "highlight" the list items which had a birthday today for e.g: setting the background colour for those items, setting up a background icon for those, changing the text colour of the list item, and with all of these I faced 1 common problem: if I had to scroll the list or expand/collapse one item group (in case of ExpandableListView ), then it would end up highlighting some random items from the list, and if I continued that excercise, finally almost all items would get highlighted. Since I had used an ExpandableListView , my getGroupView method looked somewhat like this: @Override  public View getGroupView(int groupPosition, ..., ViewGroup parent) {   Person person = (Person) getGroup(groupPosition);  ...

PreferenceFragment shows up with a transparent background

Image
In an android app that I'm currently working on, I wanted use Android's Preference API to provide an interface to modify some app specific settings. I didn't want to create another Activity or PreferenceActivity for that, and hence I thought of simply using a PreferenceFragment . Now, in my project I already had a MainActivity ( extends ActionBarActivity ) which had a MainFragment wired up to it somewhat like this in it's onCreate method: getSupportFragmentManager().beginTransaction()  .add(R.id.container, new MainFragment())  .commit(); And because of that I knew that I had to replace the existing MainFragment with my new SettingsFragment which extended PreferenceFragment . With that in mind, I went ahead and added the following to my MainActivity 's onOptionsItemSelected : getFragmentManager().beginTransaction()  .replace(R.id.container, new SettingsFragment())  .addToBackStack(null)  .commit(); When I saw this work in the emulator, it looked some...

"Application not installed" when APK is not signed

I wanted to test a small android app which I was working on, on my phone. I had already tested this app on an emulator and even on a physical device - my phone earlier, but I had done that via ADB. However, I wanted to install it this time from the apk file. Now, when I ran gradlew assembleRelease there were 2 apk files generated in the projectname /build/apk directory. One named projectname -debug-unaligned.apk and the other named projectname -release-unsigned.apk . The release unsigned one seemed to be the right one to try out. So I tried installing it on my phone. Everything went on well until I saw a message when it had just seemed like the app was installed which actually read: "Application not installed". I was a bit confused looking at the message, I didn't quite understand what went wrong as there was no details as to what went wrong in the message. I had pretty much everything configured the way I thought would be required like checking off the box against ...

Bundle containsKey() but returns null on getString()

Back to blogging after a long time, and this time its quite a lame one which I usually wouldn't want to blog about, but I did spend close to an hour trying to figure out what I was doing wrong. I just started a small android application and was trying to send a paramter from one Activity to another. Now, after a bit of searching around, I found out that I needed to create an Intent Bundle and put the parameter in it, and the new Activity which gets started will be able to fetch it from the same Bundle using the key that was provided while inserting data into the Bundle . So, I had something like this in the first activity: Intent intent = new Intent(FirstActivity.this, SecondActivity.class); EditText nameText = (EditText) findViewById(R.id.query_textbox); intent.putExtra("queryKey", nameText.getText()); startActivity(intent); and something like this in the second activity: Bundle extras = getIntent().getExtras(); String query = extras.getString("queryKey");...

Running multiple test suites using maven-karma-plugin

The maven-karma-plugin allows running your jasmine tests via karma from maven. For the most part, the documentation given in the readme.md at  https://github.com/karma-runner/maven-karma-plugin  is sufficient if you just want to run unit tests using karma. However, if you want to use the karma to run your unit tests as well as your e2e tests, you need to specify it in your pom.xml in a slightly different way. Firstly, the <configuration /> goes inside an <execution /> . Then every <execution /> needs to have an <id /> which should be unique. <plugin>     <groupId>com.kelveden</groupId>     <artifactId>maven-karma-plugin</artifactId>     <version>1.1</version>     <executions>         <execution>             <id>unit</id>             <goals>       ...