Skip to content

Commit 8f7ecb5

Browse files
committed
Better code style, also remove useless method parameters
1 parent 5b8f9a2 commit 8f7ecb5

File tree

15 files changed

+50
-61
lines changed

15 files changed

+50
-61
lines changed

library/src/main/java/github/vatsal/easyweather/Helper/TempUnitConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
*/
99
public class TempUnitConverter {
1010

11+
private static final double ABSOLUTE_ZERO = 273.15;
12+
1113
public static Double convertToCelsius(String kelvin) throws NumberFormatException {
1214
double inKelvin;
1315
try {
1416
inKelvin = Double.parseDouble(kelvin);
1517
} catch (NumberFormatException e) {
1618
throw e;
1719
}
18-
return inKelvin - 273.15;
20+
return inKelvin - ABSOLUTE_ZERO;
1921
}
2022

2123
public static Double convertToFahrenheit(String kelvin) throws NumberFormatException {
@@ -25,7 +27,7 @@ public static Double convertToFahrenheit(String kelvin) throws NumberFormatExcep
2527
} catch (NumberFormatException e) {
2628
throw e;
2729
}
28-
return (inKelvin - 273.15) * 1.8000 + 32.00;
30+
return (inKelvin - ABSOLUTE_ZERO) * 1.8000 + 32.00;
2931
}
3032

3133
}

library/src/main/java/github/vatsal/easyweather/WeatherMap.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package github.vatsal.easyweather;
22

3-
import android.content.Context;
4-
53
import github.vatsal.easyweather.Helper.ForecastCallback;
64
import github.vatsal.easyweather.Helper.WeatherCallback;
75
import github.vatsal.easyweather.retrofit.api.ApiClient;
@@ -18,26 +16,24 @@
1816
* --2:44 AM in
1917
* --OpenWeatherMapDemo
2018
*/
19+
@SuppressWarnings("unchecked")
2120
public class WeatherMap {
2221

23-
Context context;
24-
String APP_ID;
25-
22+
private String APP_ID;
2623

27-
public WeatherMap(Context context, String APP_ID) {
28-
this.context = context;
24+
public WeatherMap(String APP_ID) {
2925
this.APP_ID = APP_ID;
3026
}
3127

3228
public void getCityWeather(String city, final WeatherCallback weatherCallback) {
3329
final ApiClient objApi = ApiClient.getInstance();
3430
try {
35-
Call objCall = null;
31+
Call objCall;
3632

37-
objCall = objApi.getApi(context).getCityWeather(APP_ID, city);
33+
objCall = objApi.getApi(APP_ID).getCityWeather(city);
3834

3935
if (objCall != null) {
40-
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>(context) {
36+
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>() {
4137

4238
@Override
4339
public void onFailure(Call call, Throwable t) {
@@ -73,12 +69,12 @@ protected void common() {
7369
public void getLocationWeather(String latitude, String longitude, final WeatherCallback weatherCallback) {
7470
final ApiClient objApi = ApiClient.getInstance();
7571
try {
76-
Call objCall = null;
72+
Call objCall;
7773

78-
objCall = objApi.getApi(context).getLocationWeather(APP_ID, latitude, longitude);
74+
objCall = objApi.getApi(APP_ID).getLocationWeather(latitude, longitude);
7975

8076
if (objCall != null) {
81-
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>(context) {
77+
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>() {
8278

8379
@Override
8480
public void onFailure(Call call, Throwable t) {
@@ -114,12 +110,12 @@ protected void common() {
114110
public void getCityForecast(String city, final ForecastCallback forecastCallback) {
115111
final ApiClient objApi = ApiClient.getInstance();
116112
try {
117-
Call objCall = null;
113+
Call objCall;
118114

119-
objCall = objApi.getApi(context).getCityForcast(APP_ID, city);
115+
objCall = objApi.getApi(APP_ID).getCityForcast(city);
120116

121117
if (objCall != null) {
122-
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>(context) {
118+
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>() {
123119

124120
@Override
125121
public void onFailure(Call call, Throwable t) {
@@ -155,12 +151,12 @@ protected void common() {
155151
public void getLocationForecast(String latitude, String longitude, final ForecastCallback forecastCallback) {
156152
final ApiClient objApi = ApiClient.getInstance();
157153
try {
158-
Call objCall = null;
154+
Call objCall;
159155

160-
objCall = objApi.getApi(context).getLocationForecast(APP_ID, latitude, longitude);
156+
objCall = objApi.getApi(APP_ID).getLocationForecast(latitude, longitude);
161157

162158
if (objCall != null) {
163-
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>(context) {
159+
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>() {
164160

165161
@Override
166162
public void onFailure(Call call, Throwable t) {

library/src/main/java/github/vatsal/easyweather/retrofit/api/ApiClient.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package github.vatsal.easyweather.retrofit.api;
22

33

4-
import android.content.Context;
5-
import android.support.annotation.NonNull;
6-
74
import java.io.IOException;
85

96
import okhttp3.Interceptor;
@@ -28,7 +25,7 @@ public static synchronized ApiClient getInstance() {
2825
return uniqInstance;
2926
}
3027

31-
private void ApiClient(@NonNull final Context currContext) {
28+
private void ApiClient(final String appId) {
3229
try {
3330
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
3431
// set your desired log level
@@ -41,6 +38,7 @@ public Response intercept(Interceptor.Chain chain) throws IOException {
4138
Request.Builder builder = original.newBuilder();
4239
builder.method(original.method(), original.body());
4340

41+
builder.addHeader(WeatherInterface.FIELDS.APPID, appId);
4442
Request request = builder.build();
4543

4644
return chain.proceed(request);
@@ -94,11 +92,11 @@ public Response intercept(Chain chain) throws IOException {
9492
}
9593
}
9694

97-
public WeatherInterface getApi(Context currContext) {
95+
public WeatherInterface getApi(String appId) {
9896
if (uniqInstance == null) {
9997
getInstance();
10098
}
101-
uniqInstance.ApiClient(currContext);
99+
uniqInstance.ApiClient(appId);
102100

103101
return weatherInterface;
104102
}

library/src/main/java/github/vatsal/easyweather/retrofit/api/WeatherInterface.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@
99
public interface WeatherInterface {
1010

1111
@GET("weather")
12-
Call<WeatherResponseModel> getCityWeather(@Query("appid") String appid,
13-
@Query("q") String city);
12+
Call<WeatherResponseModel> getCityWeather(@Query(FIELDS.Q) String city);
1413

1514
@GET("weather")
16-
Call<WeatherResponseModel> getLocationWeather(@Query("appid") String appid,
17-
@Query("lat") String latitude,
18-
@Query("lon") String longitude);
15+
Call<WeatherResponseModel> getLocationWeather(@Query(FIELDS.LAT) String latitude,
16+
@Query(FIELDS.LON) String longitude);
1917

2018
@GET("forecast")
21-
Call<ForecastResponseModel> getCityForcast(@Query("appid") String appid,
22-
@Query("q") String city);
19+
Call<ForecastResponseModel> getCityForcast(@Query(FIELDS.Q) String city);
2320

2421
@GET("forecast")
25-
Call<ForecastResponseModel> getLocationForecast(@Query("appid") String appid,
26-
@Query("lat") String latitude,
27-
@Query("lon") String longitude);
22+
Call<ForecastResponseModel> getLocationForecast(@Query(FIELDS.LAT) String latitude,
23+
@Query(FIELDS.LON) String longitude);
2824

25+
interface FIELDS {
26+
String APPID = "appid";
27+
String Q = "q";
28+
String LAT = "lat";
29+
String LON = "lon";
30+
}
2931
}

library/src/main/java/github/vatsal/easyweather/retrofit/api/WeatherRetrofitCallback.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package github.vatsal.easyweather.retrofit.api;
22

3-
import android.app.Activity;
4-
import android.content.Context;
5-
63
import retrofit2.Call;
74
import retrofit2.Callback;
85
import retrofit2.Response;
@@ -11,16 +8,6 @@
118
* Created by nuhbye on 07/03/16.
129
*/
1310
public abstract class WeatherRetrofitCallback<S> implements Callback {
14-
Activity activity;
15-
Context context;
16-
17-
public WeatherRetrofitCallback(Activity activity) {
18-
this.activity = activity;
19-
}
20-
21-
public WeatherRetrofitCallback(Context context) {
22-
this.context = context;
23-
}
2411

2512
@Override
2613
public void onResponse(Call call, Response response) {

library/src/main/java/github/vatsal/easyweather/retrofit/models/City.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public void setCountry(String country) {
7171

7272
@Override
7373
public String toString() {
74-
return "ClassPojo [coord = " + coord + ", id = " + id + ", sys = " + sys + ", name = " + name + ", population = " + population + ", country = " + country + "]";
74+
return String.format("ClassPojo [coord = %s, id = %s, sys = %s, name = %s, population = %s, country = %s]", coord, id, sys, name, population, country);
7575
}
7676
}

library/src/main/java/github/vatsal/easyweather/retrofit/models/Clouds.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public void setAll(String all) {
2121

2222
@Override
2323
public String toString() {
24-
return "ClassPojo [all = " + all + "]";
24+
return String.format("ClassPojo [all = %s]", all);
2525
}
2626
}

library/src/main/java/github/vatsal/easyweather/retrofit/models/Coord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public void setLat(String lat) {
3131

3232
@Override
3333
public String toString() {
34-
return "ClassPojo [lon = " + lon + ", lat = " + lat + "]";
34+
return String.format("ClassPojo [lon = %s, lat = %s]", lon, lat);
3535
}
3636
}

library/src/main/java/github/vatsal/easyweather/retrofit/models/ForecastResponseModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public void setCity(City city) {
6161

6262
@Override
6363
public String toString() {
64-
return "ClassPojo [message = " + message + ", cnt = " + cnt + ", cod = " + cod + ", list = " + list + ", city = " + city + "]";
64+
return String.format("ClassPojo [message = %s, cnt = %s, cod = %s, list = %s, city = %s]", message, cnt, cod, list, city);
6565
}
6666
}

library/src/main/java/github/vatsal/easyweather/retrofit/models/List.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ public void setMain(Main main) {
9191

9292
@Override
9393
public String toString() {
94-
return "ClassPojo [clouds = " + clouds + ", dt = " + dt + ", wind = " + wind + ", sys = " + sys + ", weather = " + weather + ", dt_txt = " + dt_txt + ", rain = " + rain + ", main = " + main + "]";
94+
return String.format("ClassPojo [clouds = %s, dt = %s, wind = %s, sys = %s, weather = %s, dt_txt = %s, rain = %s, main = %s]", clouds, dt, wind, sys, weather, dt_txt, rain, main);
9595
}
9696
}

0 commit comments

Comments
 (0)