Search in sources :

Example 6 with Observer

use of androidx.lifecycle.Observer in project mapbox-plugins-android by mapbox.

the class LiveDataTestUtil method getValue.

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

Example 7 with Observer

use of androidx.lifecycle.Observer in project zype-android by zype.

the class SubscribeOrLoginFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_subscribe_or_login, container, false);
    final NavigationHelper navigationHelper = NavigationHelper.getInstance(getActivity());
    TextView textTitle = rootView.findViewById(R.id.textTitle);
    textTitle.setText(String.format(getString(R.string.subscribe_or_login_title), getString(R.string.app_name)));
    Button buttonSubscribe = rootView.findViewById(R.id.buttonSubscribe);
    buttonSubscribe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (AuthHelper.isLoggedIn()) {
                navigationHelper.switchToSubscriptionScreen(getActivity(), getActivity().getIntent().getExtras());
            } else {
                navigationHelper.switchToConsumerScreen(getActivity());
            }
        }
    });
    Button buttonLogin = rootView.findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            navigationHelper.switchToLoginScreen(getActivity(), null);
        }
    });
    final List<Purchase> purchases = ZypeApp.marketplaceGateway.getBillingManager().getPurchases();
    Button buttonRestorePurchases = rootView.findViewById(R.id.buttonRestorePurchases);
    buttonRestorePurchases.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (AuthHelper.isLoggedIn()) {
                // Get first purchase from the purchase list and try to validate it
                Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(purchases.get(0).getSku());
                if (subscription != null) {
                    showProgress(getString(R.string.subscription_verify));
                    ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(SubscribeOrLoginFragment.this, new Observer<Boolean>() {

                        @Override
                        public void onChanged(@Nullable Boolean result) {
                            hideProgress();
                            if (result) {
                                getActivity().setResult(RESULT_OK);
                                getActivity().finish();
                            } else {
                                DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_validation));
                            }
                        }
                    });
                } else {
                    Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + purchases.get(0).getSku());
                    DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                }
            /*for (String itemSku : purchases.get(0).getSkus()){
                        Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(itemSku);
                        if (subscription != null) {
                            showProgress(getString(R.string.subscription_verify));
                            ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(SubscribeOrLoginFragment.this, new Observer<Boolean>() {
                                @Override
                                public void onChanged(@Nullable Boolean result) {
                                    hideProgress();
                                    if (result) {
                                        getActivity().setResult(RESULT_OK);
                                        getActivity().finish();
                                    }
                                    else {
                                        DialogHelper.showErrorAlert(getActivity(),
                                                getString(R.string.subscribe_or_login_error_validation));
                                    }
                                }
                            });
                        }
                        else {
                            Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + itemSku);
                            DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                        }
                    }*/
            } else {
                navigationHelper.switchToConsumerScreen(getActivity());
            }
        }
    });
    if (purchases != null && purchases.size() > 0) {
        Logger.d("There are purchases, size=" + purchases.size());
        buttonRestorePurchases.setVisibility(View.VISIBLE);
    } else {
        Logger.d("There are no purchases on the device");
        buttonRestorePurchases.setVisibility(View.GONE);
    }
    api.subscribe(this);
    return rootView;
}
Also used : Purchase(com.android.billingclient.api.Purchase) NavigationHelper(com.zype.android.ui.NavigationHelper) Button(android.widget.Button) Observer(androidx.lifecycle.Observer) TextView(android.widget.TextView) Subscription(com.zype.android.Billing.Subscription) TextView(android.widget.TextView) View(android.view.View) Nullable(androidx.annotation.Nullable)

Example 8 with Observer

use of androidx.lifecycle.Observer in project zype-android by zype.

the class VideoDetailActivity method initModel.

// //////////
// Data
// 
private void initModel() {
    final Video video = DataRepository.getInstance(getApplication()).getVideoSync(mVideoId);
    if (video != null) {
        // Load player urls
        playerViewModel = ViewModelProviders.of(this).get(PlayerViewModel.class);
        PlayerViewModel.PlayerMode mediaType = null;
        switch(mType) {
            case PlayerFragment.TYPE_AUDIO_LOCAL:
            case PlayerFragment.TYPE_AUDIO_WEB:
                mediaType = PlayerViewModel.PlayerMode.AUDIO;
                break;
            case PlayerFragment.TYPE_VIDEO_LOCAL:
            case PlayerFragment.TYPE_VIDEO_WEB:
            case BaseVideoActivity.TYPE_WEB:
                mediaType = PlayerViewModel.PlayerMode.VIDEO;
                break;
        }
        playerViewModel.init(video.id, playlistId, mediaType);
        playerViewModel.getPlayerError().observe(this, playerErrorObserver);
        playerViewModel.getPlayerUrl().observe(this, playerUrlObserver);
        if (video.isZypeLive == 0) {
            updateDownloadUrls();
        } else {
            // showVideoThumbnail();
            videoDetailViewModel = new VideoDetailViewModel(getApplication());
            final Observer<Video> videoObserver = new Observer<Video>() {

                @Override
                public void onChanged(@Nullable Video video) {
                    if (VideoHelper.isLiveEventOnAir(video)) {
                        if (videoDetailViewModel.updateVideoOnAir(video)) {
                            showPlayer();
                        }
                    } else {
                        changeFragment(isChromecastConntected());
                        videoDetailViewModel.checkOnAir(mVideoId).observe(VideoDetailActivity.this, new Observer<Video>() {

                            @Override
                            public void onChanged(@Nullable Video video) {
                                videoDetailViewModel.onCleared();
                                videoDetailViewModel.updateVideoOnAir(video);
                                showPlayer();
                            }
                        });
                    }
                    videoDetailViewModel.getVideo(mVideoId).removeObserver(this);
                }
            };
            videoDetailViewModel.getVideo(mVideoId).observe(this, videoObserver);
        }
    }
}
Also used : Video(com.zype.android.Db.Entity.Video) Observer(androidx.lifecycle.Observer) PlayerViewModel(com.zype.android.ui.player.PlayerViewModel) Nullable(androidx.annotation.Nullable)

Example 9 with Observer

use of androidx.lifecycle.Observer in project collect by opendatakit.

the class LiveDataUtils method getOrAwaitValue.

public static <T> T getOrAwaitValue(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);
    // Don't wait indefinitely if the LiveData is not set.
    if (!latch.await(2, TimeUnit.SECONDS)) {
        throw new RuntimeException("LiveData value was never set.");
    }
    // noinspection unchecked
    return (T) data[0];
}
Also used : Observer(androidx.lifecycle.Observer) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable)

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