Search code examples
androidretrofit2android-architecture-components

How to notify activity when repository class gets the data?


I'm new in android architecture component. this is my code , i'm at the point that I don't know how to notify my activity and get the results back

these are my codes:

Activity:

 private void iniViewModels() {
        Observer<List<User>>usersObserver=new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable List<User> users) {
                Log.v("this","LiveData: ");
                for (int i=0;i<users.size();i++){
                    Log.v("this",users.get(i).getName());
                }
            }
        };

        mViewModel = ViewModelProviders.of(this)//of-->name of act or fragment
                .get(AcActivityViewModel.class);///get -->the name of viewModelClass

        mViewModel.mUsers.observe(this,usersObserver);
    }

this is my viewModel Class:

public class IpStaticViewModel extends AndroidViewModel {
public LiveData<List<Ipe>> ips;
private AppRepository repository;

public IpStaticViewModel(@NonNull Application application) {
    super(application);
    repository=AppRepository.getInstance(application.getApplicationContext());
}

public void getIpStatics() {
    repository.getStaticIps();
}
}

this is my repository class:

public class AppRepository {
private static  AppRepository ourInstance ;
private Context context;
private IpStaticInterface ipInterface;

public static AppRepository getInstance(Context context) {
    if (ourInstance == null) {
        ourInstance=new AppRepository(context);
    }
    return ourInstance;
}

private AppRepository(Context context) {
    this.context=context;
}

public void getStaticIps() {
    ipInterface= ApiConnection.getClient().create(IpStaticInterface.class);
    ipInterface.getIpes()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new SingleObserver<IpStaticList>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(IpStaticList ipStaticList) {
                    List<Ipe>ips=ipStaticList.getIpes();
                }

                @Override
                public void onError(Throwable e) {
                    Log.v("this","Eror "+ e.getMessage());
                }
            });
}
}

I'm using retrofit for fetching the data ,it fetch the data successfully but I don't know how to notify my activity

can you help me?


Solution

  • Have a MutableLiveData

    final MutableLiveData<List<Ipe>> data = new MutableLiveData<>();
    

    In onSucess

     public MutableLiveData<List<Ipe>> getStaticIps() {
       ipInterface= ApiConnection.getClient().create(IpStaticInterface.class);
       ipInterface.getIpes()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new SingleObserver<IpStaticList>() {
                @Override
                public void onSubscribe(Disposable d) {
    
                }
    
                @Override
                public void onSuccess(IpStaticList ipStaticList) {
                    List<Ipe>ips=ipStaticList.getIpes();
                    data.setValue(ips);
                }
    
                @Override
                public void onError(Throwable e) {
                    Log.v("this","Eror "+ e.getMessage());
                }
            });
            return data;
      }
    

    In repository expose this to viewmodel

     public LiveData<List<Ipe>> getIpStatics() {
     return repository.getStaticIps();
     }
    

    In Activity you observe the livedata

     IpStaticViewModel viewmodel = ViewModelProviders.of(this
                .get(IpStaticViewModel.class)
      viewModel.getIpStatics().observe(this, new Observer<List<Ipe>>() {
            @Override
            public void onChanged(@Nullable List<Ipe> ipes) {
                if (ipes != null) {
                     // dosomething
                }
            }
        });
    

    If you want to generalize your response in case you have a error or something have a look at https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/Resource.kt