Java Code Examples for androidx.lifecycle.MutableLiveData#setValue()
The following examples show how to use
androidx.lifecycle.MutableLiveData#setValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoodleRepository.java From ETSMobile-Android2 with Apache License 2.0 | 6 votes |
private LiveData<RemoteResource<MoodleToken>> getToken() { String tokenStr = ApplicationManager.userCredentials.getMoodleToken(); MutableLiveData<RemoteResource<MoodleToken>> token = new MutableLiveData<>(); token.setValue(RemoteResource.loading(new MoodleToken(tokenStr))); LiveData<ApiResponse<MoodleToken>> apiResponseLiveData = moodleWebService.getToken(ApplicationManager.userCredentials.getUsername(), ApplicationManager.userCredentials.getPassword()); apiResponseLiveData.observeForever(new Observer<ApiResponse<MoodleToken>>() { @Override public void onChanged(@Nullable ApiResponse<MoodleToken> moodleTokenApiResponse) { if (moodleTokenApiResponse != null) { MoodleToken moodleToken = moodleTokenApiResponse.body; if (moodleTokenApiResponse.isSuccessful() && moodleToken != null && moodleToken.getToken() != null && !moodleToken.getToken().isEmpty()) { ApplicationManager.userCredentials.setMoodleToken(moodleToken.getToken()); token.setValue(RemoteResource.success(moodleToken)); } else token.setValue(RemoteResource.error(context.getString(R.string.moodle_error_cant_get_token), moodleTokenApiResponse.body)); apiResponseLiveData.removeObserver(this); } } }); return token; }
Example 2
Source File: MoodleViewModelTest.java From ETSMobile-Android2 with Apache License 2.0 | 6 votes |
@Test public void getCourses() { // Prepare LiveData containing the courses MutableLiveData<RemoteResource<List<MoodleCourse>>> liveData = new MutableLiveData<>(); when(repository.getCourses()).thenReturn(liveData); // Prepare observer Observer<RemoteResource<List<MoodleCourse>>> observer = mock(Observer.class); viewModel.getCourses().observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); // Set a value and check if onChanged has been called RemoteResource<List<MoodleCourse>> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); // Set another value and check if onChanged has been called again remoteRes = RemoteResource.success(new MoodleCourses()); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); }
Example 3
Source File: MoodleViewModelTest.java From ETSMobile-Android2 with Apache License 2.0 | 6 votes |
@Test public void getAssignmentCourses() { // Prepare LiveData MutableLiveData<RemoteResource<List<MoodleAssignmentCourse>>> liveData = new MutableLiveData<>(); when(repository.getAssignmentCourses()).thenReturn(liveData); // Prepare Observer Observer<RemoteResource<List<MoodleAssignmentCourse>>> observer = mock(Observer.class); viewModel.getAssignmentCourses().observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); // Set a value and check if onChanged was called RemoteResource<List<MoodleAssignmentCourse>> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); // Set another value and check if onChanged was called List<MoodleAssignmentCourse> list = new ArrayList<>(); remoteRes = RemoteResource.success(list); liveData.setValue(remoteRes); verify(observer).onChanged(any(RemoteResource.class)); }
Example 4
Source File: MoodleViewModelTest.java From ETSMobile-Android2 with Apache License 2.0 | 6 votes |
@Test public void getAssignmentSubmission() { // Prepare LiveData MutableLiveData<RemoteResource<MoodleAssignmentSubmission>> liveData = new MutableLiveData<>(); when(repository.getAssignmentSubmission(anyInt())).thenReturn(liveData); // Prepare Observer Observer<RemoteResource<MoodleAssignmentSubmission>> observer = mock(Observer.class); viewModel.getAssignmentSubmission(anyInt()).observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); // Set a value and check if onChanged has been called RemoteResource<MoodleAssignmentSubmission> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); // Set another value and check if onChanged has been called again remoteRes = RemoteResource.success(new MoodleAssignmentSubmission()); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); }
Example 5
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@Test public void no_value_after_just_a() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataA.setValue("Hello, "); assertNoValue(combined); }
Example 6
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@Test public void no_value_after_just_b() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataB.setValue("World!"); assertNoValue(combined); }
Example 7
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@Test public void combined_value_after_a_and_b() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataA.setValue("Hello, "); liveDataB.setValue("World!"); assertEquals("Hello, World!", getValue(combined)); }
Example 8
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@Test public void on_update_a() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataA.setValue("Hello, "); liveDataB.setValue("World!"); assertEquals("Hello, World!", getValue(combined)); liveDataA.setValue("Welcome, "); assertEquals("Welcome, World!", getValue(combined)); }
Example 9
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@Test public void on_update_b() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataA.setValue("Hello, "); liveDataB.setValue("World!"); assertEquals("Hello, World!", getValue(combined)); liveDataB.setValue("Joe!"); assertEquals("Hello, Joe!", getValue(combined)); }
Example 10
Source File: LoginViewModel.java From CloudReader with Apache License 2.0 | 4 votes |
public MutableLiveData<Boolean> register() { final MutableLiveData<Boolean> data = new MutableLiveData<>(); if (!verifyData()) { data.setValue(false); return data; } HttpClient.Builder.getWanAndroidServer().register(username.get(), password.get(), password.get()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<LoginBean>() { @Override public void onSubscribe(Disposable d) { addDisposable(d); } @Override public void onNext(LoginBean bean) { if (bean != null && bean.getData() != null) { // 存入数据库 Injection.get().addData(bean.getData()); UserUtil.handleLoginSuccess(); data.setValue(true); } else { if (bean != null) { ToastUtil.showToastLong(bean.getErrorMsg()); } data.setValue(false); } } @Override public void onError(Throwable e) { data.setValue(false); } @Override public void onComplete() { } }); return data; }
Example 11
Source File: LoginViewModel.java From CloudReader with Apache License 2.0 | 4 votes |
public MutableLiveData<Boolean> login() { final MutableLiveData<Boolean> data = new MutableLiveData<>(); if (!verifyData()) { data.setValue(false); return data; } HttpClient.Builder.getWanAndroidServer().login(username.get(), password.get()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<LoginBean>() { @Override public void onSubscribe(Disposable d) { addDisposable(d); } @Override public void onNext(LoginBean bean) { if (bean != null && bean.getData() != null) { Injection.get().addData(bean.getData()); UserUtil.handleLoginSuccess(); data.setValue(true); } else { if (bean != null) { ToastUtil.showToastLong(bean.getErrorMsg()); } data.setValue(false); } } @Override public void onError(Throwable e) { data.setValue(false); } @Override public void onComplete() { } }); return data; }
Example 12
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 3 votes |
@Test public void combined_same_instance() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataA, (a, b) -> a + b); liveDataA.setValue("Echo! "); assertEquals("Echo! Echo! ", getValue(combined)); }
Example 13
Source File: LiveDataUtilTest.java From mollyim-android with GNU General Public License v3.0 | 3 votes |
@Test public void on_a_set_before_combine() { MutableLiveData<String> liveDataA = new MutableLiveData<>(); MutableLiveData<String> liveDataB = new MutableLiveData<>(); liveDataA.setValue("Hello, "); LiveData<String> combined = LiveDataUtil.combineLatest(liveDataA, liveDataB, (a, b) -> a + b); liveDataB.setValue("World!"); assertEquals("Hello, World!", getValue(combined)); }