Java Code Examples for androidx.lifecycle.ViewModelProvider#get()
The following examples show how to use
androidx.lifecycle.ViewModelProvider#get() .
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: KeywordQueryFragment.java From lttrs-android with Apache License 2.0 | 6 votes |
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final Bundle bundle = getArguments(); final KeywordLabel keywordLabel = KeywordQueryFragmentArgs.fromBundle( bundle == null ? new Bundle() : bundle ).getKeyword(); final ViewModelProvider viewModelProvider = new ViewModelProvider( getViewModelStore(), new KeywordQueryViewModelFactory( requireActivity().getApplication(), getLttrsViewModel().getAccount(), keywordLabel.getKeyword() ) ); this.keywordQueryViewModel = viewModelProvider.get(KeywordQueryViewModel.class); onLabelOpened(keywordLabel); return super.onCreateView(inflater, container, savedInstanceState); }
Example 2
Source File: SearchQueryFragment.java From lttrs-android with Apache License 2.0 | 5 votes |
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final Bundle bundle = getArguments(); final String term = SearchQueryFragmentArgs.fromBundle(bundle == null ? new Bundle() : bundle).getText(); final ViewModelProvider viewModelProvider = new ViewModelProvider( getViewModelStore(), new SearchQueryViewModelFactory( requireActivity().getApplication(), getLttrsViewModel().getAccount(), term ) ); this.searchQueryViewModel = viewModelProvider.get(SearchQueryViewModel.class); return super.onCreateView(inflater, container, savedInstanceState); }
Example 3
Source File: AbstractMailboxQueryFragment.java From lttrs-android with Apache License 2.0 | 5 votes |
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final ViewModelProvider viewModelProvider = new ViewModelProvider( getViewModelStore(), new MailboxQueryViewModelFactory( requireActivity().getApplication(), getLttrsViewModel().getAccount(), getMailboxId() ) ); this.mailboxQueryViewModel = viewModelProvider.get(MailboxQueryViewModel.class); return super.onCreateView(inflater, container, savedInstanceState); }
Example 4
Source File: AbstractLttrsFragment.java From lttrs-android with Apache License 2.0 | 5 votes |
LttrsViewModel getLttrsViewModel() { final ViewModelProvider viewModelProvider = new ViewModelProvider( requireActivity(), getDefaultViewModelProviderFactory() ); return viewModelProvider.get(LttrsViewModel.class); }
Example 5
Source File: AbstractSetupFragment.java From lttrs-android with Apache License 2.0 | 5 votes |
public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ViewModelProvider viewModelProvider = new ViewModelProvider( requireActivity(), getDefaultViewModelProviderFactory() ); this.setupViewModel = viewModelProvider.get(SetupViewModel.class); }
Example 6
Source File: SetupActivity.java From lttrs-android with Apache License 2.0 | 5 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ActivitySetupBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_setup); final NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment ); final ViewModelProvider viewModelProvider = new ViewModelProvider( this, getDefaultViewModelProviderFactory() ); final SetupViewModel setupViewModel = viewModelProvider.get(SetupViewModel.class); setupViewModel.getRedirection().observe(this, targetEvent -> { if (targetEvent.isConsumable()) { final SetupViewModel.Target target = targetEvent.consume(); switch (target) { case LTTRS: redirectToLttrs(); break; case ENTER_PASSWORD: navController.navigate(SetupNavigationDirections.enterPassword()); break; case ENTER_URL: navController.navigate(SetupNavigationDirections.enterSessionResource()); break; default: throw new IllegalStateException(String.format("Unable to navigate to target %s", target)); } } }); setupViewModel.getWarningMessage().observe(this, event -> { if (event.isConsumable()) { Snackbar.make(binding.getRoot(), event.consume(), Snackbar.LENGTH_LONG).show(); } }); }
Example 7
Source File: ThreadFragment.java From lttrs-android with Apache License 2.0 | 4 votes |
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); final Bundle bundle = getArguments(); final ThreadFragmentArgs arguments = ThreadFragmentArgs.fromBundle(bundle == null ? new Bundle() : bundle); final String threadId = arguments.getThread(); final String label = arguments.getLabel(); final boolean triggerRead = !Arrays.asList(arguments.getKeywords()).contains(Keyword.SEEN); final ViewModelProvider viewModelProvider = new ViewModelProvider( getViewModelStore(), new ThreadViewModelFactory( requireActivity().getApplication(), getLttrsViewModel().getAccount(), threadId, label, triggerRead ) ); threadViewModel = viewModelProvider.get(ThreadViewModel.class); binding = DataBindingUtil.inflate(inflater, R.layout.fragment_thread, container, false); //do we want a custom layout manager that does *NOT* remember scroll position when more //than one item is expanded. with variable sized items this might be annoying threadAdapter = new ThreadAdapter(threadViewModel.expandedItems); threadAdapter.setSubjectWithImportance(SubjectWithImportance.of( threadId, Strings.emptyToNull(arguments.getSubject()), arguments.getImportant() )); //the default change animation causes UI glitches when expanding or collapsing item //for now it's better to just disable it. In the future we may write our own animator RecyclerView.ItemAnimator itemAnimator = binding.list.getItemAnimator(); if (itemAnimator instanceof SimpleItemAnimator) { ((SimpleItemAnimator) itemAnimator).setSupportsChangeAnimations(false); } binding.list.setAdapter(threadAdapter); threadViewModel.getEmails().observe(getViewLifecycleOwner(), this::onEmailsChanged); threadViewModel.getSubjectWithImportance().observe(getViewLifecycleOwner(), threadAdapter::setSubjectWithImportance); threadViewModel.getFlagged().observe(getViewLifecycleOwner(), threadAdapter::setFlagged); threadViewModel.getMenuConfiguration().observe(getViewLifecycleOwner(), menuConfiguration -> { this.menuConfiguration = menuConfiguration; requireActivity().invalidateOptionsMenu(); }); threadAdapter.setOnFlaggedToggledListener(this); threadAdapter.setOnComposeActionTriggeredListener(this); threadViewModel.getThreadViewRedirect().observe(getViewLifecycleOwner(), this::onThreadViewRedirect); return binding.getRoot(); }
Example 8
Source File: LttrsActivity.java From lttrs-android with Apache License 2.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_lttrs); final ViewModelProvider viewModelProvider = new ViewModelProvider( this, getDefaultViewModelProviderFactory() ); lttrsViewModel = viewModelProvider.get(LttrsViewModel.class); lttrsViewModel.getHasAccounts().observe(this, this::onHasAccountsChanged); setSupportActionBar(binding.toolbar); final NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment ); binding.drawerLayout.addDrawerListener(this); labelListAdapter.setOnMailboxOverviewItemSelectedListener((label, currentlySelected) -> { binding.drawerLayout.closeDrawer(GravityCompat.START); if (currentlySelected) { return; } final boolean navigateToInbox = label.getRole() == Role.INBOX; if (navigateToInbox) { navController.navigate(LttrsNavigationDirections.actionToInbox()); } else if (label instanceof MailboxOverviewItem) { final MailboxOverviewItem mailbox = (MailboxOverviewItem) label; navController.navigate(LttrsNavigationDirections.actionToMailbox(mailbox.id)); } else if (label instanceof KeywordLabel) { final KeywordLabel keyword = (KeywordLabel) label; navController.navigate(LttrsNavigationDirections.actionToKeyword(keyword)); } else { throw new IllegalStateException(String.format("%s is an unsupported label", label.getClass())); } if (mSearchItem != null) { mSearchItem.collapseActionView(); } //currently unused should remain here in case we bring scrollable toolbar back binding.appBarLayout.setExpanded(true, false); }); binding.mailboxList.setAdapter(labelListAdapter); lttrsViewModel.getNavigatableLabels().observe(this, labelListAdapter::submitList); }
Example 9
Source File: ComposeActivity.java From lttrs-android with Apache License 2.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!SetupCache.hasAccounts(this)) { redirectToSetupActivity(); finishAffinity(); return; } binding = DataBindingUtil.setContentView(this, R.layout.activity_compose); setupActionBar(); final ViewModelProvider viewModelProvider = new ViewModelProvider( this, new ComposeViewModelFactory( getApplication(), getViewModelParameter(savedInstanceState) ) ); composeViewModel = viewModelProvider.get(ComposeViewModel.class); composeViewModel.getErrorMessage().observe(this, event -> { if (event.isConsumable()) { final String message = event.consume(); new MaterialAlertDialogBuilder(this) .setTitle(message) .setPositiveButton(R.string.ok, null) .show(); } }); binding.setComposeViewModel(composeViewModel); binding.setLifecycleOwner(this); binding.to.addTextChangedListener(new ChipTextWatcher(binding.to)); binding.to.setOnFocusChangeListener( (v, hasFocus) -> ChipDrawableSpan.apply(this, binding.to.getEditableText(), hasFocus) ); binding.cc.addTextChangedListener(new ChipTextWatcher(binding.cc)); binding.cc.setOnFocusChangeListener( (v, hasFocus) -> ChipDrawableSpan.apply(this, binding.cc.getEditableText(), hasFocus) ); binding.moreAddresses.setOnClickListener((v -> composeViewModel.showExtendedAddresses())); binding.subject.setOnFocusChangeListener(this::focusOnBodyOrSubject); binding.body.setOnFocusChangeListener(this::focusOnBodyOrSubject); binding.toLabel.setOnClickListener(v -> requestFocusAndOpenKeyboard(binding.to)); binding.placeholder.setOnClickListener(v -> requestFocusAndOpenKeyboard(binding.body)); //TODO once we handle instance state ourselves we need to call ChipDrawableSpan.reset() on `to` }