|
1 | | -package de.interaapps.localweather; |
| 1 | +package de.interaapps.localweather.example; |
2 | 2 |
|
3 | | -import androidx.annotation.NonNull; |
4 | 3 | import androidx.appcompat.app.AppCompatActivity; |
5 | 4 | import androidx.appcompat.widget.AppCompatImageView; |
6 | 5 | import androidx.appcompat.widget.AppCompatTextView; |
7 | 6 | import androidx.core.os.ConfigurationCompat; |
8 | 7 |
|
9 | 8 | import android.content.Intent; |
| 9 | +import android.location.Location; |
10 | 10 | import android.os.Bundle; |
11 | 11 | import android.util.Log; |
12 | 12 |
|
13 | | -import org.jetbrains.annotations.Nullable; |
14 | | - |
15 | 13 | import java.text.SimpleDateFormat; |
16 | 14 | import java.util.Calendar; |
17 | | -import java.util.Timer; |
18 | | -import java.util.TimerTask; |
19 | 15 |
|
| 16 | +import de.interaapps.localweather.utils.Lang; |
20 | 17 | import de.interaapps.localweather.utils.LocationFailedEnum; |
21 | | -import de.interaapps.localweather.utils.OpenWeatherIcons; |
| 18 | +import de.interaapps.localweather.example.utils.OpenWeatherIcons; |
22 | 19 | import de.interaapps.localweather.utils.Units; |
| 20 | +import de.interaapps.localweather.LocalWeather; |
| 21 | +import de.interaapps.localweather.Weather; |
23 | 22 |
|
24 | 23 | public class MainActivity extends AppCompatActivity { |
25 | 24 |
|
26 | 25 | private LocalWeather localWeather; |
27 | | - private Weather weather; |
28 | | - private boolean weatherReady = false; |
29 | 26 | private AppCompatImageView weatherImage; |
30 | 27 | private AppCompatTextView dayInfo; |
31 | 28 | private AppCompatTextView weatherTemp; |
@@ -73,90 +70,81 @@ private void initialize() { |
73 | 70 | weatherWindDeg = findViewById(R.id.weather_wind_deg); |
74 | 71 | weatherSunrise = findViewById(R.id.weather_sunrise); |
75 | 72 | weatherSunset = findViewById(R.id.weather_sunset); |
76 | | - localWeather = new LocalWeather(this, |
77 | | - "OpenWeatherApiKey", |
78 | | - true, |
79 | | - true, |
80 | | - new LocalWeather.Callbacks() { |
81 | | - @Override |
82 | | - public void onLocationFailure(LocationFailedEnum locationFailedEnum) { |
83 | | - Log.e("LOCATION-ERROR", locationFailedEnum.toString()); |
84 | | - } |
85 | | - |
86 | | - @Override |
87 | | - public void onWeatherFailure(Throwable throwable) { |
88 | | - Log.e("WEATHER-ERROR", throwable.getMessage()); |
89 | | - } |
90 | | - |
91 | | - @Override |
92 | | - public void onLocationSuccess() { |
93 | | - localWeather.fetchWeather(); |
94 | | - } |
95 | | - |
96 | | - @Override |
97 | | - public void onWeatherSuccess() { |
98 | | - weather = localWeather.getWeather(); |
99 | | - weatherReady = true; |
100 | | - updateWeatherDetails(); |
101 | | - } |
102 | | - }); |
| 73 | + localWeather = new LocalWeather(this, "OpenWeatherApiKey"); |
103 | 74 | } |
104 | 75 |
|
105 | 76 | private void initializeLogic() { |
106 | | - Timer timer = new Timer (); |
107 | | - TimerTask tenMinTask = new TimerTask () { |
| 77 | + localWeather.setUseCurrentLocation(true); |
| 78 | + localWeather.setUpdateCurrentLocation(true); |
| 79 | + localWeather.lang = Lang.ENGLISH; |
| 80 | + localWeather.unit = Units.METRIC; |
| 81 | + |
| 82 | + localWeather.setWeatherCallback(new LocalWeather.WeatherCallback() { |
| 83 | + @Override |
| 84 | + public void onSuccess(Weather weather) { |
| 85 | + updateWeatherDetails(weather); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onFailure(Throwable exception) { |
| 90 | + Log.e("Weather fetching Error", exception.getMessage()); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + localWeather.fetchCurrentLocation(new LocalWeather.CurrentLocationCallback() { |
| 95 | + @Override |
| 96 | + public void onSuccess(Location location) { |
| 97 | + Log.e("Location Success", location.toString()); |
| 98 | + localWeather.fetchCurrentWeatherByLocation(location); |
| 99 | + } |
| 100 | + |
108 | 101 | @Override |
109 | | - public void run () { |
110 | | - weatherReady = false; |
111 | | - localWeather.fetchLocation(); |
| 102 | + public void onFailure(LocationFailedEnum locationFailedEnum) { |
| 103 | + Log.e("Location fetching Error", locationFailedEnum.toString()); |
112 | 104 | } |
113 | | - }; |
114 | | - timer.schedule(tenMinTask, 0, 1000*60*10); //1000*60 = 1min *10 = 10min |
| 105 | + }); |
115 | 106 | } |
116 | 107 |
|
117 | | - private void updateWeatherDetails() { |
118 | | - //extra check but should always be true |
119 | | - if (weatherReady) { |
| 108 | + private void updateWeatherDetails(Weather weather) { |
120 | 109 | new OpenWeatherIcons(getApplicationContext(), weather.getIcons()[0], weatherImage); |
121 | | - String unit = (localWeather.getUnit() == Units.METRIC) ? "°C" : "°F" ; |
122 | | - String speed = (localWeather.getUnit() == Units.METRIC) ? "km/h" : "mi/h" ; |
123 | | - weatherTemp.setText(((int) weather.getTemp()) + unit); |
| 110 | + String unit = (localWeather.unit == Units.METRIC) ? "°C" : "°F" ; |
| 111 | + String speed = (localWeather.unit == Units.METRIC) ? "km/h" : "mi/h" ; |
| 112 | + weatherTemp.setText(((int) weather.getTemperature()) + unit); |
124 | 113 | weatherInfo.setText("Description: " + weather.getDescriptions()[0]); |
125 | 114 | weatherClouds.setText("Clouds: " + ((int) weather.getClouds())); |
126 | | - weatherMaxTemp.setText("Max. Temp.: " + ((int) weather.getMaxTemp()) + unit); |
127 | | - weatherMinTemp.setText("Min. Temp.: " + ((int) weather.getMinTemp()) + unit); |
128 | | - weatherTempKf.setText("Temp Kf.: " + ((int) weather.getTempKf())); |
| 115 | + weatherMaxTemp.setText("Max. Temp.: " + ((int) weather.getMaxTemperature()) + unit); |
| 116 | + weatherMinTemp.setText("Min. Temp.: " + ((int) weather.getMinTemperature()) + unit); |
| 117 | + weatherTempKf.setText("Temp Kf.: " + ((int) weather.getTemperatureKf())); |
129 | 118 | locationCountry.setText("Country: " + weather.getCountry()); |
130 | 119 |
|
131 | 120 | //real location based: localWeather.getLatitude(); |
132 | | - locationLat.setText("Latitude: " + weather.getLat()); |
| 121 | + locationLat.setText("Latitude: " + weather.getLatitude()); |
133 | 122 | //real location based: localWeather.getLongitude(); |
134 | | - locationLon.setText("Longitude: " + weather.getLon()); |
| 123 | + locationLon.setText("Longitude: " + weather.getLongitude()); |
135 | 124 |
|
136 | | - locationGroundLevel.setText("Ground Level: " + weather.getGrndLevel()); |
| 125 | + locationGroundLevel.setText("Ground Level: " + weather.getGroundLevel()); |
137 | 126 | locationSeaLevel.setText("Sea Level: " + weather.getSeaLevel()); |
138 | 127 |
|
139 | 128 | weatherBase.setText("Base: " + weather.getBase()); |
140 | 129 | weatherWindSpeed.setText("Wind Speed: " + weather.getWindSpeed() + speed); |
141 | | - weatherWindDeg.setText("Wind Angle: " + ((int) weather.getWindDeg()) + "°"); |
| 130 | + weatherWindDeg.setText("Wind Angle: " + ((int) weather.getWindAngle()) + "°"); |
142 | 131 | weatherSunrise.setText("Sunrise: " + weather.getSunrise()); |
143 | 132 | weatherSunset.setText("Sunset: " + weather.getSunset()); |
144 | | - } |
145 | 133 |
|
146 | 134 | Calendar calendar = Calendar.getInstance(); |
147 | 135 | SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM", ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0)); |
148 | 136 | dayInfo.setText(dateFormat.format(calendar.getTime())); |
149 | 137 | } |
150 | 138 |
|
151 | 139 | @Override |
152 | | - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 140 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
153 | 141 | super.onActivityResult(requestCode, resultCode, data); |
154 | | - localWeather.getLocationAccess().onActivityResult(requestCode, resultCode, data); |
| 142 | + localWeather.onActivityResult(requestCode, resultCode, data); |
155 | 143 | } |
156 | 144 |
|
157 | 145 | @Override |
158 | | - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 146 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
159 | 147 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
160 | | - localWeather.getLocationAccess().onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 148 | + localWeather.onRequestPermissionResult(requestCode, permissions, grantResults); |
161 | 149 | } |
162 | 150 | } |
0 commit comments