Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Kentaro Takiguchi committed May 24, 2015
1 parent 19bd09e commit 72681b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
20 changes: 19 additions & 1 deletion app/src/main/java/rejasupotaro/mds/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,31 @@
import butterknife.ButterKnife;
import butterknife.InjectView;
import rejasupotaro.mds.R;
import rejasupotaro.mds.data.services.ChannelService;
import rejasupotaro.mds.data.models.Channel;
import rejasupotaro.mds.data.services.ChannelService;
import rejasupotaro.mds.data.services.SuggestionService;
import rejasupotaro.mds.view.adapters.RecipeListAdapter;
import rejasupotaro.mds.view.components.SearchView;
import rx.Subscription;
import rx.android.app.AppObservable;
import rx.functions.Action1;
import rx.subscriptions.Subscriptions;

public class MainActivity extends BaseActivity {

@InjectView(R.id.drawer_layout)
DrawerLayout drawerLayout;
@InjectView(R.id.search_view)
SearchView searchView;
@InjectView(R.id.toolbar)
Toolbar toolbar;
@InjectView(R.id.channel_recipe_list)
StaggeredGridView recipeListView;

private RecipeListAdapter recipeListAdapter;
private Subscription channelsSubscription = Subscriptions.empty();
private Subscription querySubscription = Subscriptions.empty();
private Subscription suggestionsSubscription = Subscriptions.empty();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -47,6 +54,8 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onDestroy() {
channelsSubscription.unsubscribe();
querySubscription.unsubscribe();
suggestionsSubscription.unsubscribe();
super.onDestroy();
}

Expand Down Expand Up @@ -87,6 +96,15 @@ private void setupActionBar() {
private void setupViews() {
channelsSubscription = AppObservable.bindActivity(this, new ChannelService().getList())
.subscribe(this::setupViews);

querySubscription = AppObservable.bindActivity(this, searchView.observe())
.subscribe(this::updateSuggestions);
}

private void updateSuggestions(String query) {
suggestionsSubscription.unsubscribe();
suggestionsSubscription = AppObservable.bindActivity(this, new SuggestionService().get(query))
.subscribe(searchView::updateSuggestions);
}

private void setupViews(List<Channel> channels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.ArrayList;
import java.util.List;

import rx.Observable;

public class SuggestionService {
private static final List<String> DUMMY = new ArrayList<String>() {{
add("apple");
Expand All @@ -23,15 +25,16 @@ public class SuggestionService {
add("bbq sauce");
}};

public List<String> get(String query) {
List<String> suggestions = new ArrayList<>();
public Observable<List<String>> get(String query) {
if (TextUtils.isEmpty(query)) {
return suggestions;
return Observable.just(new ArrayList<>());
}

List<String> suggestions = new ArrayList<>();
int count = new SecureRandom().nextInt(DUMMY.size() - 1) + 1;
for (int i = 0; i < count; i++) {
suggestions.add(DUMMY.get(new SecureRandom().nextInt(DUMMY.size())));
}
return suggestions;
return Observable.just(suggestions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rejasupotaro.mds.R;
import rejasupotaro.mds.data.services.SuggestionService;

public class SuggestionListAdapter extends BindableAdapter<String> {

private SuggestionService suggestionService = new SuggestionService();

static class ViewHolder {
@InjectView(R.id.suggestion_text)
TextView suggestionText;
Expand All @@ -28,9 +27,9 @@ public SuggestionListAdapter(Context context) {
super(context);
}

public void append(String query) {
public void update(List<String> suggestions) {
clear();
addAll(suggestionService.get(query));
addAll(suggestions);
notifyDataSetChanged();
}

Expand All @@ -47,5 +46,4 @@ public void bindView(String item, int position, View view) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.suggestionText.setText(item);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.FrameLayout;
import android.widget.ListView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
Expand Down Expand Up @@ -61,13 +62,10 @@ private void setup() {

suggestionListAdapter = new SuggestionListAdapter(getContext());
suggestionListView.setAdapter(suggestionListAdapter);

ViewObservable.bindView(this, observe())
.subscribe(this::updateSuggestions);
}

private void updateSuggestions(String query) {
suggestionListAdapter.append(query);
public void updateSuggestions(List<String> suggestions) {
suggestionListAdapter.update(suggestions);
}

@Override
Expand Down

0 comments on commit 72681b0

Please sign in to comment.