package cn.flightfeather.thirdapp.module.home; import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.constraint.ConstraintLayout; import android.support.constraint.Group; import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.services.weather.LocalWeatherForecastResult; import com.amap.api.services.weather.LocalWeatherLive; import com.amap.api.services.weather.LocalWeatherLiveResult; import com.amap.api.services.weather.WeatherSearch; import com.amap.api.services.weather.WeatherSearchQuery; import com.ping.greendao.gen.ScenseDao; import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.Unbinder; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.bean.entity.Scense; import cn.flightfeather.thirdapp.bean.entity.Subtask; import cn.flightfeather.thirdapp.bean.vo.InspectionInfoVo; import cn.flightfeather.thirdapp.bean.vo.RankVo; import cn.flightfeather.thirdapp.bean.vo.TaskVo; import cn.flightfeather.thirdapp.common.net.ResultCallBack; import cn.flightfeather.thirdapp.dataanalysis.AnalysisOverViewFragment; import cn.flightfeather.thirdapp.httpservice.InspectionService; import cn.flightfeather.thirdapp.httpservice.TaskService; import cn.flightfeather.thirdapp.model.bean.NightWorkFileVo; import cn.flightfeather.thirdapp.module.nightwork.NightWorkRecordActivity; import cn.flightfeather.thirdapp.module.nightwork.NightWorkViewModel; import cn.flightfeather.thirdapp.util.Constant; import cn.flightfeather.thirdapp.util.DateFormatter; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; /** * A simple {@link Fragment} subclass. */ public class HomeFragment_scene extends Fragment implements WeatherSearch.OnWeatherSearchListener, View.OnClickListener { private TextView tv_location; private ImageView iv_weather_icon; private TextView tv_temperature; private TextView tv_weather; private LinearLayout ll_weatherBG; private TextView tv_timeCurrent; private View sv_home_page; private boolean hidden = false; private CommonApplication application; //定位相关 private AMapLocationClient mLocationClient = null; //查询天气相关 private boolean searchWeather = true; private WeatherSearchQuery query; private WeatherSearch mweathersearch; private String temperature, weather; // private ViewHolder1 viewHolder1; private ViewHolder2 viewHolder2; private ViewHolder3 viewHolder3; private ViewHolder4 viewHolder4; private ViewHolder5 viewHolder5; //ViewHolder1(今日任务) private ArrayList subtaskListOfUser = new ArrayList<>(); //是否在切换回来时刷新 private boolean isUnHiddenRefresh; private Unbinder unbinder; private NightWorkViewModel viewModel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { viewModel = ViewModelProviders.of(this).get(NightWorkViewModel.class); // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_home_pollution_scene, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); searchWeather = true; initData(); initUI(view); initCurLocation(); initCurrentTime(); initScrollView(savedInstanceState); } @Override public void onDestroy() { if (unbinder != null) { unbinder.unbind(); } super.onDestroy(); mLocationClient.onDestroy(); } @Override public void onPause() { super.onPause(); mLocationClient.stopLocation(); searchWeather = true; } @Override public void onResume() { super.onResume(); if (!hidden){ mLocationClient.startLocation(); initCurrentTime(); } // refreshView(); } @Override public void onStart() { super.onStart(); } public HomeFragment_scene() { // Required empty public constructor } private void initData(){ application = (CommonApplication) getActivity().getApplication(); } private void initUI(View view) { tv_location = (TextView) view.findViewById(R.id.tv_location); tv_location.setOnClickListener(this); iv_weather_icon = (ImageView) view.findViewById(R.id.iv_weather_icon); // iv_weather_icon.setOnClickListener(this); // tv_temperature = (TextView) view.findViewById(R.id.tv_temperature); tv_weather = (TextView) view.findViewById(R.id.tv_weather); ll_weatherBG = (LinearLayout) view.findViewById(R.id.ll_weather_bg); tv_timeCurrent = (TextView) view.findViewById(R.id.tv_now_date_time); sv_home_page = view.findViewById(R.id.sv_home_page); } //初始化时间textView private void initCurrentTime() { Calendar calendar = Calendar.getInstance(); tv_timeCurrent.setText(DateFormatter.dateFormatWeek.format(calendar.getTime())); } private void initScrollView(Bundle savedInstanceState){ // sv_home_page.setLayoutManager(new LinearLayoutManager(getContext())); // parentAdapter = new RecyclerViewAdapter(getContext(),savedInstanceState); // sv_home_page.setAdapter(parentAdapter); viewHolder1 = new ViewHolder1(savedInstanceState, sv_home_page); viewHolder2 = new ViewHolder2(savedInstanceState, sv_home_page); viewHolder3 = new ViewHolder3(savedInstanceState, sv_home_page); viewHolder4 = new ViewHolder4(savedInstanceState, sv_home_page); viewHolder5 = new ViewHolder5(); viewHolder1.initViewHolder1(); viewHolder1.refreshView(); viewHolder2.refreshView(); // viewHolder3.initViewHolder3(); viewHolder5.init(); } private void initCurLocation() { final String curLocation = null; //声明定位回调监听器 AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容 //城市地址 String address = aMapLocation.getAddress(); tv_location.setText(address); searchWeather(aMapLocation.getCity()); } else { // Toast.makeText(getActivity(), "定位失败,请检查网络", Toast.LENGTH_SHORT).show(); } } } }; //初始化定位 mLocationClient = new AMapLocationClient(getActivity().getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); //声明AMapLocationClientOption对象 AMapLocationClientOption mLocationOption = null; //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 mLocationOption.setInterval(10000); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否强制刷新WIFI,默认为true,强制刷新。 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为true,允许模拟位置 mLocationOption.setMockEnable(true); //关闭缓存机制 mLocationOption.setLocationCacheEnable(false); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); } //根据城市搜索天气 private void searchWeather(String city) { query = new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE); mweathersearch = new WeatherSearch(getContext()); mweathersearch.setOnWeatherSearchListener(this); mweathersearch.setQuery(query); mweathersearch.searchWeatherAsyn(); //异步搜索 } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); this.hidden = hidden; if (hidden) { mLocationClient.stopLocation(); searchWeather = true; } else { mLocationClient.startLocation(); initCurrentTime(); // viewHolder2.clearView(); // refreshView(); if (isUnHiddenRefresh) { refreshView(); isUnHiddenRefresh = !isUnHiddenRefresh; } } } //设置是否在切换回来时刷新 public void setUnHiddenRefresh(boolean unHiddenRefresh) { isUnHiddenRefresh = unHiddenRefresh; } @Override public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) { if (searchWeather) { if (i == 1000) { if (localWeatherLiveResult != null || localWeatherLiveResult.getLiveResult() != null) { LocalWeatherLive weatherlive = localWeatherLiveResult.getLiveResult(); temperature = weatherlive.getTemperature() + "°"; weather = weatherlive.getWeather().trim(); tv_weather.setText(weather); tv_temperature.setText(temperature); if (weather != null && !weather.trim().equals("")) { if (weather.equals("多云")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_1); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_cloudy); } else if (weather.equals("晴")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_0); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_sunny); } else if (weather.equals("阴")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_2); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_cloudy); } else if (weather.equals("小雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_3); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("中雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_4); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("大雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_5); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if(weather.equals("阵雨")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_8); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("雨夹雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_15); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("小雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_9); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("中雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_10); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("大雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_11); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } } searchWeather = false; } } } } @Override public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) { } @Override public void onClick(View view) { switch (view.getId()){ case R.id.iv_weather_icon: Toast.makeText(getContext(),"打开天气详情", Toast.LENGTH_SHORT).show(); break; } } public enum ITEM_TYPE { ITEM1, ITEM2, ITEM3, ITEM4, ITEM5 } public void refreshView(){ viewHolder1.initViewHolder1(); viewHolder1.refreshView(); viewHolder2.refreshView(); } /** * 返回当前用户执行的子任务列表 * @param msubtaskList * @return */ public ArrayList getmSubtaskListOfUser(List msubtaskList){ if (application.getCurrentUser().getUsertypeid() == 2) {//主管部门的任务展示按照区县划分 subtaskListOfUser.clear(); for (Subtask subtask : msubtaskList) { if (subtask.getDistrictcode().equals(application.getCurrentUser().getDguid()))//区县行政编码判定,只显示对应区县的任务 subtaskListOfUser.add(subtask); } } return subtaskListOfUser; } // @BindView(R.id.text_rank) TextView text_rank; @BindView(R.id.image_rank) ImageView image_rank; @BindView(R.id.text_rank_detail_1) TextView text_rank_detail_1; @BindView(R.id.text_rank_detail_2) TextView text_rank_detail_2; @BindView(R.id.text_rank_detail_3) TextView text_rank_detail_3; private List taskVoList = new ArrayList<>();//用户场景对应的区县所有的顶层任务 private Scense curScene = new Scense();//当前用户对应的场景信息 private int lastMonthRank;//上月排名 private int monthBeforeLastMonthRank;//上上月排名 public class ViewHolder1 { ViewHolder1(Bundle savedInstanceState, View itemView) { } void initViewHolder1(){ String s1 = lastMonthRank == 0 || lastMonthRank > 99 ? "您上月无排名" : "您上月排名第" + lastMonthRank + "名"; String s2 = "无排名对比"; text_rank_detail_3.setText(R.string.rank_tip_4); // if (lastMonthRank == 0 || lastMonthRank > 99) { text_rank.setText("--"); }else if (lastMonthRank > 0 && lastMonthRank < 10){ String tmp = "0" + String.valueOf(lastMonthRank); text_rank.setText(tmp); }else { text_rank.setText(String.valueOf(lastMonthRank)); } // if (lastMonthRank == 0 || monthBeforeLastMonthRank == 0) { image_rank.setImageResource(R.drawable.icon_rank_no_change_2); } else { if (lastMonthRank == monthBeforeLastMonthRank) { image_rank.setImageResource(R.drawable.icon_rank_no_change_2); s2 = "较上上月排名无变化"; text_rank_detail_3.setText(R.string.rank_tip_3); } else if (lastMonthRank - monthBeforeLastMonthRank > 0) { image_rank.setImageResource(R.drawable.icon_rank_down_2); s2 = "较上上月排名下降" + (lastMonthRank - monthBeforeLastMonthRank) + "名"; text_rank_detail_3.setText(R.string.rank_tip_2); } else { image_rank.setImageResource(R.drawable.icon_rank_up_2); s2 = "较上上月排名上升" + (monthBeforeLastMonthRank - lastMonthRank) + "名"; text_rank_detail_3.setText(R.string.rank_tip_1); } } text_rank_detail_1.setText(s1); text_rank_detail_2.setText(s2); } void refreshView() { getTaskList(); } void getTaskList() { ScenseDao scenseDao = application.getDaoSession().getScenseDao(); String sceneId = application.getCurrentUser().getDguid(); List scenses = scenseDao.queryBuilder() .where(ScenseDao.Properties.Guid.eq( sceneId == null ? "" : sceneId) ).list(); if (!scenses.isEmpty()) { curScene = scenses.get(0); } //顶层任务 Call> getTopClassTaskList = application.getRetrofit().create(TaskService.class).getTopClassTaskList(0); final Scense finalScense = curScene; getTopClassTaskList.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body() != null) { List allTaskVoList = response.body();//返回结果默认按时间降序排序 for (TaskVo t : allTaskVoList) { if (t.getDistrictcode().equals(finalScense.getDistrictcode())) {//主管部门的部门id就是区县id taskVoList.add(t); } } Date now = new Date(); if (taskVoList.size() >= 2 && (taskVoList.get(0).getStarttime().before(now))) { getRank(taskVoList.get(1)); viewHolder3.refreshView(); } } } @Override public void onFailure(Call> call, Throwable t) { } }); } private boolean isDone = false;//需要分别查询上月与上上月客户场景的排名,以便做比较 void getRank(TaskVo taskVo) { final Call getRank = application.getRetrofit().create(TaskService.class).getRank(taskVo.getTguid(), String.valueOf(curScene.getTypeid()), curScene.getGuid()); getRank.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { //上月排名 if (!isDone) { RankVo rankVo = response.body(); List sceneRanks = rankVo.getSceneRanks(); AnalysisOverViewFragment.rankS(sceneRanks); for (int i = 0; i < sceneRanks.size(); i++) { if (curScene.getGuid().equals(sceneRanks.get(i).getSceneNameId()) && sceneRanks.get(i).getScore() > -1) { lastMonthRank =sceneRanks.get(i).getRankNo();//上月排名 initViewHolder1(); break; } } if (taskVoList.size() >= 3) { isDone = true; getRank(taskVoList.get(2)); } } //上上月排名 else { RankVo rankVo = response.body(); List sceneRanks = rankVo.getSceneRanks(); AnalysisOverViewFragment.rankS(sceneRanks); for (int i = 0; i < sceneRanks.size(); i++) { if (curScene.getGuid().equals(sceneRanks.get(i).getSceneNameId()) && sceneRanks.get(i).getScore() > -1) { monthBeforeLastMonthRank = sceneRanks.get(i).getRankNo();//上上月排名 initViewHolder1(); break; } } isDone = false; } } } @Override public void onFailure(Call call, Throwable t) { } }); } } // // @BindView(R.id.rv_announcement) RecyclerView rv_announcement; @BindView(R.id.text_no_announcement) TextView text_no_announcement; public class ViewHolder2 { ViewHolder2(Bundle savedInstanceState, View itemView) { } void refreshView(){ initViewholder2(); } private void initViewholder2(){ } } // // @BindView(R.id.text_no_supervise_tips) TextView text_no_supervise_tips;//等待监管时的建议 @BindView(R.id.text_inspection_time) TextView text_inspection_time;//监管时间 @BindView(R.id.text_inspection_times) TextView text_inspection_times;//监管次数 @BindView(R.id.group_1) Group group_1;//text_inspection_time and text_inspection_times @BindView(R.id.cl_1) ConstraintLayout cl_1;//text_inspection_time and text_inspection_times @BindView(R.id.text_change_promise) TextView text_change_promise;//是否承诺 @BindView(R.id.text_change_status) TextView text_change_status;//是否整改 @BindView(R.id.cl_2) ConstraintLayout cl_2;//text_change_promise and text_change_status @BindView(R.id.image_inspection_status) ImageView image_inspection_status;//监管状态 public class ViewHolder3 { ViewHolder3(Bundle savedInstanceState, View itemView) { } void refreshView() { initViewHolder3(); } /** * 在{@link ViewHolder1#getRank(TaskVo)}第一次被调用后(确保当前场景在当前时间有对应的顶层任务),才会调用此函数 */ private void initViewHolder3(){ String sceneId = application.getCurrentUser().getDguid(); String topTaskId = taskVoList.get(0).getTguid(); Call getInspectionInfoByScene = application.getRetrofit().create(InspectionService.class).getInspectionInfoByScene(sceneId, topTaskId); getInspectionInfoByScene.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { initView(response.body()); viewHolder4.refreshView(response.body()); } } @Override public void onFailure(Call call, Throwable t) { } }); } void initView(InspectionInfoVo infoVo) { //已巡查 if (infoVo.isInspected()) { text_no_supervise_tips.setVisibility(View.INVISIBLE); cl_1.setVisibility(View.VISIBLE); cl_2.setVisibility(View.VISIBLE); image_inspection_status.setSelected(true); text_inspection_time.setText(infoVo.getInspectionTime()); text_inspection_times.setText( String.valueOf( infoVo.getUnChangedCount() + infoVo.getChangedCount() ) ); text_change_promise.setSelected(infoVo.isPromised()); text_change_promise.setText(infoVo.isPromised() ? R.string.promised : R.string.refused); text_change_status.setSelected(infoVo.isChanged()); text_change_status.setText(infoVo.isChanged() ? R.string.changed : R.string.unchanged); } //未巡查 else { text_no_supervise_tips.setVisibility(View.VISIBLE); cl_1.setVisibility(View.GONE); cl_2.setVisibility(View.INVISIBLE); image_inspection_status.setSelected(false); } } } // // @BindView(R.id.text_change_count) TextView text_change_count; @BindView(R.id.text_changed_count) TextView text_changed_count; @BindView(R.id.text_change_info) TextView text_change_info; @BindView(R.id.text_change_left_day) TextView text_change_left_day; @BindView(R.id.textView4) TextView textView4; @BindView(R.id.group_deadline) Group group_deadline; @BindView(R.id.image_done) ImageView image_done; public class ViewHolder4 { ViewHolder4(Bundle savedInstanceState, View itemView) { } void refreshView(InspectionInfoVo infoVo) { initViewHolder4(infoVo); } /** * 在{@link ViewHolder3#initViewHolder3()}函数第一次被调用后,才能调用此函数 */ void initViewHolder4(InspectionInfoVo infoVo) { text_change_count.setText(String.valueOf(infoVo.getUnChangedCount())); text_changed_count.setText(String.valueOf(infoVo.getChangedCount())); String promisedTime = infoVo.getPromisedTime(); try { //未整改完成 if (infoVo.getUnChangedCount() > 0) { Date promisedTime_d = null; if (promisedTime == null || promisedTime.equalsIgnoreCase("null")) { promisedTime_d = DateFormatter.dateFormat2.parse(infoVo.getInspectionTime()); } else { promisedTime_d = DateFormatter.dateFormat.parse(promisedTime); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date today = calendar.getTime(); int leftDays = (int) (promisedTime_d.getTime() - today.getTime()) / 1000 / 60 / 60 / 24; if (leftDays >= 0) { text_change_left_day.setText(String.valueOf(leftDays)); } else { text_change_info.setText("您已逾期"); text_change_left_day.setText(String.valueOf(-leftDays)); } group_deadline.setVisibility(View.VISIBLE); image_done.setVisibility(View.GONE); } //整改完成 else { group_deadline.setVisibility(View.GONE); image_done.setVisibility(View.VISIBLE); } } catch (ParseException e) { e.printStackTrace(); } } } // @BindView(R.id.cl_night_work) ConstraintLayout cl_night_work; @BindView(R.id.txt_news) TextView txt_news; public class ViewHolder5 { void init() { cl_night_work.setOnClickListener(v -> startActivity(new Intent(getContext(), NightWorkRecordActivity.class))); viewModel.getNightWorkFile(false, 1, new ResultCallBack>(){ @Override public void onCacheSuccess(@org.jetbrains.annotations.Nullable List result) { } @Override public void onPage(int current, int total) { } @Override public void onSuccess(@org.jetbrains.annotations.Nullable List result) { if (result == null || result.isEmpty()) { txt_news.setText("暂无新许可证"); } else { txt_news.setText("有新许可证下发,请查看"); } } @Override public void onFailure() { txt_news.setText("网络连接失败,请退出重试"); } }); } } // @OnClick({R.id.card_change_problem, R.id.card_changed_problem}) void OnClick() { Intent intent = new Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver"); intent.setAction(Constant.GOTO_INSPECTION_FRAGMENT); getActivity().sendBroadcast(intent); } // }