| | |
| | | package com.dayu.pipirrapp.fragment; |
| | | |
| | | import android.content.Intent; |
| | | import android.graphics.Typeface; |
| | | import android.os.Bundle; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | import androidx.lifecycle.Observer; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | | |
| | | import com.dayu.pipirrapp.bean.db.LatLonBean; |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.activity.OrderDetailActivity; |
| | | import com.dayu.pipirrapp.adapter.OrderAdapter; |
| | | import com.dayu.pipirrapp.bean.net.OrderListResult; |
| | | import com.dayu.pipirrapp.bean.net.WeatherResponse; |
| | | import com.dayu.pipirrapp.databinding.FragmentMainBinding; |
| | | import com.dayu.pipirrapp.databinding.FragmentOrderBinding; |
| | | import com.dayu.pipirrapp.net.ApiManager; |
| | | import com.dayu.pipirrapp.utils.ImageUtils; |
| | | import com.dayu.pipirrapp.utils.MyJsonParser; |
| | | import com.dayu.pipirrapp.utils.SharedPreferencesHelper; |
| | | import com.dayu.pipirrapp.view.TitleBar; |
| | | import com.dayu.pipirrapp.net.BaseResponse; |
| | | import com.dayu.pipirrapp.net.Constants; |
| | | import com.dayu.pipirrapp.net.subscribers.SubscriberListener; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.jeremyliao.liveeventbus.LiveEventBus; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | import com.scwang.smart.refresh.footer.ClassicsFooter; |
| | | import com.scwang.smart.refresh.header.ClassicsHeader; |
| | | import com.scwang.smart.refresh.layout.api.RefreshLayout; |
| | | import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener; |
| | | import com.scwang.smart.refresh.layout.listener.OnRefreshListener; |
| | | |
| | | import java.util.Calendar; |
| | | |
| | | import retrofit2.Call; |
| | | import retrofit2.Callback; |
| | | import retrofit2.Response; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * author: zuo |
| | |
| | | */ |
| | | public class OrderFragment extends BaseFragment { |
| | | |
| | | FragmentMainBinding binding; |
| | | WeatherResponse weatherResponse; |
| | | //收到定位广播后获取天气信息 |
| | | Observer<LatLonBean> myObserver = new Observer<LatLonBean>() { |
| | | @Override |
| | | public void onChanged(LatLonBean latLonBean) { |
| | | |
| | | String jsonStr = SharedPreferencesHelper.getInstance(OrderFragment.this.getActivity()).get("WeatherResponse", ""); |
| | | weatherResponse = MyJsonParser.getBeanFromJson(jsonStr, WeatherResponse.class); |
| | | if (weatherResponse == null) { |
| | | weatherResponse = new WeatherResponse(); |
| | | } |
| | | // 获取当前时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | // 获取当前年月日 |
| | | int year = calendar.get(Calendar.YEAR); |
| | | int month = calendar.get(Calendar.MONTH) + 1; // 注意月份是从0开始计数的,所以要加1 |
| | | int day = calendar.get(Calendar.DAY_OF_MONTH); |
| | | String date = year + "年" + month + "月" + day + "日"; |
| | | if (weatherResponse == null || !date.equals(weatherResponse.getDate())) { |
| | | weatherResponse.setDate(date); |
| | | ApiManager.getInstance().requestWeather(latLonBean.getLatitude() + ":" + latLonBean.getLongitude(), new Callback<WeatherResponse>() { |
| | | @Override |
| | | public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) { |
| | | private final int STATE_DONE = 2; |
| | | private final int STATE_UNDONE = 1; |
| | | |
| | | if (response.isSuccessful()) { |
| | | weatherResponse.setResults(response.body().getResults()); |
| | | SharedPreferencesHelper.getInstance(OrderFragment.this.getActivity()).put("WeatherResponse", weatherResponse); |
| | | setWeatherData(weatherResponse); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(Call<WeatherResponse> call, Throwable t) { |
| | | |
| | | } |
| | | }); |
| | | } else { |
| | | setWeatherData(weatherResponse); |
| | | } |
| | | LiveEventBus.get("location", LatLonBean.class).removeObserver(myObserver); |
| | | } |
| | | }; |
| | | |
| | | FragmentOrderBinding binding; |
| | | OrderAdapter mAdapter; |
| | | int page = 1; |
| | | int pageSize = 10; |
| | | int state = 1; |
| | | List<OrderListResult.Data> recordsListDone = new ArrayList<>(); |
| | | List<OrderListResult.Data> recordsList = new ArrayList<>(); |
| | | |
| | | /** |
| | | * 设置天气相关信息 |
| | | * |
| | | * @param data |
| | | * @param |
| | | */ |
| | | private void setWeatherData(WeatherResponse data) { |
| | | try { |
| | | if (data.getResults() != null) { |
| | | binding.cityName.setText(data.getResults()[0].getLocation().getName()); |
| | | binding.time.setText(data.getDate()); |
| | | int weatherImg = ImageUtils.getWeatherDrawable(data.getResults()[0].getDaily()[0].getCode_day()); |
| | | if (weatherImg != 0) { |
| | | binding.weatherImg.setImageDrawable(getResources().getDrawable(weatherImg)); |
| | | } |
| | | binding.weatherName.setText(data.getResults()[0].getDaily()[0].getText_day()); |
| | | binding.weatherTemperature.setText(data.getResults()[0].getDaily()[0].getLow() + "°C ~ " + data.getResults()[0].getDaily()[0].getHigh() + "°C"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | // private void setWeatherData(WeatherResponse data) { |
| | | // try { |
| | | // if (data.getResults() != null) { |
| | | // binding.cityName.setText(data.getResults()[0].getLocation().getName()); |
| | | // binding.time.setText(data.getDate()); |
| | | // int weatherImg = ImageUtils.getWeatherDrawable(data.getResults()[0].getDaily()[0].getCode_day()); |
| | | // if (weatherImg != 0) { |
| | | // binding.weatherImg.setImageDrawable(getResources().getDrawable(weatherImg)); |
| | | // } |
| | | // binding.weatherName.setText(data.getResults()[0].getDaily()[0].getText_day()); |
| | | // binding.weatherTemperature.setText(data.getResults()[0].getDaily()[0].getLow() + "°C ~ " + data.getResults()[0].getDaily()[0].getHigh() + "°C"); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // CrashReport.postCatchedException(e); |
| | | // } |
| | | // } |
| | | @Override |
| | | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| | | binding = FragmentMainBinding.inflate(inflater, container, false); |
| | | //获取到经纬度后获取天气信息 |
| | | LiveEventBus |
| | | .get("location", LatLonBean.class) |
| | | .observeForever(myObserver); |
| | | binding = FragmentOrderBinding.inflate(inflater, container, false); |
| | | initView(); |
| | | //获取定位服务传过来的坐标点 |
| | | LiveEventBus.get(CommonKeyName.MQTTData).observeForever(new Observer<Object>() { |
| | | @Override |
| | | public void onChanged(Object o) { |
| | | |
| | | } |
| | | }); |
| | | |
| | | |
| | | return binding.getRoot(); |
| | | } |
| | | |
| | | |
| | | void initView() { |
| | | //未完成 |
| | | binding.manageStateProgress.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | chooseStateView(true); |
| | | state = STATE_UNDONE;//1 未开始 2 已完成 |
| | | |
| | | } |
| | | }); |
| | | //已完成 |
| | | binding.manageStateFinish.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | chooseStateView(false); |
| | | state = STATE_DONE;//1 未开始 2 已完成 |
| | | } |
| | | }); |
| | | //未完成 |
| | | RefreshLayout myRefreshLayout = (RefreshLayout) binding.refreshLayout; |
| | | myRefreshLayout.setRefreshHeader(new ClassicsHeader(this.getContext())); |
| | | myRefreshLayout.setRefreshFooter(new ClassicsFooter(this.getContext())); |
| | | |
| | | myRefreshLayout.setOnRefreshListener(new OnRefreshListener() { |
| | | @Override |
| | | public void onRefresh(RefreshLayout refreshlayout) { |
| | | // refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 |
| | | recordsList.clear(); |
| | | page = 0; |
| | | getMarkerData(refreshlayout, true, STATE_UNDONE); |
| | | } |
| | | }); |
| | | myRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { |
| | | @Override |
| | | public void onLoadMore(RefreshLayout refreshlayout) { |
| | | page = page + 1; |
| | | getMarkerData(refreshlayout, false, STATE_UNDONE); |
| | | } |
| | | }); |
| | | |
| | | mAdapter = new OrderAdapter(this.getContext(), recordsList, this); |
| | | LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext()); |
| | | binding.recyclerView.setLayoutManager(layoutManager); |
| | | binding.recyclerView.setAdapter(mAdapter); |
| | | getMarkerData(myRefreshLayout, true, STATE_UNDONE); |
| | | //已完成 |
| | | RefreshLayout myRefreshLayoutDone = (RefreshLayout) binding.refreshLayoutDone; |
| | | myRefreshLayoutDone.setRefreshHeader(new ClassicsHeader(this.getContext())); |
| | | myRefreshLayoutDone.setRefreshFooter(new ClassicsFooter(this.getContext())); |
| | | myRefreshLayoutDone.setOnRefreshListener(new OnRefreshListener() { |
| | | @Override |
| | | public void onRefresh(RefreshLayout refreshlayout) { |
| | | // refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 |
| | | recordsList.clear(); |
| | | page = 0; |
| | | getMarkerData(refreshlayout, true, STATE_DONE); |
| | | } |
| | | }); |
| | | myRefreshLayoutDone.setOnLoadMoreListener(new OnLoadMoreListener() { |
| | | @Override |
| | | public void onLoadMore(RefreshLayout refreshlayout) { |
| | | page = page + 1; |
| | | getMarkerData(refreshlayout, false, STATE_DONE); |
| | | } |
| | | }); |
| | | mAdapter = new OrderAdapter(this.getContext(), recordsListDone, this); |
| | | LinearLayoutManager layoutManagerDone = new LinearLayoutManager(this.getContext()); |
| | | binding.recyclerViewDone.setLayoutManager(layoutManagerDone); |
| | | binding.recyclerViewDone.setAdapter(mAdapter); |
| | | getMarkerData(myRefreshLayoutDone, true, 2); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onStart() { |
| | | super.onStart(); |
| | | new TitleBar(OrderFragment.this.getActivity()).setTitleText("工单"); |
| | | } |
| | | |
| | | /** |
| | | * 获取未删除工单 |
| | | */ |
| | | private void getMarkerData(RefreshLayout refreshlayout, boolean isRefresh, int state) { |
| | | |
| | | Map<String, Object> params = new HashMap<>(); |
| | | if (isRefresh) { |
| | | page = 1; |
| | | if (state == STATE_UNDONE) { |
| | | recordsList.clear(); |
| | | } else { |
| | | recordsListDone.clear(); |
| | | } |
| | | } |
| | | params.put("pageSize", pageSize); |
| | | params.put("pageCurr", page); |
| | | params.put("state", state); |
| | | // params.put("inspectorId", MyApplication.myApplication.userId); |
| | | |
| | | ApiManager.getInstance().requestGetHideLoading(this.getContext(), Constants.BASE_URL + "/app/workOrder/getWorkOrders", OrderListResult.class, params, new SubscriberListener<BaseResponse<OrderListResult>>() { |
| | | @Override |
| | | public void onNext(BaseResponse<OrderListResult> t) { |
| | | if (t.isSuccess()) { |
| | | if (t.isSuccess()) { |
| | | if (t.getContent().getObj() != null && t.getContent().getObj().size() > 0) { |
| | | if (state == STATE_UNDONE) { |
| | | recordsList.addAll(t.getContent().getObj()); |
| | | } else { |
| | | recordsListDone.addAll(t.getContent().getObj()); |
| | | } |
| | | if (t.getContent().getObj().size() < pageSize) { |
| | | refreshlayout.finishLoadMoreWithNoMoreData(); |
| | | } |
| | | } else { |
| | | refreshlayout.finishLoadMoreWithNoMoreData(); |
| | | } |
| | | } |
| | | } else { |
| | | ToastUtil.showToast(OrderFragment.this.getContext(), t.getMsg()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onCloose() { |
| | | super.onCloose(); |
| | | layoutFinish(refreshlayout, isRefresh); |
| | | mAdapter.notifyDataSetChanged(); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | public void startDetail(String id) { |
| | | Intent intent = new Intent(this.getContext(), OrderDetailActivity.class); |
| | | intent.putExtra("workOrderId", id); |
| | | startActivity(intent); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改进度状态 |
| | | * |
| | | * @param state true为未完成 false为已完成 |
| | | */ |
| | | |
| | | void chooseStateView(boolean state) { |
| | | if (state) { |
| | | binding.manageStateProgress.setTextColor(getResources().getColor(R.color.title_color)); |
| | | binding.manageStateProgress.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit)); |
| | | binding.manageStateProgress.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); |
| | | binding.manageStateFinish.setTextColor(getResources().getColor(R.color.black)); |
| | | binding.manageStateFinish.setBackground(getResources().getDrawable(R.color.title_choose_bg)); |
| | | binding.manageStateFinish.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); |
| | | binding.refreshLayout.setVisibility(View.VISIBLE); |
| | | binding.refreshLayoutDone.setVisibility(View.GONE); |
| | | } else { |
| | | binding.manageStateFinish.setTextColor(getResources().getColor(R.color.title_color)); |
| | | binding.manageStateFinish.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit)); |
| | | binding.manageStateFinish.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); |
| | | binding.manageStateProgress.setTextColor(getResources().getColor(R.color.black)); |
| | | binding.manageStateProgress.setBackground(getResources().getDrawable(R.color.title_choose_bg)); |
| | | binding.manageStateProgress.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); |
| | | binding.refreshLayout.setVisibility(View.GONE); |
| | | binding.refreshLayoutDone.setVisibility(View.VISIBLE); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |