Search in sources :

Example 1 with ForceCompleteTextView

use of com.quran.labs.androidquran.widgets.ForceCompleteTextView in project quran_android by quran.

the class JumpFragment method onCreateDialog.

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Activity activity = getActivity();
    LayoutInflater inflater = activity.getLayoutInflater();
    @SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.jump_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(activity.getString(R.string.menu_jump));
    // Sura chooser
    final ForceCompleteTextView suraInput = (ForceCompleteTextView) layout.findViewById(R.id.sura_spinner);
    final String[] suras = activity.getResources().getStringArray(R.array.sura_names);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < suras.length; i++) {
        sb.append(QuranUtils.getLocalizedNumber(activity, (i + 1)));
        sb.append(". ");
        sb.append(suras[i]);
        suras[i] = sb.toString();
        sb.setLength(0);
    }
    InfixFilterArrayAdapter suraAdapter = new InfixFilterArrayAdapter(activity, android.R.layout.simple_spinner_dropdown_item, suras);
    suraInput.setAdapter(suraAdapter);
    // Ayah chooser
    final EditText ayahInput = (EditText) layout.findViewById(R.id.ayah_spinner);
    // Page chooser
    final EditText pageInput = (EditText) layout.findViewById(R.id.page_number);
    pageInput.setOnEditorActionListener((v, actionId, event) -> {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_GO) {
            dismiss();
            goToPage(pageInput.getText().toString());
            handled = true;
        }
        return handled;
    });
    suraInput.setOnForceCompleteListener((v, position, rowId) -> {
        List<String> suraList = Arrays.asList(suras);
        String enteredText = suraInput.getText().toString();
        String suraName;
        if (position >= 0) {
            // user selects
            suraName = suraAdapter.getItem(position);
        } else if (suraList.contains(enteredText)) {
            suraName = enteredText;
        } else if (suraAdapter.isEmpty()) {
            // leave to the next code
            suraName = null;
        } else {
            // maybe first initialization or invalid input
            suraName = suraAdapter.getItem(0);
        }
        int sura = suraList.indexOf(suraName) + 1;
        if (sura == 0)
            // default to al-Fatiha
            sura = 1;
        suraInput.setTag(sura);
        suraInput.setText(suras[sura - 1]);
        // trigger ayah change
        CharSequence ayahValue = ayahInput.getText();
        // space is intentional, to differentiate with value set by the user (delete/backspace)
        ayahInput.setText(ayahValue.length() > 0 ? ayahValue : " ");
    });
    ayahInput.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Context context = getActivity();
            String ayahString = s.toString();
            int ayah = parseInt(ayahString, 1);
            Object suraTag = suraInput.getTag();
            if (suraTag != null) {
                int sura = (int) suraTag;
                int ayahCount = quranInfo.getNumAyahs(sura);
                // ensure in 1..ayahCount
                ayah = Math.max(1, Math.min(ayahCount, ayah));
                int page = quranInfo.getPageFromSuraAyah(sura, ayah);
                pageInput.setHint(QuranUtils.getLocalizedNumber(context, page));
                pageInput.setText(null);
            }
            ayahInput.setTag(ayah);
            // seems numeric IM always use western arabic (not localized)
            String correctText = String.valueOf(ayah);
            // empty input means the user clears the input, we don't force to fill it, let him type
            if (s.length() > 0 && !correctText.equals(ayahString)) {
                s.replace(0, s.length(), correctText);
            }
        }
    });
    builder.setView(layout);
    builder.setPositiveButton(getString(R.string.dialog_ok), (dialog, which) -> {
        try {
            dismiss();
            String pageStr = pageInput.getText().toString();
            if (TextUtils.isEmpty(pageStr)) {
                pageStr = pageInput.getHint().toString();
                int page = Integer.parseInt(pageStr);
                int selectedSura = (int) suraInput.getTag();
                int selectedAyah = (int) ayahInput.getTag();
                if (activity instanceof JumpDestination) {
                    ((JumpDestination) activity).jumpToAndHighlight(page, selectedSura, selectedAyah);
                }
            } else {
                goToPage(pageStr);
            }
        } catch (Exception e) {
            Timber.d(e, "Could not jump, something went wrong...");
        }
    });
    return builder.create();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) EditText(android.widget.EditText) Context(android.content.Context) Activity(android.app.Activity) View(android.view.View) ForceCompleteTextView(com.quran.labs.androidquran.widgets.ForceCompleteTextView) TextView(android.widget.TextView) ForceCompleteTextView(com.quran.labs.androidquran.widgets.ForceCompleteTextView) SuppressLint(android.annotation.SuppressLint) LayoutInflater(android.view.LayoutInflater) SuppressLint(android.annotation.SuppressLint) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) JumpDestination(com.quran.labs.androidquran.ui.helpers.JumpDestination) NonNull(android.support.annotation.NonNull)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 AlertDialog (android.support.v7.app.AlertDialog)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 EditText (android.widget.EditText)1 TextView (android.widget.TextView)1 JumpDestination (com.quran.labs.androidquran.ui.helpers.JumpDestination)1 ForceCompleteTextView (com.quran.labs.androidquran.widgets.ForceCompleteTextView)1