Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit c6d52b4

Browse files
kunall17niftynei
authored andcommitted
Inititate Adapter's for AutoCompleteTextView
Intitating adapters for auto suggestion for the streams, Emails and Topics.
1 parent 9971c12 commit c6d52b4

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.support.v4.view.GravityCompat;
3535
import android.support.v4.widget.DrawerLayout;
3636
import android.support.v4.widget.SimpleCursorAdapter;
37+
import android.text.TextUtils;
3738
import android.util.Log;
3839
import android.view.Menu;
3940
import android.view.MenuInflater;
@@ -43,12 +44,14 @@
4344
import android.widget.AdapterView.OnItemClickListener;
4445
import android.widget.AutoCompleteTextView;
4546
import android.widget.EditText;
47+
import android.widget.FilterQueryProvider;
4648
import android.widget.ImageView;
4749
import android.widget.ListView;
4850
import android.widget.SearchView;
4951
import android.widget.TextView;
5052

5153
import com.j256.ormlite.android.AndroidDatabaseResults;
54+
import com.zulip.android.database.DatabaseHelper;
5255
import com.zulip.android.models.Message;
5356
import com.zulip.android.models.MessageType;
5457
import com.zulip.android.filters.NarrowFilter;
@@ -109,6 +112,9 @@ public class ZulipActivity extends FragmentActivity implements
109112
private ImageView sendBtn;
110113
private ImageView togglePrivateStreamBtn;
111114
Notifications notifications;
115+
SimpleCursorAdapter streamActvAdapter;
116+
SimpleCursorAdapter subjectActvAdapter;
117+
SimpleCursorAdapter emailActvAdapter;
112118

113119
private BroadcastReceiver onGcmMessage = new BroadcastReceiver() {
114120
public void onReceive(Context contenxt, Intent intent) {
@@ -423,6 +429,157 @@ public void onClick(View v) {
423429
switchView();
424430
}
425431
});
432+
setUpAdapter();
433+
streamActv.setAdapter(streamActvAdapter);
434+
topicActv.setAdapter(subjectActvAdapter);
435+
436+
public void setUpAdapter() {
437+
streamActvAdapter = new SimpleCursorAdapter(
438+
that, R.layout.stream_tile, null,
439+
new String[]{Stream.NAME_FIELD},
440+
new int[]{R.id.name}, 0);
441+
streamActvAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
442+
@Override
443+
public CharSequence convertToString(Cursor cursor) {
444+
int index = cursor.getColumnIndex(Stream.NAME_FIELD);
445+
return cursor.getString(index);
446+
}
447+
});
448+
streamActvAdapter.setFilterQueryProvider(new FilterQueryProvider() {
449+
@Override
450+
public Cursor runQuery(CharSequence charSequence) {
451+
try {
452+
return makeStreamCursor(charSequence);
453+
} catch (SQLException e) {
454+
Log.e("SQLException", "SQL not correct", e);
455+
return null;
456+
}
457+
}
458+
});
459+
subjectActvAdapter = new SimpleCursorAdapter(
460+
that, R.layout.stream_tile, null,
461+
new String[]{Message.SUBJECT_FIELD},
462+
new int[]{R.id.name}, 0);
463+
subjectActvAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
464+
@Override
465+
public CharSequence convertToString(Cursor cursor) {
466+
int index = cursor.getColumnIndex(Message.SUBJECT_FIELD);
467+
return cursor.getString(index);
468+
}
469+
});
470+
subjectActvAdapter.setFilterQueryProvider(new FilterQueryProvider() {
471+
@Override
472+
public Cursor runQuery(CharSequence charSequence) {
473+
try {
474+
return makeSubjectCursor(streamActv.getText().toString(), charSequence);
475+
} catch (SQLException e) {
476+
Log.e("SQLException", "SQL not correct", e);
477+
return null;
478+
}
479+
}
480+
});
481+
482+
emailActvAdapter = new SimpleCursorAdapter(
483+
that, R.layout.stream_tile, null,
484+
new String[]{Person.EMAIL_FIELD},
485+
new int[]{R.id.name}, 0);
486+
emailActvAdapter
487+
.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
488+
@Override
489+
public CharSequence convertToString(Cursor cursor) {
490+
String text = topicActv.getText().toString();
491+
String prefix;
492+
int lastIndex = text.lastIndexOf(",");
493+
if (lastIndex != -1) {
494+
prefix = text.substring(0, lastIndex + 1);
495+
} else {
496+
prefix = "";
497+
}
498+
int index = cursor.getColumnIndex(Person.EMAIL_FIELD);
499+
return prefix + cursor.getString(index);
500+
}
501+
});
502+
emailActvAdapter.setFilterQueryProvider(new FilterQueryProvider() {
503+
@Override
504+
public Cursor runQuery(CharSequence charSequence) {
505+
try {
506+
return makePeopleCursor(charSequence);
507+
} catch (SQLException e) {
508+
Log.e("SQLException", "SQL not correct", e);
509+
return null;
510+
}
511+
}
512+
});
513+
514+
}
515+
516+
private Cursor makeStreamCursor(CharSequence streamName)
517+
throws SQLException {
518+
if (streamName == null) {
519+
streamName = "";
520+
}
521+
522+
return ((AndroidDatabaseResults) app
523+
.getDao(Stream.class)
524+
.queryRaw(
525+
"SELECT rowid _id, * FROM streams WHERE "
526+
+ Stream.SUBSCRIBED_FIELD + " = 1 AND "
527+
+ Stream.NAME_FIELD
528+
+ " LIKE ? ESCAPE '\\' ORDER BY "
529+
+ Stream.NAME_FIELD + " COLLATE NOCASE",
530+
DatabaseHelper.likeEscape(streamName.toString()) + "%")
531+
.closeableIterator().getRawResults()).getRawCursor();
532+
}
533+
534+
private Cursor makeSubjectCursor(CharSequence stream, CharSequence subject)
535+
throws SQLException {
536+
if (subject == null) {
537+
subject = "";
538+
}
539+
if (stream == null) {
540+
stream = "";
541+
}
542+
543+
AndroidDatabaseResults results = (AndroidDatabaseResults) app
544+
.getDao(Message.class)
545+
.queryRaw(
546+
"SELECT DISTINCT "
547+
+ Message.SUBJECT_FIELD
548+
+ ", 1 AS _id FROM messages JOIN streams ON streams."
549+
+ Stream.ID_FIELD + " = messages."
550+
+ Message.STREAM_FIELD + " WHERE "
551+
+ Message.SUBJECT_FIELD
552+
+ " LIKE ? ESCAPE '\\' AND "
553+
+ Stream.NAME_FIELD + " = ? ORDER BY "
554+
+ Message.SUBJECT_FIELD + " COLLATE NOCASE",
555+
DatabaseHelper.likeEscape(subject.toString()) + "%",
556+
stream.toString()).closeableIterator().getRawResults();
557+
return results.getRawCursor();
558+
}
559+
560+
private Cursor makePeopleCursor(CharSequence email) throws SQLException {
561+
if (email == null) {
562+
email = "";
563+
}
564+
String[] pieces = TextUtils.split(email.toString(), ",");
565+
String piece;
566+
if (pieces.length == 0) {
567+
piece = "";
568+
} else {
569+
piece = pieces[pieces.length - 1].trim();
570+
}
571+
return ((AndroidDatabaseResults) app
572+
.getDao(Person.class)
573+
.queryRaw(
574+
"SELECT rowid _id, * FROM people WHERE "
575+
+ Person.ISBOT_FIELD + " = 0 AND "
576+
+ Person.ISACTIVE_FIELD + " = 1 AND "
577+
+ Person.EMAIL_FIELD
578+
+ " LIKE ? ESCAPE '\\' ORDER BY "
579+
+ Person.NAME_FIELD + " COLLATE NOCASE",
580+
DatabaseHelper.likeEscape(piece) + "%")
581+
.closeableIterator().getRawResults()).getRawCursor();
582+
}
426583
public void switchView() {
427584
if (isCurrentModeStream()) { //Person
428585
togglePrivateStreamBtn.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_bullhorn));

0 commit comments

Comments
 (0)