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];
}
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;
}
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);
}
}
}
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];
}
Aggregations