-
Notifications
You must be signed in to change notification settings - Fork 11
/
MainActivity.java
128 lines (104 loc) · 4.32 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.amsen.par.searchview.demo.simple;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.amsen.par.searchview.AutoCompleteSearchView;
import com.amsen.par.searchview.demo.R;
import com.amsen.par.searchview.demo.api.MockApi;
import com.amsen.par.searchview.prediction.Prediction;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Pär Amsen 2016
*/
public class MainActivity extends AppCompatActivity {
private MockApi api;
private AutoCompleteSearchView searchView;
private TimerTask fakeNetworkCall;
private Timer fakeNetworkThread;
private View callToActionView;
private TextView andSearch;
private View searchedTitle;
private TextView predictionResultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
api = new MockApi();
fakeNetworkThread = new Timer();
callToActionView = findViewById(R.id.clickSearch);
andSearch = (TextView) findViewById(R.id.andSearch);
searchedTitle = findViewById(R.id.title);
predictionResultView = (TextView) findViewById(R.id.selectedPrediction);
andSearch.setText(getString(R.string.and_search_ARG1, "\uD83E\uDD13")); //set nerd
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
searchView = (AutoCompleteSearchView) searchViewItem.getActionView();
searchView.setUseDefaultProgressBar(true);
searchView.setOnPredictionClickListener((position, prediction) -> {
callToActionView.setVisibility(View.GONE);
searchedTitle.setVisibility(View.VISIBLE);
predictionResultView.setVisibility(View.VISIBLE);
Toast.makeText(this, String.format("clicked [position:%d, value:%s, displayString:%s]", position, prediction.value, prediction.displayString), Toast.LENGTH_SHORT).show();
predictionResultView.setText("You tapped: " + prediction.displayString);
searchViewItem.collapseActionView();
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
searchView.showProgressBar();
fakeNetworkPredictions(newText);
}
return true;
}
});
return true;
}
/**
* Async predictions with a delay of 400-1500ms
* If called again before the old "request" finished
* the old will be canceled and the new starts
* instead.
*/
private void fakeNetworkPredictions(final String query) {
if (fakeNetworkCall != null) {
fakeNetworkCall.cancel();
fakeNetworkCall = null;
}
fakeNetworkCall = new TimerTask() {
@Override
public void run() {
runOnUiThread(() -> {
fakeNetworkCall = null;
List<String> rawPredictions = api.getPredictions(query);
List<Prediction> predictions = toSearchViewPredictions(rawPredictions);
searchView.applyPredictions(predictions);
searchView.hideProgressBar();
});
}
};
fakeNetworkThread.schedule(fakeNetworkCall, 400 + (long) (Math.random() * 1100));
}
private List<com.amsen.par.searchview.prediction.Prediction> toSearchViewPredictions(List<String> predictions) {
List<Prediction> forSearchView = new ArrayList<>();
for (String prediction : predictions) {
forSearchView.add(new Prediction(prediction, prediction)); //first param is for complex objects, second for display string.
}
return forSearchView;
}
}