use of androidx.lifecycle.Observer in project ETSMobile-Android2 by ApplETS.
the class MoodleWebServiceTest method getValue.
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];
}
use of androidx.lifecycle.Observer in project Signal-Android by WhisperSystems.
the class SafetyNumberChangeDialog method handleSendAnyway.
private void handleSendAnyway(DialogInterface dialogInterface, int which) {
Log.d(TAG, "handleSendAnyway");
boolean skipCallbacks = requireArguments().getBoolean(SKIP_CALLBACKS_EXTRA, false);
Activity activity = getActivity();
Callback callback;
if (activity instanceof Callback && !skipCallbacks) {
callback = (Callback) activity;
} else {
Fragment parent = getParentFragment();
if (parent instanceof Callback && !skipCallbacks) {
callback = (Callback) parent;
} else {
callback = null;
}
}
LiveData<TrustAndVerifyResult> trustOrVerifyResultLiveData = viewModel.trustOrVerifyChangedRecipients();
Observer<TrustAndVerifyResult> observer = new Observer<TrustAndVerifyResult>() {
@Override
public void onChanged(TrustAndVerifyResult result) {
if (callback != null) {
switch(result.getResult()) {
case TRUST_AND_VERIFY:
Log.d(TAG, "Trust and verify");
callback.onSendAnywayAfterSafetyNumberChange(result.getChangedRecipients());
break;
case TRUST_VERIFY_AND_RESEND:
Log.d(TAG, "Trust, verify, and resent");
callback.onMessageResentAfterSafetyNumberChange();
break;
}
}
trustOrVerifyResultLiveData.removeObserver(this);
}
};
trustOrVerifyResultLiveData.observeForever(observer);
}
use of androidx.lifecycle.Observer in project zype-android by zype.
the class GalleryViewModel method getGalleryRows.
public LiveData<List<GalleryRow>> getGalleryRows(String parentPlaylistId) {
if (data == null) {
this.parentPlaylistId = parentPlaylistId;
data = new MediatorLiveData<>();
liveDataPlaylists = getPlaylists(parentPlaylistId);
liveDataVideos = new HashMap<>();
liveDataNestedPlaylists = new HashMap<>();
data.addSource(liveDataPlaylists, new Observer<List<Playlist>>() {
@Override
public void onChanged(@Nullable List<Playlist> playlists) {
Logger.d("onChanged(): Playlists, size=" + playlists.size());
if (!playlists.isEmpty()) {
data.removeSource(liveDataPlaylists);
}
final List<GalleryRow> galleryRows = new ArrayList<>();
for (final Playlist playlist : playlists) {
final GalleryRow row = new GalleryRow(playlist);
// Add video items to the row if playlist contains any video
if (playlist.playlistItemCount > 0) {
LiveData<List<Video>> playlistVideos = liveDataVideos.get(playlist.id);
if (playlistVideos == null) {
playlistVideos = getPlaylistVideos(playlist.id);
liveDataVideos.put(playlist.id, playlistVideos);
data.addSource(playlistVideos, new Observer<List<Video>>() {
@Override
public void onChanged(@Nullable List<Video> videos) {
Logger.d("onChanged(): Videos (" + playlist.title + "), size=" + videos.size());
row.videos = videos;
if (allDataLoaded(galleryRows)) {
data.setValue(galleryRows);
ZypeApp.needToLoadData = false;
}
}
});
}
} else // Otherwise add nested playlists
{
LiveData<List<Playlist>> nestedPlaylists = liveDataNestedPlaylists.get(playlist.id);
if (nestedPlaylists == null) {
nestedPlaylists = getPlaylists(playlist.id);
liveDataNestedPlaylists.put(playlist.id, nestedPlaylists);
data.addSource(nestedPlaylists, new Observer<List<Playlist>>() {
@Override
public void onChanged(@Nullable List<Playlist> playlists) {
Logger.d("onChanged(): Nested playlists (" + playlist.title + "), size=" + playlists.size());
if (playlists.isEmpty()) {
if (playlistApiRequests.containsKey(playlist.id)) {
if (playlistApiRequests.get(playlist.id)) {
Logger.d("onChanged(): Nested playlists (" + playlist.title + "), wait for API response");
} else {
Logger.d("onChanged(): Nested playlists (" + playlist.title + "), row removed");
galleryRows.remove(row);
}
} else {
Logger.d("onChanged(): Nested playlists (" + playlist.title + "), row removed");
galleryRows.remove(row);
}
} else {
row.nestedPlaylists = playlists;
playlistApiRequests.remove(playlist.id);
}
if (allDataLoaded(galleryRows)) {
data.setValue(galleryRows);
ZypeApp.needToLoadData = false;
}
}
});
}
}
galleryRows.add(row);
}
data.setValue(galleryRows);
}
});
}
return data;
}
use of androidx.lifecycle.Observer in project zype-android by zype.
the class VideoDetailActivity method createContentUriObserver.
private Observer<String> createContentUriObserver() {
return new Observer<String>() {
@Override
public void onChanged(@Nullable String url) {
Logger.d("getContentUri(): onChanged(): url=" + url);
if (!TextUtils.isEmpty(url)) {
if (playerViewModel.isTrailer().getValue()) {
Logger.d("getContentUri(): onChanged(): playing trailer");
mType = PlayerFragment.TYPE_VIDEO_TRAILER;
Fragment fragment = PlayerFragment.newInstance(mType, url, null);
// showFragment(fragment);
} else {
switch(playerViewModel.getPlayerMode().getValue()) {
case AUDIO:
if (playerViewModel.isAudioDownloaded()) {
mType = PlayerFragment.TYPE_AUDIO_LOCAL;
} else {
mType = PlayerFragment.TYPE_AUDIO_WEB;
}
break;
case VIDEO:
if (playerViewModel.isVideoDownloaded()) {
mType = PlayerFragment.TYPE_VIDEO_LOCAL;
} else {
mType = PlayerFragment.TYPE_VIDEO_WEB;
}
break;
}
hideProgress();
changeFragment(isChromecastConntected());
}
} else {
showThumbnailView(videoDetailViewModel.getVideo().getValue());
}
}
};
}
use of androidx.lifecycle.Observer in project bitcoin-wallet by bitcoin-wallet.
the class AbstractWalletActivityViewModel method broadcastTransaction.
public ListenableFuture<Transaction> broadcastTransaction(final Transaction tx) throws VerificationException {
final SettableFuture<Transaction> future = SettableFuture.create();
wallet.observeForever(new Observer<Wallet>() {
@Override
public void onChanged(final Wallet wallet) {
blockchainService.observeForever(new Observer<BlockchainService>() {
@Override
public void onChanged(final BlockchainService blockchainService) {
if (wallet.isTransactionRelevant(tx)) {
wallet.receivePending(tx, null);
final TransactionBroadcast broadcast = blockchainService.broadcastTransaction(tx);
if (broadcast != null) {
broadcast.future().addListener(() -> {
log.info("broadcasting transaction {} complete, dropping all peers", tx.getTxId());
blockchainService.dropAllPeers();
}, Threading.SAME_THREAD);
future.setFuture(broadcast.future());
} else {
log.info("impediments; will send {} later", tx.getTxId());
future.cancel(false);
}
} else {
log.info("tx {} irrelevant", tx.getTxId());
future.cancel(false);
}
AbstractWalletActivityViewModel.this.blockchainService.removeObserver(this);
}
});
AbstractWalletActivityViewModel.this.wallet.removeObserver(this);
}
});
return future;
}
Aggregations