{"id":540,"date":"2021-04-20T05:39:16","date_gmt":"2021-04-20T05:39:16","guid":{"rendered":"http:\/\/meatpie\/?p=540"},"modified":"2021-05-13T04:04:35","modified_gmt":"2021-05-13T04:04:35","slug":"weather-app-convert-to-kotlin","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/weather-app-convert-to-kotlin\/","title":{"rendered":"Weather App &#8211; Convert to Kotlin"},"content":{"rendered":"\n<p>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&#8217;s job.<\/p>\n\n\n\n<p>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. \u2603\ufe0e<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Where we want to be<\/h2>\n\n\n\n<p>I&#8217;ve written enough Java. It is a very standard language &#8211; 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.<\/p>\n\n\n\n<p>My student colleague says Kotlin is the future. Well be that as it may. From where I&#8217;m sitting, it might well be that Swift is my future. And Kotlin and Swift seem to share some common philosophies.<\/p>\n\n\n\n<p>Of course the big difference between Kotlin and Swift is &#8216;let&#8217; vs &#8216;val&#8217; for declaring a constant variable. Which makes flipping between languages a little annoying. No matter, that is what IntelliSense&#8217;s job is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Convert to Kotlin<\/h2>\n\n\n\n<p>Android Studio very helpfully offers to convert your code for you. Let&#8217;s see how it goes.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"243\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_044253-300x243.png\" alt=\"Screenshot showing where the Convert Java to Kotlin File menu item lives\" class=\"wp-image-548\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_044253-300x243.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_044253-1024x828.png 1024w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_044253-768x621.png 768w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_044253.png 1077w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"76\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045458-300x76.png\" alt=\"A screen shot showing Android Studio pointing out that Kotlin is not configured in the project\" class=\"wp-image-555\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045458-300x76.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045458.png 726w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Yes Please!<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"147\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045714-300x147.png\" alt=\"Screenshot showing Android Studio asking whether to add Kotlin to all projects, or just this one\" class=\"wp-image-559\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045714-300x147.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_045714.png 561w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>There&#8217;s no turning back now!<\/figcaption><\/figure><\/div>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Warnings<\/h2>\n\n\n\n<p>My first warning is a Kotlin version thing:<\/p>\n\n\n\n<p> <code>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)<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"154\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_050452-300x154.png\" alt=\"Screen shot showing a Kotlin version warning in the build.gradle\" class=\"wp-image-564\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_050452-300x154.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_050452-1024x524.png 1024w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_050452-768x393.png 768w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_050452.png 1111w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>This screenshot in build.gradle<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"158\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_051301-300x158.png\" alt=\"Screenshot showing the Project settings\" class=\"wp-image-567\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_051301-300x158.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_051301-1024x539.png 1024w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_051301-768x404.png 768w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_051301.png 1425w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Never seen this page before. Looks more friendly than fixing Gradle files in the code. <br>DO NOT choose Kotlin.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Well I give up. I have the latest version of the plugin. And the only Plugin version in the dropdown. <\/p>\n\n\n\n<p>After all that, it still did not convert my file to Kotlin. So go back and re-convert.<\/p>\n\n\n\n<p>It created errors on iterators:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"85\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image_2021-04-11_055100-300x85.png\" alt=\"Screen shot showing error message\" class=\"wp-image-577\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_055100-300x85.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_055100-1024x289.png 1024w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_055100-768x217.png 768w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image_2021-04-11_055100.png 1145w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Class &#8216;kotlin.collections.IntIterator&#8217; is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler<\/figcaption><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>I don&#8217;t think we have a gradle issue or a plugin issue. Just a converter issue. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"58\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/04\/image-300x58.png\" alt=\"Screenshot showing suggestion\" class=\"wp-image-580\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image-300x58.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image-768x150.png 768w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/04\/image.png 904w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Replace with .. operator seems to do the trick<\/figcaption><\/figure><\/div>\n\n\n\n<p>There&#8217;s probably a more elegant way of doing it, but I have checked for null so should be OK:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (null != minutely) {\n    for (minCounter in 0..minutely.size - 1) {\n        if (minutely[minCounter].getWeatherWords().contains(precipType)) {\n            timeTil = minCounter.toLong()\n            break\n        }\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Problems<\/h2>\n\n\n\n<p>I&#8217;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. <\/p>\n\n\n\n<p>This helped me: <a href=\"https:\/\/stackoverflow.com\/questions\/43980110\/using-kotlin-class-in-java-cannot-find-symbol\">https:\/\/stackoverflow.com\/questions\/43980110\/using-kotlin-class-in-java-cannot-find-symbol<\/a>  At least it helped to produce more helpful errors. <\/p>\n\n\n\n<p>I&#8217;ve finally got it to compile after two days, by clicking every damn suggestion lightbulb, randomly selecting solutions, and then reverting in git.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fixing Compile Errors<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"has-text-align-left\">Auto-generated failing code:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><code>for (wordCounter in 0 until weatherWordsJson.length())<\/code><\/p>\n\n\n\n<p class=\"has-text-align-left\">Fix:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><code>for (wordCounter in 0..weatherWordsJson.length() - 1)<\/code><\/p>\n\n\n\n<p>It made all my private properties public &#8211; 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IntelliJ<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>IntelliJ looks identical to Android Studio. There is no learning curve with moving.<\/li><li>Open the Android Studio project &#8211; there is no importing or faffing around required.<\/li><li>Costs the same as Android Studio &#8211; ie it there is a free version<\/li><\/ul>\n\n\n\n<p>Yup &#8211; thumbs up all the way to <a href=\"https:\/\/www.jetbrains.com\/idea\/download\/#section=windows\">IntelliJ<\/a>.<\/p>\n\n\n\n<p>But the best is &#8211; it is <em>much<\/em> better at converting Java to Kotlin. Thank you IntelliJ<\/p>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Progress<\/h2>\n\n\n\n<p>New rules = documentation. No more hacky home projects.<\/p>\n\n\n\n<p>I&#8217;ve managed to convert all the data classes to Kotlin now, except the main data class. It is really like a data manager. I&#8217;m cleaning this as I go. Discovered I have quite a few methods related to wind. Hmm Refactoring required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">User Stories<\/h2>\n\n\n\n<p>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\u2019t think we need the temperature duplicated on two pages, do we? &#8211; <strong>Done<\/strong><\/p>\n\n\n\n<p>As a wind geek, I want to know the speed of wind gusts.- <strong>Done<\/strong><\/p>\n\n\n\n<p>I want the download to be quicker. I think I need less data \u2013 10 days is more than a sensible forecast in any case &#8211; <strong>Done<\/strong><\/p>\n\n\n\n<p>Sometimes Current conditions is missing data. In this case we get the wind from hour 1. But I&#8217;m only doing this for the Beaufort number. Need to do this for the other wind speeds as well<\/p>\n\n\n\n<p>As a planner, I want to know more about the rain \u2013 is it patchy or widespread. I don\u2019t think min and max will help me here.<\/p>\n\n\n\n<p>I&#8217;m waiting for some decent rain, so that I can try to validate the graphs.<\/p>\n\n\n\n<p>As a planner, I want to know what weather words are featuring today and tomorrow.<\/p>\n\n\n\n<p>I like the idea of seeing tomorrows weather as well<\/p>\n\n\n\n<p>The scale seems wrong for probability<\/p>\n\n\n\n<p>I\u2019m not getting the washing icon because there are too many hours we are looking at \u2013 maybe we just need to restrict the number of hours we keep to 48, and don\u2019t bother about limits<\/p>\n\n\n\n<p>Can we please have a proper washing algorithm. I do need to know<\/p>\n\n\n\n<p>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&#8217; weather.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s job. Thanks to my colleague, recently a student, who inspires me with <a class=\"read-more\" href=\"https:\/\/snersbots.co.uk\/index.php\/weather-app-convert-to-kotlin\/\">The full story here&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":555,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[36,35],"class_list":["post-540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-software","tag-convert-to-kotlin","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":50,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/540\/revisions\/606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/555"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}