Skip to main content

Posts

Showing posts from July, 2019

Manage network states with the Paging library

With Room implementation, the Paging Library is not such a big deal. However it needs some small configurations when it comes to using it for API-calls.

One thing to keep in mind, is the initial state, which basically is a different task, which might fail with an Exception. And the same logic is followed by the continues state after the first data are loaded. When using coroutines, we might wanna have an extra problem: managing the CoroutineScope inside a DataSource.

Implementation

With the help of Kotlin data classes, we might be able to manage Paging Library state:

After the basic setup is made, we might want to make some setup on our PagingDataSource, which on my case, is of type PagedKeyDataSource:

Notice the initial loading and the after loading need different fields for same network state of theirs.
After that, we should be able to produce the DataSourceFactory observing our PagingDataSource with LiveData<NewsDataSource>. The main goal of this is to centralize what the P…

Getting hands dirty with KotlinJs

Although web development hasn't been my thing since I started programming, I was really attracted the way Kotlin does solve things. So I did give a try to KotlinJS.

This is how to set up a KotlinJs page in less than an hour. 

Note: Chose to use the simplest set up without gradle or maven. You are free to chose what you like.

First of all, you start with a new project in your IntelliJ and choose Kotlin -> Javascript.


After that, you can name your project and start. The IDE will generate an empty folder for you.

So to start of, you have lots of options here, for example, you may program the html and css on your own, and than connect the dots with kotlin. Or, you can fully type in Kotlin (as I chose to do it) with this nice library called Kotlinx Html.
Since I'm not using gradle nor maven, I should import it as a JAR (which is not that cool, but for a demo project, no problem) 👎

On with the show. 
So what the app will do? The user inputs a name, and he gets back a Chuck Norris

A fusion between WorkManager and AlarmManager

Androids' WorkManager has been around for a while. However my own expectations about it were a little higher. I wished that the WorkManager could fire events at exact timing. But since it was made to respect doze mode, I should respect that too. That means that if the phone is idle, the WorkManager wont run. 

Lets explore my case: 

Once the user logs into my app, I need to schedule 5 alarms for him. But the alarm times come from a server and not locally. So basically I need some background work to do: 
Sync timing -> Save to database -> Schedule Alarms. (btw on the time alarm will fire, it's just a notification to be shown)
Now this has to happen every day at 00:01. The data coming from the server hold the alarm timings for a year, but certainly I can't schedule alarms from current moment till the end of the year. It would drain phones battery. So I need to start a WorkManager to save data to my local Room database and after that I need to repeatedly  schedule alarms ev…