package com.dayu.pipirrapp.fragment;
|
|
import android.os.Bundle;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import androidx.lifecycle.Observer;
|
|
import com.dayu.pipirrapp.bean.db.LatLonBean;
|
import com.dayu.pipirrapp.bean.net.WeatherResponse;
|
import com.dayu.pipirrapp.databinding.FragmentMainBinding;
|
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.jeremyliao.liveeventbus.LiveEventBus;
|
import com.tencent.bugly.crashreport.CrashReport;
|
|
import java.util.Calendar;
|
|
import retrofit2.Call;
|
import retrofit2.Callback;
|
import retrofit2.Response;
|
|
/**
|
* author: zuo
|
* Date: 2023/12/20
|
* Time: 10:12
|
* 备注:首页
|
*/
|
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) {
|
|
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);
|
}
|
};
|
|
|
/**
|
* 设置天气相关信息
|
*
|
* @param data
|
*/
|
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);
|
|
return binding.getRoot();
|
}
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
new TitleBar(OrderFragment.this.getActivity()).setTitleText("工单");
|
}
|
}
|