NoteDialogFragment.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2018 Tobias Kaminsky
  6. * Copyright (C) 2018 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.dialog;
  22. import android.annotation.SuppressLint;
  23. import android.app.Dialog;
  24. import android.content.DialogInterface;
  25. import android.os.Bundle;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.Window;
  29. import android.view.WindowManager.LayoutParams;
  30. import com.google.android.material.textfield.TextInputEditText;
  31. import com.google.android.material.textfield.TextInputLayout;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.lib.resources.shares.OCShare;
  34. import com.owncloud.android.ui.activity.ComponentsGetter;
  35. import com.owncloud.android.utils.DisplayUtils;
  36. import com.owncloud.android.utils.ThemeUtils;
  37. import androidx.annotation.NonNull;
  38. import androidx.annotation.Nullable;
  39. import androidx.appcompat.app.AlertDialog;
  40. import androidx.fragment.app.DialogFragment;
  41. import butterknife.BindView;
  42. import butterknife.ButterKnife;
  43. import butterknife.Unbinder;
  44. /**
  45. * Dialog to input a multiline note for a share
  46. */
  47. public class NoteDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
  48. private static final String ARG_SHARE = "SHARE";
  49. public static final String NOTE_FRAGMENT = "NOTE_FRAGMENT";
  50. private OCShare share;
  51. private Unbinder unbinder;
  52. @BindView(R.id.user_input_container)
  53. public TextInputLayout noteEditTextInputLayout;
  54. @BindView(R.id.user_input)
  55. public TextInputEditText noteEditText;
  56. public static NoteDialogFragment newInstance(OCShare share) {
  57. NoteDialogFragment frag = new NoteDialogFragment();
  58. Bundle args = new Bundle();
  59. args.putParcelable(ARG_SHARE, share);
  60. frag.setArguments(args);
  61. return frag;
  62. }
  63. @Override
  64. public void onCreate(@Nullable Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. if (getArguments() == null) {
  67. throw new IllegalArgumentException("Arguments may not be null");
  68. }
  69. share = getArguments().getParcelable(ARG_SHARE);
  70. }
  71. @Override
  72. public void onStart() {
  73. super.onStart();
  74. int color = ThemeUtils.primaryAccentColor(getContext());
  75. AlertDialog alertDialog = (AlertDialog) getDialog();
  76. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  77. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  78. }
  79. @NonNull
  80. @Override
  81. public Dialog onCreateDialog(Bundle savedInstanceState) {
  82. int accentColor = ThemeUtils.primaryAccentColor(getContext());
  83. // Inflate the layout for the dialog
  84. LayoutInflater inflater = requireActivity().getLayoutInflater();
  85. @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.note_dialog, null, false);
  86. unbinder = ButterKnife.bind(this, view);
  87. // Setup layout
  88. noteEditText.setText(share.getNote());
  89. noteEditText.setHighlightColor(ThemeUtils.primaryColor(getActivity()));
  90. noteEditText.requestFocus();
  91. ThemeUtils.colorTextInputLayout(noteEditTextInputLayout, accentColor);
  92. // Build the dialog
  93. AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
  94. builder.setView(view)
  95. .setPositiveButton(R.string.note_confirm, this)
  96. .setNegativeButton(R.string.common_cancel, this)
  97. .setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.send_note),
  98. accentColor));
  99. Dialog dialog = builder.create();
  100. Window window = dialog.getWindow();
  101. if (window != null) {
  102. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  103. }
  104. return dialog;
  105. }
  106. @Override
  107. public void onClick(DialogInterface dialog, int which) {
  108. if (which == AlertDialog.BUTTON_POSITIVE) {
  109. ComponentsGetter componentsGetter = (ComponentsGetter) getActivity();
  110. if (componentsGetter != null) {
  111. String note = "";
  112. if (noteEditText.getText() != null) {
  113. note = noteEditText.getText().toString().trim();
  114. }
  115. componentsGetter.getFileOperationsHelper().updateNoteToShare(share, note);
  116. } else {
  117. DisplayUtils.showSnackMessage(requireActivity(), R.string.note_could_not_sent);
  118. }
  119. }
  120. }
  121. @Override
  122. public void onStop() {
  123. unbinder.unbind();
  124. super.onStop();
  125. }
  126. }