private void loadFullUser(String url) { ArchiApplication application = ArchiApplication.get(context); 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); } }); }
public RepositoryViewModel(Context context, final Repository repository) { ArchiApplication.get(context).getGithubComponent().inject(this); this.repository = repository; this.context = context; this.ownerName = new ObservableField<>(); this.ownerEmail = new ObservableField<>(); this.ownerLocation = new ObservableField<>(); this.ownerLayoutVisibility = new ObservableInt(View.INVISIBLE); this.ownerEmailVisibility = new ObservableInt(View.VISIBLE); this.ownerLocationVisibility = new ObservableInt(View.VISIBLE); // Trigger loading the rest of the user data as soon as the view model is created. // It's odd having to trigger this from here. Cases where accessing to the data model // needs to happen because of a change in the Activity/Fragment lifecycle // (i.e. an activity created) don't work very well with this MVVM pattern. // It also makes this class more difficult to test. Hopefully a better solution will be found loadFullUser(repository.owner.url); }