管灌系统巡查员智能手机App
zuoxiao
2024-11-13 e7df063a027c0f066317da4437d01cf3f3bc8d31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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("工单");
    }
}