100% Kotlin

Code And Wild
Code & Wild
Published in
5 min readJun 27, 2018

Hey, this is Adam & Aleks from the Mobile Team. This week was very exciting for us as we launched a brand new app, albeit for internal use. We’ll talk more about the app shortly, but the big deal was that it was written in Kotlin (as in 100%). If you’ve not heard of Kotlin, check out Google’s Android site: https://developer.android.com/kotlin/

First, let us explain what the business problem we were looking to solve…

Earlier this year, we started to deliver some of our bouquet ourselves. There were plenty of benefits of using our delivery, such as fewer bouquets going back to a courier depot and of course a colourful van…

However we lacked a key feature, that is, when a bouquet is delivered, we had no way of recording the event in our system, thus we couldn’t inform the sender that we had delivered the bouquet. (That was one of the advantages with using a courier, as they already had the technology and an API for us to poll)

There were plenty of ways we could solve this, but we felt an app would be the most appropriate, whereby drivers could record the delivery of the bouquets. We designed the app to be as simple as possible, minimising the number of taps, so that the driver could log the delivery in seconds.

Working with our backend team, the label on each box had a QR code for the delivery ID. The app would then use the camera to scan the QR code, get the delivery ID and allow the driver to select the status (posted, left with neighbour etc), add a photo and move on! We also catered for the device being offline and store the deliveries locally until a network was established (and then attempting to upload).

Screenshots from the app

With the team consisting of experts in Android and iOS, we made the decision to keep costs down (Android devices being cheaper to buy than Apple devices) and create an Android app. It was also a great opportunity to delve in Kotlin…

  • Creating a new function is fun (literally). No longer do I have to type public static void blah(), instead it would be fun blah()
  • Defining variables, calling functions & building completion blocks is very intuitive and much easier than in Java. Here’s an example to give a bit more colour…

Let’s say we would like to create Shared Preferences entries. Normally with Java we would need to have context when creating them, then set them through the Editor and then apply.

But in Kotlin, you would create a class something like this:

class BLWPrefs (context: Context) {
val PREFS_FILENAME = "your.package.com.prefs"
val EMAIL
= "email"
val prefs
: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0);
...var email: String
get() = prefs.getString(EMAIL, "")
set(value) = prefs.edit().putString(EMAIL, value).apply()
fun clearPrefs() {
prefs.edit().clear().apply()
}
}

Creating getter and setter methods is far easier and you only need to create context once in your Application() class on creation. Saving it as companion object like this:

companion object {
var prefs: BLWPrefs? = null
}
override fun onCreate() {
prefs = BLWPrefs(applicationContext)
}

Using the preference would then look like:

fun loadUser() {
this.email = BLWApp.prefs!!.email
}
fun deleteUser() {
this.email = “”
}
fun saveUser(email: String, bwToken: String, amazonToken: String) {
BLWApp.prefs!!.email = email
}

There you go — all saved persistently.

Very similar to the situation in Apple’s Xcode (with Objective C and Swift), Android Studio allows you to have Java and Kotlin in one project. The big difference here is that you don’t have to worry about bridging headers, cross method calls, finding function definitions with one click and so on and so on. Everything works as if you have only one language.

Not only that, but you can change a whole java file to Kotlin with one click!

When you copy some java code and paste it to a Kotlin file, Android Studio converts it for you automatically! I’ve read on some blogs that you cannot find a lot of solutions on the internet with Kotlin, because it’s a new language, however with this easy change you don’t have to find the solution in Kotlin as you can change it in a second! (For building our Own Delivery app we didn’t have problems in needing Kotlin only solutions)

In our Own delivery project we use a Room database. One of the biggest wins here is that you get database errors… ON COMPILE TIME instead of runtime. That saved a lot of time and makes the code itself safer. The Room database builds abstraction over the SQLite raw database and translates every line of a table into an object. That makes using the database extremely easy! One very good example of this is migration. In our experience with SQLite database that was always a place to be very, very careful as many things can go wrong. Here is a sample of our migration, which reveals the power of Room and Kotlin together:

val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(“ALTER TABLE ‘table_name’ “ + “ ADD COLUMN ‘column_name’ INTEGER NOT NULL DEFAULT 0”)
}
}

We personally don’t like the game of wrapping/unwrapping a variable with question and exclamation marks. Whilst the concept or wrapping/unwrapping is powerful and can create a safer app, it can wreck your head sometimes!

There are no static variables in Kotlin, so you have to to do companion object:

companion object {
val instance: BLWUser by lazy { Holder.INSTANCE }
}

Instead of the known Java way of public static BLWUser instance = BLWUser();

You cannot “cheat” and use context in a static variable and use it outside of an activity. Which can be set as a good thing, because saving a context in a static variable isn’t a good approach anyway. But let’s be honest, everybody had it at some point.

As with any app, we already have a backlog of new features to add to the delivery app, so we’ll crack on with that, but another important win for us is that it has kick started the initiative of migrating our main Android app from Java to Kotlin. The good news is that we have already started with new features will be written in Kotlin :)

Written by the Mobile Team — Adam Francis & Aleksandar Penev

Originally published at medium.com.

--

--