Skip to main content

Posts

Showing posts from June, 2019

Why the ViewModel is now a must

Inspired by Lyla Fujiwars latest post, ViewModels with Saved State, Jetpack Navigation, Data Binding and Coroutines, I decided to list some of the reasons why the ViewModel is strongly suggested to be used on the Android app.


1. Views are independent

The ViewModel does fully respect the Single Responsibility Principle, leaving the Activity/Fragment do its thing. When the ViewModel is included,  views are passive and just wait for changes without actually doing anything. This is done through LiveData or RxJava, which provide observable fields: 


The MutableLiveData is the variable we are prepared that it might take changes of the value asigned during the flow. And giving its value to the live data means that whatever happens that variable value LiveData is going to observe it. 
After that, the Fragment/Activity just waits for that variable: 

If you notice, the view does nothing except handles the child views.

2. Presenter destroyed

I don't want to be rude with people using the MVP arc…

What I learned from unit testing

Testing, testing testing. I was getting inside the "Fear of getting behind" every time I heard that word. So I decided to react quickly. I knew nothing about testing and this is my experience getting my hands dirty with it. Please feel free to correct anywhere I'm wrong. This is a reason why I'm writing blogs.

So what the hell is testing?

Testing is just a piece of code where you invoke your written production code and check its' behavior.

So lets get more specific. In android there is 3 kind of tests:

Unit testing:  tests that validate your app's behavior one class at a timeIntegration testing: tests that validate either interactions between levels of the stack within a module, or interactions between related modulesEnd - to end tests: tests that validate user journeys spanning multiple modules of your appDefinitions from the official Android documentation
This article is about the first point , but you shouldn't ignore any of them.  The other 2 kinds, …

How to setup Android Navigation Component with Drawer

How to setup Android Navigation Component with Drawer . It’s been a wile since Android team launched the new Navigation style in Android . For me , it looks amazingly cool and I thought I should… It’s been a wile since Android team launched the new Navigation style in Android. For me , it looks amazingly cool and I thought I should share it with you.Let’s start :First of all , let’s start with setting up the navigation dependencies :implementation "androidx.navigation:navigation-fragment-ktx:$rootProject.navigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$rootProject.navigationVersion"where navigationVersion is stored in the build.gradle project module:ext {
navigationVersion = "2.0.0"
...
}On with the show : 
In your res menu , define a new directory named navigation.After that, create a nav_graph:<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.…

Room, the basics

Official Stavro Xhardhas page · Post Room, the basics Posting as Stavro Xhardha Room, the basics It has been a while since Roompersistence library is out. It was about time, the SQLiteimplementation was awful, long work and sometimes… It has been a while since Room persistence library is out. It was about time, the SQLite implementation was awful, long work and sometimes confusing.
Therefore, the Android team built Room:The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.According to the above statement from the official documentation, Room is nothing more than a “refactored and improved” SQLite library.The setup:The third dependency is not necessary for room but I am using Room with CoroutinesThe Entity:After you have imported the dependencies, you may start constructing your database. What your database needs at first is a table (or on Room language the Entity):This is a ta…

Using Idling Resources in your Espresso Tests

Using Idling Resources in your Espresso Tests Testing is wow. And UI testing in Android is awesome. Espresso is a UI testing framework for Android. I can say that it was pretty easy and… Testing is wow. And UI testing in Android is awesome. Espresso is a UI testing framework for Android. I can say that it was pretty easy and you can get used to it in a couple of hours. It’s just like structured English, but in Java/Kotlin.A simple example from the documentation when using the Espresso framework is this:@Test
fun greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"))
onView(withId(R.id.greet_button)).perform(click())
onView(withText("Hello Steve!")).check(matches(isDisplayed()))
}Note: These tests run under the androidtest package.But sometimes (or mostly) we are not working on the main thread. We have lots of code that blocks our UI and force us to run on another thread. Since Espresso test is quick and straightforward it doesn’t rea…

Retrofit met Coroutines

Retrofit met Coroutines Finally the latest version of Retrofit (2.6.0) has got out. While it was already really easy to use and so much fun, Retrofit is now easy… Finally the latest version of Retrofit (2.6.0) has got out. While it was already really easy to use and so much fun, Retrofit is now easy, fun and shorter to write. Let’s deal with a refactor scenario from the previous version:Version 2.5.0:Obviously we have to return the same type until we await() for that response:Notice we are returning the Deferredtype which holds our response. We won’t stop on what a Deffered is apart from sayin that it’s just a Future.And when we want to get that response:Notice we are calling the await() method in order to wait until our asynchronous operation has finished.Well, no need for that anymore!Version 2.6.0:Drop the Deferred type and mark your methods as suspend . Again we wont stop on what suspend methods are apart from saying that it is a way to tell my program that this method is going to…

Usage of the ViewModelScope

Usage of the ViewModelScope Based on my last blog post about easy implementation on Kotlin Coroutines,we were also introduced with the CoroutineContext .That was not… Based on my last post about easy implementation on Kotlin Coroutines, we were also introduced with the CoroutineContext .That was not too hard to understand, but I think there is a lot of code inside a ViewModel . First you needed a Job() , than you needed a CoroutineScope and in the instantiation you needed to specify one default Dispatcher . After that, you needed to keep track of the CoroutineContext corresponding to the ViewModel/Activity/Fragment lifecycle.Note: If you are not using a ViewModel skip this post entirely.Current code:This is not very long code. However, think of every ViewModel you will be having inside your project. So, you will need some refactoring,or not.
This approach looks nice but has some unnecessary code.Luckily there is another way of dealing with the CoroutineContext , thanks to Google. There…

Annotation processor: Say less, mean more

Annotation processor: Say less, mean more I’ve always been curious on what is behind an annotation. As much as they made my angry, believe me they are so fun. This is my experience… I’ve always been curious on what is behind an annotation. As much as they made my angry, believe me they are so fun. This is my experience as a beginner on the Annotation Processing. I will give my own definition for an annotation: An annotation is just a way to mark a class, field, another annotation, method etc. Why? It just tells that the marked component has a special attribute. But how do you handle it? The way to handle an Annotation is through generating code at compile time. This has always intrigued me. Why? This depends on the use case. Have you ever thought how Dagger2 knows what dependencies you are using? Or perhaps how Butterknife knows how to bind views or set an onClickListener? Yep, generating code at compile time. The process of generating code at compile time to handle the annotations is…