Search in sources :

Example 86 with ViewModelProvider

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;
        });
}
Also used : Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Intent(android.content.Intent) PreferenceFragmentCompat(androidx.preference.PreferenceFragmentCompat) Handler(android.os.Handler) Toast(android.widget.Toast) View(android.view.View) PERMISSION_AUDIO(org.openbot.utils.Constants.PERMISSION_AUDIO) Settings(android.provider.Settings) PERMISSION_STORAGE(org.openbot.utils.Constants.PERMISSION_STORAGE) R(org.openbot.R) PERMISSION_CAMERA(org.openbot.utils.Constants.PERMISSION_CAMERA) Vehicle(org.openbot.vehicle.Vehicle) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) ViewModelProvider(androidx.lifecycle.ViewModelProvider) SwitchPreferenceCompat(androidx.preference.SwitchPreferenceCompat) Constants(org.openbot.utils.Constants) PERMISSION_LOCATION(org.openbot.utils.Constants.PERMISSION_LOCATION) Timber(timber.log.Timber) PermissionUtils(org.openbot.utils.PermissionUtils) Nullable(androidx.annotation.Nullable) ListPreference(androidx.preference.ListPreference) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) Activity(android.app.Activity) AlertDialog(androidx.appcompat.app.AlertDialog) ListPreference(androidx.preference.ListPreference) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 87 with ViewModelProvider

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);
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) AppCompatResources(androidx.appcompat.content.res.AppCompatResources) DecimalFormatSymbols(java.text.DecimalFormatSymbols) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) TransitionManager(androidx.transition.TransitionManager) ViewUtil(org.thoughtcrime.securesms.util.ViewUtil) HashMap(java.util.HashMap) Currency(java.util.Currency) DrawableCompat(androidx.core.graphics.drawable.DrawableCompat) Drawable(android.graphics.drawable.Drawable) R(org.thoughtcrime.securesms.R) FiatMoneyUtil(org.thoughtcrime.securesms.payments.FiatMoneyUtil) SafeNavigation(org.thoughtcrime.securesms.util.navigation.SafeNavigation) FormatterOptions(org.whispersystems.signalservice.api.payments.FormatterOptions) Map(java.util.Map) View(android.view.View) RecipientHasNotEnabledPaymentsDialog(org.thoughtcrime.securesms.payments.preferences.RecipientHasNotEnabledPaymentsDialog) ContextCompat(androidx.core.content.ContextCompat) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) MoneyView(org.thoughtcrime.securesms.payments.MoneyView) TextUtils(android.text.TextUtils) SpanUtil(org.thoughtcrime.securesms.util.SpanUtil) CommunicationActions(org.thoughtcrime.securesms.util.CommunicationActions) ConstraintSet(androidx.constraintlayout.widget.ConstraintSet) AlertDialog(android.app.AlertDialog) Objects(java.util.Objects) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) ConstraintLayout(androidx.constraintlayout.widget.ConstraintLayout) Toolbar(androidx.appcompat.widget.Toolbar) Navigation(androidx.navigation.Navigation) LoggingFragment(org.thoughtcrime.securesms.LoggingFragment) Money(org.whispersystems.signalservice.api.payments.Money) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) TextView(android.widget.TextView) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) View(android.view.View) MoneyView(org.thoughtcrime.securesms.payments.MoneyView) TextView(android.widget.TextView) Toolbar(androidx.appcompat.widget.Toolbar) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections)

Example 88 with ViewModelProvider

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();
    });
}
Also used : ViewModelProvider(androidx.lifecycle.ViewModelProvider) Toolbar(androidx.appcompat.widget.Toolbar)

Example 89 with ViewModelProvider

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);
}
Also used : Spinner(android.widget.Spinner) View(android.view.View) TextView(android.widget.TextView) AfterTextChanged(org.thoughtcrime.securesms.util.text.AfterTextChanged) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 90 with ViewModelProvider

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();
    });
}
Also used : RecyclerView(androidx.recyclerview.widget.RecyclerView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)159 View (android.view.View)38 TextView (android.widget.TextView)28 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)25 Intent (android.content.Intent)24 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)24 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)23 Nullable (androidx.annotation.Nullable)21 Snackbar (com.google.android.material.snackbar.Snackbar)20 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)20 Bundle (android.os.Bundle)18 RecyclerView (androidx.recyclerview.widget.RecyclerView)18 Toolbar (androidx.appcompat.widget.Toolbar)16 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)16 Uri (android.net.Uri)10 ViewGroup (android.view.ViewGroup)10 NonNull (androidx.annotation.NonNull)10 Handler (android.os.Handler)9 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)8 IdpResponse (com.firebase.ui.auth.IdpResponse)8