Search in sources :

Example 1 with Observer

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];
}
Also used : Observer(androidx.lifecycle.Observer) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable)

Example 2 with Observer

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);
}
Also used : Observer(androidx.lifecycle.Observer) FragmentActivity(androidx.fragment.app.FragmentActivity) VerifyIdentityActivity(org.thoughtcrime.securesms.verify.VerifyIdentityActivity) Activity(android.app.Activity) Fragment(androidx.fragment.app.Fragment) DialogFragment(androidx.fragment.app.DialogFragment)

Example 3 with 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;
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) LiveData(androidx.lifecycle.LiveData) MediatorLiveData(androidx.lifecycle.MediatorLiveData) GalleryRow(com.zype.android.ui.Gallery.Model.GalleryRow) Playlist(com.zype.android.Db.Entity.Playlist) Video(com.zype.android.Db.Entity.Video) Observer(androidx.lifecycle.Observer) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(androidx.annotation.Nullable)

Example 4 with Observer

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());
            }
        }
    };
}
Also used : Observer(androidx.lifecycle.Observer) Fragment(androidx.fragment.app.Fragment) PlayerFragment(com.zype.android.ui.player.PlayerFragment) Nullable(androidx.annotation.Nullable)

Example 5 with Observer

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;
}
Also used : Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet) Observer(androidx.lifecycle.Observer) TransactionBroadcast(org.bitcoinj.core.TransactionBroadcast) BlockchainService(de.schildbach.wallet.service.BlockchainService)

Aggregations

Observer (androidx.lifecycle.Observer)9 Nullable (androidx.annotation.Nullable)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 Fragment (androidx.fragment.app.Fragment)2 Video (com.zype.android.Db.Entity.Video)2 Activity (android.app.Activity)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 DialogFragment (androidx.fragment.app.DialogFragment)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 LiveData (androidx.lifecycle.LiveData)1 MediatorLiveData (androidx.lifecycle.MediatorLiveData)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 Purchase (com.android.billingclient.api.Purchase)1 Subscription (com.zype.android.Billing.Subscription)1 Playlist (com.zype.android.Db.Entity.Playlist)1 GalleryRow (com.zype.android.ui.Gallery.Model.GalleryRow)1 NavigationHelper (com.zype.android.ui.NavigationHelper)1 PlayerFragment (com.zype.android.ui.player.PlayerFragment)1