Skip to content

Commit 229304b

Browse files
committed
Implement SearchTechnologies
1 parent d45a81f commit 229304b

File tree

2 files changed

+90
-26
lines changed

2 files changed

+90
-26
lines changed

src/AndroidClient/techstacks/src/main/java/servicestack/net/techstacks/App.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public static class AppData
3333
AndroidServiceClient client;
3434

3535
ArrayList<AppDataListener> listeners = new ArrayList<>();
36-
AppOverviewResponse appOverviewResponse;
37-
QueryResponse<TechnologyStack> techStacksResponse;
3836

3937
public AppData addListener(AppDataListener callback){
4038
if (!listeners.contains(callback)){
@@ -67,6 +65,11 @@ public void success(AppOverviewResponse response){
6765
return this;
6866
}
6967

68+
AppOverviewResponse appOverviewResponse;
69+
public AppOverviewResponse getAppOverviewResponse(){
70+
return appOverviewResponse;
71+
}
72+
7073
String lastTechStacksQuery = null;
7174

7275
public AppData searchTechStacks(final String query){
@@ -76,27 +79,52 @@ public AppData searchTechStacks(final String query){
7679

7780
lastTechStacksQuery = query;
7881
client.getAsync(new FindTechStacks(),
79-
Utils.createMap("NameContains",query,"DescriptionContains",query),
80-
new AsyncResult<QueryResponse<TechnologyStack>>() {
81-
@Override
82-
public void success(QueryResponse<TechnologyStack> response) {
83-
if (Utils.equals(query,lastTechStacksQuery)){
84-
techStacksResponse = response;
85-
onUpdate(DataType.SearchTechStacks);
82+
Utils.createMap("NameContains",query,"DescriptionContains",query),
83+
new AsyncResult<QueryResponse<TechnologyStack>>() {
84+
@Override
85+
public void success(QueryResponse<TechnologyStack> response) {
86+
if (Utils.equals(query,lastTechStacksQuery)){
87+
techStacksResponse = response;
88+
onUpdate(DataType.SearchTechStacks);
89+
}
8690
}
87-
}
88-
});
91+
});
8992

9093
return this;
9194
}
9295

93-
public AppOverviewResponse getAppOverviewResponse(){
94-
return appOverviewResponse;
95-
}
96-
96+
QueryResponse<TechnologyStack> techStacksResponse;
9797
public QueryResponse<TechnologyStack> getSearchTechStacksResponse() {
9898
return techStacksResponse;
9999
}
100+
101+
String lastTechnologiesQuery = null;
102+
103+
public AppData searchTechnologies(final String query){
104+
if (technologiesResponse != null && Utils.equals(query,lastTechnologiesQuery)){
105+
onUpdate(DataType.SearchTechnologies);
106+
}
107+
108+
lastTechnologiesQuery = query;
109+
client.getAsync(new FindTechnologies(),
110+
Utils.createMap("NameContains",query,"DescriptionContains",query),
111+
new AsyncResult<QueryResponse<Technology>>() {
112+
@Override
113+
public void success(QueryResponse<Technology> response) {
114+
if (Utils.equals(query,lastTechnologiesQuery)){
115+
technologiesResponse = response;
116+
onUpdate(DataType.SearchTechnologies);
117+
}
118+
}
119+
});
120+
121+
return this;
122+
}
123+
124+
QueryResponse<Technology> technologiesResponse;
125+
public QueryResponse<Technology> getSearchTechnologiesResponse() {
126+
return technologiesResponse;
127+
}
100128
}
101129

102130
public static interface AppDataListener
@@ -108,6 +136,7 @@ public static enum DataType
108136
{
109137
AppOverview,
110138
SearchTechStacks,
139+
SearchTechnologies,
111140
}
112141

113142

src/AndroidClient/techstacks/src/main/java/servicestack/net/techstacks/MainActivity.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
299299
return rootView;
300300
}
301301

302-
EditText getSearchTechStacks(){
303-
if (getActivity() == null)
304-
return null;
305-
return (EditText) getActivity().findViewById(R.id.searchTechStacks);
306-
}
307-
308-
ListView getTechStacksListView(){
302+
ListView getListViewResults(){
309303
if (getActivity() == null)
310304
return null;
311305
return (ListView) getActivity().findViewById(R.id.listTechStacks);
@@ -315,10 +309,9 @@ ListView getTechStacksListView(){
315309
public void onUpdate(App.AppData data, App.DataType dataType) {
316310
switch (dataType) {
317311
case SearchTechStacks:
318-
ListView list = getTechStacksListView();
312+
ListView list = getListViewResults();
319313
if (list != null){
320-
ArrayList<TechnologyStack> searchResults = data.getSearchTechStacksResponse().getResults();
321-
ArrayList<String> results = map(searchResults, new Function<TechnologyStack, String>() {
314+
ArrayList<String> results = map(data.getSearchTechStacksResponse().getResults(), new Function<TechnologyStack, String>() {
322315
@Override
323316
public String apply(TechnologyStack o) {
324317
return o.getName();
@@ -331,7 +324,7 @@ public String apply(TechnologyStack o) {
331324
}
332325
}
333326

334-
public static class TechnologiesFragment extends Fragment {
327+
public static class TechnologiesFragment extends Fragment implements App.AppDataListener {
335328
public static TechnologiesFragment create(int sectionNumber) {
336329
TechnologiesFragment fragment = new TechnologiesFragment();
337330
Bundle args = new Bundle();
@@ -340,12 +333,54 @@ public static TechnologiesFragment create(int sectionNumber) {
340333
return fragment;
341334
}
342335

336+
@Override
337+
public void onCreate(Bundle savedInstanceState) {
338+
super.onCreate(savedInstanceState);
339+
App.getData().addListener(this);
340+
}
341+
343342
@Override
344343
public View onCreateView(LayoutInflater inflater, ViewGroup container,
345344
Bundle savedInstanceState) {
346345
View rootView = inflater.inflate(R.layout.fragment_technologies, container, false);
346+
347+
EditText txtSearch = (EditText)rootView.findViewById(R.id.searchTechnologies);
348+
txtSearch.addTextChangedListener(new TextWatcher() {
349+
@Override
350+
public void onTextChanged(CharSequence s, int start, int before, int count) {
351+
App.getData().searchTechnologies(s.toString());
352+
}
353+
354+
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
355+
@Override public void afterTextChanged(Editable s) {}
356+
});
357+
347358
return rootView;
348359
}
360+
361+
ListView getListViewResults(){
362+
if (getActivity() == null)
363+
return null;
364+
return (ListView) getActivity().findViewById(R.id.listTechnologies);
365+
}
366+
367+
@Override
368+
public void onUpdate(App.AppData data, App.DataType dataType) {
369+
switch (dataType) {
370+
case SearchTechnologies:
371+
ListView list = getListViewResults();
372+
if (list != null){
373+
ArrayList<String> results = map(data.getSearchTechnologiesResponse().getResults(), new Function<Technology, String>() {
374+
@Override
375+
public String apply(Technology o) {
376+
return o.getName();
377+
}
378+
});
379+
list.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, results));
380+
}
381+
break;
382+
}
383+
}
349384
}
350385

351386
}

0 commit comments

Comments
 (0)