NoteDialogFragment.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.app.Dialog;
  23. import android.content.DialogInterface;
  24. import android.os.Bundle;
  25. import android.view.LayoutInflater;
  26. import android.view.View;
  27. import android.view.Window;
  28. import android.view.WindowManager.LayoutParams;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.databinding.NoteDialogBinding;
  31. import com.owncloud.android.lib.resources.shares.OCShare;
  32. import com.owncloud.android.ui.activity.ComponentsGetter;
  33. import com.owncloud.android.utils.DisplayUtils;
  34. import com.owncloud.android.utils.theme.ThemeButtonUtils;
  35. import com.owncloud.android.utils.theme.ThemeColorUtils;
  36. import com.owncloud.android.utils.theme.ThemeTextInputUtils;
  37. import androidx.annotation.NonNull;
  38. import androidx.annotation.Nullable;
  39. import androidx.appcompat.app.AlertDialog;
  40. import androidx.fragment.app.DialogFragment;
  41. /**
  42. * Dialog to input a multiline note for a share
  43. */
  44. public class NoteDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
  45. private static final String ARG_SHARE = "SHARE";
  46. public static final String NOTE_FRAGMENT = "NOTE_FRAGMENT";
  47. private OCShare share;
  48. private NoteDialogBinding binding;
  49. public static NoteDialogFragment newInstance(OCShare share) {
  50. NoteDialogFragment frag = new NoteDialogFragment();
  51. Bundle args = new Bundle();
  52. args.putParcelable(ARG_SHARE, share);
  53. frag.setArguments(args);
  54. return frag;
  55. }
  56. @Override
  57. public void onCreate(@Nullable Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. if (getArguments() == null) {
  60. throw new IllegalArgumentException("Arguments may not be null");
  61. }
  62. share = getArguments().getParcelable(ARG_SHARE);
  63. }
  64. @Override
  65. public void onStart() {
  66. super.onStart();
  67. AlertDialog alertDialog = (AlertDialog) getDialog();
  68. ThemeButtonUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE),
  69. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL));
  70. }
  71. @NonNull
  72. @Override
  73. public Dialog onCreateDialog(Bundle savedInstanceState) {
  74. int primaryColor = ThemeColorUtils.primaryColor(getContext());
  75. // Inflate the layout for the dialog
  76. LayoutInflater inflater = requireActivity().getLayoutInflater();
  77. binding = NoteDialogBinding.inflate(inflater, null, false);
  78. View view = binding.getRoot();
  79. // Setup layout
  80. binding.noteText.setText(share.getNote());
  81. binding.noteText.requestFocus();
  82. ThemeTextInputUtils.colorTextInput(binding.noteContainer, binding.noteText, primaryColor);
  83. // Build the dialog
  84. AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
  85. builder.setView(view)
  86. .setPositiveButton(R.string.note_confirm, this)
  87. .setNeutralButton(R.string.common_cancel, this)
  88. .setTitle(R.string.send_note);
  89. Dialog dialog = builder.create();
  90. Window window = dialog.getWindow();
  91. if (window != null) {
  92. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  93. }
  94. return dialog;
  95. }
  96. @Override
  97. public void onClick(DialogInterface dialog, int which) {
  98. if (which == AlertDialog.BUTTON_POSITIVE) {
  99. ComponentsGetter componentsGetter = (ComponentsGetter) getActivity();
  100. if (componentsGetter != null) {
  101. String note = "";
  102. if (binding.noteText.getText() != null) {
  103. note = binding.noteText.getText().toString().trim();
  104. }
  105. componentsGetter.getFileOperationsHelper().updateNoteToShare(share, note);
  106. } else {
  107. DisplayUtils.showSnackMessage(requireActivity(), R.string.note_could_not_sent);
  108. }
  109. }
  110. }
  111. @Override
  112. public void onDestroyView() {
  113. super.onDestroyView();
  114. binding = null;
  115. }
  116. }