增加天气显示

This commit is contained in:
tongtong committed 2026-03-06 09:50:58 +08:00
1 parent 962a022bf9
commit 0f2adad060
30 files changed
+5567 -64

No files matched your search

@@ -33,7 +33,7 @@ import me.jessyan.autosize.AutoSize;
public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBinding> {
private static final String TAG = "MainActivity";
protected MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private FragmentManager mFragmentManager = getSupportFragmentManager();
// private BaseFragmentPagerAdapter mBaseFragmentPagerAdapter;
@@ -0,0 +1,105 @@
package com.ttstd.dialer.activity.weather.main;
import android.util.Log;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.baidu.location.BDLocation;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.qweather.sdk.response.weather.WeatherDaily;
import com.qweather.sdk.response.weather.WeatherNow;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.R;
import com.ttstd.dialer.adapter.WeatherAdapter;
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.databinding.ActivityWeatherMainBinding;
import com.ttstd.dialer.utils.DateUtil;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
public class WeatherMainActivity extends BaseMvvmActivity<WeatherMainViewModel, ActivityWeatherMainBinding> {
private static final String TAG = "WeatherMainActivity";
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private WeatherAdapter mWeatherAdapter;
// @Override
// public boolean setfitWindow() {
// return true;
// }
@Override
protected int getLayoutId() {
return R.layout.activity_weather_main;
}
@Override
protected void initDataBinding() {
mViewModel.setContext(this);
mViewModel.setVDBinding(mViewDataBinding);
mViewModel.setLifecycle(getLifecycleSubject());
mViewDataBinding.setClick(new BtnClick());
}
@Override
protected void initView() {
mWeatherAdapter = new WeatherAdapter();
mViewDataBinding.recyclerView.setAdapter(mWeatherAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WeatherMainActivity.this, LinearLayoutManager.VERTICAL, false);
mViewDataBinding.recyclerView.setLayoutManager(linearLayoutManager);
}
@Override
protected void initData() {
Log.e(TAG, "initData: ");
mViewModel.mWeatherNowData.observe(this, new Observer<WeatherNow>() {
@Override
public void onChanged(WeatherNow weatherNow) {
mViewDataBinding.setWeatherNow(weatherNow);
}
});
mViewModel.mWeatherDailyListData.observe(this, new Observer<List<WeatherDaily>>() {
@Override
public void onChanged(List<WeatherDaily> weatherDailies) {
Optional<WeatherDaily> dailyOptional = weatherDailies.stream().filter(new Predicate<WeatherDaily>() {
@Override
public boolean test(WeatherDaily weatherDaily) {
return DateUtil.isTodayAll(weatherDaily.getFxDate());
}
}).findAny();
if (dailyOptional.isPresent()) {
WeatherDaily weatherDaily = dailyOptional.get();
mViewDataBinding.tvTempRange.setText("最高" + weatherDaily.getTempMax() + "°" + "\t" + "最低" + weatherDaily.getTempMin() + "°");
} else {
}
mWeatherAdapter.setWeatherDailyList(weatherDailies);
}
});
boolean manually = mMMKV.decodeBool(CommonConfig.MANUALLY_SELECT_LOCATION, false);
if (manually) {
} else {
LiveEventBus.get(CommonConfig.LOCATION_CHANGED_KEY, BDLocation.class)
.observeSticky(this, new Observer<BDLocation>() {
@Override
public void onChanged(BDLocation bdLocation) {
mViewDataBinding.tvLocation.setText(bdLocation.getDistrict());
mViewModel.getWeatherNow(bdLocation.getAdCode());
mViewModel.getWeather10D(bdLocation.getAdCode());
}
});
}
}
public class BtnClick {
}
}
@@ -0,0 +1,75 @@
package com.ttstd.dialer.activity.weather.main;
import android.util.Log;
import androidx.lifecycle.MutableLiveData;
import com.qweather.sdk.Callback;
import com.qweather.sdk.response.error.ErrorResponse;
import com.qweather.sdk.response.weather.WeatherDaily;
import com.qweather.sdk.response.weather.WeatherDailyResponse;
import com.qweather.sdk.response.weather.WeatherNow;
import com.qweather.sdk.response.weather.WeatherNowResponse;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.ActivityWeatherMainBinding;
import com.ttstd.dialer.gson.GsonUtils;
import com.ttstd.dialer.manager.WeatherManager;
import java.util.List;
public class WeatherMainViewModel extends BaseViewModel<ActivityWeatherMainBinding, ActivityEvent> {
private static final String TAG = "WeatherMainViewModel";
public MutableLiveData<WeatherNow> mWeatherNowData = new MutableLiveData<>();
public void getWeatherNow(String adCode) {
Log.e(TAG, "getWeatherNow: " + adCode);
WeatherManager.getInstance().getWeatherNow(adCode, new Callback<WeatherNowResponse>() {
@Override
public void onSuccess(WeatherNowResponse response) {
Log.e("getWeatherNow", "onSuccess: " + response);
WeatherNow weatherNow = response.getNow();
Log.e("getWeatherNow", "onSuccess: " + weatherNow);
mWeatherNowData.postValue(weatherNow);
}
@Override
public void onFailure(ErrorResponse errorResponse) {
Log.e("getWeatherNow", "onFailure: " + errorResponse.getError().getStatus());
Log.e("getWeatherNow", "onFailure: " + errorResponse.getError().getDetail());
}
@Override
public void onException(Throwable e) {
Log.e("getWeatherNow", "onException: " + e.getMessage());
}
});
}
public MutableLiveData<List<WeatherDaily>> mWeatherDailyListData = new MutableLiveData<>();
public void getWeather10D(String adCode) {
Log.e(TAG, "getWeather10D: " + adCode);
WeatherManager.getInstance().getWeather10D(adCode, new Callback<WeatherDailyResponse>() {
@Override
public void onSuccess(WeatherDailyResponse weatherDailyResponse) {
Log.e("getWeather10D", "onSuccess: ");
List<WeatherDaily> weatherDailyList = weatherDailyResponse.getDaily();
Log.e("getWeather10D", "onSuccess: "+ GsonUtils.toJSONString(weatherDailyList));
mWeatherDailyListData.postValue(weatherDailyList);
}
@Override
public void onFailure(ErrorResponse errorResponse) {
Log.e("getWeather10D", "onFailure: ");
}
@Override
public void onException(Throwable throwable) {
Log.e("getWeather10D", "onException: " + throwable.getMessage());
}
});
}
}