Skip to content

European Union (and Scotland) example

Maciej Gorywoda edited this page Feb 8, 2021 · 11 revisions

The European Union (+ Scotland) example is an expansion on the original "dummy app" based on a Fifty States example from Gluon. I made it to test a bit JavaFX widgets, especially:

You can download the compiled APK from here. The source code for the example starts from here. And here's a screenshot of how it should look when you run it. If you want to learn how to do it yourself, please first read What do you need and How to compile and run an example.

Notes

  • Early in the process of working on this example, I was bitten by this bug: java.lang.NoSuchMethodException: java.lang.invoke.VarHandle.releaseFence(). You can follow the sources to learn more, but the simplest method to fix it is to add one tiny dependency to your Maven's `pom.xml:
        <dependency>
            <groupId>org.scalameta</groupId>
            <artifactId>svm-subs</artifactId>
            <version>101.0.0</version>
        </dependency>

It was made by Ólafur Páll Geirsson (https://github.com/olafurpg) from ScalaMeta. As I understand, the issue should be fixed in the Scala compiler at some point in the close future?... Anyway, it shows one thing that is very impressive to me. To write an Android app this way, I put together work of many brilliant people who do not necessarily even know about each other. Some of them work on Scala, some one GraalVM, others on Gluon Mobile and JavaFX. And there are also people from Android and yet others who develop other libraries I plan to use. They create new features and fix bugs for a lot of reasons not really connected to me writing an Android app in Scala. And yet, it all fits together.

  • One of the main functionalities in any Android app is to download pictures and display them. Here, I just used JavaFX - to download and display countries' flags from Wikipedia. And this is also how I learned that working with JavaFX listeners proved to be a bit tricky: they use Java generics and that doesn't translate very well to Scala. To spare you the details, in the end I wrote a thin wrapper over the one I used, ChangeListener, which clears the pics cache if the downloading of a picture ends with an error. It wasn't difficult, but it can be tedious on a larger scale, so I will look into alternatives.

  • ... Although simply learning scala.jdk converters might help a lot. I'd like to write as much in Scala as possible, but of course in many places cooperation with Java is necessary. Besides, Java 15 is much nicer than Delvik/ART (Java 7, basically) I had to use when writing standard Android apps.

  • In Android, cells of a scrollable list can be re-used when they leave the screen. Instead of just sitting there in the memory, waiting to show up again, or being destroyed and recreated, Android can change their data to something else and display them as those which show up on the screen while the original ones are scrolled out of the screen. It saves resources, but goodbye FP. In short, that's why the country's data is set here in updateItem instead of the constructor - it can be changed. To make it more visible, I added Scotland to the list of displayed countries, but I made the background of its entry a different colour to show it's not in the EU (yet). In general, it's always good to have Scotland on your side.

Next steps:

  • I will try to use Gluon's SceneBuilder to write the same UI. In this example I created the whole UI programatically in Scala. I understand that the same is possible with a mix of programming and FXML files, so in a manner similar to standard Android's widgets library, and that Scene Builder works as a WYSIWYG editor for FXML.

  • I see that I can also use CSS. I'm not a big fan but I'd like to at least read more.

  • The APK crashes on Google Pixel 5 with Android 11. This is because if this. And just as in the case of the releaseFence bug mentioned above, people working on Gluon Substrate are already working on a fix.

  • Because of quirkiness of JavaFX listeners (mentioned above) I'd like to look into Glide as an alternative for downloading and displaying pictures. Another step on this trail would be to look into OkHttp as a http requests library for Android. Glide and OkHttp work well together and they are both very popular in Android app development. Unfortunately, they are both in Java and OkHttp v4 is in Kotlin (v3 is on long time support). I'd prefer to have Scala solutions for both.

  • Because of the same problem, I may look into ScalaFX. Gluon Mobile uses JavaFX directly, and scala.jdk converters are pretty cool, but anyway I want to experiment with ScalaFX a bit. Maybe I will find something in it that will convince me to use it instead of JavaFX + converters.

  • Gluon lets me create a cache for storing pictures downloaded from Wikipedia, but for more complicated purposes I need to know how to (1) access Android's file storage directly, and also how to (2) access Android's database.