que;
19 |
20 | public ImageCardView(Context context) {
21 | this(context, null);
22 | }
23 |
24 | public ImageCardView(Context context, AttributeSet attrs) {
25 | this(context, attrs, 0);
26 | }
27 |
28 | public ImageCardView(Context context, AttributeSet attrs, int defStyle) {
29 | super(context, attrs, defStyle);
30 | ((BaseActivity) context).inject(this);
31 | }
32 |
33 |
34 | @Override
35 | protected void onFinishInflate() {
36 | super.onFinishInflate();
37 | bindImageData();
38 |
39 | }
40 |
41 | public void bindImageData() {
42 | Result image = que.remove();
43 | if (image != null) {
44 | Uri uri = Uri.parse(image.getUnescapedUrl());
45 | SquareDraweeView draweeView = (SquareDraweeView) findViewById(R.id.avatar);
46 | draweeView.setImageURI(uri);
47 | }
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rx/demo/ui/view/SearchView.java:
--------------------------------------------------------------------------------
1 | package com.rx.demo.ui.view;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.ViewGroup;
6 | import android.widget.EditText;
7 | import android.widget.LinearLayout;
8 | import android.widget.ScrollView;
9 |
10 | import com.digitalbuddha.rx.demo.R;
11 | import com.rx.demo.ui.activity.BaseActivity;
12 | import com.rx.demo.ui.view.presenter.ImageSearchPresenter;
13 |
14 | import javax.inject.Inject;
15 |
16 | import butterknife.ButterKnife;
17 | import butterknife.InjectView;
18 |
19 | public class SearchView extends ScrollView {
20 | @InjectView(R.id.searchBox)
21 | EditText search;
22 | @InjectView(R.id.cards)
23 | public
24 | LinearLayout cardsLayout;
25 | @Inject
26 | ImageSearchPresenter presenter;
27 |
28 |
29 | public SearchView(Context context) {
30 | this(context, null);
31 | }
32 |
33 | public SearchView(Context context, AttributeSet attrs) {
34 | this(context, attrs, 0);
35 | }
36 |
37 | public SearchView(Context context, AttributeSet attrs, int defStyle) {
38 | super(context, attrs, defStyle);
39 | ((BaseActivity) context).inject(this);
40 | }
41 |
42 |
43 | @Override
44 | protected void onFinishInflate() {
45 | super.onFinishInflate();
46 | ButterKnife.inject(this);
47 | presenter.takeView(this);
48 | }
49 |
50 |
51 | @Override
52 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
53 | super.onSizeChanged(w, h, oldw, oldh);
54 | presenter.drawNewRowIfNeeded();
55 | }
56 |
57 | /**
58 | * Inflate a new row
59 | * Bind data to row
60 | * Add row to container
61 | *
62 | *
63 | */
64 | public void addRow() {
65 | ViewGroup row = getLastResultsRow();
66 | while (row.getChildCount() < 3) {
67 | inflate(getContext(), R.layout.image_card, row);
68 | }
69 | presenter.drawNewRowIfNeeded();
70 | }
71 |
72 | /**
73 | * if less than 3 images in last row return it
74 | * else return a newly added linear layout
75 | *
76 | * @return ViewGroup with 0-2 images
77 | */
78 | private ViewGroup getLastResultsRow() {
79 | ViewGroup bottomRow = (ViewGroup) cardsLayout.getChildAt(cardsLayout.getChildCount() - 1);
80 | if (bottomRow == null || bottomRow.getChildCount() == 3) {
81 | inflate(getContext(), R.layout.row_view, cardsLayout);
82 | bottomRow = (ViewGroup) cardsLayout.getChildAt(cardsLayout.getChildCount() - 1);
83 | }
84 | return bottomRow;
85 | }
86 |
87 |
88 | /**
89 | * detach from presenter
90 | */
91 | @Override
92 | protected void onDetachedFromWindow() {
93 | super.onDetachedFromWindow();
94 | presenter.dropView();
95 | }
96 |
97 | public EditText getSearchView() {
98 | return getSearch();
99 | }
100 |
101 | public EditText getSearch() {
102 | return search;
103 | }
104 |
105 | public void updateSearchView(String searchTerm) {
106 | search.setText(searchTerm);
107 | presenter.clearResults();
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rx/demo/ui/view/SquareDraweeView.java:
--------------------------------------------------------------------------------
1 | package com.rx.demo.ui.view;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 |
6 | import com.facebook.drawee.view.SimpleDraweeView;
7 |
8 | public class SquareDraweeView extends SimpleDraweeView {
9 |
10 |
11 | public SquareDraweeView(Context context) {
12 | this(context, null);
13 | }
14 |
15 | public SquareDraweeView(Context context, AttributeSet attrs) {
16 | this(context, attrs, 0);
17 | }
18 |
19 | public SquareDraweeView(Context context, AttributeSet attrs, int defStyle) {
20 | super(context, attrs, defStyle);
21 | }
22 |
23 | /**
24 | * width is defined by weight attribute, set height to same to make square images
25 | * @param widthSpec
26 | * @param heightSpec
27 | */
28 | @Override
29 | public void onMeasure(int widthSpec, int heightSpec) {
30 | super.onMeasure(widthSpec, heightSpec);
31 | setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
32 | }
33 |
34 | @Override
35 | protected void onFinishInflate() {
36 | super.onFinishInflate();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rx/demo/ui/view/presenter/IViewPresenter.java:
--------------------------------------------------------------------------------
1 | package com.rx.demo.ui.view.presenter;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by Nakhimovich on 4/10/15.
7 | */
8 | public interface IViewPresenter {
9 | void takeView(View view);
10 |
11 | void dropView();
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rx/demo/ui/view/presenter/ImageSearchPresenter.java:
--------------------------------------------------------------------------------
1 | package com.rx.demo.ui.view.presenter;
2 |
3 | import android.graphics.Rect;
4 | import android.util.Log;
5 | import android.view.View;
6 |
7 | import com.rx.demo.dao.ImageDao;
8 | import com.rx.demo.di.annotation.HistoryViewBus;
9 | import com.rx.demo.di.annotation.ImageViewBus;
10 | import com.rx.demo.model.ImageRequest;
11 | import com.rx.demo.model.Result;
12 | import com.rx.demo.ui.view.SearchView;
13 | import com.rx.demo.util.SubscriptionManager;
14 |
15 | import java.util.Queue;
16 | import java.util.concurrent.TimeUnit;
17 |
18 | import javax.inject.Inject;
19 | import javax.inject.Singleton;
20 |
21 | import rx.Observable;
22 | import rx.android.schedulers.AndroidSchedulers;
23 | import rx.android.widget.WidgetObservable;
24 | import rx.schedulers.Schedulers;
25 | import rx.subjects.PublishSubject;
26 |
27 | @Singleton
28 | public class ImageSearchPresenter implements IViewPresenter {
29 | @Inject
30 | Queue que;
31 |
32 | @Inject
33 | ImageDao dao;
34 |
35 | @Inject
36 | SubscriptionManager subs;
37 |
38 | @Inject
39 | @ImageViewBus
40 | PublishSubject