forked from sdwfqin/AndroidQuick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherViewModel.java
More file actions
48 lines (40 loc) · 1.49 KB
/
WeatherViewModel.java
File metadata and controls
48 lines (40 loc) · 1.49 KB
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
package com.sdwfqin.quickseed.ui.mvvm;
import androidx.lifecycle.MutableLiveData;
import com.sdwfqin.quicklib.mvvm.BaseViewModel;
import com.sdwfqin.quickseed.retrofit.RetrofitClient;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* 天气ViewModel
* <p>
* 注意:ViewModel 绝不能引用视图、Lifecycle 或可能存储对 Activity 上下文的引用的任何类。
*
* @author 张钦
* @date 2020/4/15
*/
public class WeatherViewModel extends BaseViewModel {
public final MutableLiveData<WeatherBean> weatherBean = new MutableLiveData<>();
public final MutableLiveData<Long> birthDate = new MutableLiveData<>();
public void loadWeather() {
isLoading.postValue(true);
Map<String, Object> map = new HashMap();
RetrofitClient
.getInstance()
.gService.getWeather(map)
.enqueue(new Callback<WeatherBean>() {
@Override
public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
weatherBean.postValue(response.body());
isLoading.postValue(false);
}
@Override
public void onFailure(Call<WeatherBean> call, Throwable t) {
isLoading.postValue(false);
networkError.postValue(t);
}
});
}
}