Java Code Examples for androidx.lifecycle.LiveData#observeForever()

The following examples show how to use androidx.lifecycle.LiveData#observeForever() . 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: ThreadViewModel.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
public void waitForEdit(UUID uuid) {
    final WorkManager workManager = WorkManager.getInstance(getApplication());
    LiveData<WorkInfo> liveData = workManager.getWorkInfoByIdLiveData(uuid);
    liveData.observeForever(new Observer<WorkInfo>() {
        @Override
        public void onChanged(WorkInfo workInfo) {
            if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                final Data data = workInfo.getOutputData();
                final String threadId = data.getString("threadId");
                if (threadId != null && !ThreadViewModel.this.threadId.equals(threadId)) {
                    LOGGER.info("redirecting to thread {}", threadId);
                    threadViewRedirect.postValue(new Event<>(threadId));
                }
                liveData.removeObserver(this);
            } else if (workInfo.getState() == WorkInfo.State.FAILED) {
                liveData.removeObserver(this);
            }
        }
    });
}
 
Example 2
Source File: LiveDataTestUtil.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
 * Once we got a notification via onChanged, we stop observing.
 */
public static <T> T getValue(final LiveData<T> liveData) throws InterruptedException {
  final Object[] data = new Object[1];
  final CountDownLatch latch = new CountDownLatch(1);
  Observer<T> observer = new Observer<T>() {
    @Override
    public void onChanged(@Nullable T o) {
      data[0] = o;
      latch.countDown();
      liveData.removeObserver(this);
    }
  };
  liveData.observeForever(observer);
  latch.await(2, TimeUnit.SECONDS);
  //noinspection unchecked
  return (T) data[0];
}
 
Example 3
Source File: LiveDataTestUtil.java    From android-room-with-a-view with Apache License 2.0 6 votes vote down vote up
/**
 * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
 * Once we got a notification via onChanged, we stop observing.
 */
public static <T> T getValue(final LiveData<T> liveData) throws InterruptedException {
    final Object[] data = new Object[1];
    final CountDownLatch latch = new CountDownLatch(1);
    Observer<T> observer = new Observer<T>() {
        @Override
        public void onChanged(@Nullable T o) {
            data[0] = o;
            latch.countDown();
            liveData.removeObserver(this);
        }
    };
    liveData.observeForever(observer);
    latch.await(2, TimeUnit.SECONDS);
    //noinspection unchecked
    return (T) data[0];
}
 
Example 4
Source File: MoodleRepository.java    From ETSMobile-Android2 with Apache License 2.0 6 votes vote down vote up
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 5
Source File: MoodleWebServiceTest.java    From ETSMobile-Android2 with Apache License 2.0 6 votes vote down vote up
private <T> T getValue(@NonNull LiveData<T> liveData) throws InterruptedException {
    final Object[] data = new Object[1];
    CountDownLatch latch = new CountDownLatch(1);
    Observer<T> observer = new Observer<T>() {
        @Override
        public void onChanged(@Nullable T o) {
            data[0] = o;
            latch.countDown();
            liveData.removeObserver(this);
        }
    };
    liveData.observeForever(observer);
    latch.await(TIME_OUT, TimeUnit.SECONDS);
    //noinspection unchecked
    return (T) data[0];
}
 
Example 6
Source File: LiveDataTestUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Observes and then instantly un-observes the supplied live data.
 * <p>
 * This will therefore only work in conjunction with {@link LiveDataRule}.
 */
public static <T> T getValue(final LiveData<T> liveData) {
  AtomicReference<T> data     = new AtomicReference<>();
  Observer<T>        observer = data::set;

  liveData.observeForever(observer);
  liveData.removeObserver(observer);

  return data.get();
}
 
Example 7
Source File: LiveDataTestUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static  <T> void assertNoValue(final LiveData<T> liveData) {
  AtomicReference<Boolean> data     = new AtomicReference<>(false);
  Observer<T>              observer = newValue -> data.set(true);

  liveData.observeForever(observer);
  liveData.removeObserver(observer);

  assertFalse("Expected no value", data.get());
}
 
Example 8
Source File: WatchFaceUtil.java    From PixelWatchFace with GNU General Public License v3.0 5 votes vote down vote up
public static <T> void observeUntilFinished(final LiveData<WorkInfo> liveData,
    final Observer<WorkInfo> observer) {
  liveData.observeForever(new Observer<WorkInfo>() {
    @Override
    public void onChanged(WorkInfo workInfo) {
      if (workInfo != null && workInfo.getState().isFinished()) {
        Log.d("observeOnce", "Removing observer, work finished.");
        liveData.removeObserver(this);
      }
      observer.onChanged(workInfo);
    }
  });
}
 
Example 9
Source File: TestObserver.java    From livedata-testing with Apache License 2.0 4 votes vote down vote up
public static <T> TestObserver<T> test(LiveData<T> liveData) {
  TestObserver<T> observer = new TestObserver<>();
  liveData.observeForever(observer);
  return observer;
}
 
Example 10
Source File: WeekTimesViewHolder.java    From trackworktime with GNU General Public License v3.0 4 votes vote down vote up
public void bind(@NonNull LiveData<WeekState> weekStateLiveData) {
	this.weekStateLiveData = weekStateLiveData;
	weekStateLiveData.observeForever(this);
}