本文整理匯總了Java中android.arch.lifecycle.MutableLiveData.setValue方法的典型用法代碼示例。如果您正苦於以下問題:Java MutableLiveData.setValue方法的具體用法?Java MutableLiveData.setValue怎麽用?Java MutableLiveData.setValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.arch.lifecycle.MutableLiveData
的用法示例。
在下文中一共展示了MutableLiveData.setValue方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: goToNetwork
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void goToNetwork() {
MutableLiveData<User> dbData = new MutableLiveData<>();
when(userDao.findByLogin("foo")).thenReturn(dbData);
User user = TestUtil.createUser("foo");
LiveData<ApiResponse<User>> call = ApiUtil.successCall(user);
when(githubService.getUser("foo")).thenReturn(call);
Observer<Resource<User>> observer = mock(Observer.class);
repo.loadUser("foo").observeForever(observer);
verify(githubService, never()).getUser("foo");
MutableLiveData<User> updatedDbData = new MutableLiveData<>();
when(userDao.findByLogin("foo")).thenReturn(updatedDbData);
dbData.setValue(null);
verify(githubService).getUser("foo");
}
開發者ID:hbmartin,項目名稱:firebase-chat-android-architecture-components,代碼行數:17,代碼來源:UserRepositoryTest.java
示例2: sendResultToUI
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void sendResultToUI() {
MutableLiveData<Resource<User>> foo = new MutableLiveData<>();
MutableLiveData<Resource<User>> bar = new MutableLiveData<>();
when(userRepository.loadUser("foo")).thenReturn(foo);
when(userRepository.loadUser("bar")).thenReturn(bar);
Observer<Resource<User>> observer = mock(Observer.class);
userViewModel.getUser().observeForever(observer);
userViewModel.setLogin("foo");
verify(observer, never()).onChanged(any(Resource.class));
User fooUser = TestUtil.createUser("foo");
Resource<User> fooValue = Resource.success(fooUser);
foo.setValue(fooValue);
verify(observer).onChanged(fooValue);
reset(observer);
User barUser = TestUtil.createUser("bar");
Resource<User> barValue = Resource.success(barUser);
bar.setValue(barValue);
userViewModel.setLogin("bar");
verify(observer).onChanged(barValue);
}
開發者ID:hbmartin,項目名稱:firebase-chat-android-architecture-components,代碼行數:23,代碼來源:UserViewModelTest.java
示例3: insert
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
LiveData<Long> insert(@NonNull Context context, @NonNull final ChosenPhoto chosenPhoto,
@Nullable final String callingApplication) {
final MutableLiveData<Long> asyncInsert = new MutableLiveData<>();
if (persistUriAccess(context, chosenPhoto)) {
new Thread() {
@Override
public void run() {
long id = insertInternal(chosenPhoto);
if (id != 0L && callingApplication != null) {
Metadata metadata = new Metadata(ChosenPhoto.getContentUri(id));
metadata.date = new Date();
metadata.location = context.getString(R.string.gallery_shared_from, callingApplication);
GalleryDatabase.getInstance(context).metadataDao().insert(metadata);
}
asyncInsert.postValue(id);
}
}.start();
} else {
asyncInsert.setValue(0L);
}
return asyncInsert;
}
示例4: getLogin
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
public LiveData<ApiResponse<LoginBean>> getLogin(String email, String password) {
MutableLiveData<ApiResponse<LoginBean>> liveData = new MutableLiveData<>();
LoginBean loginBean = new LoginBean(email, "MyCompany");
loginBean.setIsLogin(1);
liveData.setValue(new ApiResponse<>(Response.success(loginBean)));
return liveData;
}
示例5: getLogin
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
public LiveData<ApiResponse<LoginBean>> getLogin(String email, String password) {
MutableLiveData<ApiResponse<LoginBean>> liveData = new MutableLiveData<>();
LoginBean loginBean = new LoginBean(email, "Facebook");
loginBean.setIsLogin(1);
liveData.setValue(new ApiResponse<>(Response.success(loginBean)));
return liveData;
}
示例6: getLogin
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
public LiveData<ApiResponse<LoginBean>> getLogin(String email, String password) {
MutableLiveData<ApiResponse<LoginBean>> liveData = new MutableLiveData<>();
LoginBean loginBean = new LoginBean(email, "Google");
loginBean.setIsLogin(1);
liveData.setValue(new ApiResponse<>(Response.success(loginBean)));
return liveData;
}
示例7: dbSuccessWithFetchFailure
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void dbSuccessWithFetchFailure() {
Foo dbValue = new Foo(1);
AtomicBoolean saved = new AtomicBoolean(false);
shouldFetch = (foo) -> foo == dbValue;
saveCallResult = foo -> {
saved.set(true);
return null;
};
ResponseBody body = ResponseBody.create(MediaType.parse("text/html"), "error");
MutableLiveData<ApiResponse<Foo>> apiResponseLiveData = new MutableLiveData();
createCall = (aVoid) -> apiResponseLiveData;
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
networkBoundResource.asLiveData().observeForever(observer);
drain();
verify(observer).onChanged(Resource.loading(null));
reset(observer);
dbData.setValue(dbValue);
drain();
verify(observer).onChanged(Resource.loading(dbValue));
apiResponseLiveData.setValue(new ApiResponse<>(Response.error(400, body)));
drain();
assertThat(saved.get(), is(false));
verify(observer).onChanged(Resource.error("error", dbValue));
Foo dbValue2 = new Foo(2);
dbData.setValue(dbValue2);
drain();
verify(observer).onChanged(Resource.error("error", dbValue2));
verifyNoMoreInteractions(observer);
}
示例8: dbSuccessWithReFetchSuccess
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void dbSuccessWithReFetchSuccess() {
Foo dbValue = new Foo(1);
Foo dbValue2 = new Foo(2);
AtomicReference<Foo> saved = new AtomicReference<>();
shouldFetch = (foo) -> foo == dbValue;
saveCallResult = foo -> {
saved.set(foo);
dbData.setValue(dbValue2);
return null;
};
MutableLiveData<ApiResponse<Foo>> apiResponseLiveData = new MutableLiveData();
createCall = (aVoid) -> apiResponseLiveData;
Observer<Resource<Foo>> observer = Mockito.mock(Observer.class);
networkBoundResource.asLiveData().observeForever(observer);
drain();
verify(observer).onChanged(Resource.loading(null));
reset(observer);
dbData.setValue(dbValue);
drain();
final Foo networkResult = new Foo(1);
verify(observer).onChanged(Resource.loading(dbValue));
apiResponseLiveData.setValue(new ApiResponse<>(Response.success(networkResult)));
drain();
assertThat(saved.get(), is(networkResult));
verify(observer).onChanged(Resource.success(dbValue2));
verifyNoMoreInteractions(observer);
}
示例9: dontGoToNetwork
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void dontGoToNetwork() {
MutableLiveData<User> dbData = new MutableLiveData<>();
User user = TestUtil.createUser("foo");
dbData.setValue(user);
when(userDao.findByLogin("foo")).thenReturn(dbData);
Observer<Resource<User>> observer = mock(Observer.class);
repo.loadUser("foo").observeForever(observer);
verify(githubService, never()).getUser("foo");
verify(observer).onChanged(Resource.success(user));
}
開發者ID:hbmartin,項目名稱:firebase-chat-android-architecture-components,代碼行數:12,代碼來源:UserRepositoryTest.java
示例10: createCall
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
public static <T> LiveData<ApiResponse<T>> createCall(Response<T> response) {
MutableLiveData<ApiResponse<T>> data = new MutableLiveData<>();
data.setValue(new ApiResponse<>(response));
return data;
}
示例11: loadContributors
import android.arch.lifecycle.MutableLiveData; //導入方法依賴的package包/類
@Test
public void loadContributors() throws IOException {
MutableLiveData<List<Contributor>> dbData = new MutableLiveData<>();
when(dao.loadContributors("foo", "bar")).thenReturn(dbData);
LiveData<Resource<List<Contributor>>> data = repository.loadContributors("foo",
"bar");
verify(dao).loadContributors("foo", "bar");
verify(service, never()).getContributors(anyString(), anyString());
Repo repo = TestUtil.createRepo("foo", "bar", "desc");
Contributor contributor = TestUtil.createContributor(repo, "log", 3);
// network does not send these
contributor.setRepoOwner(null);
contributor.setRepoName(null);
List<Contributor> contributors = Collections.singletonList(contributor);
LiveData<ApiResponse<List<Contributor>>> call = successCall(contributors);
when(service.getContributors("foo", "bar"))
.thenReturn(call);
Observer<Resource<List<Contributor>>> observer = mock(Observer.class);
data.observeForever(observer);
verify(observer).onChanged(Resource.loading( null));
MutableLiveData<List<Contributor>> updatedDbData = new MutableLiveData<>();
when(dao.loadContributors("foo", "bar")).thenReturn(updatedDbData);
dbData.setValue(Collections.emptyList());
verify(service).getContributors("foo", "bar");
ArgumentCaptor<List<Contributor>> inserted = ArgumentCaptor.forClass((Class) List.class);
verify(dao).insertContributors(inserted.capture());
assertThat(inserted.getValue().size(), is(1));
Contributor first = inserted.getValue().get(0);
assertThat(first.getRepoName(), is("bar"));
assertThat(first.getRepoOwner(), is("foo"));
updatedDbData.setValue(contributors);
verify(observer).onChanged(Resource.success(contributors));
}