-
Notifications
You must be signed in to change notification settings - Fork 0
/
CityListPresenter.java
86 lines (69 loc) · 2.91 KB
/
CityListPresenter.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
package weather.vvolkov.view.list;
import android.support.annotation.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import weather.vvolkov.domain.city.ICityInteractor;
import weather.vvolkov.domain.location.ILocationInteractor;
import weather.vvolkov.domain.weather.IWeatherInteractor;
import weather.vvolkov.utils.Log;
import weather.vvolkov.utils.rx2.IRx2Scheduler;
class CityListPresenter {
private static final String TAG = CityListPresenter.class.getSimpleName();
@NonNull
private final ICityListView weatherView;
@NonNull
private final IWeatherInteractor weatherInteractor;
@NonNull
private final ILocationInteractor locationInteractor;
@NonNull
private final ICityInteractor cityInteractor;
@NonNull
private final IRx2Scheduler rx2Scheduler;
@NonNull
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
public CityListPresenter(@NonNull ICityListView weatherView,
@NonNull IWeatherInteractor weatherInteractor,
@NonNull ILocationInteractor locationInteractor,
@NonNull ICityInteractor cityInteractor,
@NonNull IRx2Scheduler rx2Scheduler) {
this.weatherView = weatherView;
this.weatherInteractor = weatherInteractor;
this.locationInteractor = locationInteractor;
this.cityInteractor = cityInteractor;
this.rx2Scheduler = rx2Scheduler;
}
void onViewAttached() {
weatherView.showSearchMenu();
}
void onViewDetached() {
compositeDisposable.clear();
}
void onFabClicked() {
final Disposable disposable = locationInteractor.getCurrentLocation()
.flatMapSingle(weatherInteractor::getWeatherInfo)
.subscribeOn(rx2Scheduler.getIOScheduler())
.observeOn(rx2Scheduler.getMainThreadScheduler())
.subscribe(weatherView::showWeatherInfoScreen,
throwable -> {
weatherView.showToast(throwable.getMessage());
Log.e(throwable);
});
compositeDisposable.add(disposable);
}
void onSearchMenuItemClicked() {
weatherView.showSearchMenu();
}
void onCancelSearchClicked() {
weatherView.clearList();
weatherView.hideSearchMenu();
}
void onSearchTextChanged(@NonNull String text) {
weatherView.clearList();
final Disposable disposable = cityInteractor.searchCityNames(text)
.subscribeOn(rx2Scheduler.getIOScheduler())
.observeOn(rx2Scheduler.getMainThreadScheduler())
.subscribe(weatherView::addListItem,
throwable -> Log.e(TAG, throwable.getMessage(), throwable));
compositeDisposable.add(disposable);
}
}