use of com.xinshang.audient.model.entities.Token in project Audient by komamj.
the class LoginPresenter method loadAccessToken.
@Override
public void loadAccessToken(String code) {
mView.setLoadIndicator(true);
Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {
@Override
public void accept(Token token) throws Exception {
mRepository.persistenceAccessToken(token.accessToken);
mRepository.persistenceRefreshToken(token.refreshToken);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {
@Override
public void onNext(Token token) {
}
@Override
public void onError(Throwable t) {
LogUtils.e(TAG, "loadAccessToken error :" + t.toString());
mRepository.persistenceLoginStatus(false);
if (mView.isActive()) {
mView.setLoadIndicator(false);
mView.showLoadingError();
}
}
@Override
public void onComplete() {
mRepository.persistenceLoginStatus(true);
if (mView.isActive()) {
mView.setLoadIndicator(false);
mView.showSuccessfulMessage();
}
}
});
mDisposables.add(disposable);
}
use of com.xinshang.audient.model.entities.Token in project Audient by komamj.
the class SplashPresenter method loadAccessToken.
@Override
public void loadAccessToken(String code) {
Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {
@Override
public void accept(Token token) throws Exception {
mRepository.persistenceAccessToken(token.accessToken);
mRepository.persistenceRefreshToken(token.refreshToken);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {
@Override
public void onNext(Token token) {
if (mView.isActive()) {
mView.showStoresUI(true);
loadStores();
}
}
@Override
public void onError(Throwable t) {
LogUtils.e(TAG, "loadAccessToken error :" + t.toString());
mRepository.persistenceLoginStatus(false);
if (mView.isActive()) {
mView.showLoadingError();
}
}
@Override
public void onComplete() {
if (mView.isActive()) {
mView.showSuccessfulMessage();
}
}
});
mDisposables.add(disposable);
}
use of com.xinshang.audient.model.entities.Token in project Audient by komamj.
the class WXEntryPresenter method loadAccessToken.
@Override
public void loadAccessToken(String code) {
mView.setLoadIndicator(true);
Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {
@Override
public void accept(Token token) throws Exception {
mRepository.persistenceAccessToken(token.accessToken);
mRepository.persistenceRefreshToken(token.refreshToken);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {
@Override
public void onNext(Token token) {
}
@Override
public void onError(Throwable t) {
mRepository.persistenceLoginStatus(false);
if (mView.isActive()) {
mView.setLoadIndicator(false);
mView.showLoadingError();
}
}
@Override
public void onComplete() {
mRepository.persistenceLoginStatus(true);
if (mView.isActive()) {
mView.setLoadIndicator(false);
mView.showSuccessfulMessage();
}
}
});
mDisposables.add(disposable);
}
use of com.xinshang.audient.model.entities.Token in project Audient by komamj.
the class AudientRepositoryModule method provideOkHttpClient.
@Singleton
@Provides
OkHttpClient provideOkHttpClient(Cache cache, final SharedPreferences sharedPreferences, final Gson gson) {
final HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
}
return new OkHttpClient.Builder().authenticator(new Authenticator() {
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
String refershToken = sharedPreferences.getString(Constants.REFRESH_TOKEN, "");
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.AUDIENT_HOST).client(new OkHttpClient.Builder().addInterceptor(logInterceptor).build()).addConverterFactory(GsonConverterFactory.create(gson)).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
AudientApi audientApi = retrofit.create(AudientApi.class);
audientApi.refreshAccessToken(GRANT_TYPE, refershToken, Constants.CLIENT_ID, Constants.CLIENT_SECRET).subscribeWith(new DisposableSubscriber<Token>() {
@Override
public void onNext(Token token) {
sharedPreferences.edit().putString(Constants.ACCESS_TOKEN, token.accessToken).commit();
sharedPreferences.edit().putString(Constants.REFRESH_TOKEN, token.refreshToken).commit();
}
@Override
public void onError(Throwable t) {
LogUtils.e(TAG, "refresh token error :" + t.getMessage());
sharedPreferences.edit().putBoolean(Constants.LOGIN_STATUS, false).commit();
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onComplete() {
}
});
return response.request().newBuilder().header("Authorization", "Bearer " + sharedPreferences.getString(Constants.ACCESS_TOKEN, "")).build();
}
}).addInterceptor(new TokenInterceptor(sharedPreferences)).addInterceptor(logInterceptor).cache(cache).connectTimeout(15, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).retryOnConnectionFailure(true).build();
}
Aggregations