123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package com.sunzee.mvp.home;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import com.orhanobut.hawk.Hawk;
- import com.sunzee.R;
- import com.sunzee.base.BaseApplication;
- import com.sunzee.base.BasePresenter;
- import com.sunzee.model.Heartbeat;
- import com.sunzee.model.HttpResult;
- import com.sunzee.model.domain.ApkInfoBean;
- import com.sunzee.model.domain.Name;
- import com.sunzee.retrofit.ApiCallback;
- import com.sunzee.service.DownServer;
- import com.sunzee.utils.FileUtil;
- import com.sunzee.utils.SharedPreferencesUtils;
- import com.sunzee.utils.ToastUtil;
- import com.sunzee.utils.UiUtil;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 后台首页 presenter
- */
- public class HomePresenter extends BasePresenter<HomeView> {
- private static final String TAG = "HomePresenter";
- private Intent downloadApkIntent;
- public Intent getDownloadApkIntent() {
- return downloadApkIntent;
- }
- public HomePresenter(HomeView view) {
- attachView(view);
- }
- public void connectionSystem(String text) {
- SharedPreferencesUtils.setParam(Name.CONNECT_STATE, 1);
- mvpView.showLoading();
- if (Heartbeat.clientId == null || Heartbeat.clientId.equals("")) {
- ToastUtil.showToast(UiUtil.getStringRes(R.string.idsb));
- return;
- }
- SharedPreferencesUtils.setParam(Name.CONNECT_STATE, 2);
- Heartbeat.deviceId = FileUtil.getDeviceId();
- SharedPreferencesUtils.setParam(Name.CONNECT_STATE, -1);
- Map<String, String> params = new HashMap<>();
- params.put("clientId", Heartbeat.deviceId);
- params.put("managerId", text);
- params.put("gtClientId", Heartbeat.clientId);
- Heartbeat.managerId = text;
- addSubscription(apiStores.init(params), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- Log.d(TAG, "onSuccess: " + model);
- if (model.getData()==null) {
- mvpView.initSuccess(model.getErrorMsg());
- }else if (model.getErrorMsg()==null){
- mvpView.initSuccess(model.getData());
- }
- }
- @Override
- public void onFailure(String msg) {
- Log.d(TAG, "onFailure: " + msg);
- }
- @Override
- public void onFinish() {
- mvpView.hideLoading();
- }
- });
- }
- /**
- * 获取下载连接
- */
- public void updataapk() {
- Log.d(TAG, "updataapk: ");
- Map<String, String> map = new HashMap<>();
- map.put("model", "A");
- addSubscription(apiStores.updataAPK(map), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- Log.d(TAG, "onSuccess: "+model);
- if (model.getData()!=null) {
- Gson gson=new Gson();
- ApkInfoBean apkInfoBean = gson.fromJson(model.getData(), ApkInfoBean.class);
- mvpView.updataSuccess(apkInfoBean);
- }else if(model.getErrorMsg()!=null){
- mvpView.updataFailure(model.getErrorMsg());
- }
- }
- @Override
- public void onFailure(String msg) {
- Log.d(TAG, "onFailure: "+msg);
- }
- @Override
- public void onFinish() {
- }
- });
- }
- /**
- * 下载apk
- *
- * @param url 地址
- * @param version 版本号(用于起名)
- */
- public void downloadApk(String url, int version) {
- //开始下载
- downloadApkIntent = new Intent(BaseApplication.getContext(), DownServer.class);
- Bundle bundle = new Bundle();
- bundle.putString("downloadApkUrl", url);
- bundle.putString("title", "mht" + ".apk");
- bundle.putString("downloadtype", "apk");
- downloadApkIntent.putExtra("download", bundle);
- BaseApplication.getContext().startService(downloadApkIntent);
- }
- public void isCleaned() {
- addSubscription(apiStores.getBackgroundTime(), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- try {
- String timedata = model.getData();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timedata));
- //第一次就会出现报错。
- Long overdueDate = (Long) Hawk.get(Name.OVERDUE_DATE);//购买的到期的时间
- Long timeInMillis = calendar.getTimeInMillis();//当前时间
- String data = (String) Hawk.get(Name.REMAINING);
- long remaining = Long.valueOf(data);
- Log.d(TAG, "onSuccess: 时间过期remaining" + remaining);
- //如果时间没过期,那么可以继续制作
- if (overdueDate > timeInMillis) {
- Log.d(TAG, "onSuccess: 没有过期");
- ToastUtil.showToast("按天数清洗");
- //发送开始清洗的订单。
- getCleanReport(0);
- //如果还有次那么就开始购买。
- } else if (remaining > 0) {
- Log.d(TAG, "onSuccess: 时间过期,按次数计算");
- ToastUtil.showToast("按次数清洗");
- Hawk.put(Name.REMAINING, String.valueOf(remaining - 1));
- //发送开始清洗的订单。
- getCleanReport(0);
- } else {
- Log.d(TAG, "onSuccess: 全部过期,全部不能清洗");
- ToastUtil.showToast("已过期,请充值后使用哦。");
- }
- } catch (Exception e) {
- }
- }
- @Override
- public void onFailure(String msg) {
- }
- @Override
- public void onFinish() {
- }
- });
- }
- private void getCleanReport(int type) {
- Map<String, String> params = new HashMap<>();
- params.put("clientId", Heartbeat.deviceId);
- params.put("type", type + "");
- params.put("water", 1 + "");
- params.put("electricity", 1 + "");
- addSubscription(apiStores.getCleanReport(params), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- if ("success".equals(model.getData())) {
- mvpView.startClean();
- } else {
- mvpView.haveExpired();
- }
- }
- @Override
- public void onFailure(String msg) {
- }
- @Override
- public void onFinish() {
- }
- });
- }
- }
|