package com.app.simplitend.apputils;

import android.text.Editable;
import android.text.TextWatcher;

public abstract class TextUtils {

    public static final TextWatcher LEADING_SPACE_WATCHER = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.toString().trim().isEmpty()) {
                editable.delete(0, editable.length());
            }else if (editable.length() > 1 && editable.charAt(editable.length() - 1) == ' ' &&
                        editable.charAt(editable.length() - 2) == ' '){
                // leading 2 spaces
                // only one allowed. Thus, removing last
                editable.delete(editable.length() - 1, editable.length());
            }else if (editable.length() > 0 && editable.charAt(0) == ' '){
                editable.delete(0, 1);
            }
        }
    };

}
