39
39
import android .support .v4 .widget .SimpleCursorAdapter ;
40
40
import android .support .v7 .app .AppCompatDelegate ;
41
41
import android .support .v7 .widget .Toolbar ;
42
+ import android .text .Editable ;
42
43
import android .text .TextUtils ;
44
+ import android .text .TextWatcher ;
43
45
import android .util .Log ;
44
46
import android .view .Menu ;
45
47
import android .view .MenuItem ;
@@ -185,6 +187,17 @@ public enum Flag {
185
187
RESET_DATABASE ,
186
188
}
187
189
190
+ private EditText etSearchPeople ;
191
+ private ImageView ivSearchPeopleCancel ;
192
+ private EditText etSearchStream ;
193
+ private ImageView ivSearchStreamCancel ;
194
+ private ListView peopleDrawer ;
195
+ // row number which is used to differentiate the 'All private messages'
196
+ // row from the people
197
+ final int allPeopleId = -1 ;
198
+
199
+ //
200
+ private String streamSearchFilterKeyword ="" ;
188
201
189
202
public HashMap <String , Bitmap > getGravatars () {
190
203
return gravatars ;
@@ -307,6 +320,34 @@ protected void onCreate(Bundle savedInstanceState) {
307
320
textView = (TextView ) findViewById (R .id .textView );
308
321
sendBtn = (ImageView ) findViewById (R .id .send_btn );
309
322
appBarLayout = (AppBarLayout ) findViewById (R .id .appBarLayout );
323
+ etSearchPeople = (EditText )findViewById (R .id .people_drawer_search );
324
+ ivSearchPeopleCancel = (ImageView )findViewById (R .id .iv_people__search_cancel_button );
325
+ onTextChangeOfPeopleSearchEditText ();
326
+ ivSearchPeopleCancel .setOnClickListener (new View .OnClickListener () {
327
+ @ Override
328
+ public void onClick (View v ) {
329
+ //set default people list
330
+ setUpPeopleList (true );
331
+ //set visibility of this image false
332
+ ivSearchPeopleCancel .setVisibility (View .GONE );
333
+ //set search editText text empty
334
+ etSearchPeople .setText ("" );
335
+ }
336
+ });
337
+ etSearchStream = (EditText )findViewById (R .id .stream_drawer_search );
338
+ ivSearchStreamCancel = (ImageView )findViewById (R .id .iv_stream_search_cancel_button );
339
+ onTextChangeOfStreamSearchEditText ();
340
+ ivSearchStreamCancel .setOnClickListener (new View .OnClickListener () {
341
+ @ Override
342
+ public void onClick (View v ) {
343
+ //set default stream list
344
+ setupListViewAdapter (true );
345
+ //set visibility of this image false
346
+ ivSearchStreamCancel .setVisibility (View .GONE );
347
+ //set search editText text empty
348
+ etSearchStream .setText ("" );
349
+ }
350
+ });
310
351
app .setZulipActivity (this );
311
352
togglePrivateStreamBtn = (ImageView ) findViewById (R .id .togglePrivateStream_btn );
312
353
drawerLayout = (DrawerLayout ) findViewById (R .id .drawer_layout );
@@ -334,61 +375,11 @@ public void onDrawerOpened(View drawerView) {
334
375
// Set the drawer toggle as the DrawerListener
335
376
drawerLayout .setDrawerListener (drawerToggle );
336
377
337
- ListView peopleDrawer = (ListView ) findViewById (R .id .people_drawer );
338
-
339
- // row number which is used to differentiate the 'All private messages'
340
- // row from the people
341
- final int allPeopleId = -1 ;
342
- Callable <Cursor > peopleGenerator = new Callable <Cursor >() {
343
-
344
- @ Override
345
- public Cursor call () throws Exception {
346
- // TODO Auto-generated method stub
347
- List <Person > people = app .getDao (Person .class ).queryBuilder ()
348
- .where ().eq (Person .ISBOT_FIELD , false ).and ()
349
- .eq (Person .ISACTIVE_FIELD , true ).query ();
350
-
351
- Person .sortByPresence (app , people );
352
-
353
- String [] columnsWithPresence = new String []{"_id" ,
354
- Person .EMAIL_FIELD , Person .NAME_FIELD };
355
-
356
- MatrixCursor sortedPeopleCursor = new MatrixCursor (
357
- columnsWithPresence );
358
- for (Person person : people ) {
359
- Object [] row = new Object []{person .getId (), person .getEmail (),
360
- person .getName ()};
361
- sortedPeopleCursor .addRow (row );
362
- }
363
-
364
- // add private messages row
365
- MatrixCursor allPrivateMessages = new MatrixCursor (
366
- sortedPeopleCursor .getColumnNames ());
367
- Object [] row = new Object []{allPeopleId , "" ,
368
- "All private messages" };
369
-
370
- allPrivateMessages .addRow (row );
371
-
372
- return new MergeCursor (new Cursor []{
373
- allPrivateMessages , sortedPeopleCursor });
374
-
375
- }
376
378
377
- };
378
- try {
379
- this .peopleAdapter = new RefreshableCursorAdapter (
380
- this .getApplicationContext (), R .layout .stream_tile ,
381
- peopleGenerator .call (), peopleGenerator , new String []{
382
- Person .NAME_FIELD , Person .EMAIL_FIELD }, new int []{
383
- R .id .name , R .id .stream_dot }, 0 );
384
- peopleAdapter .setViewBinder (peopleBinder );
379
+ peopleDrawer = (ListView ) findViewById (R .id .people_drawer );
385
380
386
- peopleDrawer .setAdapter (peopleAdapter );
387
- } catch (SQLException e ) {
388
- throw new RuntimeException (e );
389
- } catch (Exception e ) {
390
- ZLog .logException (e );
391
- }
381
+ //set up people list
382
+ setUpPeopleList (true );
392
383
393
384
peopleDrawer .setOnItemClickListener (new OnItemClickListener () {
394
385
@ Override
@@ -540,6 +531,131 @@ public Cursor runQuery(CharSequence charSequence) {
540
531
}
541
532
}
542
533
534
+ private void onTextChangeOfStreamSearchEditText () {
535
+ etSearchStream .addTextChangedListener (new TextWatcher () {
536
+ @ Override
537
+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
538
+
539
+ }
540
+
541
+ @ Override
542
+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
543
+ if (etSearchStream .getText ().toString ().equals ("" ) || etSearchStream .getText ().toString ().isEmpty ())
544
+ {
545
+ //set default stream list
546
+ setupListViewAdapter (true );
547
+ //set visibility of this image false
548
+ ivSearchStreamCancel .setVisibility (View .GONE );
549
+ }else
550
+ {
551
+ //filter stream list
552
+ streamSearchFilterKeyword = etSearchStream .getText ().toString ();
553
+ setupListViewAdapter (false );
554
+ //set visibility of this image false
555
+ ivSearchStreamCancel .setVisibility (View .VISIBLE );
556
+ }
557
+ }
558
+
559
+ @ Override
560
+ public void afterTextChanged (Editable s ) {
561
+
562
+ }
563
+ });
564
+ }
565
+
566
+ private void setUpPeopleList (final boolean isDefault , final String ... filterKeyWord ) {
567
+ Callable <Cursor > peopleGenerator = new Callable <Cursor >() {
568
+
569
+ @ Override
570
+ public Cursor call () throws Exception {
571
+ // TODO Auto-generated method stub
572
+ List <Person > people ;
573
+ if (isDefault ) {
574
+ people = app .getDao (Person .class ).queryBuilder ()
575
+ .where ().eq (Person .ISBOT_FIELD , false ).and ()
576
+ .eq (Person .ISACTIVE_FIELD , true ).query ();
577
+ }else
578
+ {
579
+ people = app .getDao (Person .class ).queryBuilder ()
580
+ .where ().eq (Person .ISBOT_FIELD , false ).and ()
581
+ .like (Person .NAME_FIELD ,"%" +filterKeyWord [0 ]+"%" ).and ()
582
+ .eq (Person .ISACTIVE_FIELD , true ).query ();
583
+ }
584
+
585
+ Person .sortByPresence (app , people );
586
+
587
+ String [] columnsWithPresence = new String []{"_id" ,
588
+ Person .EMAIL_FIELD , Person .NAME_FIELD };
589
+
590
+ MatrixCursor sortedPeopleCursor = new MatrixCursor (
591
+ columnsWithPresence );
592
+ for (Person person : people ) {
593
+ Object [] row = new Object []{person .getId (), person .getEmail (),
594
+ person .getName ()};
595
+ sortedPeopleCursor .addRow (row );
596
+ }
597
+
598
+ // add private messages row
599
+ MatrixCursor allPrivateMessages = new MatrixCursor (
600
+ sortedPeopleCursor .getColumnNames ());
601
+ Object [] row = new Object []{allPeopleId , "" ,
602
+ "All private messages" };
603
+
604
+ allPrivateMessages .addRow (row );
605
+
606
+ return new MergeCursor (new Cursor []{
607
+ allPrivateMessages , sortedPeopleCursor });
608
+
609
+ }
610
+
611
+ };
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
+ });
657
+ }
658
+
543
659
@ Override
544
660
protected void onNewIntent (Intent intent ) {
545
661
super .onNewIntent (intent );
@@ -835,10 +951,20 @@ public Cursor call() throws Exception {
835
951
}
836
952
};
837
953
954
+ Callable <Cursor > streamsGeneratorWithFilter = new Callable <Cursor >() {
955
+ @ Override
956
+ public Cursor call () throws Exception {
957
+ int pointer = app .getPointer ();
958
+ return ((AndroidDatabaseResults ) app .getDao (Stream .class ).queryRaw ("SELECT s.id as _id, s.name, s.color," +
959
+ " count(case when m.id > " + pointer + " or m." + Message .MESSAGE_READ_FIELD + " = 0 then 1 end) as " + ExpandableStreamDrawerAdapter .UNREAD_TABLE_NAME
960
+ + " FROM streams as s LEFT JOIN messages as m ON s.id=m.stream" +" WHERE s.name LIKE '%" + streamSearchFilterKeyword +"%' group by s.name order by s.name COLLATE NOCASE" ).closeableIterator ().getRawResults ()).getRawCursor ();
961
+ }
962
+ };
963
+
838
964
/**
839
965
* Setup the streams Drawer which has a {@link ExpandableListView} categorizes the stream and subject
840
966
*/
841
- private void setupListViewAdapter () {
967
+ private void setupListViewAdapter (boolean isDefault ) {
842
968
streamsDrawerAdapter = null ;
843
969
String [] groupFrom = {Stream .NAME_FIELD , Stream .COLOR_FIELD , ExpandableStreamDrawerAdapter .UNREAD_TABLE_NAME };
844
970
int [] groupTo = {R .id .name , R .id .stream_dot , R .id .unread_group };
@@ -848,10 +974,18 @@ private void setupListViewAdapter() {
848
974
final ExpandableListView streamsDrawer = (ExpandableListView ) findViewById (R .id .streams_drawer );
849
975
streamsDrawer .setGroupIndicator (null );
850
976
try {
851
- streamsDrawerAdapter = new ExpandableStreamDrawerAdapter (this , streamsGenerator .call (),
852
- R .layout .stream_tile_new , groupFrom ,
853
- groupTo , R .layout .stream_tile_child , childFrom ,
854
- childTo );
977
+ if (isDefault ) {
978
+ streamsDrawerAdapter = new ExpandableStreamDrawerAdapter (this , streamsGenerator .call (),
979
+ R .layout .stream_tile_new , groupFrom ,
980
+ groupTo , R .layout .stream_tile_child , childFrom ,
981
+ childTo );
982
+ }else
983
+ {
984
+ streamsDrawerAdapter = new ExpandableStreamDrawerAdapter (this , streamsGeneratorWithFilter .call (),
985
+ R .layout .stream_tile_new , groupFrom ,
986
+ groupTo , R .layout .stream_tile_child , childFrom ,
987
+ childTo );
988
+ }
855
989
} catch (Exception e ) {
856
990
ZLog .logException (e );
857
991
}
@@ -954,7 +1088,7 @@ public void onClick(View v) {
954
1088
* Initiates the streams Drawer if the streams in the drawer is 0.
955
1089
*/
956
1090
public void checkAndSetupStreamsDrawer () {
957
- setupListViewAdapter ();
1091
+ setupListViewAdapter (true );
958
1092
}
959
1093
960
1094
private void sendMessage () {
0 commit comments