use of androidx.lifecycle.ViewModelProvider in project OpenBot by isl-org.
the class SettingsFragment method onCreatePreferences.
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
vehicle = mViewModel.getVehicle().getValue();
connection = findPreference("connection");
if (connection != null) {
connection.setTitle("No Device");
if (vehicle != null && vehicle.isUsbConnected()) {
connection.setChecked(true);
connection.setTitle(vehicle.getUsbConnection().getProductName());
} else {
connection.setTitle("No Device");
connection.setChecked(false);
}
connection.setOnPreferenceClickListener(preference -> {
Timber.d(String.valueOf(connection.isChecked()));
if (vehicle != null) {
if (connection.isChecked()) {
vehicle.connectUsb();
if (vehicle.isUsbConnected())
connection.setTitle(vehicle.getUsbConnection().getProductName());
else {
connection.setTitle("No Device");
connection.setChecked(false);
Toast.makeText(requireContext().getApplicationContext(), "Please check the USB connection.", Toast.LENGTH_SHORT).show();
}
} else {
vehicle.disconnectUsb();
connection.setTitle("No Device");
}
mViewModel.setUsbStatus(vehicle.isUsbConnected());
}
return true;
});
}
camera = findPreference("camera");
if (camera != null) {
camera.setChecked(PermissionUtils.hasCameraPermission(requireActivity()));
camera.setOnPreferenceChangeListener((preference, newValue) -> {
if (camera.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_CAMERA)) {
startInstalledAppDetailsActivity(requireActivity());
} else {
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_CAMERA });
}
}
return false;
});
}
storage = findPreference("storage");
if (storage != null) {
storage.setChecked(PermissionUtils.hasStoragePermission(requireActivity()));
storage.setOnPreferenceChangeListener((preference, newValue) -> {
if (storage.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_STORAGE)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_STORAGE });
}
return false;
});
}
location = findPreference("location");
if (location != null) {
location.setChecked(PermissionUtils.hasLocationPermission(requireActivity()));
location.setOnPreferenceChangeListener((preference, newValue) -> {
if (location.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), PERMISSION_LOCATION)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { PERMISSION_LOCATION });
}
return false;
});
}
mic = findPreference("mic");
if (mic != null) {
mic.setChecked(PermissionUtils.hasAudioPermission(requireActivity()));
mic.setOnPreferenceChangeListener((preference, newValue) -> {
if (mic.isChecked())
startInstalledAppDetailsActivity(requireActivity());
else {
if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_AUDIO)) {
startInstalledAppDetailsActivity(requireActivity());
} else
requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_AUDIO });
}
return false;
});
}
ListPreference streamMode = findPreference("video_server");
if (streamMode != null)
streamMode.setOnPreferenceChangeListener((preference, newValue) -> {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(R.string.confirm_title);
builder.setMessage(R.string.stream_change_body);
builder.setPositiveButton("Yes", (dialog, id) -> {
streamMode.setValue(newValue.toString());
restartApp();
});
builder.setNegativeButton("Cancel", (dialog, id) -> streamMode.setValue(streamMode.getEntry().toString()));
AlertDialog dialog = builder.create();
dialog.show();
return false;
});
}
use of androidx.lifecycle.ViewModelProvider in project Signal-Android by WhisperSystems.
the class CreatePaymentFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = view.findViewById(R.id.create_payment_fragment_toolbar);
toolbar.setNavigationOnClickListener(this::goBack);
CreatePaymentFragmentArgs arguments = CreatePaymentFragmentArgs.fromBundle(requireArguments());
CreatePaymentViewModel.Factory factory = new CreatePaymentViewModel.Factory(arguments.getPayee(), arguments.getNote());
CreatePaymentViewModel viewModel = new ViewModelProvider(Navigation.findNavController(view).getViewModelStoreOwner(R.id.payments_create), factory).get(CreatePaymentViewModel.class);
constraintLayout = view.findViewById(R.id.create_payment_fragment_amount_header);
request = view.findViewById(R.id.create_payment_fragment_request);
amount = view.findViewById(R.id.create_payment_fragment_amount);
exchange = view.findViewById(R.id.create_payment_fragment_exchange);
pay = view.findViewById(R.id.create_payment_fragment_pay);
balance = view.findViewById(R.id.create_payment_fragment_balance);
note = view.findViewById(R.id.create_payment_fragment_note);
addNote = view.findViewById(R.id.create_payment_fragment_add_note);
toggle = view.findViewById(R.id.create_payment_fragment_toggle);
TextView decimal = view.findViewById(R.id.create_payment_fragment_keyboard_decimal);
decimal.setText(String.valueOf(DecimalFormatSymbols.getInstance().getDecimalSeparator()));
View infoTapTarget = view.findViewById(R.id.create_payment_fragment_info_tap_region);
// noinspection CodeBlock2Expr
infoTapTarget.setOnClickListener(v -> {
new AlertDialog.Builder(requireContext()).setMessage(R.string.CreatePaymentFragment__conversions_are_just_estimates).setPositiveButton(android.R.string.ok, (dialog, which) -> dialog.dismiss()).setNegativeButton(R.string.LearnMoreTextView_learn_more, (dialog, which) -> {
dialog.dismiss();
CommunicationActions.openBrowserLink(requireContext(), getString(R.string.CreatePaymentFragment__learn_more__conversions));
}).show();
});
initializeInfoIcon();
note.setOnClickListener(v -> SafeNavigation.safeNavigate(Navigation.findNavController(v), R.id.action_createPaymentFragment_to_editPaymentNoteFragment));
addNote.setOnClickListener(v -> SafeNavigation.safeNavigate(Navigation.findNavController(v), R.id.action_createPaymentFragment_to_editPaymentNoteFragment));
pay.setOnClickListener(v -> {
NavDirections directions = CreatePaymentFragmentDirections.actionCreatePaymentFragmentToConfirmPaymentFragment(viewModel.getCreatePaymentDetails()).setFinishOnConfirm(arguments.getFinishOnConfirm());
SafeNavigation.safeNavigate(Navigation.findNavController(v), directions);
});
toggle.setOnClickListener(v -> viewModel.toggleMoneyInputTarget());
initializeConstraintSets();
initializeKeyboardButtons(view, viewModel);
viewModel.getInputState().observe(getViewLifecycleOwner(), inputState -> {
updateAmount(inputState);
updateExchange(inputState);
updateMoneyInputTarget(inputState.getInputTarget());
});
viewModel.getIsPaymentsSupportedByPayee().observe(getViewLifecycleOwner(), isSupported -> {
if (!isSupported)
RecipientHasNotEnabledPaymentsDialog.show(requireContext(), () -> goBack(requireView()));
});
viewModel.isValidAmount().observe(getViewLifecycleOwner(), this::updateRequestAmountButtons);
viewModel.getNote().observe(getViewLifecycleOwner(), this::updateNote);
viewModel.getSpendableBalance().observe(getViewLifecycleOwner(), this::updateBalance);
viewModel.getCanSendPayment().observe(getViewLifecycleOwner(), this::updatePayAmountButtons);
}
use of androidx.lifecycle.ViewModelProvider in project Signal-Android by WhisperSystems.
the class PaymentsTransferFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PaymentsTransferViewModel viewModel = new ViewModelProvider(Navigation.findNavController(view).getViewModelStoreOwner(R.id.payments_transfer), new PaymentsTransferViewModel.Factory()).get(PaymentsTransferViewModel.class);
Toolbar toolbar = view.findViewById(R.id.payments_transfer_toolbar);
view.findViewById(R.id.payments_transfer_scan_qr).setOnClickListener(v -> scanQrCode());
view.findViewById(R.id.payments_transfer_next).setOnClickListener(v -> next(viewModel.getOwnAddress()));
address = view.findViewById(R.id.payments_transfer_to_address);
address.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
return next(viewModel.getOwnAddress());
}
return false;
});
viewModel.getAddress().observe(getViewLifecycleOwner(), address::setText);
toolbar.setNavigationOnClickListener(v -> {
ViewUtil.hideKeyboard(requireContext(), v);
Navigation.findNavController(v).popBackStack();
});
}
use of androidx.lifecycle.ViewModelProvider in project Signal-Android by WhisperSystems.
the class DeleteAccountFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Spinner countrySpinner = view.findViewById(R.id.delete_account_fragment_country_spinner);
View confirm = view.findViewById(R.id.delete_account_fragment_delete);
bullets = view.findViewById(R.id.delete_account_fragment_bullets);
countryCode = view.findViewById(R.id.delete_account_fragment_country_code);
number = view.findViewById(R.id.delete_account_fragment_number);
viewModel = new ViewModelProvider(requireActivity(), new DeleteAccountViewModel.Factory(new DeleteAccountRepository())).get(DeleteAccountViewModel.class);
viewModel.getCountryDisplayName().observe(getViewLifecycleOwner(), this::setCountryDisplay);
viewModel.getRegionCode().observe(getViewLifecycleOwner(), this::handleRegionUpdated);
viewModel.getEvents().observe(getViewLifecycleOwner(), this::handleEvent);
viewModel.getWalletBalance().observe(getViewLifecycleOwner(), this::updateBullets);
initializeNumberInput();
countryCode.getInput().addTextChangedListener(new AfterTextChanged(this::afterCountryCodeChanged));
countryCode.getInput().setImeOptions(EditorInfo.IME_ACTION_NEXT);
confirm.setOnClickListener(unused -> viewModel.submit());
initializeSpinner(countrySpinner);
}
use of androidx.lifecycle.ViewModelProvider in project Signal-Android by WhisperSystems.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAdapter adapter = new MyAdapter(this);
RecyclerView list = findViewById(R.id.list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
list.setAdapter(adapter);
list.setLayoutManager(layoutManager);
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
adapter.setPagingController(viewModel.getPagingController());
viewModel.getList().observe(this, newList -> {
adapter.submitList(newList);
});
findViewById(R.id.invalidate_btn).setOnClickListener(v -> {
viewModel.getPagingController().onDataInvalidated();
});
findViewById(R.id.down250_btn).setOnClickListener(v -> {
int target = Math.min(adapter.getItemCount() - 1, layoutManager.findFirstVisibleItemPosition() + 250);
layoutManager.scrollToPosition(target);
});
findViewById(R.id.up250_btn).setOnClickListener(v -> {
int target = Math.max(0, layoutManager.findFirstVisibleItemPosition() - 250);
layoutManager.scrollToPosition(target);
});
findViewById(R.id.prepend_btn).setOnClickListener(v -> {
viewModel.prependItems();
});
}
Aggregations