2019-05-30 15:19:02 +02:00
|
|
|
package app.fedilab.android.imageeditor;
|
|
|
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.ColorDrawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.ColorInt;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.DialogFragment;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-05-30 19:09:36 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2019-05-30 15:19:02 +02:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Burhanuddin Rashid on 1/16/2018.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class TextEditorDialogFragment extends DialogFragment {
|
|
|
|
|
|
|
|
public static final String TAG = TextEditorDialogFragment.class.getSimpleName();
|
|
|
|
public static final String EXTRA_INPUT_TEXT = "extra_input_text";
|
|
|
|
public static final String EXTRA_COLOR_CODE = "extra_color_code";
|
|
|
|
private EditText mAddTextEditText;
|
|
|
|
private TextView mAddTextDoneTextView;
|
|
|
|
private InputMethodManager mInputMethodManager;
|
|
|
|
private int mColorCode;
|
|
|
|
private TextEditor mTextEditor;
|
|
|
|
|
|
|
|
public interface TextEditor {
|
|
|
|
void onDone(String inputText, int colorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Show dialog with provide text and text color
|
|
|
|
public static TextEditorDialogFragment show(@NonNull AppCompatActivity appCompatActivity,
|
|
|
|
@NonNull String inputText,
|
|
|
|
@ColorInt int colorCode) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString(EXTRA_INPUT_TEXT, inputText);
|
|
|
|
args.putInt(EXTRA_COLOR_CODE, colorCode);
|
|
|
|
TextEditorDialogFragment fragment = new TextEditorDialogFragment();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
fragment.show(appCompatActivity.getSupportFragmentManager(), TAG);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Show dialog with default text input as empty and text color white
|
|
|
|
public static TextEditorDialogFragment show(@NonNull AppCompatActivity appCompatActivity) {
|
|
|
|
return show(appCompatActivity,
|
|
|
|
"", ContextCompat.getColor(appCompatActivity, R.color.white));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
Dialog dialog = getDialog();
|
|
|
|
//Make dialog full screen with transparent background
|
|
|
|
if (dialog != null) {
|
|
|
|
int width = ViewGroup.LayoutParams.MATCH_PARENT;
|
|
|
|
int height = ViewGroup.LayoutParams.MATCH_PARENT;
|
|
|
|
dialog.getWindow().setLayout(width, height);
|
|
|
|
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2019-05-30 19:09:36 +02:00
|
|
|
public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2019-05-30 15:19:02 +02:00
|
|
|
return inflater.inflate(R.layout.add_text_dialog, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-30 19:09:36 +02:00
|
|
|
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
|
2019-05-30 15:19:02 +02:00
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
mAddTextEditText = view.findViewById(R.id.add_text_edit_text);
|
|
|
|
mInputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
mAddTextDoneTextView = view.findViewById(R.id.add_text_done_tv);
|
|
|
|
|
|
|
|
//Setup the color picker for text color
|
|
|
|
RecyclerView addTextColorPickerRecyclerView = view.findViewById(R.id.add_text_color_picker_recycler_view);
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
|
|
|
|
addTextColorPickerRecyclerView.setLayoutManager(layoutManager);
|
|
|
|
addTextColorPickerRecyclerView.setHasFixedSize(true);
|
|
|
|
ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(getActivity());
|
|
|
|
//This listener will change the text color when clicked on any color from picker
|
|
|
|
colorPickerAdapter.setOnColorPickerClickListener(new ColorPickerAdapter.OnColorPickerClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onColorPickerClickListener(int colorCode) {
|
|
|
|
mColorCode = colorCode;
|
|
|
|
mAddTextEditText.setTextColor(colorCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addTextColorPickerRecyclerView.setAdapter(colorPickerAdapter);
|
|
|
|
mAddTextEditText.setText(getArguments().getString(EXTRA_INPUT_TEXT));
|
|
|
|
mColorCode = getArguments().getInt(EXTRA_COLOR_CODE);
|
|
|
|
mAddTextEditText.setTextColor(mColorCode);
|
|
|
|
mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
|
|
|
|
|
|
|
|
//Make a callback on activity when user is done with text editing
|
|
|
|
mAddTextDoneTextView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
|
|
dismiss();
|
|
|
|
String inputText = mAddTextEditText.getText().toString();
|
|
|
|
if (!TextUtils.isEmpty(inputText) && mTextEditor != null) {
|
|
|
|
mTextEditor.onDone(inputText, mColorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Callback to listener if user is done with text editing
|
|
|
|
public void setOnTextEditorListener(TextEditor textEditor) {
|
|
|
|
mTextEditor = textEditor;
|
|
|
|
}
|
|
|
|
}
|