Skip to content

Commit

Permalink
add request entiies
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartDengg committed Nov 17, 2015
1 parent 76441f0 commit dc6de20
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.rxweather.domain.usercase;

import com.joker.rxweather.model.entities.AddressEntity;
import com.joker.rxweather.model.entities.MainEntity;
import com.joker.rxweather.model.entities.RequestCitiesEntity;
import com.joker.rxweather.model.request.ListRequest;
import com.joker.rxweather.model.service.ServiceRest;
import java.util.List;
import rx.Observable;

/**
* Created by Joker on 2015/10/31.
*/
public class ListUseCase extends UseCase<List<MainEntity>> {
public class ListUseCase extends UseCase<List<MainEntity>, ListRequest> {

private ServiceRest serviceRest;
private AddressEntity addressEntity;
private List<RequestCitiesEntity.RequestCity> requestCities;

public ListUseCase(AddressEntity addressEntity,
List<RequestCitiesEntity.RequestCity> requestCities) {
this.addressEntity = addressEntity;
this.requestCities = requestCities;
this.serviceRest = ServiceRest.getInstance();
public ListUseCase() {
}

@Override protected Observable<List<MainEntity>> interactor() {
@Override protected Observable<List<MainEntity>> interactor(ListRequest listRequest) {

return serviceRest.getWeatherByCityOrCityId(addressEntity, requestCities);
return ServiceRest.getInstance()
.getWeatherByCityOrCityId(listRequest.addressEntity, listRequest.requestCityList);
}
}
21 changes: 10 additions & 11 deletions domain/src/main/java/com/rxweather/domain/usercase/PrepareCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.joker.rxweather.common.rx.rxAndroid.SchedulersCompat;
import com.joker.rxweather.model.entities.AddressEntity;
import com.joker.rxweather.model.entities.RequestCitiesEntity;
import com.joker.rxweather.model.request.PrepareRequest;
import com.joker.rxweather.model.service.ServiceRest;
import com.rxweather.domain.LocationListenerAdapter;
import java.io.IOException;
Expand All @@ -29,28 +30,24 @@
/**
* Created by Joker on 2015/10/31.
*/
public class PrepareCase extends UseCase<SparseArray> {
public class PrepareCase extends UseCase<SparseArray, PrepareRequest> {

private static final String TAG = PrepareCase.class.getSimpleName();

private LocationManager locationManager;
private AssetManager assetManager;
private HandlerThread handlerThread;

private RequestCitiesEntity requestCitiesEntity = null;

public PrepareCase(LocationManager locationManager, AssetManager assetManager) {
this.locationManager = locationManager;
this.assetManager = assetManager;
public PrepareCase() {
}

@Override protected Observable<SparseArray> interactor() {
@Override protected Observable<SparseArray> interactor(PrepareRequest prepareRequest) {

this.handlerThread = new HandlerThread("backgroundThread");
this.handlerThread.start();

return Observable.zip(PrepareCase.this.getLocationObservable(),
PrepareCase.this.getRequestCitiesObservable(),
return Observable.zip(PrepareCase.this.getLocationObservable(prepareRequest.locationManager),
PrepareCase.this.getRequestCitiesObservable(prepareRequest.assetManager),
new Func2<AddressEntity, List<RequestCitiesEntity.RequestCity>, SparseArray>() {
@Override public SparseArray call(AddressEntity locationEntity,
List<RequestCitiesEntity.RequestCity> requestCities) {
Expand All @@ -64,7 +61,8 @@ public PrepareCase(LocationManager locationManager, AssetManager assetManager) {
});
}

@NonNull private Observable<AddressEntity> getLocationObservable() {
@NonNull
private Observable<AddressEntity> getLocationObservable(final LocationManager locationManager) {

return Observable.create(new Observable.OnSubscribe<Location>() {

Expand Down Expand Up @@ -104,7 +102,8 @@ public void onLocationChanged(final Location location) {
});
}

private Observable<List<RequestCitiesEntity.RequestCity>> getRequestCitiesObservable() {
private Observable<List<RequestCitiesEntity.RequestCity>> getRequestCitiesObservable(
final AssetManager assetManager) {

return Observable.create(new Observable.OnSubscribe<List<RequestCitiesEntity.RequestCity>>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
/**
* Created by Joker on 2015/10/31.
*/
public class SearchUseCase extends UseCase<SearchEntity> {
public class SearchUseCase extends UseCase<SearchEntity, AddressEntity> {

private static final String TAG = SearchUseCase.class.getSimpleName();

private AddressEntity addressEntity;

public SearchUseCase(AddressEntity addressEntity) {
this.addressEntity = addressEntity;
public SearchUseCase() {
}

@Override protected Observable<SearchEntity> interactor() {
@Override protected Observable<SearchEntity> interactor(AddressEntity addressEntity) {

return ServiceRest.getInstance().searchWeatherByCityName(addressEntity.city);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
/**
* Created by Joker on 2015/10/31.
*/
public abstract class UseCase<T> {
public abstract class UseCase<T, R> {

private Subscription subscription = Subscriptions.empty();

@SuppressWarnings("unchecked") public void subscribe(Observer<T> UseCaseSubscriber) {
@SuppressWarnings("unchecked") public void subscribe(Observer<T> UseCaseSubscriber, R params) {

UseCase.this.subscription = this.interactor()//
UseCase.this.subscription = this.interactor(params)//
.onBackpressureBuffer()//
.take(1)//
.filter(new Func1<T, Boolean>() {
Expand All @@ -32,5 +32,5 @@ public void unsubscribe() {
}
}

@CheckResult protected abstract Observable<T> interactor();
@CheckResult protected abstract Observable<T> interactor(R params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class AddressEntity {
public String city;
public String district;

public AddressEntity setCity(String city) {
this.city = city;
return AddressEntity.this;
}

@Override public String toString() {
return "LocationEntity{" +
"province='" + province + '\'' +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.joker.rxweather.model.request;

import com.joker.rxweather.model.entities.AddressEntity;
import com.joker.rxweather.model.entities.RequestCitiesEntity;
import java.util.List;

/**
* Created by Joker on 2015/11/18.
*/
public class ListRequest {

public AddressEntity addressEntity;
public List<RequestCitiesEntity.RequestCity> requestCityList;

public ListRequest(AddressEntity addressEntity,
List<RequestCitiesEntity.RequestCity> requestCityList) {
this.addressEntity = addressEntity;
this.requestCityList = requestCityList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.joker.rxweather.model.request;

import android.content.res.AssetManager;
import android.location.LocationManager;

/**
* Created by Joker on 2015/11/18.
*/
public class PrepareRequest {

public LocationManager locationManager;
public AssetManager assetManager;

public PrepareRequest(LocationManager locationManager, AssetManager assetManager) {
this.locationManager = locationManager;
this.assetManager = assetManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.joker.rxweather.presenter;

import android.content.Context;
import android.content.res.AssetManager;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.util.SparseArray;
Expand All @@ -11,11 +10,12 @@
import com.joker.rxweather.model.entities.AddressEntity;
import com.joker.rxweather.model.entities.MainEntity;
import com.joker.rxweather.model.entities.RequestCitiesEntity;
import com.joker.rxweather.model.request.ListRequest;
import com.joker.rxweather.model.request.PrepareRequest;
import com.joker.rxweather.views.ListView;
import com.rxweather.domain.usercase.ListUseCase;
import com.rxweather.domain.usercase.PrepareCase;
import com.rxweather.domain.usercase.UseCase;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
Expand All @@ -29,31 +29,28 @@ public class ListPresenterImp implements ListPresenter<ListView<Observable<List<

private ListView<Observable<List<MainEntity>>> listView;

private UseCase<SparseArray> prepareCase;
private UseCase<List<MainEntity>> listUseCase;
private UseCase<SparseArray, PrepareRequest> prepareCase;
private UseCase<List<MainEntity>, ListRequest> listUseCase;

private AddressEntity addressEntity = new AddressEntity();
private List<RequestCitiesEntity.RequestCity> requestCities = new ArrayList<>();
private LocationManager locationManager;
private AssetManager assetManager;
private PrepareRequest prepareRequest;

public ListPresenterImp() {
}

@Override public void attachView(@NonNull ListView<Observable<List<MainEntity>>> view) {
this.listView = view;

this.locationManager =
(LocationManager) this.listView.getContext().getSystemService(Context.LOCATION_SERVICE);
this.assetManager = this.listView.getContext().getAssets();
this.prepareRequest = new PrepareRequest(
(LocationManager) this.listView.getContext().getSystemService(Context.LOCATION_SERVICE),
this.listView.getContext().getAssets());

this.prepareCase = new PrepareCase(locationManager, assetManager);
this.listUseCase = new ListUseCase(addressEntity, requestCities);
this.prepareCase = new PrepareCase();
this.listUseCase = new ListUseCase();
}

@Override public void loadData() {
ListPresenterImp.this.showLoading();
prepareCase.subscribe(new PrepareSubscriber());
this.prepareCase.subscribe(new PrepareSubscriber(), prepareRequest);
}

private void showLoading() {
Expand All @@ -75,18 +72,18 @@ private void showError(int messageId) {
/*准备工作Subscriber,位置,城市列表等*/
@RxLogSubscriber private final class PrepareSubscriber extends Subscriber<SparseArray> {

private ListRequest listRequest;

@Override public void onCompleted() {
ListPresenterImp.this.listUseCase.subscribe(new ListSubscriber());
ListPresenterImp.this.listUseCase.subscribe(new ListSubscriber(), listRequest);
}

@Override public void onError(Throwable e) {
ListPresenterImp.this.showError(ErrorHanding.propagate(e));
}

@Override public void onNext(SparseArray sparseArray) {
addressEntity.city = ((AddressEntity) sparseArray.get(Constants.LOCATION_TAG)).city;
requestCities.clear();
requestCities.addAll(
listRequest = new ListRequest(((AddressEntity) sparseArray.get(Constants.LOCATION_TAG)),
(List<RequestCitiesEntity.RequestCity>) sparseArray.get(Constants.FORECAST_TAG));
}
}
Expand All @@ -110,8 +107,5 @@ private void showError(int messageId) {
@Override public void detachView() {
this.prepareCase.unsubscribe();
this.listUseCase.unsubscribe();

this.locationManager = null;
this.assetManager = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class SearchPresenterImp implements SearchPresenter<SearchView<Observable<SearchEntity>>> {

private SearchView<Observable<SearchEntity>> searchView;
private UseCase<SearchEntity> searchCase;
private UseCase<SearchEntity, AddressEntity> searchCase;

private AddressEntity addressEntity = new AddressEntity();

Expand All @@ -25,15 +25,13 @@ public SearchPresenterImp() {

@Override public void attachView(SearchView<Observable<SearchEntity>> view) {
this.searchView = view;
this.searchCase = new SearchUseCase(addressEntity);
this.searchCase = new SearchUseCase();
}

@Override public void search(String cityName) {

SearchPresenterImp.this.showLoading();

this.addressEntity.city = cityName;
this.searchCase.subscribe(new SearchSubscriber());
this.searchCase.subscribe(new SearchSubscriber(), addressEntity.setCity(cityName));
}

private void showLoading() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.joker.rxweather.views;

/**
* Created by Joker on 2015/11/18.
*/
public interface ContentView {

boolean isContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
/**
* Created by Joker on 2015/10/29.
*/
public interface ListView<T> extends LoadingView {
public interface ListView<T> extends LoadingView, ContentView {

void showForecasts(T t);

boolean isContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private void SubscribeToFinish() {
}

@CallSuper @Override protected void onDestroy() {
if (subscribe != null && !subscribe.isUnsubscribed()) subscribe.unsubscribe();
super.onDestroy();

if (subscribe != null && !subscribe.isUnsubscribed()) subscribe.unsubscribe();
ButterKnife.unbind(BaseActivity.this);
MyApplication.get().getRefWatcher().watch(this);
}
Expand Down

0 comments on commit dc6de20

Please sign in to comment.