Git Product home page Git Product logo

mvvm-livedata-retrofit-rxjava_hilt's Introduction

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)

    implementation "io.reactivex.rxjava2:rxjava:2.2.7"
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation "com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2"

    implementation "com.squareup.retrofit2:retrofit:2.6.2"
    implementation "com.squareup.retrofit2:converter-gson:2.4.0"
    implementation 'com.android.support:recyclerview-v7:29.0.0'
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    
    
    // Hilt
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    annotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
    annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha02'

Android Studio:build.gradle(Project):

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
       
    }
}

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 .

public class ClientAPI {

  private static ClientAPI clientAPI;
  private APISettings apiSettings;

  public ClientAPI() {
          String base_url = "https://dataservice.accuweather.com/forecasts/v1/daily/5day/";
      Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(base_url)
              .addConverterFactory(GsonConverterFactory.create())
              .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
              .build();
      apiSettings = retrofit.create(APISettings.class);
  }

  public static ClientAPI getClientAPI() {
      if (clientAPI == null){
          clientAPI= new ClientAPI();
      }
      return clientAPI;
  }

  public Single<DailyForecasts> getTemps(){
      return apiSettings.getTemp();
  }
}

Development - 2nd API Interface

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

public interface APISettings {

    @GET("2606235?apikey=gJAkBa36Nt1cYt5LesFNER34ekTp5bUR")
    Single<DailyForecasts> getTemp();
}

Development - 3rd Model

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

public class DailyForecasts {

    @SerializedName("DailyForecasts")
    private List<Temperature> temperatureList = null;

    public DailyForecasts() { }

    public DailyForecasts(List<Temperature> temperatureList) {
        this.temperatureList = temperatureList;
    }

    public List<Temperature> getTemperatureList() {
        return temperatureList;
    }

    public void setTemperatureList(List<Temperature> temperatureList) {
        this.temperatureList = temperatureList;
    }
}

/****************************/

public class Temperature {

    @SerializedName("Date")
    private String Date;
    @SerializedName("EpochDate")
    private String EpochDate;
    @SerializedName("MobileLink")
    private String MobileLink;
    @SerializedName("Link")
    private String Link;

    public Temperature() { }

    public Temperature(String date, String epochDate, String mobileLink, String link) {
        Date = date;
        EpochDate = epochDate;
        MobileLink = mobileLink;
        Link = link;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        Date = date;
    }

    public String getEpochDate() {
        return EpochDate;
    }

    public void setEpochDate(String epochDate) {
        EpochDate = epochDate;
    }

    public String getMobileLink() {
        return MobileLink;
    }

    public void setMobileLink(String mobileLink) {
        MobileLink = mobileLink;
    }

    public String getLink() {
        return Link;
    }

    public void setLink(String link) {
        Link = link;
    }
}

Development - 4th UI viewModel

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

public class PostViewModel extends ViewModel {
    public    MutableLiveData<List<Temperature>> liveData= new MutableLiveData<>() ;
    CompositeDisposable disposable= new CompositeDisposable();

    public LiveData<List<Temperature>> getLiveData(){
        if (liveData==null)
            liveData = new MutableLiveData<>();
        return liveData;
    }
    public void getTempS(){

        Single<DailyForecasts> dailyForecastsSingle = ClientAPI.getClientAPI().getTemps()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        dailyForecastsSingle.subscribe(new SingleObserver<DailyForecasts>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposable.add(d);
            }

            @Override
            public void onSuccess(@NonNull DailyForecasts dailyForecasts) {
                    liveData.setValue(dailyForecasts.getTemperatureList());
                    Log.e("Temps"," "+dailyForecasts.getTemperatureList().get(1).getDate());
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.e("Temps",e.getMessage());
            }
        });

    }
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        disposable.clear();
    }
}


Development - 5th User End

How Will Show the Data , UI Update and show

public class MainActivity extends AppCompatActivity {

    PostViewModel postViewModel;
    RecyclerView recyclerView;
    PostAdapter postAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        postViewModel = ViewModelProviders.of(this).get(PostViewModel.class);
        postViewModel.getTempS();

        recyclerView = findViewById(R.id.r1);
        postAdapter = new PostAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(postAdapter);

        postViewModel.getLiveData().observe(this, new Observer<List<Temperature>>() {
            @Override
            public void onChanged(List<Temperature> temperatures) {
                postAdapter.setList(temperatures);
            }
        });
    }
}


Meta

Ayoub GHOUDAN โ€“ [@ayoub_ghoudan] โ€“ [email protected]

mvvm-livedata-retrofit-rxjava_hilt's People

Contributors

ghoudanayoub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.