/
SearchActivity.java
300 lines (253 loc) · 12 KB
/
SearchActivity.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.joker.rxweather.views.activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.joker.rxweather.MyApplication;
import com.joker.rxweather.R;
import com.joker.rxweather.common.Constants;
import com.joker.rxweather.common.Utils;
import com.joker.rxweather.common.event.FinishActEvent;
import com.joker.rxweather.common.rx.rxAndroid.SimpleObserver;
import com.joker.rxweather.common.rx.rxAndroid.schedulers.AndroidSchedulers;
import com.joker.rxweather.common.rx.rxbus.RxBus;
import com.joker.rxweather.model.entities.SearchEntity;
import com.joker.rxweather.presenter.SearchPresenter;
import com.joker.rxweather.presenter.SearchPresenterImp;
import com.joker.rxweather.ui.ProgressLayout;
import com.joker.rxweather.ui.animation.SupportAnimator;
import com.joker.rxweather.ui.animation.ViewAnimationUtils;
import com.joker.rxweather.ui.widget.RevealFrameLayout;
import com.joker.rxweather.views.SearchView;
import com.trello.rxlifecycle.ActivityEvent;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;
/**
* Created by Joker on 2015/11/4.
*/
public class SearchActivity extends BaseActivity implements SearchView<Observable<SearchEntity>> {
private static final String TAG = SearchActivity.class.getSimpleName();
@Nullable
@Bind(R.id.search_layout_root_view)
RelativeLayout rootView;
@Nullable
@Bind(R.id.toolbar_layout_root_view)
Toolbar toolbar;
@Nullable
@Bind(R.id.toolbar_layout_title_tv)
TextView titleTv;
@Nullable
@Bind(R.id.search_layout_reveal_view)
RevealFrameLayout revealFrameLayout;
@Nullable
@Bind(R.id.search_layout_pl)
ProgressLayout progressLayout;
@Nullable
@Bind(R.id.search_layout_ill)
TextInputLayout inputLayout;
@Nullable
@Bind(R.id.search_layout_et)
EditText editText;
@Nullable
@Bind(R.id.search_layout_text_tv)
TextView weatherTv;
@Nullable
@Bind(R.id.search_layout_text_iv)
ImageView weatherIv;
/*要查询的城市名称*/
private String cityName;
private SearchPresenter<SearchView<Observable<SearchEntity>>> searchPresenter;
private Observable<SearchEntity> cacheObservable;
private RxBus rxBus;
private WeakReference<AppCompatActivity> weakReference;
private CompositeSubscription compositeSubscription = new CompositeSubscription();
private View.OnClickListener retryClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
SearchActivity.this.searchPresenter.search(cityName);
}
};
public static void navigateToSearch(AppCompatActivity startingActivity) {
Intent intent = new Intent(startingActivity, SearchActivity.class);
startingActivity.startActivity(intent);
}
@Override
protected int getLayoutId() {
return R.layout.search_layout;
}
@Override
protected void onCreated() {
searchPresenter = new SearchPresenterImp();
searchPresenter.attachView(SearchActivity.this);
}
@Override
protected void initView(Bundle savedInstanceState) {
SearchActivity.this.setSupportActionBar(toolbar);
SearchActivity.this.getSupportActionBar().setTitle("");
this.titleTv.setText(getResources().getString(R.string.bottom_bar_search));
this.inputLayout.setHint(getResources().getString(R.string.hint));
SearchActivity.this.setListener();
if (savedInstanceState == null) {
rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
rootView.getViewTreeObserver().removeOnPreDrawListener(this);
SearchActivity.this.runEnterAnim();
return true;
}
});
} else {
SearchEntity searchEntity = (SearchEntity) savedInstanceState.getSerializable(Constants.CACHE);
SearchActivity.this.cityName = (String) savedInstanceState.getSerializable(Constants.CACHE_CITY);
if (searchEntity != null) {
SearchActivity.this.showResult(Observable.just(searchEntity));
} else if (!TextUtils.isEmpty(cityName)) {
searchPresenter.search(cityName);
}
}
}
private void setListener() {
this.compositeSubscription.add(RxTextView.textChanges(editText)
.skip(1)
.debounce(Constants.MILLISECONDS_600, TimeUnit.MILLISECONDS)
.switchMap(new Func1<CharSequence, Observable<CharSequence>>() {
@Override
public Observable<CharSequence> call(CharSequence charSequence) {
return Observable.just(charSequence);
}
})
.observeOn(AndroidSchedulers.mainThread())
.compose(SearchActivity.this.<CharSequence>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new SimpleObserver<CharSequence>() {
@Override
public void onNext(CharSequence charSequence) {
String temp = charSequence.toString();
if (!TextUtils.isEmpty(temp.trim()) &&
!temp.equals(SearchActivity.this.cityName)) {
SearchActivity.this.cityName =
charSequence.toString().trim();
SearchActivity.this.searchPresenter.search(cityName);
}
}
}));
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if (cacheObservable != null) {
this.compositeSubscription.add(cacheObservable//
.compose(SearchActivity.this.<SearchEntity>bindUntilEvent(
ActivityEvent.DESTROY))
.subscribe(new SimpleObserver<SearchEntity>() {
@Override
public void onNext(SearchEntity searchEntity) {
outState.putSerializable(Constants.CACHE,
searchEntity);
outState.putSerializable(Constants.CACHE_CITY,
SearchActivity.this.cityName);
}
}));
}
}
private void runEnterAnim() {
final Rect bounds = new Rect();
revealFrameLayout.getHitRect(bounds);
SearchActivity.this.revealFrameLayout.setLayerType(View.LAYER_TYPE_HARDWARE, null);
SupportAnimator revealAnimator = ViewAnimationUtils.createCircularReveal(revealFrameLayout.getChildAt(0),
0,
bounds.left,
0,
Utils.pythagorean(bounds.width(),
bounds.height()));
revealAnimator.setDuration(Constants.MILLISECONDS_400);
revealAnimator.setInterpolator(new AccelerateInterpolator());
revealAnimator.addListener(new SupportAnimator.SimpleAnimatorListener() {
@Override
public void onAnimationEnd() {
SearchActivity.this.revealFrameLayout.setLayerType(View.LAYER_TYPE_NONE, null);
}
});
revealAnimator.start();
}
@OnClick(R.id.bottom_bar_forecast_tv)
void onForecastClick() {
SearchActivity.this.finish();
overridePendingTransition(0, 0);
}
@Override
public void showSearchResult(Observable<SearchEntity> searchEntityObservable) {
SearchActivity.this.showResult(searchEntityObservable);
}
private void showResult(Observable<SearchEntity> searchEntityObservable) {
this.cacheObservable = searchEntityObservable;
this.compositeSubscription.add(cacheObservable.compose(SearchActivity.this.<SearchEntity>bindUntilEvent(
ActivityEvent.DESTROY)).subscribe(new SimpleObserver<SearchEntity>() {
@Override
public void onNext(SearchEntity searchEntity) {
SearchActivity.this.weatherTv.setText(
Utils.formatTemp(searchEntity.currentTemp) + " , " + searchEntity.weatherText);
Glide.with(SearchActivity.this)
.load(Constants.ICON_URL + searchEntity.weatherCode + ".png")
.dontAnimate()
.placeholder(R.drawable.holding_icon)
.error(R.drawable.holding_icon)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(SearchActivity.this.weatherIv);
}
}));
}
@Override
public boolean isContent() {
return progressLayout.isContent();
}
@Override
public void showLoading() {
progressLayout.showLoading();
}
@Override
public void showContent() {
if (!progressLayout.isContent()) progressLayout.showContent();
}
@Override
public void showError(int messageId) {
if (!progressLayout.isError()) progressLayout.showError(messageId, retryClickListener);
}
@Override
public Context getContext() {
if (weakReference == null) {
weakReference = new WeakReference<AppCompatActivity>(SearchActivity.this);
}
return weakReference.get();
}
@Override
protected void exit() {
if (rxBus == null) rxBus = MyApplication.get().getRxBus();
rxBus.postEvent(new FinishActEvent());
}
@Override
protected void onDestroy() {
if (this.compositeSubscription.hasSubscriptions()) this.compositeSubscription.clear();
super.onDestroy();
this.searchPresenter.detachView();
}
}