项目作者: GhoudanAyoub

项目描述 :
Update Weather Data From the Server Using MVVM LIVEDATA RETROFIT RxJava daggerHilt
高级语言: Java
项目地址: git://github.com/GhoudanAyoub/MVVM-LIVEDATA-RETROFIT-RXJAVA_HILT.git


ModelView View Model with LiveData, RxJava Using Retrofit Network Call

How You can use mvvm for RxJava LiveData and retofit its a piece of cake follow these steps hope this will help you.

Dependency

Android Studio:build.gradle(module app)

  1. implementation "io.reactivex.rxjava2:rxjava:2.2.7"
  2. implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
  3. implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
  4. implementation "com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2"
  5. implementation "com.squareup.retrofit2:retrofit:2.6.2"
  6. implementation "com.squareup.retrofit2:converter-gson:2.4.0"
  7. implementation 'com.android.support:recyclerview-v7:29.0.0'
  8. implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
  9. // Hilt
  10. implementation "com.google.dagger:hilt-android:2.28-alpha"
  11. annotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
  12. implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
  13. annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha02'

Android Studio:build.gradle(Project):

  1. buildscript {
  2. repositories {
  3. google()
  4. jcenter()
  5. }
  6. dependencies {
  7. classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
  8. }
  9. }
  10. allprojects {
  11. repositories {
  12. google()
  13. jcenter()
  14. }
  15. }

Usage example

We Need

  1. Connection ( For API Interface Estrablishment)
  2. API Interface ( For Server-Client Networking)
  3. Models (Data Structure that will show which type of data should repository product and structure )
  4. UI
    -> Viewmodel (From Where User Will See the Data)
    -> view (From Where User Will See the Data)

Development - 1st Connection

For Any Network We need Connection : Here You can make Connection .

  1. public class ClientAPI {
  2. private static ClientAPI clientAPI;
  3. private APISettings apiSettings;
  4. public ClientAPI() {
  5. String base_url = "https://dataservice.accuweather.com/forecasts/v1/daily/5day/";
  6. Retrofit retrofit = new Retrofit.Builder()
  7. .baseUrl(base_url)
  8. .addConverterFactory(GsonConverterFactory.create())
  9. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  10. .build();
  11. apiSettings = retrofit.create(APISettings.class);
  12. }
  13. public static ClientAPI getClientAPI() {
  14. if (clientAPI == null){
  15. clientAPI= new ClientAPI();
  16. }
  17. return clientAPI;
  18. }
  19. public Single<DailyForecasts> getTemps(){
  20. return apiSettings.getTemp();
  21. }
  22. }

Development - 2nd API Interface

We Need API Interface to END to END Point Connection and Method

  1. public interface APISettings {
  2. @GET("2606235?apikey=gJAkBa36Nt1cYt5LesFNER34ekTp5bUR")
  3. Single<DailyForecasts> getTemp();
  4. }

Development - 3rd Model

We Need To Define What type of Model Should ViewModel Follow to Produce data

  1. public class DailyForecasts {
  2. @SerializedName("DailyForecasts")
  3. private List<Temperature> temperatureList = null;
  4. public DailyForecasts() { }
  5. public DailyForecasts(List<Temperature> temperatureList) {
  6. this.temperatureList = temperatureList;
  7. }
  8. public List<Temperature> getTemperatureList() {
  9. return temperatureList;
  10. }
  11. public void setTemperatureList(List<Temperature> temperatureList) {
  12. this.temperatureList = temperatureList;
  13. }
  14. }
  15. /****************************/
  16. public class Temperature {
  17. @SerializedName("Date")
  18. private String Date;
  19. @SerializedName("EpochDate")
  20. private String EpochDate;
  21. @SerializedName("MobileLink")
  22. private String MobileLink;
  23. @SerializedName("Link")
  24. private String Link;
  25. public Temperature() { }
  26. public Temperature(String date, String epochDate, String mobileLink, String link) {
  27. Date = date;
  28. EpochDate = epochDate;
  29. MobileLink = mobileLink;
  30. Link = link;
  31. }
  32. public String getDate() {
  33. return Date;
  34. }
  35. public void setDate(String date) {
  36. Date = date;
  37. }
  38. public String getEpochDate() {
  39. return EpochDate;
  40. }
  41. public void setEpochDate(String epochDate) {
  42. EpochDate = epochDate;
  43. }
  44. public String getMobileLink() {
  45. return MobileLink;
  46. }
  47. public void setMobileLink(String mobileLink) {
  48. MobileLink = mobileLink;
  49. }
  50. public String getLink() {
  51. return Link;
  52. }
  53. public void setLink(String link) {
  54. Link = link;
  55. }
  56. }

Development - 4th UI viewModel

How viewModel Will Product the data From The Server and How it will Supply to the View

  1. public class PostViewModel extends ViewModel {
  2. public MutableLiveData<List<Temperature>> liveData= new MutableLiveData<>() ;
  3. CompositeDisposable disposable= new CompositeDisposable();
  4. public LiveData<List<Temperature>> getLiveData(){
  5. if (liveData==null)
  6. liveData = new MutableLiveData<>();
  7. return liveData;
  8. }
  9. public void getTempS(){
  10. Single<DailyForecasts> dailyForecastsSingle = ClientAPI.getClientAPI().getTemps()
  11. .subscribeOn(Schedulers.newThread())
  12. .observeOn(AndroidSchedulers.mainThread());
  13. dailyForecastsSingle.subscribe(new SingleObserver<DailyForecasts>() {
  14. @Override
  15. public void onSubscribe(@NonNull Disposable d) {
  16. disposable.add(d);
  17. }
  18. @Override
  19. public void onSuccess(@NonNull DailyForecasts dailyForecasts) {
  20. liveData.setValue(dailyForecasts.getTemperatureList());
  21. Log.e("Temps"," "+dailyForecasts.getTemperatureList().get(1).getDate());
  22. }
  23. @Override
  24. public void onError(@NonNull Throwable e) {
  25. Log.e("Temps",e.getMessage());
  26. }
  27. });
  28. }
  29. @Override
  30. protected void finalize() throws Throwable {
  31. super.finalize();
  32. disposable.clear();
  33. }
  34. }

Development - 5th User End

How Will Show the Data , UI Update and show

  1. public class MainActivity extends AppCompatActivity {
  2. PostViewModel postViewModel;
  3. RecyclerView recyclerView;
  4. PostAdapter postAdapter;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main3);
  9. postViewModel = ViewModelProviders.of(this).get(PostViewModel.class);
  10. postViewModel.getTempS();
  11. recyclerView = findViewById(R.id.r1);
  12. postAdapter = new PostAdapter();
  13. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  14. recyclerView.setAdapter(postAdapter);
  15. postViewModel.getLiveData().observe(this, new Observer<List<Temperature>>() {
  16. @Override
  17. public void onChanged(List<Temperature> temperatures) {
  18. postAdapter.setList(temperatures);
  19. }
  20. });
  21. }
  22. }

Meta

Ayoub GHOUDAN – [@ayoub_ghoudan] – ayoubghoudanos@gmail.com