Android Dagger 2.11 and Retrofit dynamic URLs - android

I’m working on a project that is based on the GithubBrowserSample demo application that’s on the Android Architecture Repo on Github.
So far, I was able to inject dependencies to my App, activities and fragments and ViewModels. Also I was able to add more Modules; right now I use two, one for Shared preference and the other a NetModule that has all the mumbo jumbo related to Retrofit/OkHttp.
But here’s the catch: currently the demos that I found that shows how to implement the Retrofit/OkHttp module has a static base URL, which doesn’t work for me since that information is available until the User provide it on the Login section…
Digging around I found that one solution is to create a Submodule (StackOverflow post) with a given Scope Instantiate the component inside my Activity/Fragment. But because I’m using Dagger 2.11 for Android to perform the injection, I have no idea how to do so…
Is there an example that I can look around, or should I give up on this path and take the OkHttp interceptor to change the URL?
Thanks in advance.

Related

Network module on a multi-module project android

I'm trying to implement this multi-module design on my app, the thing is that I'm thinking about create an "api" or "network" module where I create the retrofit instance (I'm using Dagger2, but it doesn't matter, if you have an example or pseudo with any other DI framework feel free).
Problem?
I've been working on an app that I had this similar architecture but it ended up with a big module because I had organised the instance of a retrofit and then inside of it a sub-packages with each feature where I have the services and the responses.
This helped me with a problem that I had for instance, use the same service on a different features.
Example
Home feature I need to call the Feature1Service to show X
Feature1 need to call Feature1Service to show more stuff
I've thought with a solution that instead of creating the api or network module create every service inside of that feature, but doing this is a problem.
So I'd like to listen options or if you have worked with that app with modules how did you do that?
Short question
How do you use on multi-module project where you share services between features? (Retrofit)

Architecture for a Custom View library Android

I am building a library that every 30 seconds displays a question (obtained from a REST Api) and allows the user to select one of the possible answers.
Also, I need to make use of that library in an app, displaying a video underneath the question.
Desired result
All the business and UI logic should be handled in the library.
Does it make sense to use an MVVM approach, with repository pattern in the library?
With this package structure?
Possible package structure
You're creating the library, which is totally different context and way of creating the codebase comparing to the app.
First of all you need to make decisions about the dependencies. Of course it is super easy to put retrofit, mvvm and other dependencies into the app and manage them further.
With library it's not that easy. What I would expect from library in the first place is to have as little dependencies as possible. If you're going to provide aar file, then the developer would have to include those dependencies in the project. On the other hand, if you're publishing the library to maven repo, the gradle will take care of the library's dependencies. However the biggest challenge here is maintaining the library and resolving conflicts in actual app that uses the library. It can lead to frustration and eventually someone can throw your library away, which is the worst thing from library creator's perspective.
What I would suggest for this small library (calling api and providing data to custom view) is to:
Use only OkHttp without retrofit to provide the data from the REST api.
If it's the case for the library, create some abstraction over the OkHttp client with public functions/methods so the developers can get the data by themselves. Cool approach for creating this kind of interface is to use Fluent Interface (for example if you want the client of the library to put their access token/secret key for the REST api to detect who is calling the api, how much, limit their requests etc)
If you want to also provide the Custom View I would suggest to think twice about separating the REST client and Custom View itself and let the developer put the data by themselves. On the other hand if you want to handle it by yourself, you have to consider error handling, state management, lifecycle callbacks for cancelling the requests etc. You can provide some Listener interfaces for the developers so they know what is going on in the custom view.
The way you structure the library is up to you, if you have only one dependency (the rest client) just pass the objects via constructor so you won't need to add another dependency. Or just simply use ServiceLocator pattern, however it should also be connected to the Application/Activity itself when setting up the library so the objects are destroyed properly

Injecting dependency inisde Worker from other module

I have been struggling with providing dependency on Android workers using the dagger2. I have followed numerous blogs about how I can achieve it but I somehow have missed something that I am not able to build my dependency graph with it.
The following is the git repo for the complete source code https://github.com/youranshul/Dagger_worker
The issue I am facing is that how can I provide the dependency which is a part of the activity module but the custom work factory is the part of AppModule.
EDIT 1:
The link github.com/google/iosched provided by #Yavor is a little difficult for me to grab the things keeping my basics in dagger in mind. I have actually looked into the [Dagger from google link][4] and built some understanding of how things work with subcomponents. I have implemented the learning into the mentioned git project and found that I could provide all the dependencies to my worker class. I would like to have a review of my changes and know if I need to improve further on it.

How to use dagger library in android library project?

I want to use dagger library to android library project. I also fallows How to use dagger in a android library project but does not get proper implementation. Anyone has demo or any Ideas about it ? I also want these library classes extends in project for some changes.
I don't know what the library is, but I would assume most of the code doesn't contain classes with uncontrolled lifecycle (activities, services, fragments, etc.).
If it is true then just use constructors to pass your dependencies.
If it is not then, you have to make a decision who is going to hold knowledge about dependencies graph. A usual solution that context is castable to objects graph knowledge, or it know how to get it. Usually, classes with uncontrolled lifecycle have access to the context

Creating test dependencies when using Dagger2

While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this:
Dagger 2 doesn't support overrides. Modules that
override for simple testing fakes can create
a subclass of the module to emulate that behaviour.
Modules that use overrides and rely on dependency injection
should be decomposed so that the overridden modules are instead
represented as a choice between two modules.
I don't understand how I would set up such a configuration on Android, anyone can explain?
This is currently impossible with Dagger 2 (as of v2.0.0) without some workarounds. You can read about it here.
I've proposed one workaround but this requires changes to the production code.
In short:
provide additional setter for #Component (e.g. in Android setter in Application class)
test component must extend the production component
For more information please check both links. Hope this issue will be addressed in future versions of Dagger 2.

Categories

Resources