How I build an app in 2 hours
Tags: android, kotlin, database
Published: 2021-02-18 14:51:06

Yesterday I built an android app in 2 hours with `kotlin`

without even knowing how to write `hello world` in `kotlin`  before starting to work on it.

Granted, the app is really simple and I've written some demo app at around 2014 when I'm still at school.

The point of this article is not to showing off or anything like that, the point is to share the methods to `get things done quickly`

### Background

My father is a teacher. He always wanted an app on his phone that can do the followings:

1. Store the information of his students (name, score, student id number, etc)
2. Standard CRUD operation on the student information.
3. Customized theme and accent color.
4. Easily add the score of one of his students by 1.
5. Sort the students by score or by id.

At first glance, these functions seem to be not that hard. However, my time is valuable and I do not want to spend too much time on this project (for obvious reasons).

### Analyze the core

Given the task, I need to find the core part of the application. The essential parts of the system are:

1. A database, to store 
2. A list view, to show all entities.
3. A detail view, to inspect, edit or delete a specific entity.
4. Three events, to link the action of user input to 
   - Jump from list view to detail view
   - Switch between two methods of sorting
   - Add the score of entity by 1

Now we have the core function of this app, we need to figure out how to implement them in android, after some quick google search and google documentations. I now know:

1. Database -> room database
2. List view -> recycle view
3. Detail view -> just an activity
4. Three events -> event listeners
5. Kotlin is the preferred language for android development

### The power of boilerplate

After knowing the core tech I need, I now search for `android kotlin room crud recyclerview boilerplate` on google.  I was very fortunate to find a boilerplate that fits my exact need on github. The tech you might need is different, but the general approach is the same.

With the boilerplate, I now need to identify the changes I need to make:

1. Customize the name, theme and accent color the app.
2. Change the data model to be a `student`
3. Change all related code to make sure CRUD operations work perfectly.
4. Add event listeners like the one in the boilerplate for the actions I need.

### Knowledge transfer

When making the changes,  I encountered two key problems:

1. There's no built-in double click event, I need to somehow make it myself
2. Database operation cannot reside in main thread, it must be working with something called `suspend` functions in kotlin.

I developed a lot of javascript so I applied a few techniques I learned in the javascript world and managed to solve the problems like:

1. Use a boolean to mimic double click, basically there's a boolean and a timer. The first click reverse the boolean and start the timer, if the second click occured within the timer time frame, it's counted as a double click. After timer expires, the boolean go back to its original value.
2. `suspend` is basically `async`, in javascript we solve this kind of problem with `iife` : `Immediately invoked function expression`, maybe there's something like `suspend lambda` that was invoked immediately in kotlin as well.

By applying what I've learned developing using javascript, I managed to solve my problems and finished the project within 2 hours, I installed it on my dad's phone and he's super happy.

### Key takeaway

When time is limited and money is short, the best way to maximize your output is to leverage all the help you can get. Open source is a gift for all developers, there's no shame in using it. All you need to do is see the core of the application and apply knowledges of different domain.