|
| 1 | +package io.github.subhamtyagi.ocr; |
| 2 | + |
| 3 | +import android.content.ClipData; |
| 4 | +import android.content.ClipboardManager; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.content.Intent; |
| 8 | +import android.net.Uri; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.text.Editable; |
| 11 | +import android.view.LayoutInflater; |
| 12 | +import android.view.View; |
| 13 | +import android.view.ViewGroup; |
| 14 | +import android.view.inputmethod.InputMethodManager; |
| 15 | +import android.widget.Button; |
| 16 | +import android.widget.ImageButton; |
| 17 | +import android.widget.ImageView; |
| 18 | +import android.widget.ListView; |
| 19 | +import android.graphics.drawable.ColorDrawable; |
| 20 | +import android.widget.EditText; |
| 21 | +import android.widget.RadioButton; |
| 22 | +import com.google.android.material.bottomsheet.BottomSheetBehavior; |
| 23 | +import io.github.subhamtyagi.ocr.spinner.ArrayAdapterWithContainsFilter; |
| 24 | +import android.text.TextWatcher; |
| 25 | +import android.widget.TextView; |
| 26 | +import android.widget.Toast; |
| 27 | + |
| 28 | +import androidx.annotation.NonNull; |
| 29 | +import androidx.annotation.Nullable; |
| 30 | + |
| 31 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import kotlin.Triple; |
| 35 | +import java.util.Set; |
| 36 | +import io.github.subhamtyagi.ocr.utils.Utils; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +public class ShareFragment extends BottomSheetDialogFragment { |
| 40 | + |
| 41 | + private static final String LANGUAGE = "language"; |
| 42 | + |
| 43 | + |
| 44 | + private Bundle bundle; |
| 45 | + static ArrayList<String> items; |
| 46 | + OnSpinnerItemClick onSpinnerItemClick; |
| 47 | + |
| 48 | + boolean showKeyboard = false; |
| 49 | + boolean useContainsFilter = false; |
| 50 | + |
| 51 | + int pos; |
| 52 | + |
| 53 | + @Nullable |
| 54 | + @Override |
| 55 | + public View onCreateView( |
| 56 | + LayoutInflater inflater, |
| 57 | + @Nullable ViewGroup container, |
| 58 | + @Nullable Bundle savedInstanceState) { |
| 59 | + |
| 60 | + View vw = inflater.inflate(R.layout.dialog_layout, container, false); |
| 61 | + |
| 62 | + |
| 63 | + bundle = getArguments(); |
| 64 | + |
| 65 | + assert bundle != null; |
| 66 | + |
| 67 | + // BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(vw.findViewById(R.id.mainView)); |
| 68 | + // bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
| 69 | + // bottomSheetBehavior.setHideable(false); |
| 70 | + |
| 71 | + TextView title = vw.findViewById(R.id.spinerTitle); |
| 72 | + ImageView searchIcon = vw.findViewById(R.id.searchIcon); |
| 73 | + title.setText(R.string.select_search_language); |
| 74 | + |
| 75 | + final ListView listView = vw.findViewById(R.id.list); |
| 76 | + |
| 77 | + // ColorDrawable sage = new ColorDrawable(itemDividerColor); |
| 78 | + // listView.setDivider(sage); |
| 79 | + // listView.setDividerHeight(1); |
| 80 | + |
| 81 | + final EditText searchBox = vw.findViewById(R.id.searchBox); |
| 82 | + if (isShowKeyboard()) { |
| 83 | + showKeyboard(searchBox); |
| 84 | + } |
| 85 | + |
| 86 | + final ArrayAdapterWithContainsFilter<String> adapter = |
| 87 | + new ArrayAdapterWithContainsFilter<String>( |
| 88 | + getActivity(), R.layout.items_view, items) { |
| 89 | + @NonNull |
| 90 | + @Override |
| 91 | + public View getView( |
| 92 | + int position, @Nullable View convertView, @NonNull ViewGroup parent) { |
| 93 | + View view = super.getView(position, convertView, parent); |
| 94 | + TextView text1 = view.findViewById(R.id.text1); |
| 95 | + // text1.setTextColor(itemColor); |
| 96 | + return view; |
| 97 | + } |
| 98 | + }; |
| 99 | + listView.setAdapter(adapter); |
| 100 | + |
| 101 | + listView.setOnItemClickListener( |
| 102 | + (adapterView, view, i, l) -> { |
| 103 | + TextView t = view.findViewById(R.id.text1); |
| 104 | + for (int j = 0; j < items.size(); j++) { |
| 105 | + if (t.getText().toString().equalsIgnoreCase(items.get(j))) { |
| 106 | + pos = j; |
| 107 | + } |
| 108 | + } |
| 109 | + //onSpinnerItemClick.onClick(t.getText().toString(), pos); |
| 110 | + }); |
| 111 | + |
| 112 | + searchBox.addTextChangedListener( |
| 113 | + new TextWatcher() { |
| 114 | + @Override |
| 115 | + public void beforeTextChanged( |
| 116 | + CharSequence charSequence, int i, int i1, int i2) {} |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} |
| 120 | + |
| 121 | + @Override |
| 122 | + public void afterTextChanged(Editable editable) { |
| 123 | + if (isUseContainsFilter()) { |
| 124 | + adapter.getContainsFilter(searchBox.getText().toString()); |
| 125 | + } else { |
| 126 | + adapter.getFilter().filter(searchBox.getText().toString()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + }); |
| 131 | + |
| 132 | + // startOCRFromShareMenu(imageUri, Collections.singleton(new Language(this, item))); |
| 133 | + RadioButton radioButton1 = vw.findViewById(R.id.rb_language1); |
| 134 | + RadioButton radioButton2 = vw.findViewById(R.id.rb_language2); |
| 135 | + RadioButton radioButton3 = vw.findViewById(R.id.rb_language3); |
| 136 | + Triple<Set<Language>, Set<Language>, Set<Language>> languages = |
| 137 | + Utils.getLast3UsedLanguage(getContext()); |
| 138 | + |
| 139 | + radioButton1.setText( |
| 140 | + languages.getFirst().stream() |
| 141 | + .map(Language::getName) |
| 142 | + .collect(Collectors.joining(", "))); |
| 143 | + radioButton2.setText( |
| 144 | + languages.getSecond().stream() |
| 145 | + .map(Language::getName) |
| 146 | + .collect(Collectors.joining(", "))); |
| 147 | + radioButton3.setText( |
| 148 | + languages.getThird().stream() |
| 149 | + .map(Language::getName) |
| 150 | + .collect(Collectors.joining(", "))); |
| 151 | + |
| 152 | + // radioButton1.setOnClickListener(view1 -> startOCRFromShareMenu(imageUri, |
| 153 | + // languages.getFirst())); |
| 154 | + // radioButton2.setOnClickListener(view1 -> startOCRFromShareMenu(imageUri, |
| 155 | + // languages.getSecond())); |
| 156 | + // radioButton3.setOnClickListener(view1 -> startOCRFromShareMenu(imageUri, |
| 157 | + // languages.getThird())); |
| 158 | + |
| 159 | + return vw; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onCancel(@NonNull DialogInterface dialog) { |
| 164 | + cancel(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void dismiss() { |
| 169 | + cancel(); |
| 170 | + super.dismiss(); |
| 171 | + } |
| 172 | + |
| 173 | + private void cancel() {} |
| 174 | + |
| 175 | + public static ShareFragment newInstance(Uri imageUri, ArrayList<String> languagesName) { |
| 176 | + ShareFragment fragment = new ShareFragment(); |
| 177 | + items=languagesName; |
| 178 | + Bundle bundle = new Bundle(); |
| 179 | + bundle.putString(LANGUAGE, "string"); |
| 180 | + fragment.setArguments(bundle); |
| 181 | + return fragment; |
| 182 | + } |
| 183 | + |
| 184 | + private void hideKeyboard() { |
| 185 | + try { |
| 186 | + InputMethodManager inputManager = |
| 187 | + (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 188 | + // inputManager.hideSoftInputFromWindow( |
| 189 | + //getContext().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); |
| 190 | + } catch (Exception ignore) { |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private void showKeyboard(final EditText ettext) { |
| 195 | + ettext.requestFocus(); |
| 196 | + ettext.postDelayed( |
| 197 | + () -> { |
| 198 | + InputMethodManager keyboard = |
| 199 | + (InputMethodManager) |
| 200 | + getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 201 | + keyboard.showSoftInput(ettext, 0); |
| 202 | + }, |
| 203 | + 200); |
| 204 | + } |
| 205 | + |
| 206 | + private boolean isShowKeyboard() { |
| 207 | + return showKeyboard; |
| 208 | + } |
| 209 | + |
| 210 | + public void setShowKeyboard(boolean showKeyboard) { |
| 211 | + this.showKeyboard = showKeyboard; |
| 212 | + } |
| 213 | + |
| 214 | + private boolean isUseContainsFilter() { |
| 215 | + return useContainsFilter; |
| 216 | + } |
| 217 | + |
| 218 | + public void setUseContainsFilter(boolean useContainsFilter) { |
| 219 | + this.useContainsFilter = useContainsFilter; |
| 220 | + } |
| 221 | + |
| 222 | + public interface OnSpinnerItemClick { |
| 223 | + public void onClick(String item, int position); |
| 224 | + } |
| 225 | +} |
0 commit comments