Now In Android with Koin — part 2 | by Arnaud Giuliani | Dec, 2022 | Videogames Tech

Now In Android is an open-source Android utility that covers Trendy Android Growth greatest practices. The mission is maintained by the Google Android crew.

I suggest to proceed our tour with the model constructed with the Koin dependency injection framework. It is a good time to refresh practices, from normal elements construction to extra superior instances.

Let’s make a walkthrough into the frequent core elements, giving us the important constructing blocks to allow us to write our options.

The screens of the NowInAndroid app are developed with Jetpack Compose and use repository & usecase elements:

  • Repository to entry information (community, database …)
  • Usecase to deal with enterprise logic

Let’s open the DataKoinModule.kt to see our Repository elements definitions:

Widespread Knowledge Elements

All of the repository elements are declared utilizing the singleOf key phrase plus a bind() part to specify the certain kind. This may create every one as a singleton occasion.

It’s follow to make use of the contains() operate to record explicitly any Koin module that might be wanted for any definition of the present module. This additionally expresses a powerful hyperlink, that can be utilized by the confirm() API to confirm our Koin configuration.

Elements from the Knowledge layer may be declared as singleton cases. The Knowledge layer is impartial of the UI layer: there isn’t a have to affiliate them to a lifecycle.

For Usecase area elements, we’ll see about them intimately within the part later.

Structure layers from

The primary information modules we’ll have a look at, are the database elements indaosKoinModule.

The mission is utilizing Room. To declare a Room database occasion, we have to create it with the Room.databaseBuilder() builder operate. This occasion might be registered as singleton and referred by DAO elements.

Beneath, the databaseKoinModule declares the definition of the database occasion:

Declaring Room Database

We merely use a single definition, adopted by a operate to be executed. Be aware right here that we use the androidContext() operate to retrieve the Android context from Koin.

Subsequent in daosKoinModule, is pretty simple to declare every DAO. Every of them is referenced from the NiaDatabase interface. We will reference every DAO as comply with. We use the get<NiaDatabase>() expression to retrieve our database occasion, and use it to name our DAO occasion as comply with:

Declaring DAOs

Every of these DAO is outlined with the single key phrase (singleton occasion). We embody the database definition module.

In Dagger Hilt, the philosophy stays the identical however it’s nonetheless verbose:

Hilt NiaDatabase Declaration
Hilt DAOs declaration

Let’s proceed with the DataStore elements within the following part.

On this half, we have to put together the creation of theDataStoreFactory occasion to learn offline information. Let’s open the dataStoreKoinModule to see these elements:

DataStore Module

We want a number of singletons right here:

  • UserPreferencesSerializer to be handed to our DataStore, to serialize information from UserPreferences
  • DataStoreFactory the DataStore occasion itself
  • NiaPreferencesDataSource Datasource element, which makes use of the DataStore to learn native information

On this module, we have to inject the Kotlin coroutines “default dispatcher”. This one is included in our module, and may be written like this:

Declaring Default Coroutines Dispatcher

Be aware that we are able to simply override such a definition in a take a look at setting by offering a brand new definition that may override the default one. When you’ve got a number of definitions of the identical kind, it’s also possible to add a qualifier.

The community module is an attention-grabbing case: we have to load completely different definitions of implementation relying on the flavour of the module. We’ve 2 flavors: demo (static demo content material) & prod (content material requested over the community).

Tips on how to dynamically use the proper taste implementation?

Let’s first write the networkKoinModulefile first. This module will embody baby implementation:

The community module

We will declare right here, all frequent definitions utilized by the included modules (just like the JSON serializer occasion).

The decision tocontains(networkFlavoredKoinModule) will load the appropriate Koin module. From there, let’s write the networkFlavoredKoinModule module for every taste. Naturally, the mission will hyperlink and compile the proper file.

The demo taste folder:

Demo Taste

The demo taste networkFlavoredKoinModule file, declaring a faux implementation:

Demo Community Module

The prod taste folder:

Prod taste

The prod taste networkFlavoredKoinModule file, declaring a Retrofit implementation:

Prod NiaNetworkDataSource implementation

Every implementation will present a NiaNetworkDataSource element. We don’t have to declare nor embody any Json definition, as Koin will discover it from the dad or mum module.

Now that we’ve got core elements to assist work with our information, we are able to have elements devoted to enterprise logic and permit us to reuse them on the UI layer.

Let’s open the domainKoinModule file to see our Usecases definitions:

Use-cases definitions

Every use-case element is outlined as a “manufacturing unit” right here. Why? We need to guarantee that we are going to have a “stateless” enterprise logic unit, and keep away from conserving in reminiscence something linked to the UI. These use-case elements are launching Coroutines Flows to hearken to incoming information updates. We don’t need to hold Movement reference between screens.

GetFollowableTopicsStream Usecase

The usage of factoryOf guarantee that we are going to recreate an occasion, every time we’ll ask for it and rubbish accumulate beforehand used cases (let the occasion be destroyed).

Lastly, one particular and vital element of this mission is the SyncWorker class, devoted to resyncing information to repositories. That is serving to get information for the “offline first” technique: we have a look at information domestically and remotely. We will show already-fetched information whereas asking for brand new information remotely, and keep away from displaying empty content material to the consumer.

As you see, this class is demanding nearly all our frequent elements:

SyncWorker Class — To assist sync information repositories

Let’s open the syncWorkerKoinModule file to declare our WorkManager. You might even see that we merely have to declare our element with workerOf key phrase, and that’s it:

Declaring Sync Employee

Don’t neglect to begin WorkManager Koin manufacturing unit on the Utility begin, with the workManagerFactory()operate:

WorkManager setup with Koin

The decision to Sync.initialize() asks to initialize the info sync for offline content material.

Don’t hesitate to examine the documentation for extra particulars:

We at the moment are able to inject the whole lot in a single Jetpack Compose composable operate. In our AuthorRoute display screen composable, we use the koinViewModel() operate to get the ViewModel.

injecting ViewModel in Compose

To declare a ViewModel element, we merely want the viewModelOf key phrase following by our class constructor:

Declaring AuthorViewModel

With such a key phrase, your ViewModel may be routinely injected with SavedStateHandle parameter in case you want.

AuthorViewModel class constructor

Now In Android with Koin — part 2 | by Arnaud Giuliani | Dec, 2022