chinwag-android/app/src/main/java/com/keylesspalace/tusky/util/MentionTokenizer.java
Ivan Kupalov 4736462911 Fix sending wrong requests for autocompletion (#414)
The problem was that Tusky kept sending requests for autocompletion
while writing toots even when the user wasn't typing a username
anymore. As it happened very often we would exceed the API request
limit and user wouldn't be able to send the toot.

This happened because Tokenizer is not used as expected. In fact,
during testing, findTokenEnd() and terminateToken() were never called.
I've tried setting a Validator but it wasn't used either. I'm not
sure what is the reason.

I am afraid it still may work incorrectly for the full nicknames
(ones with the instance name, like @name@isntance) because
the search may happen for the instance name but it's not as
critical.
2017-10-24 23:02:38 +02:00

68 lines
2.2 KiB
Java

/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.util;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;
public class MentionTokenizer implements MultiAutoCompleteTextView.Tokenizer {
@Override
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') {
if (!Character.isLetterOrDigit(text.charAt(i - 1))) return cursor;
i--;
}
if (i < 1 || text.charAt(i - 1) != '@') {
return cursor;
}
return i;
}
@Override
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int length = text.length();
while (i < length) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}
return length;
}
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else if (text instanceof Spanned) {
SpannableString s = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, s, 0);
return s;
} else {
return text + " ";
}
}
}