2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
5
+ import java .util .Timer ;
6
+ import java .util .TimerTask ;
5
7
6
8
import org .astonbitecode .rustkeylock .R ;
7
9
import org .astonbitecode .rustkeylock .adapters .EntriesAdapter ;
13
15
import android .app .ListFragment ;
14
16
import android .content .Context ;
15
17
import android .os .Bundle ;
18
+ import android .text .Editable ;
19
+ import android .text .TextWatcher ;
16
20
import android .util .Log ;
17
21
import android .view .LayoutInflater ;
18
22
import android .view .View ;
19
23
import android .view .View .OnClickListener ;
20
24
import android .view .ViewGroup ;
21
25
import android .view .inputmethod .InputMethodManager ;
22
26
import android .widget .Button ;
27
+ import android .widget .EditText ;
23
28
import android .widget .ListView ;
24
29
25
30
public class ListEntries extends ListFragment implements OnClickListener , BackButtonHandler {
26
31
private static final long serialVersionUID = 8765819759487480794L ;
27
32
private final String TAG = getClass ().getName ();
28
33
private List <JavaEntry > entries ;
29
34
private EntriesAdapter entriesAdapter ;
35
+ private String filter ;
30
36
31
37
public ListEntries () {
32
38
this .entries = new ArrayList <>();
33
39
}
34
40
35
- public ListEntries (List <JavaEntry > entries ) {
41
+ public ListEntries (List <JavaEntry > entries , String filter ) {
36
42
this .entries = entries ;
43
+ this .filter = filter ;
37
44
}
38
45
39
46
@ Override
40
47
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
48
+ restore (savedInstanceState );
41
49
if (savedInstanceState != null ) {
42
- InterfaceWithRust .INSTANCE .go_to_menu (Defs .MENU_ENTRIES_LIST );
50
+ InterfaceWithRust .INSTANCE .go_to_menu_plus_arg (Defs .MENU_ENTRIES_LIST , Defs . EMPTY_ARG , filter );
43
51
}
44
52
View rootView = inflater .inflate (R .layout .fragment_list_entries , container , false );
45
53
Button nb = (Button ) rootView .findViewById (R .id .addNewButton );
46
54
nb .setOnClickListener (this );
47
55
56
+ EditText filterText = (EditText ) rootView .findViewById (R .id .editFilter );
57
+ filterText .setText (filter );
58
+ filterText .addTextChangedListener (new TextWatcher () {
59
+ private Timer timer = new Timer ();
60
+ private final long DELAY = 500 ;
61
+
62
+ @ Override
63
+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
64
+ // ignore
65
+ }
66
+
67
+ @ Override
68
+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
69
+ // ignore
70
+ }
71
+
72
+ @ Override
73
+ public void afterTextChanged (final Editable s ) {
74
+ timer .cancel ();
75
+ timer = new Timer ();
76
+ timer .schedule (new TimerTask () {
77
+ @ Override
78
+ public void run () {
79
+ InterfaceWithRust .INSTANCE .go_to_menu_plus_arg (Defs .MENU_ENTRIES_LIST , Defs .EMPTY_ARG ,
80
+ s != null ? s .toString () : "" );
81
+ }
82
+ }, DELAY );
83
+ }
84
+ });
85
+ if (filter .length () > 0 ) {
86
+ filterText .setFocusableInTouchMode (true );
87
+ filterText .requestFocus ();
88
+ }
89
+
48
90
// Hide the soft keyboard
49
91
final InputMethodManager imm = (InputMethodManager ) getActivity ()
50
92
.getSystemService (Context .INPUT_METHOD_SERVICE );
@@ -64,7 +106,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
64
106
public void onListItemClick (ListView l , View v , int pos , long id ) {
65
107
Log .d (TAG , "Clicked entry with index " + pos + " in the list of entries" );
66
108
super .onListItemClick (l , v , pos , id );
67
- InterfaceWithRust .INSTANCE .go_to_menu_plus_arg (Defs .MENU_SHOW_ENTRY , pos );
109
+ InterfaceWithRust .INSTANCE .go_to_menu_plus_arg (Defs .MENU_SHOW_ENTRY , pos + "" , Defs . EMPTY_ARG );
68
110
}
69
111
70
112
@ Override
@@ -78,4 +120,16 @@ public void onBackButton() {
78
120
Log .d (TAG , "Back button pressed" );
79
121
InterfaceWithRust .INSTANCE .go_to_menu (Defs .MENU_MAIN );
80
122
}
123
+
124
+ @ Override
125
+ public void onSaveInstanceState (Bundle state ) {
126
+ state .putString ("filter" , filter );
127
+ }
128
+
129
+ private void restore (Bundle state ) {
130
+ if (state != null ) {
131
+ filter = state .getString ("filter" );
132
+ }
133
+ }
134
+
81
135
}
0 commit comments