Java Code Examples for androidx.lifecycle.LiveData#observe()
The following examples show how to use
androidx.lifecycle.LiveData#observe() .
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: GroupMembersDialog.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
public void display() { AlertDialog dialog = new AlertDialog.Builder(fragmentActivity) .setTitle(R.string.ConversationActivity_group_members) .setIconAttribute(R.attr.group_members_dialog_icon) .setCancelable(true) .setView(R.layout.dialog_group_members) .setPositiveButton(android.R.string.ok, null) .show(); GroupMemberListView memberListView = dialog.findViewById(R.id.list_members); LiveGroup liveGroup = new LiveGroup(groupRecipient.requireGroupId()); LiveData<List<GroupMemberEntry.FullMember>> fullMembers = liveGroup.getFullMembers(); //noinspection ConstantConditions fullMembers.observe(fragmentActivity, memberListView::setMembers); dialog.setOnDismissListener(d -> fullMembers.removeObservers(fragmentActivity)); memberListView.setRecipientClickListener(recipient -> { dialog.dismiss(); contactClick(recipient); }); }
Example 2
Source File: AbstractQueryFragment.java From lttrs-android with Apache License 2.0 | 6 votes |
private void observeThreadOverviewItems(LiveData<PagedList<ThreadOverviewItem>> liveData) { final AtomicBoolean actionModeRefreshed = new AtomicBoolean(false); liveData.observe(getViewLifecycleOwner(), threadOverviewItems -> { final RecyclerView.LayoutManager layoutManager = binding.threadList.getLayoutManager(); final boolean atTop; if (layoutManager instanceof LinearLayoutManager) { atTop = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() == 0; } else { atTop = false; } threadOverviewAdapter.submitList(threadOverviewItems, () -> { if (atTop && binding != null) { binding.threadList.scrollToPosition(0); } if (actionMode != null && actionModeRefreshed.compareAndSet(false, true)) { actionMode.invalidate(); } }); }); }
Example 3
Source File: AddedAppKeyAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 6 votes |
public AddedAppKeyAdapter(@NonNull final LifecycleOwner owner, @NonNull final List<ApplicationKey> appKeys, @NonNull final LiveData<ProvisionedMeshNode> meshNodeLiveData) { this.appKeys.clear(); this.appKeys.addAll(appKeys); Collections.sort(this.appKeys, Utils.appKeyComparator); meshNodeLiveData.observe((LifecycleOwner) owner, meshNode -> { addedAppKeys.clear(); for (NodeKey nodeKey : meshNode.getAddedAppKeys()) { for (ApplicationKey applicationKey : appKeys) { if (nodeKey.getIndex() == applicationKey.getKeyIndex()) { addedAppKeys.add(applicationKey); } } } Collections.sort(addedAppKeys, Utils.appKeyComparator); notifyDataSetChanged(); }); }
Example 4
Source File: ManageNetKeyAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 6 votes |
public ManageNetKeyAdapter(@NonNull final LifecycleOwner owner, @NonNull final LiveData<ProvisionedMeshNode> meshNodeLiveData, @NonNull final List<NetworkKey> netKeys) { meshNodeLiveData.observe(owner, node -> { networkKeys.clear(); for (NodeKey key : node.getAddedNetKeys()) { for (NetworkKey networkKey : netKeys) { if (networkKey.getKeyIndex() == key.getIndex()) { networkKeys.add(networkKey); } } } Collections.sort(networkKeys, Utils.netKeyComparator); notifyDataSetChanged(); }); }
Example 5
Source File: BoundAppKeysAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 6 votes |
public BoundAppKeysAdapter(@NonNull final LifecycleOwner owner, @NonNull final List<ApplicationKey> appKeys, @NonNull final LiveData<MeshModel> meshModelLiveData) { meshModelLiveData.observe(owner, meshModel -> { if (meshModel != null) { this.appKeys.clear(); for (Integer index : meshModel.getBoundAppKeyIndexes()) { for (ApplicationKey applicationKey : appKeys) { if (index == applicationKey.getKeyIndex()) { this.appKeys.add(applicationKey); } } } Collections.sort(this.appKeys, Utils.appKeyComparator); notifyDataSetChanged(); } }); }
Example 6
Source File: AddedNetKeyAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 6 votes |
public AddedNetKeyAdapter(@NonNull final LifecycleOwner owner, @NonNull final List<NetworkKey> netKeys, @NonNull final LiveData<ProvisionedMeshNode> meshNodeLiveData) { this.netKeys.addAll(netKeys); Collections.sort(this.netKeys, Utils.netKeyComparator); meshNodeLiveData.observe(owner, meshNode -> { addedNetKeys.clear(); for (NodeKey nodeKey : meshNode.getAddedNetKeys()) { for (NetworkKey networkKey : netKeys) { if (nodeKey.getIndex() == networkKey.getKeyIndex()) { addedNetKeys.add(networkKey); } } } Collections.sort(addedNetKeys, Utils.netKeyComparator); notifyDataSetChanged(); }); }
Example 7
Source File: NotificationsFragment.java From zephyr with MIT License | 5 votes |
private void subscribeUi(LiveData<List<ZephyrNotificationPreference>> liveData) { liveData.observe(getViewLifecycleOwner(), preferences -> { if (preferences != null && !preferences.isEmpty()) { // Show results mAdapter.setNotificationPreferences(preferences); mSpinner.setVisibility(View.GONE); mAppList.setVisibility(View.VISIBLE); mFastScrollerThumbView.setVisibility(View.VISIBLE); mFastScrollerView.setVisibility(View.VISIBLE); mNoResultsFound.setVisibility(View.GONE); } else if (mViewModel.getSearchQuery().getValue() != null && !mViewModel.getSearchQuery().getValue().isEmpty()) { // Show no result found mAppList.setVisibility(View.GONE); mFastScrollerThumbView.setVisibility(View.GONE); mFastScrollerView.setVisibility(View.GONE); mSpinner.setVisibility(View.GONE); mNoResultsFound.setVisibility(View.VISIBLE); } else { // Show spinner mAppList.setVisibility(View.GONE); mFastScrollerThumbView.setVisibility(View.GONE); mFastScrollerView.setVisibility(View.GONE); mSpinner.setVisibility(View.VISIBLE); mNoResultsFound.setVisibility(View.GONE); } }); }
Example 8
Source File: MainActivity.java From Instagram-Profile-Downloader with MIT License | 5 votes |
public void logoutFromInsta(String user_id) { LiveData<List<Logins>> loggedInUsers = DataObjectRepositry.dataObjectRepositry.getAllUsers(); loggedInUsers.observe(this, new Observer<List<Logins>>() { @Override public void onChanged(List<Logins> logins) { if (logins.size() > 0) { for (Logins logins1 : logins) { if (logins1.getUserId().equals(user_id)) { dataObjectRepositry.deleteExistingUser(logins1.getId()); ToastUtils.SuccessToast(context, GlobalConstant.LOGOUT); PreferencesManager.clear(); ZoomstaUtil.clearPref(context); InstaUtils.setSessionId(null); InstaUtils.setCsrf(null, null); InstaUtils.setCookies(null); Intent intent = new Intent(MainActivity.this, IntroScreenActivity.class); PreferencesManager.savePref("isLogin", false); intent.putExtra("isLogOut", true); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; } } } } }); }
Example 9
Source File: SubGroupAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 5 votes |
public SubGroupAdapter(@NonNull final Context context, @NonNull final MeshNetwork network, @NonNull final LiveData<Group> groupedModels) { this.mContext = context; this.mMeshNetwork = network; groupedModels.observe((LifecycleOwner) context, group -> { mGroup = group; updateAdapterData(); }); }
Example 10
Source File: GroupAddressAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 5 votes |
public GroupAddressAdapter(@NonNull final Context context, @NonNull final MeshNetwork network, @NonNull final LiveData<MeshModel> meshModelLiveData) { this.mContext = context; this.network = network; meshModelLiveData.observe((LifecycleOwner) context, meshModel -> { if (meshModel != null) { final List<Integer> tempAddresses = meshModel.getSubscribedAddresses(); if (tempAddresses != null) { mAddresses.clear(); mAddresses.addAll(tempAddresses); notifyDataSetChanged(); } } }); }
Example 11
Source File: NodeAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 5 votes |
public NodeAdapter(@NonNull final LifecycleOwner owner, @NonNull final LiveData<List<ProvisionedMeshNode>> provisionedNodesLiveData) { provisionedNodesLiveData.observe(owner, nodes -> { if (nodes != null) { mNodes.clear(); mNodes.addAll(nodes); notifyDataSetChanged(); } }); }
Example 12
Source File: ElementAdapter.java From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License | 5 votes |
public ElementAdapter(@NonNull final LifecycleOwner owner, @NonNull final LiveData<ProvisionedMeshNode> meshNodeLiveData) { meshNodeLiveData.observe(owner, meshNode -> { if (meshNode != null) { mProvisionedMeshNode = meshNode; mElements.clear(); mElements.addAll(mProvisionedMeshNode.getElements().values()); notifyDataSetChanged(); } }); }
Example 13
Source File: PaginationActivity.java From AdapterDelegates with Apache License 2.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pagination); RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); AdapterDelegatesManager<List<DisplayableItem>> delegatesManager = new AdapterDelegatesManager<List<DisplayableItem>>() .addDelegate(new AdvertisementAdapterDelegate(this)) .addDelegate(new CatAdapterDelegate(this)) .addDelegate(new DogAdapterDelegate(this)) .addDelegate(new GeckoAdapterDelegate(this)) .addDelegate(new SnakeListItemAdapterDelegate(this)) .setFallbackDelegate(new LoadingAdapterDelegate(this)); final PagedListDelegationAdapter<DisplayableItem> adapter = new PagedListDelegationAdapter<DisplayableItem>(delegatesManager, callback); recyclerView.setAdapter(adapter); LiveData<PagedList<DisplayableItem>> pagedListLiveData = new LivePagedListBuilder<>(new SampleDataSource.Factory(), 20) .setBoundaryCallback(new PagedList.BoundaryCallback<DisplayableItem>() { @Override public void onZeroItemsLoaded() { Log.d("PaginationSource", "onZeroItemsLoaded"); super.onZeroItemsLoaded(); } @Override public void onItemAtFrontLoaded(@NonNull DisplayableItem itemAtFront) { Log.d("PaginationSource", "onItemAtFrontLoaded "+itemAtFront); super.onItemAtFrontLoaded(itemAtFront); } @Override public void onItemAtEndLoaded(@NonNull DisplayableItem itemAtEnd) { Log.d("PaginationSource", "onItemAtEndLoaded "+itemAtEnd); super.onItemAtEndLoaded(itemAtEnd); } }) .build(); pagedListLiveData.observe(this, new Observer<PagedList<DisplayableItem>>() { @Override public void onChanged(PagedList<DisplayableItem> displayableItems) { adapter.submitList(displayableItems); } }); }
Example 14
Source File: MoodleAssignmentsActivity.java From ETSMobile-Android2 with Apache License 2.0 | 4 votes |
private void displaySelectedAssignment(final MoodleAssignment selectedAssignment) { binding.setSelectedAssignment(selectedAssignment); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); final LiveData<RemoteResource<MoodleAssignmentSubmission>> submissionLiveData = moodleViewModel.getAssignmentSubmission(selectedAssignment.getId()); submissionLiveData.observe(MoodleAssignmentsActivity.this, new Observer<RemoteResource<MoodleAssignmentSubmission>>() { @Override public void onChanged(@Nullable RemoteResource<MoodleAssignmentSubmission> moodleAssignmentSubmission) { boolean noLongerNeedtoObserve = true; if (moodleAssignmentSubmission == null || moodleAssignmentSubmission.status == RemoteResource.ERROR) { if (moodleAssignmentSubmission != null && moodleAssignmentSubmission.data != null) { binding.setSelectedAssignmentFeedback(moodleAssignmentSubmission.data.getFeedback()); binding.setSelectedAssignmentLastAttempt(moodleAssignmentSubmission.data.getLastAttempt()); } binding.setLoadingSelectedAssignmentSubmission(false); bottomSheet.requestLayout(); boolean noDataToDsiplay = moodleAssignmentSubmission == null || moodleAssignmentSubmission.data == null; if (noDataToDsiplay) bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); String errorMsg = getString(R.string.toast_Sync_Fail); displayErrorMessage(errorMsg); } else if (moodleAssignmentSubmission.status == RemoteResource.SUCCESS) { MoodleAssignmentSubmission submission = moodleAssignmentSubmission.data; if (submission != null) { binding.setSelectedAssignmentFeedback(submission.getFeedback()); binding.setSelectedAssignmentLastAttempt(submission.getLastAttempt()); } else { binding.setSelectedAssignmentFeedback(null); binding.setSelectedAssignmentLastAttempt(null); } binding.setLoadingSelectedAssignmentSubmission(false); } else if (moodleAssignmentSubmission.status == RemoteResource.LOADING) { binding.setLoadingSelectedAssignmentSubmission(true); noLongerNeedtoObserve = false; bottomSheet.requestLayout(); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); } if (noLongerNeedtoObserve) submissionLiveData.removeObserver(this); } }); }
Example 15
Source File: MainActivity.java From zephyr with MIT License | 4 votes |
private void subscribeUi(LiveData<Boolean> liveData) { liveData.observe(this, isServiceRunning -> { mConnectButton.setServiceRunning(isServiceRunning); }); }
Example 16
Source File: MainActivity.java From service with Apache License 2.0 | 4 votes |
/** * This will create a oneshot workerA task and schedule it to run once. * commented out code shows how to make it recur every 24 hours. */ public void oneshot() { //for a schedule once OneTimeWorkRequest runWorkA = new OneTimeWorkRequest.Builder(WorkerA.class) .build(); /* //for recur schedule, say every 24 hours, comment out above, since duplicate variables. PeriodicWorkRequest.Builder workerkBuilder = new PeriodicWorkRequest.Builder(WorkerA.class, 24, TimeUnit.HOURS); // ...if you want, you can apply constraints to the builder here... Constraints myConstraints = new Constraints.Builder() //.setRequiresDeviceIdle(true) .setRequiresCharging(true) // Many other constraints are available, see the // Constraints.Builder reference .build(); // Create the actual work object: PeriodicWorkRequest runWorkA = workerkBuilder.setConstraints(myConstraints) .build(); */ //now enqueue the task so it can be run. WorkManager.getInstance(getApplicationContext()).enqueue(runWorkA); //not necessary, but this will tell us the status of the task. LiveData<WorkInfo> status = WorkManager.getInstance(getApplicationContext()).getWorkInfoByIdLiveData(runWorkA.getId()); status.observe(this, new Observer<WorkInfo>() { @Override public void onChanged(@Nullable WorkInfo workStatus) { switch (workStatus.getState()) { case BLOCKED: tv_oneshot.setText("Status is Blocked"); break; case CANCELLED: tv_oneshot.setText("Status is canceled"); break; case ENQUEUED: tv_oneshot.setText("Status is enqueued"); break; case FAILED: tv_oneshot.setText("Status is failed"); break; case RUNNING: tv_oneshot.setText("Status is running"); break; case SUCCEEDED: tv_oneshot.setText("Status is succeeded"); break; default: tv_oneshot.setText("Status is unknown"); } } }); }
Example 17
Source File: MainActivity.java From Instagram-Profile-Downloader with MIT License | 4 votes |
public void changeAccount(String usernameSelecte) { LiveData<List<Logins>> loggedInUsers = DataObjectRepositry.dataObjectRepositry.getAllUsers(); loggedInUsers.observe(this, new Observer<List<Logins>>() { @Override public void onChanged(List<Logins> logins) { if (logins.size() > 0) { for (Logins logins1 : logins) { if (logins1.getUserName().equals(usernameSelecte)) { Intent intent = new Intent(MainActivity.this, SplashActivity.class); if (BuildConfig.DEBUG) Log.d(username + "userId", InstaUtils.getUserId()); ZoomstaUtil.setStringPreference(MainActivity.this, logins1.getUserId(), "userid"); ZoomstaUtil.setStringPreference(MainActivity.this, usernameSelecte, "username"); ZoomstaUtil.setStringPreference(MainActivity.this, logins1.getCooki(), "cooki"); ZoomstaUtil.setStringPreference(MainActivity.this, logins1.getCsrf(), "csrf"); ZoomstaUtil.setStringPreference(MainActivity.this, logins1.getSession_id(), "sessionid"); InstaUtils.setCookies(logins1.getCooki()); InstaUtils.setCsrf(logins1.getCsrf(), logins1.getCooki()); InstaUtils.setSessionId(logins1.getSession_id()); PreferencesManager.savePref(GlobalConstant.PROFILE_PIC, logins1.getProfilePic()); PreferencesManager.savePref(GlobalConstant.USERNAME, logins1.getUserName()); PreferencesManager.savePref(GlobalConstant.USER_ID, logins1.getUserId()); PreferencesManager.savePref(GlobalConstant.TOKEN, logins1.getSession_id()); PreferencesManager.savePref(GlobalConstant.FULL_NAME, logins1.getFullName()); PreferencesManager.savePref(GlobalConstant.BIO, logins1.getBio()); PreferencesManager.savePref(GlobalConstant.FOLLOWED_BY, logins1.getFollowedBy()); PreferencesManager.savePref(GlobalConstant.FOLLOWS, logins1.getFollows()); PreferencesManager.savePref(GlobalConstant.MEDIA, logins1.getMedia()); startActivity(intent); } } } } }); }
Example 18
Source File: MainActivity.java From Instagram-Profile-Downloader with MIT License | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); AudienceNetworkAds.initialize(this); dataObjectRepositry = DataObjectRepositry.dataObjectRepositry; ButterKnife.bind(this); getSafeIntent(); initUI(); onClick(); addToFirebase(); interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); AdRequest adRequest = new AdRequest.Builder().build(); interstitialAd.loadAd(adRequest); allLoginUserList.clear(); LiveData<List<Logins>> loggedInUsers = DataObjectRepositry.dataObjectRepositry.getAllUsers(); loggedInUsers.observe(MainActivity.this, new Observer<List<Logins>>() { @Override public void onChanged(List<Logins> logins) { if (logins.size() > 0) { allLoginUserList.clear(); for (Logins logins1 : logins) { DrawerMenuPojo drawerMenuPojo1 = new DrawerMenuPojo(); drawerMenuPojo1.setMenuName(logins1.getUserName()); drawerMenuPojo1.setImage(R.drawable.ic_account); allLoginUserList.add(drawerMenuPojo1); } } } }); if (!TextUtils.isEmpty(user_id)) { showLoading(); new GetUserInfo(user_id).execute(); } else { changeFragment(new StoriesFragment()); toolbar.setTitle("Stories"); } toolbar.setTitle("Stories"); }
Example 19
Source File: DownloadHistoryVideoFragment.java From Instagram-Profile-Downloader with MIT License | 4 votes |
public void setDownloades() { LiveData<List<Downloads>> loggedInUsers = dataObjectRepositry.getAllDownloads(); loggedInUsers.observe(getActivity(), new Observer<List<Downloads>>() { @Override public void onChanged(List<Downloads> downloads) { if (downloads.size() > 0) { modelListCopy.clear(); modelList.clear(); stories.clear(); for (Downloads d : downloads) { if (d.getType() == 1) { if(new File(d.getPath()).exists()) { modelList.add(d.getPath()); modelListCopy.add(d.getPath()); StoryModel storyModel = new StoryModel(); storyModel.setFileName(d.getFilename()); storyModel.setFilePath(d.getPath()); storyModel.setSaved(true); storyModel.setType(d.getType()); storyModel.setId(d.getId()); stories.add(storyModel); }else dataObjectRepositry.deleteDownloadedData(d.getId()); } } Collections.reverse(modelList); Collections.reverse(stories); Collections.reverse(modelListCopy); adapter.addAll(modelList,stories, modelListCopy); adLoader.loadAds(new AdRequest.Builder().build(), modelList.size() / spaceBetweenAds + 1); } else { no_downloads.setVisibility(View.VISIBLE); } if (modelListCopy.size() == 0) { no_downloads.setVisibility(View.VISIBLE); } else no_downloads.setVisibility(View.GONE); } }); }
Example 20
Source File: DownloadHistoryPhotoFragment.java From Instagram-Profile-Downloader with MIT License | 4 votes |
public void setDownloades() { LiveData<List<Downloads>> loggedInUsers = dataObjectRepositry.getAllDownloads(); loggedInUsers.observe(getActivity(), new Observer<List<Downloads>>() { @Override public void onChanged(List<Downloads> downloads) { if (downloads.size() > 0) { modelListCopy.clear(); modelList.clear(); stories.clear(); for (Downloads d : downloads) { if (d.getType() == 0) { if(new File(d.getPath()).exists()) { modelList.add(d.getPath()); modelListCopy.add(d.getPath()); StoryModel storyModel = new StoryModel(); storyModel.setFileName(d.getFilename()); storyModel.setFilePath(d.getPath()); storyModel.setSaved(true); storyModel.setType(d.getType()); storyModel.setId(d.getId()); stories.add(storyModel); }else dataObjectRepositry.deleteDownloadedData(d.getId()); } } Collections.reverse(modelList); Collections.reverse(stories); Collections.reverse(modelListCopy); adapter.addAll(modelList, stories, modelListCopy); adLoader.loadAds(new AdRequest.Builder().build(), modelList.size() / spaceBetweenAds + 1); } else { no_downloads.setVisibility(View.VISIBLE); } if (modelListCopy.size() == 0) { no_downloads.setVisibility(View.VISIBLE); } else no_downloads.setVisibility(View.GONE); } }); }