-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMainActivity.java
More file actions
151 lines (127 loc) · 4.49 KB
/
MainActivity.java
File metadata and controls
151 lines (127 loc) · 4.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package github.vatsal.easyweatherdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import github.vatsal.easyweather.Helper.TempUnitConverter;
import github.vatsal.easyweather.Helper.WeatherCallback;
import github.vatsal.easyweather.retrofit.api.WeatherMap;
import github.vatsal.easyweather.retrofit.models.CurrentWeatherResponseModel;
import github.vatsal.easyweather.retrofit.models.DailyForecastResponseModel;
import github.vatsal.easyweather.retrofit.models.ForecastResponseModel;
import github.vatsal.easyweather.retrofit.models.Weather;
import trikita.log.Log;
public class MainActivity extends AppCompatActivity {
public final String APP_ID = BuildConfig.OWM_API_KEY;
public final String lang = "fr";
String city = "San Francisco";
@BindView(R.id.weather_title)
TextView weatherTitle;
@BindView(R.id.refresh)
ImageButton refresh;
@BindView(R.id.weather_icon)
ImageView weatherIcon;
@BindView(R.id.location)
TextView location;
@BindView(R.id.condition)
TextView condition;
@BindView(R.id.temp)
TextView temp;
@BindView(R.id.tvHumidity)
TextView tvHumidity;
@BindView(R.id.tvPressure)
TextView tvPressure;
@BindView(R.id.tvWind)
TextView tvWind;
@BindView(R.id.tvWindDeg)
TextView tvWindDeg;
@BindView(R.id.et_city)
EditText etCity;
@BindView(R.id.tv_go)
TextView tvGo;
@BindView(R.id.textLayout)
LinearLayout textLayout;
@BindView(R.id.humidity_desc)
TextView humidityDesc;
@BindView(R.id.pres_desc)
TextView presDesc;
@BindView(R.id.ws_desc)
TextView wsDesc;
@BindView(R.id.wd_desc)
TextView wdDesc;
@BindView(R.id.ll_extraWeather)
LinearLayout llExtraWeather;
@BindView(R.id.weatherCard)
CardView weatherCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
loadWeather(city);
}
@OnClick(R.id.refresh)
public void refresh() {
loadWeather(city);
}
private void loadWeather(String city) {
WeatherMap weatherMap = new WeatherMap(this, APP_ID, lang);
weatherMap.getCityWeather(city, new WeatherCallback<CurrentWeatherResponseModel>() {
@Override
public void success(CurrentWeatherResponseModel response) {
populateWeather(response);
Log.i(response.toString());
}
@Override
public void failure(String message) {
}
});
weatherMap.getCityForecast(city, new WeatherCallback<ForecastResponseModel>() {
@Override
public void success(ForecastResponseModel response) {
Log.i(response.toString());
}
@Override
public void failure(String message) {
}
});
weatherMap.getCityDailyForecast(city, "3", new WeatherCallback<DailyForecastResponseModel>() {
@Override
public void success(DailyForecastResponseModel response) {
Log.i(response.toString());
}
@Override
public void failure(String message) {
}
});
}
private void populateWeather(CurrentWeatherResponseModel response) {
Weather weather[] = response.getWeather();
condition.setText(weather[0].getMain());
temp.setText(TempUnitConverter.convertToCelsius(response.getMain().getTemp()).intValue() + " °C");
location.setText(response.getName());
tvHumidity.setText(response.getMain().getHumidity() + "%");
tvPressure.setText(response.getMain().getPressure() + " hPa");
tvWind.setText(response.getWind().getSpeed() + "m/s");
tvWindDeg.setText(response.getWind().getDeg() + "°");
String link = weather[0].getIconLink();
Picasso.with(this).load(link).into(weatherIcon);
}
@OnClick(R.id.tv_go)
public void go() {
city = etCity.getText().toString().trim();
loadWeather(city);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}