Search in sources :

Example 1 with ActionButton

use of xyz.zedler.patrick.grocy.view.ActionButton in project grocy-android by patzly.

the class DateBottomSheet method onCreateView.

@SuppressLint("SimpleDateFormat")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    binding = FragmentBottomsheetDateBinding.inflate(inflater, container, false);
    activity = (MainActivity) requireActivity();
    args = requireArguments();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    ;
    keyboardInputEnabled = sharedPrefs.getBoolean(BEHAVIOR.DATE_KEYBOARD_INPUT, SETTINGS_DEFAULT.BEHAVIOR.DATE_KEYBOARD_INPUT);
    boolean reverseDateFormat = sharedPrefs.getBoolean(BEHAVIOR.DATE_KEYBOARD_REVERSE, SETTINGS_DEFAULT.BEHAVIOR.DATE_KEYBOARD_REVERSE);
    calendar = Calendar.getInstance();
    dateUtil = new DateUtil(requireContext());
    dateFormatGrocy = new SimpleDateFormat("yyyy-MM-dd");
    if (reverseDateFormat) {
        dateFormatKeyboardInput = new SimpleDateFormat("ddMMyy");
        dateFormatKeyboardInputShort = new SimpleDateFormat("ddMM");
        binding.textInputDate.setHint("DDMM | DDMMYY");
    } else {
        dateFormatKeyboardInput = new SimpleDateFormat("yyMMdd");
        dateFormatKeyboardInputShort = new SimpleDateFormat("MMdd");
    }
    String selectedDate = args.getString(Constants.ARGUMENT.SELECTED_DATE);
    defaultDueDays = args.getString(Constants.ARGUMENT.DEFAULT_DAYS_FROM_NOW);
    binding.frameHelpButton.setOnClickListener(v -> {
        if (keyboardInputEnabled) {
            binding.helpKeyboard.setVisibility(View.VISIBLE);
        } else {
            binding.help.setVisibility(View.VISIBLE);
        }
    });
    binding.help.setOnClickListener(v -> navigateToSettingsCatBehavior());
    binding.helpKeyboard.setOnClickListener(v -> navigateToSettingsCatBehavior());
    if (keyboardInputEnabled) {
        binding.linearBodyPicker.setVisibility(View.GONE);
        binding.linearBodyKeyboard.setVisibility(View.VISIBLE);
        if (selectedDate == null || selectedDate.equals(DATE.NEVER_OVERDUE)) {
            binding.editTextDate.setText("");
        } else {
            try {
                Date date = dateFormatGrocy.parse(selectedDate);
                if (date != null) {
                    calendar.setTime(date);
                    binding.editTextDate.setText(dateFormatKeyboardInput.format(calendar.getTime()));
                } else {
                    binding.editTextDate.setText("");
                }
            } catch (ParseException e) {
                binding.editTextDate.setText("");
            }
        }
        if (savedInstanceState == null) {
            new Handler().postDelayed(() -> activity.showKeyboard(binding.editTextDate), 50);
        }
        updateDateHint();
        binding.editTextDate.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateDateHint();
            }
        });
        binding.editTextDate.setOnEditorActionListener((TextView v, int actionId, KeyEvent event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE && getTextFieldDate() != null) {
                dismiss();
                return true;
            }
            return false;
        });
        ActionButton moreMonth = reverseDateFormat ? binding.moreMonthReverse : binding.moreMonth;
        ActionButton lessMonth = reverseDateFormat ? binding.lessMonthReverse : binding.lessMonth;
        binding.linearMonth.setVisibility(reverseDateFormat ? View.GONE : View.VISIBLE);
        binding.linearMonthReverse.setVisibility(reverseDateFormat ? View.VISIBLE : View.GONE);
        moreMonth.setOnClickListener(view -> {
            Date date = getTextFieldDate();
            if (date != null) {
                String input = binding.editTextDate.getText() != null ? binding.editTextDate.getText().toString().trim() : "";
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, 1);
                if (input.length() == 6) {
                    binding.editTextDate.setText(dateFormatKeyboardInput.format(calendar.getTime()));
                } else if (input.length() == 4) {
                    binding.editTextDate.setText(dateFormatKeyboardInputShort.format(calendar.getTime()));
                }
            }
        });
        binding.moreDay.setOnClickListener(view -> {
            Date date = getTextFieldDate();
            if (date != null) {
                String input = binding.editTextDate.getText() != null ? binding.editTextDate.getText().toString().trim() : "";
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                if (input.length() == 6) {
                    binding.editTextDate.setText(dateFormatKeyboardInput.format(calendar.getTime()));
                } else if (input.length() == 4) {
                    binding.editTextDate.setText(dateFormatKeyboardInputShort.format(calendar.getTime()));
                }
            }
        });
        lessMonth.setOnClickListener(view -> {
            Date date = getTextFieldDate();
            if (date != null) {
                String input = binding.editTextDate.getText() != null ? binding.editTextDate.getText().toString().trim() : "";
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, -1);
                if (input.length() == 6) {
                    binding.editTextDate.setText(dateFormatKeyboardInput.format(calendar.getTime()));
                } else if (input.length() == 4) {
                    binding.editTextDate.setText(dateFormatKeyboardInputShort.format(calendar.getTime()));
                }
            }
        });
        binding.lessDay.setOnClickListener(view -> {
            Date date = getTextFieldDate();
            if (date != null) {
                String input = binding.editTextDate.getText() != null ? binding.editTextDate.getText().toString().trim() : "";
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                if (input.length() == 6) {
                    binding.editTextDate.setText(dateFormatKeyboardInput.format(calendar.getTime()));
                } else if (input.length() == 4) {
                    binding.editTextDate.setText(dateFormatKeyboardInputShort.format(calendar.getTime()));
                }
            }
        });
        binding.clear.setOnClickListener(v -> {
            binding.editTextDate.setText("");
            activity.showKeyboard(binding.editTextDate);
        });
    } else {
        initDatePickerLayout();
        fillDatePickerForm(selectedDate);
    }
    setSkipCollapsedInPortrait();
    return binding.getRoot();
}
Also used : SharedPreferences(android.content.SharedPreferences) DateUtil(xyz.zedler.patrick.grocy.util.DateUtil) Handler(android.os.Handler) Date(java.util.Date) FormDataMasterProductCatDueDate(xyz.zedler.patrick.grocy.model.FormDataMasterProductCatDueDate) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) KeyEvent(android.view.KeyEvent) ActionButton(xyz.zedler.patrick.grocy.view.ActionButton) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) SuppressLint(android.annotation.SuppressLint)

Example 2 with ActionButton

use of xyz.zedler.patrick.grocy.view.ActionButton in project grocy-android by patzly.

the class ShoppingListsBottomSheet method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_bottomsheet_list_selection, container, false);
    activity = (MainActivity) requireActivity();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    boolean multipleListsFeature = sharedPrefs.getBoolean(Constants.PREF.FEATURE_MULTIPLE_SHOPPING_LISTS, true);
    MutableLiveData<Integer> selectedIdLive = activity.getCurrentFragment().getSelectedShoppingListIdLive();
    if (selectedIdLive == null) {
        dismiss();
        return view;
    }
    ShoppingListRepository repository = new ShoppingListRepository(activity.getApplication());
    TextView textViewTitle = view.findViewById(R.id.text_list_selection_title);
    textViewTitle.setText(activity.getString(R.string.property_shopping_lists));
    RecyclerView recyclerView = view.findViewById(R.id.recycler_list_selection);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(new ShoppingPlaceholderAdapter());
    repository.getShoppingListsLive().observe(getViewLifecycleOwner(), shoppingLists -> {
        if (shoppingLists == null) {
            return;
        }
        if (recyclerView.getAdapter() == null || !(recyclerView.getAdapter() instanceof ShoppingListAdapter)) {
            recyclerView.setAdapter(new ShoppingListAdapter(shoppingLists, selectedIdLive.getValue(), this, activity.getCurrentFragment() instanceof ShoppingListFragment && activity.isOnline()));
        } else {
            ((ShoppingListAdapter) recyclerView.getAdapter()).updateData(shoppingLists, selectedIdLive.getValue());
        }
    });
    selectedIdLive.observe(getViewLifecycleOwner(), selectedId -> {
        if (recyclerView.getAdapter() == null || !(recyclerView.getAdapter() instanceof ShoppingListAdapter)) {
            return;
        }
        ((ShoppingListAdapter) recyclerView.getAdapter()).updateSelectedId(selectedIdLive.getValue());
    });
    ActionButton buttonNew = view.findViewById(R.id.button_list_selection_new);
    if (activity.isOnline() && multipleListsFeature && activity.getCurrentFragment() instanceof ShoppingListFragment) {
        buttonNew.setVisibility(View.VISIBLE);
        buttonNew.setOnClickListener(v -> {
            dismiss();
            navigate(ShoppingListFragmentDirections.actionShoppingListFragmentToShoppingListEditFragment());
        });
    }
    progressConfirm = view.findViewById(R.id.progress_confirmation);
    return view;
}
Also used : SharedPreferences(android.content.SharedPreferences) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListFragment(xyz.zedler.patrick.grocy.fragment.ShoppingListFragment) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) ShoppingListAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListAdapter) ActionButton(xyz.zedler.patrick.grocy.view.ActionButton) ShoppingListRepository(xyz.zedler.patrick.grocy.repository.ShoppingListRepository) TextView(android.widget.TextView) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

SharedPreferences (android.content.SharedPreferences)2 TextView (android.widget.TextView)2 ActionButton (xyz.zedler.patrick.grocy.view.ActionButton)2 SuppressLint (android.annotation.SuppressLint)1 Paint (android.graphics.Paint)1 Handler (android.os.Handler)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 KeyEvent (android.view.KeyEvent)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ShoppingListAdapter (xyz.zedler.patrick.grocy.adapter.ShoppingListAdapter)1 ShoppingPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter)1 ShoppingListFragment (xyz.zedler.patrick.grocy.fragment.ShoppingListFragment)1