package cn.vesns.sunweather.activity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.qweather.sdk.bean.IndicesBean;
import com.qweather.sdk.bean.air.AirNowBean;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.IndicesType;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherHourlyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.QWeather;
import java.io.IOException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.vesns.sunweather.R;
import cn.vesns.sunweather.adapter.ViewPagerAdapter;
import cn.vesns.sunweather.entity.City;
import cn.vesns.sunweather.entity.CityList;
import cn.vesns.sunweather.fragment.OneFragment;
import cn.vesns.sunweather.util.CommonUtil;
import cn.vesns.sunweather.util.DisplayUtil;
import cn.vesns.sunweather.util.HttpUtil;
import cn.vesns.sunweather.util.SpUtils;
import cn.vesns.sunweather.util.WeekUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* created by vesns on 2021/06/06.
*/
public class WeatherActivity extends AppCompatActivity {
private static final String TAG = "WeatherActivity.class";
private final int REQUEST_GPS = 1;
private int mNum = 0;
private List<Fragment> fragments;
CityList cityList = new CityList();
private List<String> locaitons;
private List<String> cityIds;
private LinearLayout llRound;
public SwipeRefreshLayout swipeRefresh;
private ImageView navButton;
private TextView titleCity;
private TextView degreeText;
private TextView weatherInfoText;
private TextView cloudageText;
private TextView humidityText;
private LinearLayout forecastLayout;
private TextView comfortText;
private TextView carWashText;
private TextView sportText;
private ImageView backBingImg;
private ProgressBar progressBar;
private TextView progressBar_text;
private TextView gradle_air_text;
private TextView traffic_text;
private TextView fish_text;
private TextView travel_text;
private LinearLayout linearLayout;
private int[] image = {R.mipmap.cloudy, R.mipmap.fine, R.mipmap.rain, R.mipmap.thunder, R.mipmap.dawu, R.mipmap.haze, R.mipmap.fog};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
init();
// initFragment();
loadBingPic();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_GPS);
swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
requestWeather(CommonUtil.CITY_ID);
swipeRefresh.setOnRefreshListener(() -> {
requestWeather(CommonUtil.CITY_ID);
loadBingPic();
});
navButton.setOnClickListener(v->{
if (v.getId() == R.id.nav_button) {
startActivity(new Intent(this,SearchActivity.class));
}
});
}
public void init() {
swipeRefresh = findViewById(R.id.swipe_refresh);
titleCity = findViewById(R.id.title_city);
degreeText = findViewById(R.id.degree_text);
weatherInfoText = findViewById(R.id.weather_info_text);
cloudageText = findViewById(R.id.cloudage_text);
humidityText = findViewById(R.id.humidity_text);
forecastLayout = findViewById(R.id.forecast_layout);
comfortText = findViewById(R.id.comfort_text);
carWashText = findViewById(R.id.car_wash_text);
sportText = findViewById(R.id.sport_text);
backBingImg = findViewById(R.id.bing_pic_img);
swipeRefresh = findViewById(R.id.swipe_refresh);
swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
// drawerLayout = findViewById(R.id.drawer_layout);
navButton = findViewById(R.id.nav_button);
progressBar = findViewById(R.id.progressBar);
progressBar_text = findViewById(R.id.progressBar_text);
gradle_air_text = findViewById(R.id.gradle_air);
linearLayout = findViewById(R.id.linear);
travel_text = findViewById(R.id.travel_text);
fish_text = findViewById(R.id.fish_text);
traffic_text = findViewById(R.id.traffic_text);
navButton = findViewById(R.id.nav_button);
llRound = findViewById(R.id.ll_round);
}
private void initFragment() {
cityList = SpUtils.getBean(WeatherActivity.this, "cityBean", CityList.class);
CityList cityBean = SpUtils.getBean(WeatherActivity.this, "cityBean", CityList.class);
locaitons = new ArrayList<>();
if (cityBean != null) {
for (City city : cityBean.getCityBeans()) {
String cityName = city.getCityName();
locaitons.add(cityName);
}
}
cityIds = new ArrayList<>();
fragments = new ArrayList<>();
}
/**
* 根据天气id差城市天气信息
*
* @param weatherId
*/
public void requestWeather(final String weatherId) {
/**
* 实况天气数据
* @param location 所查询的地区,可通过该地区名称、ID、IP和经纬度进行查询经纬度格式:经度,纬度
* (英文,分隔,十进制格式,北纬东经为正,南纬西经为负)
* @param lang (选填)多语言,可以不使用该参数,默认为简体中文
* @param unit (选填)单位选择,公制(m)或英制(i),默认为公制单位
* @param listener 网络访问结果回调
*/
QWeather.getGeoCityLookup(WeatherActivity.this, CommonUtil.CITY_INITIAL, new QWeather.OnResultGeoListener() {
@Override
public void onError(Throwable throwable) {
runOnUiThread(() -> {
// Toast.makeText(WeatherActivity.this, CommonUtil.CITY_INITIAL, Toast.LENGTH_SHORT).show();
swipeRefresh.setRefreshing(false);
});
Log.i(TAG, "getGeoCityLookup onError: " + throwable);
}
@Override
public void onSuccess(GeoBean geoBean) {
Log.i(TAG, "getGeoCityLookup onSuccess: " + new Gson().toJson(geoBean));
if (Code.OK == geoBean.getCode()) {
List<GeoBean.LocationBean> locationBean = geoBean.getLocationBean();
List<City> cityBeans = new ArrayList<>();
City cityBean = new City();
for (GeoBean.LocationBean bean : locationBean) {
cityBean.setCityName(bean.getName())