PageRenderTime 113ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/gzsll/hupu/ui/splash/SplashPresenter.java

https://gitlab.com/gzsll/TLint
Java | 89 lines | 77 code | 9 blank | 3 comment | 8 complexity | b5ee167bdb0174f66240319745e096a7 MD5 | raw file
  1. package com.gzsll.hupu.ui.splash;
  2. import android.content.Context;
  3. import android.support.annotation.NonNull;
  4. import com.alibaba.fastjson.JSON;
  5. import com.gzsll.hupu.Constants;
  6. import com.gzsll.hupu.bean.UpdateInfo;
  7. import com.gzsll.hupu.components.okhttp.OkHttpHelper;
  8. import com.gzsll.hupu.injector.PerActivity;
  9. import com.gzsll.hupu.util.ChannelUtils;
  10. import com.gzsll.hupu.util.SettingPrefUtils;
  11. import com.umeng.analytics.MobclickAgent;
  12. import java.util.concurrent.TimeUnit;
  13. import javax.inject.Inject;
  14. import rx.Observable;
  15. import rx.Subscription;
  16. import rx.android.schedulers.AndroidSchedulers;
  17. import rx.functions.Action1;
  18. import rx.functions.Func1;
  19. import rx.schedulers.Schedulers;
  20. /**
  21. * Created by sll on 2016/5/31.
  22. */
  23. @PerActivity public class SplashPresenter implements SplashContract.Presenter {
  24. private SplashContract.View mSplashView;
  25. private Subscription mSubscription;
  26. private Context mContext;
  27. private OkHttpHelper mOkHttpHelper;
  28. @Inject public SplashPresenter(Context mContext, OkHttpHelper mOkHttpHelper) {
  29. this.mContext = mContext;
  30. this.mOkHttpHelper = mOkHttpHelper;
  31. }
  32. @Override public void initUmeng() {
  33. MobclickAgent.UMAnalyticsConfig config =
  34. new MobclickAgent.UMAnalyticsConfig(mContext, "55f1993be0f55a0fd9004fbc",
  35. ChannelUtils.getChannel(mContext));
  36. MobclickAgent.startWithConfigure(config);
  37. }
  38. @Override public void initHuPuSign() {
  39. mSubscription = Observable.just(Constants.UPDATE_URL)
  40. .timeout(2, TimeUnit.SECONDS)
  41. .subscribeOn(Schedulers.io())
  42. .map(new Func1<String, UpdateInfo>() {
  43. @Override public UpdateInfo call(String s) {
  44. try {
  45. String result = mOkHttpHelper.getStringFromServer(Constants.UPDATE_URL);
  46. return JSON.parseObject(result, UpdateInfo.class);
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. return null;
  51. }
  52. })
  53. .observeOn(AndroidSchedulers.mainThread())
  54. .subscribe(new Action1<UpdateInfo>() {
  55. @Override public void call(UpdateInfo updateInfo) {
  56. if (updateInfo != null) {
  57. if (updateInfo.extra != null) {
  58. SettingPrefUtils.setNeedExam(mContext, updateInfo.extra.needExam == 1);
  59. }
  60. SettingPrefUtils.setHuPuSign(mContext, updateInfo.hupuSign);
  61. }
  62. mSplashView.showMainUi();
  63. }
  64. }, new Action1<Throwable>() {
  65. @Override public void call(Throwable throwable) {
  66. throwable.printStackTrace();
  67. mSplashView.showMainUi();
  68. }
  69. });
  70. }
  71. @Override public void attachView(@NonNull SplashContract.View view) {
  72. mSplashView = view;
  73. }
  74. @Override public void detachView() {
  75. if (mSubscription != null && !mSubscription.isUnsubscribed()) {
  76. mSubscription.unsubscribe();
  77. }
  78. mSplashView = null;
  79. }
  80. }