use of androidx.lifecycle.ViewModelProvider in project Signal-Android by WhisperSystems.
the class ConversationFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bundle) {
final View view = inflater.inflate(R.layout.conversation_fragment, container, false);
videoContainer = view.findViewById(R.id.video_container);
list = view.findViewById(android.R.id.list);
composeDivider = view.findViewById(R.id.compose_divider);
layoutTransition = new LayoutTransition();
transitionListener = new TransitionListener(list);
scrollToBottomButton = view.findViewById(R.id.scroll_to_bottom);
scrollToMentionButton = view.findViewById(R.id.scroll_to_mention);
scrollDateHeader = view.findViewById(R.id.scroll_date_header);
toolbarShadow = requireActivity().findViewById(R.id.conversation_toolbar_shadow);
reactionsShade = view.findViewById(R.id.reactions_shade);
bottomActionBar = view.findViewById(R.id.conversation_bottom_action_bar);
final LinearLayoutManager layoutManager = new SmoothScrollingLinearLayoutManager(getActivity(), true);
final ConversationItemAnimator conversationItemAnimator = new ConversationItemAnimator(() -> {
ConversationAdapter adapter = getListAdapter();
if (adapter == null) {
return false;
} else {
return Util.hasItems(adapter.getSelectedItems());
}
}, () -> conversationViewModel.shouldPlayMessageAnimations() && list.getScrollState() == RecyclerView.SCROLL_STATE_IDLE, () -> list.canScrollVertically(1) || list.canScrollVertically(-1));
multiselectItemDecoration = new MultiselectItemDecoration(requireContext(), () -> conversationViewModel.getWallpaper().getValue());
list.setHasFixedSize(false);
list.setLayoutManager(layoutManager);
RecyclerViewColorizer recyclerViewColorizer = new RecyclerViewColorizer(list);
list.addItemDecoration(multiselectItemDecoration);
list.setItemAnimator(conversationItemAnimator);
getViewLifecycleOwner().getLifecycle().addObserver(multiselectItemDecoration);
snapToTopDataObserver = new ConversationSnapToTopDataObserver(list, new ConversationScrollRequestValidator());
conversationBanner = (ConversationBannerView) inflater.inflate(R.layout.conversation_item_banner, container, false);
topLoadMoreView = (ViewSwitcher) inflater.inflate(R.layout.load_more_header, container, false);
bottomLoadMoreView = (ViewSwitcher) inflater.inflate(R.layout.load_more_header, container, false);
initializeLoadMoreView(topLoadMoreView);
initializeLoadMoreView(bottomLoadMoreView);
typingView = (ConversationTypingView) inflater.inflate(R.layout.conversation_typing_view, container, false);
new ConversationItemSwipeCallback(conversationMessage -> actionMode == null && MenuState.canReplyToMessage(recipient.get(), MenuState.isActionMessage(conversationMessage.getMessageRecord()), conversationMessage.getMessageRecord(), messageRequestViewModel.shouldShowMessageRequest(), groupViewModel.isNonAdminInAnnouncementGroup()), this::handleReplyMessage).attachToRecyclerView(list);
giphyMp4ProjectionRecycler = initializeGiphyMp4();
this.groupViewModel = new ViewModelProvider(getParentFragment(), new ConversationGroupViewModel.Factory()).get(ConversationGroupViewModel.class);
this.messageCountsViewModel = new ViewModelProvider(getParentFragment()).get(MessageCountsViewModel.class);
this.conversationViewModel = new ViewModelProvider(getParentFragment(), new ConversationViewModel.Factory()).get(ConversationViewModel.class);
conversationViewModel.getChatColors().observe(getViewLifecycleOwner(), recyclerViewColorizer::setChatColors);
conversationViewModel.getMessages().observe(getViewLifecycleOwner(), messages -> {
ConversationAdapter adapter = getListAdapter();
if (adapter != null) {
getListAdapter().submitList(messages, () -> {
list.post(() -> conversationViewModel.onMessagesCommitted(messages));
});
}
});
conversationViewModel.getConversationMetadata().observe(getViewLifecycleOwner(), this::presentConversationMetadata);
conversationViewModel.getShowMentionsButton().observe(getViewLifecycleOwner(), shouldShow -> {
if (shouldShow) {
ViewUtil.animateIn(scrollToMentionButton, mentionButtonInAnimation);
} else {
ViewUtil.animateOut(scrollToMentionButton, mentionButtonOutAnimation, View.INVISIBLE);
}
});
conversationViewModel.getShowScrollToBottom().observe(getViewLifecycleOwner(), shouldShow -> {
if (shouldShow) {
ViewUtil.animateIn(scrollToBottomButton, scrollButtonInAnimation);
} else {
ViewUtil.animateOut(scrollToBottomButton, scrollButtonOutAnimation, View.INVISIBLE);
}
});
scrollToBottomButton.setOnClickListener(v -> scrollToBottom());
scrollToMentionButton.setOnClickListener(v -> scrollToNextMention());
updateToolbarDependentMargins();
colorizer = new Colorizer();
conversationViewModel.getNameColorsMap().observe(getViewLifecycleOwner(), nameColorsMap -> {
colorizer.onNameColorsChanged(nameColorsMap);
ConversationAdapter adapter = getListAdapter();
if (adapter != null) {
adapter.notifyItemRangeChanged(0, adapter.getItemCount(), ConversationAdapter.PAYLOAD_NAME_COLORS);
}
});
conversationUpdateTick = new ConversationUpdateTick(this::updateConversationItemTimestamps);
getViewLifecycleOwner().getLifecycle().addObserver(conversationUpdateTick);
listener.getVoiceNoteMediaController().getVoiceNotePlayerViewState().observe(getViewLifecycleOwner(), state -> conversationViewModel.setInlinePlayerVisible(state.isPresent()));
conversationViewModel.getConversationTopMargin().observe(getViewLifecycleOwner(), topMargin -> {
lastSeenScrollOffset = topMargin;
ViewUtil.setTopMargin(scrollDateHeader, topMargin + ViewUtil.dpToPx(8));
});
conversationViewModel.getActiveNotificationProfile().observe(getViewLifecycleOwner(), this::updateNotificationProfileStatus);
initializeScrollButtonAnimations();
initializeResources();
initializeMessageRequestViewModel();
initializeListAdapter();
conversationViewModel.getSearchQuery().observe(getViewLifecycleOwner(), this::onSearchQueryUpdated);
return view;
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class WalletAddressDialogFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log.info("opening dialog {}", getClass().getName());
viewModel = new ViewModelProvider(getParentFragment()).get(WalletAddressViewModel.class);
viewModel.qrCode.observe(this, qrCode -> {
final BitmapDrawable qrDrawable = new BitmapDrawable(getResources(), qrCode);
qrDrawable.setFilterBitmap(false);
imageView.setImageDrawable(qrDrawable);
});
viewModel.currentAddress.observe(this, currentAddress -> {
final CharSequence label = WalletUtils.formatAddress(currentAddress, Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE);
labelView.setText(label);
});
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class AddressBookActivity method onCreate.
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FragmentManager fragmentManager = getSupportFragmentManager();
setContentView(R.layout.address_book_content);
final ViewPager2 pager = findViewById(R.id.address_book_pager);
final ViewPagerTabs pagerTabs = findViewById(R.id.address_book_pager_tabs);
pagerTabs.addTabLabels(TAB_LABELS);
final boolean twoPanes = getResources().getBoolean(R.bool.address_book_two_panes);
walletActivityViewModel = new ViewModelProvider(this).get(AbstractWalletActivityViewModel.class);
walletActivityViewModel.wallet.observe(this, wallet -> invalidateOptionsMenu());
viewModel = new ViewModelProvider(this).get(AddressBookViewModel.class);
viewModel.pageTo.observe(this, new Event.Observer<Integer>() {
@Override
protected void onEvent(final Integer position) {
if (!twoPanes)
pager.setCurrentItem(position, true);
}
});
viewModel.showEditAddressBookEntryDialog.observe(this, new Event.Observer<Address>() {
@Override
protected void onEvent(final Address address) {
EditAddressBookEntryFragment.edit(fragmentManager, address);
}
});
viewModel.showScanOwnAddressDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
final DialogBuilder dialog = DialogBuilder.dialog(AddressBookActivity.this, R.string.address_book_options_scan_title, R.string.address_book_options_scan_own_address);
dialog.singleDismissButton(null);
dialog.show();
}
});
viewModel.showScanInvalidDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
final DialogBuilder dialog = DialogBuilder.dialog(AddressBookActivity.this, R.string.address_book_options_scan_title, R.string.address_book_options_scan_invalid);
dialog.singleDismissButton(null);
dialog.show();
}
});
if (twoPanes) {
final RecyclerView recyclerView = (RecyclerView) pager.getChildAt(0);
recyclerView.setClipToPadding(false);
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
final int width = recyclerView.getWidth();
recyclerView.setPadding(0, 0, width / 2, 0);
pager.setCurrentItem(0);
});
pager.setUserInputEnabled(false);
pagerTabs.setMode(ViewPagerTabs.Mode.STATIC);
} else {
pager.setPageTransformer(new ZoomOutPageTransformer());
pager.registerOnPageChangeCallback(pagerTabs.getPageChangeCallback());
pagerTabs.setMode(ViewPagerTabs.Mode.DYNAMIC);
}
pager.setOffscreenPageLimit(1);
pager.setAdapter(new AddressBookActivity.PagerAdapter());
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class WalletTransactionsFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fragmentManager = getChildFragmentManager();
setHasOptionsMenu(true);
activityViewModel = new ViewModelProvider(activity).get(WalletActivityViewModel.class);
viewModel = new ViewModelProvider(this).get(WalletTransactionsViewModel.class);
viewModel.direction.observe(this, direction -> activity.invalidateOptionsMenu());
viewModel.transactions.observe(this, transactions -> {
if (transactions.isEmpty()) {
viewGroup.setDisplayedChild(0);
final WalletTransactionsViewModel.Direction direction = viewModel.direction.getValue();
final WarningType warning = viewModel.warning.getValue();
final SpannableStringBuilder emptyText = new SpannableStringBuilder(getString(direction == WalletTransactionsViewModel.Direction.SENT ? R.string.wallet_transactions_fragment_empty_text_sent : R.string.wallet_transactions_fragment_empty_text_received));
emptyText.setSpan(new StyleSpan(Typeface.BOLD), 0, emptyText.length(), SpannableStringBuilder.SPAN_POINT_MARK);
if (direction != WalletTransactionsViewModel.Direction.SENT)
emptyText.append("\n\n").append(getString(R.string.wallet_transactions_fragment_empty_text_howto));
if (warning == WarningType.BACKUP) {
final int start = emptyText.length();
emptyText.append("\n\n").append(getString(R.string.wallet_transactions_fragment_empty_remind_backup));
emptyText.setSpan(new StyleSpan(Typeface.BOLD), start, emptyText.length(), SpannableStringBuilder.SPAN_POINT_MARK);
}
emptyView.setText(emptyText);
} else {
viewGroup.setDisplayedChild(1);
}
});
viewModel.selectedTransaction.observe(this, transactionId -> {
adapter.setSelectedTransaction(transactionId);
final int position = adapter.positionOf(transactionId);
if (position != RecyclerView.NO_POSITION)
recyclerView.smoothScrollToPosition(position);
});
viewModel.list.observe(this, listItems -> {
adapter.submitList(listItems);
activityViewModel.transactionsLoadingFinished();
});
viewModel.showBitmapDialog.observe(this, new Event.Observer<Bitmap>() {
@Override
protected void onEvent(final Bitmap bitmap) {
BitmapFragment.show(fragmentManager, bitmap);
}
});
viewModel.showEditAddressBookEntryDialog.observe(this, new Event.Observer<Address>() {
@Override
protected void onEvent(final Address address) {
EditAddressBookEntryFragment.edit(fragmentManager, address);
}
});
viewModel.showReportIssueDialog.observe(this, new Event.Observer<Sha256Hash>() {
@Override
protected void onEvent(final Sha256Hash transactionHash) {
ReportIssueDialogFragment.show(fragmentManager, R.string.report_issue_dialog_title_transaction, R.string.report_issue_dialog_message_issue, Constants.REPORT_SUBJECT_ISSUE, transactionHash);
}
});
adapter = new TransactionsAdapter(activity, this, this);
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class BlockListFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
walletActivityViewModel = new ViewModelProvider(activity).get(AbstractWalletActivityViewModel.class);
walletActivityViewModel.wallet.observe(this, wallet -> maybeSubmitList());
activityViewModel = new ViewModelProvider(activity).get(NetworkMonitorViewModel.class);
activityViewModel.selectedItem.observe(this, item -> {
if (item instanceof Sha256Hash) {
final Sha256Hash blockHash = (Sha256Hash) item;
adapter.setSelectedBlock(blockHash);
final int position = adapter.positionOf(blockHash);
if (position != RecyclerView.NO_POSITION)
recyclerView.smoothScrollToPosition(position);
} else {
adapter.setSelectedBlock(null);
}
});
viewModel = new ViewModelProvider(this).get(BlockListViewModel.class);
viewModel.blocks.observe(this, blocks -> {
maybeSubmitList();
viewGroup.setDisplayedChild(1);
viewModel.getTransactions().loadTransactions();
});
viewModel.getTransactions().observe(this, transactions -> maybeSubmitList());
viewModel.getTime().observe(this, time -> maybeSubmitList());
adapter = new BlockListAdapter(activity, this, this);
}
Aggregations