Weather App – Convert to Kotlin

Starting immediately, all future code needs to be in Kotlin. There is a tag in git, so I know where to go back to if it fails. My student colleague assures me it is simple to convert a Java class to Kotlin. Tomorrow’s job.

Thanks to my colleague, recently a student, who inspires me with daft ideas like converting my code to Kotlin. It is people like you who keep me young. ☃︎

Where we want to be

I’ve written enough Java. It is a very standard language – it looks like any other C language, except for the for loops, which seem to differ in every language. You can pretty much google any code snippet you want. There is no more learning to be got from sticking with it, I think.

My student colleague says Kotlin is the future. Well be that as it may. From where I’m sitting, it might well be that Swift is my future. And Kotlin and Swift seem to share some common philosophies.

Of course the big difference between Kotlin and Swift is ‘let’ vs ‘val’ for declaring a constant variable. Which makes flipping between languages a little annoying. No matter, that is what IntelliSense’s job is.

Convert to Kotlin

Android Studio very helpfully offers to convert your code for you. Let’s see how it goes.

Screenshot showing where the Convert Java to Kotlin File menu item lives
A screen shot showing Android Studio pointing out that Kotlin is not configured in the project
Yes Please!
Screenshot showing Android Studio asking whether to add Kotlin to all projects, or just this one
There’s no turning back now!

After this a lot of Gradle stuff happens. I shall leave it to sort itself out, before the moment of truth when I click the Build button.

Warnings

My first warning is a Kotlin version thing:

Kotlin version that is used for building with Gradle (1.5.0-M2) differs from the one bundled into the IDE plugin (1.4.32)

Screen shot showing a Kotlin version warning in the build.gradle
This screenshot in build.gradle
Screenshot showing the Project settings
Never seen this page before. Looks more friendly than fixing Gradle files in the code.
DO NOT choose Kotlin.

Well I give up. I have the latest version of the plugin. And the only Plugin version in the dropdown.

After all that, it still did not convert my file to Kotlin. So go back and re-convert.

It created errors on iterators:

Screen shot showing error message
Class ‘kotlin.collections.IntIterator’ is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler

I don’t think we have a gradle issue or a plugin issue. Just a converter issue.

Screenshot showing suggestion
Replace with .. operator seems to do the trick

There’s probably a more elegant way of doing it, but I have checked for null so should be OK:

if (null != minutely) {
    for (minCounter in 0..minutely.size - 1) {
        if (minutely[minCounter].getWeatherWords().contains(precipType)) {
            timeTil = minCounter.toLong()
            break
        }
    }
}

Problems

I’m fed up. Done it a dozen times. Losing the will to live. I think I might now make my old class into a base class, then inherit a Kotlin class. That way I can pick my fights, instead of trying to get an entire class to compile.

This helped me: https://stackoverflow.com/questions/43980110/using-kotlin-class-in-java-cannot-find-symbol At least it helped to produce more helpful errors.

I’ve finally got it to compile after two days, by clicking every damn suggestion lightbulb, randomly selecting solutions, and then reverting in git.

Bottom line, my project had so many out of date background things, that Android Studio itself was confused. The trick was persuading the Kotlin gradle version to match the Kotlin plugin version, but without being a gradle guru, it was a bit confusing to put it mildly.

Fixing Compile Errors

Auto-generated failing code:

for (wordCounter in 0 until weatherWordsJson.length())

Fix:

for (wordCounter in 0..weatherWordsJson.length() - 1)

It made all my private properties public – is that right? Well it broke the code. I had to manually change all my properties back to private and add properties. Surely this is not right. Need to find out more.

IntelliJ

  • IntelliJ looks identical to Android Studio. There is no learning curve with moving.
  • Open the Android Studio project – there is no importing or faffing around required.
  • Costs the same as Android Studio – ie it there is a free version

Yup – thumbs up all the way to IntelliJ.

But the best is – it is much better at converting Java to Kotlin. Thank you IntelliJ

Progress

New rules = documentation. No more hacky home projects.

I’ve managed to convert all the data classes to Kotlin now, except the main data class. It is really like a data manager. I’m cleaning this as I go. Discovered I have quite a few methods related to wind. Hmm Refactoring required.

User Stories

As a fool, I want to know how cold it actually feels, in order to know whether to take my coat to the shops. I don’t think we need the temperature duplicated on two pages, do we? – Done

As a wind geek, I want to know the speed of wind gusts.- Done

I want the download to be quicker. I think I need less data – 10 days is more than a sensible forecast in any case – Done

Sometimes Current conditions is missing data. In this case we get the wind from hour 1. But I’m only doing this for the Beaufort number. Need to do this for the other wind speeds as well

As a planner, I want to know more about the rain – is it patchy or widespread. I don’t think min and max will help me here.

I’m waiting for some decent rain, so that I can try to validate the graphs.

As a planner, I want to know what weather words are featuring today and tomorrow.

I like the idea of seeing tomorrows weather as well

The scale seems wrong for probability

I’m not getting the washing icon because there are too many hours we are looking at – maybe we just need to restrict the number of hours we keep to 48, and don’t bother about limits

Can we please have a proper washing algorithm. I do need to know

Please may I have a menu so that I can add my favourites to the dropdown, so that I can nosey parker on my families’ weather.

Leave a Reply

Your email address will not be published. Required fields are marked *