Search in sources :

Example 1 with GithubService

use of uk.ivanc.archimvvm.model.GithubService in project archi by ivacf.

the class MainViewModelTest method setUp.

@Before
public void setUp() {
    githubService = mock(GithubService.class);
    dataListener = mock(MainViewModel.DataListener.class);
    application = (ArchiApplication) RuntimeEnvironment.application;
    // Mock the retrofit service so we don't call the API directly
    application.setGithubService(githubService);
    // Change the default subscribe schedulers so all observables
    // will now run on the same thread
    application.setDefaultSubscribeScheduler(Schedulers.immediate());
    mainViewModel = new MainViewModel(application, dataListener);
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) MainViewModel(uk.ivanc.archimvvm.viewmodel.MainViewModel) Before(org.junit.Before)

Example 2 with GithubService

use of uk.ivanc.archimvvm.model.GithubService in project archi by ivacf.

the class RepositoryViewModel method loadFullUser.

private void loadFullUser(String url) {
    ArchiApplication application = ArchiApplication.get(context);
    GithubService githubService = application.getGithubService();
    subscription = githubService.userFromUrl(url).observeOn(AndroidSchedulers.mainThread()).subscribeOn(application.defaultSubscribeScheduler()).subscribe(new Action1<User>() {

        @Override
        public void call(User user) {
            Log.i(TAG, "Full user data loaded " + user);
            ownerName.set(user.name);
            ownerEmail.set(user.email);
            ownerLocation.set(user.location);
            ownerEmailVisibility.set(user.hasEmail() ? View.VISIBLE : View.GONE);
            ownerLocationVisibility.set(user.hasLocation() ? View.VISIBLE : View.GONE);
            ownerLayoutVisibility.set(View.VISIBLE);
        }
    });
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) Action1(rx.functions.Action1) User(uk.ivanc.archimvvm.model.User) ArchiApplication(uk.ivanc.archimvvm.ArchiApplication)

Example 3 with GithubService

use of uk.ivanc.archimvvm.model.GithubService in project archi by ivacf.

the class MainViewModel method loadGithubRepos.

private void loadGithubRepos(String username) {
    progressVisibility.set(View.VISIBLE);
    recyclerViewVisibility.set(View.INVISIBLE);
    infoMessageVisibility.set(View.INVISIBLE);
    if (subscription != null && !subscription.isUnsubscribed())
        subscription.unsubscribe();
    ArchiApplication application = ArchiApplication.get(context);
    GithubService githubService = application.getGithubService();
    subscription = githubService.publicRepositories(username).observeOn(AndroidSchedulers.mainThread()).subscribeOn(application.defaultSubscribeScheduler()).subscribe(new Subscriber<List<Repository>>() {

        @Override
        public void onCompleted() {
            if (dataListener != null)
                dataListener.onRepositoriesChanged(repositories);
            progressVisibility.set(View.INVISIBLE);
            if (!repositories.isEmpty()) {
                recyclerViewVisibility.set(View.VISIBLE);
            } else {
                infoMessage.set(context.getString(R.string.text_empty_repos));
                infoMessageVisibility.set(View.VISIBLE);
            }
        }

        @Override
        public void onError(Throwable error) {
            Log.e(TAG, "Error loading GitHub repos ", error);
            progressVisibility.set(View.INVISIBLE);
            if (isHttp404(error)) {
                infoMessage.set(context.getString(R.string.error_username_not_found));
            } else {
                infoMessage.set(context.getString(R.string.error_loading_repos));
            }
            infoMessageVisibility.set(View.VISIBLE);
        }

        @Override
        public void onNext(List<Repository> repositories) {
            Log.i(TAG, "Repos loaded " + repositories);
            MainViewModel.this.repositories = repositories;
        }
    });
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) Repository(uk.ivanc.archimvvm.model.Repository) ArchiApplication(uk.ivanc.archimvvm.ArchiApplication) Subscriber(rx.Subscriber) List(java.util.List)

Example 4 with GithubService

use of uk.ivanc.archimvvm.model.GithubService in project archi by ivacf.

the class RepositoryViewModelTest method setUp.

@Before
public void setUp() {
    githubService = mock(GithubService.class);
    application = (ArchiApplication) RuntimeEnvironment.application;
    // Mock the retrofit service so we don't call the API directly
    application.setGithubService(githubService);
    // Change the default subscribe schedulers so all observables
    // will now run on the same thread
    application.setDefaultSubscribeScheduler(Schedulers.immediate());
    // Default behaviour is to load a mock owner when the view model is instantiated
    repository = MockModelFabric.newRepository("Repository");
    owner = MockModelFabric.newUser("owner");
    when(githubService.userFromUrl(repository.owner.url)).thenReturn(Observable.just(owner));
    viewModel = new RepositoryViewModel(application, repository);
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) RepositoryViewModel(uk.ivanc.archimvvm.viewmodel.RepositoryViewModel) Before(org.junit.Before)

Aggregations

GithubService (uk.ivanc.archimvvm.model.GithubService)4 Before (org.junit.Before)2 ArchiApplication (uk.ivanc.archimvvm.ArchiApplication)2 List (java.util.List)1 Subscriber (rx.Subscriber)1 Action1 (rx.functions.Action1)1 Repository (uk.ivanc.archimvvm.model.Repository)1 User (uk.ivanc.archimvvm.model.User)1 MainViewModel (uk.ivanc.archimvvm.viewmodel.MainViewModel)1 RepositoryViewModel (uk.ivanc.archimvvm.viewmodel.RepositoryViewModel)1