Hilt Data Injection Retrofit View Module Live Data demo | Example
First you don't like reading don't worry , I have a video for you with full implementations check that here
we'll implement the needed dependencies
Add this in your project level gradle.build
dependencies { | |
classpath "com.android.tools.build:gradle:4.1.2" | |
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.33-beta' | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} |
Add this in your app level gradle.build
// Hilt dependencies | |
implementation 'com.google.dagger:hilt-android:2.33-beta' | |
annotationProcessor 'com.google.dagger:hilt-android-compiler:2.33-beta' | |
// Retrofit dependencies | |
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' | |
// glider dependencies | |
implementation 'com.github.bumptech.glide:glide:4.12.0' | |
// anderoid lifecycle extensions | |
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' |
For Hilt you need to create a application class, application class annotate with the @HiltAndroidApp
@HiltAndroidApp | |
public class HiltApplication extends Application { | |
} |
Then we have a Github Api for for demo use , in Data injection module class we need to annotate with the @Module for the class and for Hilt we need one more annotations @InstallIn(XXX.class)
@Module | |
@InstallIn(SingletonComponent.class) | |
public class DataInjectModule { | |
String baseUrl = "https://api.github.com/search/"; //repositories?q=india | |
@Singleton | |
@Provides | |
public ServiceInterface getServiceInterface(Retrofit retrofit){ | |
return retrofit.create(ServiceInterface.class); | |
} | |
@Singleton | |
@Provides | |
public Retrofit getInstance(){ | |
return new Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
} | |
} |
you want to create the module classes for the JSON response and api retrofit service class if you have any doubt check the video or git hub project, use the ServiceRepocitory for make api calls
public class ServiceRepocitory { | |
private ServiceInterface serviceInterface; | |
public ServiceRepocitory(ServiceInterface serviceInterface) { | |
this.serviceInterface = serviceInterface; | |
} | |
public void makeCalls(String query, MutableLiveData<List<ItemsItem>> liveData){ | |
Call<Response> call = serviceInterface.getAllData(query); | |
call.enqueue(new Callback<Response>() { | |
@Override | |
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) { | |
if (response.isSuccessful()){ | |
liveData.postValue(response.body().getItems()); | |
} | |
} | |
@Override | |
public void onFailure(Call<Response> call, Throwable t) { | |
} | |
}); | |
} | |
} |
Then create the view module class for the main activity class need to annotate the View module with the @HiltViewModule
@HiltViewModel | |
public class ActivityViewModel extends androidx.lifecycle.ViewModel { | |
MutableLiveData<List<ItemsItem>> liveData; | |
@Inject | |
ServiceInterface serviceInterface; | |
@Inject | |
public ActivityViewModel(){ | |
liveData = new MutableLiveData<>(); | |
} | |
public MutableLiveData<List<ItemsItem>> getLiveData() { | |
return liveData; | |
} | |
public void makeApiCall(){ | |
ServiceRepocitory repocitory = new ServiceRepocitory(serviceInterface); | |
repocitory.makeCalls("us",liveData); | |
} | |
} |
Create the adapter for the recyclerview with the needed layouts it will be a normal recyclerview adapter then initialize the recyclerview and live data in the main activity , Activity your using the Hilt Data Injection need to annotate with the @AndroidEntryPoint
@AndroidEntryPoint | |
public class MainActivity extends AppCompatActivity { | |
RecyclerviewAdapter recyclerviewAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initRecyclerview(); | |
initData(); | |
} | |
private void initRecyclerview(){ | |
RecyclerView recyclerView = findViewById(R.id.recyclerview); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
recyclerviewAdapter = new RecyclerviewAdapter(); | |
recyclerView.setAdapter(recyclerviewAdapter); | |
} | |
private void initData(){ | |
ActivityViewModel activityViewModel = new ViewModelProvider(this).get(ActivityViewModel.class); | |
activityViewModel.getLiveData().observe(this, new Observer<List<ItemsItem>>() { | |
@Override | |
public void onChanged(List<ItemsItem> itemsItems) { | |
if (itemsItems != null){ | |
recyclerviewAdapter.setItemList(itemsItems); | |
}else { | |
Toast.makeText(MainActivity.this, "No Data", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
activityViewModel.makeApiCall(); | |
} | |
} |
This is just for your understanding if you have any doubts or any misunderstanding check the video , or you can download the full project from the github check here
Comments
Post a Comment