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

Commit f075fbd

Browse files
committed
On text change change people cursor instead of setting adapter
1 parent e932a90 commit f075fbd

File tree

1 file changed

+54
-52
lines changed

1 file changed

+54
-52
lines changed

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

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ protected void onCreate(Bundle savedInstanceState) {
327327
@Override
328328
public void onClick(View v) {
329329
//set default people list
330-
setUpPeopleList(true);
331-
//set visibility of this image false
332-
ivSearchPeopleCancel.setVisibility(View.GONE);
330+
try {
331+
peopleAdapter.changeCursor(getPeopleCursorGenerator().call());
332+
} catch (Exception e) {
333+
e.printStackTrace();
334+
}
333335
//set search editText text empty
334336
etSearchPeople.setText("");
335337
}
@@ -379,7 +381,7 @@ public void onDrawerOpened(View drawerView) {
379381
peopleDrawer = (ListView) findViewById(R.id.people_drawer);
380382

381383
//set up people list
382-
setUpPeopleList(true);
384+
setUpPeopleList();
383385

384386
peopleDrawer.setOnItemClickListener(new OnItemClickListener() {
385387
@Override
@@ -563,23 +565,67 @@ public void afterTextChanged(Editable s) {
563565
});
564566
}
565567

566-
private void setUpPeopleList(final boolean isDefault, final String... filterKeyWord) {
568+
private void setUpPeopleList() {
569+
try {
570+
this.peopleAdapter = new RefreshableCursorAdapter(
571+
this.getApplicationContext(), R.layout.stream_tile,
572+
getPeopleCursorGenerator().call(), getPeopleCursorGenerator(), new String[]{
573+
Person.NAME_FIELD, Person.EMAIL_FIELD}, new int[]{
574+
R.id.name, R.id.stream_dot}, 0);
575+
peopleAdapter.setViewBinder(peopleBinder);
576+
577+
peopleDrawer.setAdapter(peopleAdapter);
578+
} catch (SQLException e) {
579+
throw new RuntimeException(e);
580+
} catch (Exception e) {
581+
ZLog.logException(e);
582+
}
583+
}
584+
585+
private void onTextChangeOfPeopleSearchEditText() {
586+
etSearchPeople.addTextChangedListener(new TextWatcher() {
587+
@Override
588+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
589+
590+
}
591+
592+
@Override
593+
public void onTextChanged(CharSequence s, int start, int before, int count) {
594+
try {
595+
peopleAdapter.changeCursor(getPeopleCursorGenerator().call());
596+
} catch (Exception e) {
597+
e.printStackTrace();
598+
}
599+
}
600+
601+
@Override
602+
public void afterTextChanged(Editable s) {
603+
604+
}
605+
});
606+
}
607+
608+
private Callable<Cursor> getPeopleCursorGenerator() {
567609
Callable<Cursor> peopleGenerator = new Callable<Cursor>() {
568610

569611
@Override
570612
public Cursor call() throws Exception {
571613
// TODO Auto-generated method stub
572614
List<Person> people;
573-
if (isDefault) {
615+
if (etSearchPeople.getText().toString().equals("") || etSearchPeople.getText().toString().isEmpty()) {
574616
people = app.getDao(Person.class).queryBuilder()
575617
.where().eq(Person.ISBOT_FIELD, false).and()
576618
.eq(Person.ISACTIVE_FIELD, true).query();
619+
//set visibility of this image false
620+
ivSearchPeopleCancel.setVisibility(View.GONE);
577621
}else
578622
{
579623
people = app.getDao(Person.class).queryBuilder()
580624
.where().eq(Person.ISBOT_FIELD, false).and()
581-
.like(Person.NAME_FIELD,"%"+filterKeyWord[0]+"%").and()
625+
.like(Person.NAME_FIELD,"%"+etSearchPeople.getText().toString()+"%").and()
582626
.eq(Person.ISACTIVE_FIELD, true).query();
627+
//set visibility of this image false
628+
ivSearchPeopleCancel.setVisibility(View.VISIBLE);
583629
}
584630

585631
Person.sortByPresence(app, people);
@@ -609,51 +655,7 @@ public Cursor call() throws Exception {
609655
}
610656

611657
};
612-
try {
613-
this.peopleAdapter = new RefreshableCursorAdapter(
614-
this.getApplicationContext(), R.layout.stream_tile,
615-
peopleGenerator.call(), peopleGenerator, new String[]{
616-
Person.NAME_FIELD, Person.EMAIL_FIELD}, new int[]{
617-
R.id.name, R.id.stream_dot}, 0);
618-
peopleAdapter.setViewBinder(peopleBinder);
619-
620-
peopleDrawer.setAdapter(peopleAdapter);
621-
} catch (SQLException e) {
622-
throw new RuntimeException(e);
623-
} catch (Exception e) {
624-
ZLog.logException(e);
625-
}
626-
}
627-
628-
private void onTextChangeOfPeopleSearchEditText() {
629-
etSearchPeople.addTextChangedListener(new TextWatcher() {
630-
@Override
631-
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
632-
633-
}
634-
635-
@Override
636-
public void onTextChanged(CharSequence s, int start, int before, int count) {
637-
if (etSearchPeople.getText().toString().equals("") || etSearchPeople.getText().toString().isEmpty())
638-
{
639-
//set default people list
640-
setUpPeopleList(true);
641-
//set visibility of this image false
642-
ivSearchPeopleCancel.setVisibility(View.GONE);
643-
}else
644-
{
645-
//filter people list
646-
setUpPeopleList(false,etSearchPeople.getText().toString());
647-
//set visibility of this image false
648-
ivSearchPeopleCancel.setVisibility(View.VISIBLE);
649-
}
650-
}
651-
652-
@Override
653-
public void afterTextChanged(Editable s) {
654-
655-
}
656-
});
658+
return peopleGenerator;
657659
}
658660

659661
@Override

0 commit comments

Comments
 (0)