NoteDialogFragment.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.graphics.PorterDuff;
  26. import android.os.Bundle;
  27. import android.support.annotation.NonNull;
  28. import android.support.annotation.Nullable;
  29. import android.support.v4.app.DialogFragment;
  30. import android.support.v7.app.AlertDialog;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.Window;
  34. import android.view.WindowManager.LayoutParams;
  35. import android.widget.EditText;
  36. import com.owncloud.android.R;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.ui.activity.ComponentsGetter;
  39. import com.owncloud.android.utils.DisplayUtils;
  40. import com.owncloud.android.utils.ThemeUtils;
  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)
  53. EditText noteEditText;
  54. public static NoteDialogFragment newInstance(OCShare share) {
  55. NoteDialogFragment frag = new NoteDialogFragment();
  56. Bundle args = new Bundle();
  57. args.putParcelable(ARG_SHARE, share);
  58. frag.setArguments(args);
  59. return frag;
  60. }
  61. @Override
  62. public void onCreate(@Nullable Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. if (getArguments() == null) {
  65. throw new IllegalArgumentException("Arguments may not be null");
  66. }
  67. share = getArguments().getParcelable(ARG_SHARE);
  68. }
  69. @Override
  70. public void onStart() {
  71. super.onStart();
  72. int color = ThemeUtils.primaryAccentColor(getContext());
  73. AlertDialog alertDialog = (AlertDialog) getDialog();
  74. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  75. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  76. }
  77. @NonNull
  78. @Override
  79. public Dialog onCreateDialog(Bundle savedInstanceState) {
  80. int accentColor = ThemeUtils.primaryAccentColor(getContext());
  81. // Inflate the layout for the dialog
  82. LayoutInflater inflater = requireActivity().getLayoutInflater();
  83. @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.note_dialog, null, false);
  84. unbinder = ButterKnife.bind(this, view);
  85. // Setup layout
  86. noteEditText.setText(share.getNote());
  87. noteEditText.requestFocus();
  88. noteEditText.getBackground().setColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
  89. // Build the dialog
  90. AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
  91. builder.setView(view)
  92. .setPositiveButton(R.string.note_confirm, this)
  93. .setNegativeButton(R.string.common_cancel, this)
  94. .setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.send_note),
  95. accentColor));
  96. Dialog dialog = builder.create();
  97. Window window = dialog.getWindow();
  98. if (window != null) {
  99. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  100. }
  101. return dialog;
  102. }
  103. @Override
  104. public void onClick(DialogInterface dialog, int which) {
  105. if (which == AlertDialog.BUTTON_POSITIVE) {
  106. ComponentsGetter componentsGetter = (ComponentsGetter) getActivity();
  107. if (componentsGetter != null) {
  108. componentsGetter.getFileOperationsHelper().updateNoteToShare(share,
  109. noteEditText.getText().toString().trim());
  110. } else {
  111. DisplayUtils.showSnackMessage(requireActivity(), R.string.note_could_not_sent);
  112. }
  113. }
  114. }
  115. @Override
  116. public void onStop() {
  117. unbinder.unbind();
  118. super.onStop();
  119. }
  120. }