Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void refresh() {
}

private void loadWeather(String city) {
WeatherMap weatherMap = new WeatherMap(this, APP_ID);
WeatherMap weatherMap = new WeatherMap(APP_ID);
weatherMap.getCityWeather(city, new WeatherCallback() {
@Override
public void success(WeatherResponseModel response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
*/
public class TempUnitConverter {

private static final double ABSOLUTE_ZERO = 273.15;

public static Double convertToCelsius(String kelvin) throws NumberFormatException {
double inKelvin;
try {
inKelvin = Double.parseDouble(kelvin);
} catch (NumberFormatException e) {
throw e;
}
return inKelvin - 273.15;
return inKelvin - ABSOLUTE_ZERO;
}

public static Double convertToFahrenheit(String kelvin) throws NumberFormatException {
Expand All @@ -25,7 +27,7 @@ public static Double convertToFahrenheit(String kelvin) throws NumberFormatExcep
} catch (NumberFormatException e) {
throw e;
}
return (inKelvin - 273.15) * 1.8000 + 32.00;
return (inKelvin - ABSOLUTE_ZERO) * 1.8000 + 32.00;
}

}
34 changes: 15 additions & 19 deletions library/src/main/java/github/vatsal/easyweather/WeatherMap.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package github.vatsal.easyweather;

import android.content.Context;

import github.vatsal.easyweather.Helper.ForecastCallback;
import github.vatsal.easyweather.Helper.WeatherCallback;
import github.vatsal.easyweather.retrofit.api.ApiClient;
Expand All @@ -18,26 +16,24 @@
* --2:44 AM in
* --OpenWeatherMapDemo
*/
@SuppressWarnings("unchecked")
public class WeatherMap {

Context context;
String APP_ID;

private String APP_ID;

public WeatherMap(Context context, String APP_ID) {
this.context = context;
public WeatherMap(String APP_ID) {
this.APP_ID = APP_ID;
}

public void getCityWeather(String city, final WeatherCallback weatherCallback) {
final ApiClient objApi = ApiClient.getInstance();
try {
Call objCall = null;
Call objCall;

objCall = objApi.getApi(context).getCityWeather(APP_ID, city);
objCall = objApi.getApi(APP_ID).getCityWeather(city);

if (objCall != null) {
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>(context) {
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>() {

@Override
public void onFailure(Call call, Throwable t) {
Expand Down Expand Up @@ -73,12 +69,12 @@ protected void common() {
public void getLocationWeather(String latitude, String longitude, final WeatherCallback weatherCallback) {
final ApiClient objApi = ApiClient.getInstance();
try {
Call objCall = null;
Call objCall;

objCall = objApi.getApi(context).getLocationWeather(APP_ID, latitude, longitude);
objCall = objApi.getApi(APP_ID).getLocationWeather(latitude, longitude);

if (objCall != null) {
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>(context) {
objCall.enqueue(new WeatherRetrofitCallback<WeatherResponseModel>() {

@Override
public void onFailure(Call call, Throwable t) {
Expand Down Expand Up @@ -114,12 +110,12 @@ protected void common() {
public void getCityForecast(String city, final ForecastCallback forecastCallback) {
final ApiClient objApi = ApiClient.getInstance();
try {
Call objCall = null;
Call objCall;

objCall = objApi.getApi(context).getCityForcast(APP_ID, city);
objCall = objApi.getApi(APP_ID).getCityForcast(city);

if (objCall != null) {
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>(context) {
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>() {

@Override
public void onFailure(Call call, Throwable t) {
Expand Down Expand Up @@ -155,12 +151,12 @@ protected void common() {
public void getLocationForecast(String latitude, String longitude, final ForecastCallback forecastCallback) {
final ApiClient objApi = ApiClient.getInstance();
try {
Call objCall = null;
Call objCall;

objCall = objApi.getApi(context).getLocationForecast(APP_ID, latitude, longitude);
objCall = objApi.getApi(APP_ID).getLocationForecast(latitude, longitude);

if (objCall != null) {
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>(context) {
objCall.enqueue(new WeatherRetrofitCallback<ForecastResponseModel>() {

@Override
public void onFailure(Call call, Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package github.vatsal.easyweather.retrofit.api;


import android.content.Context;
import android.support.annotation.NonNull;

import java.io.IOException;

import okhttp3.Interceptor;
Expand All @@ -28,7 +25,7 @@ public static synchronized ApiClient getInstance() {
return uniqInstance;
}

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

builder.addHeader(WeatherInterface.FIELDS.APPID, appId);
Request request = builder.build();

return chain.proceed(request);
Expand Down Expand Up @@ -94,11 +92,11 @@ public Response intercept(Chain chain) throws IOException {
}
}

public WeatherInterface getApi(Context currContext) {
public WeatherInterface getApi(String appId) {
if (uniqInstance == null) {
getInstance();
}
uniqInstance.ApiClient(currContext);
uniqInstance.ApiClient(appId);

return weatherInterface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
public interface WeatherInterface {

@GET("weather")
Call<WeatherResponseModel> getCityWeather(@Query("appid") String appid,
@Query("q") String city);
Call<WeatherResponseModel> getCityWeather(@Query(FIELDS.Q) String city);

@GET("weather")
Call<WeatherResponseModel> getLocationWeather(@Query("appid") String appid,
@Query("lat") String latitude,
@Query("lon") String longitude);
Call<WeatherResponseModel> getLocationWeather(@Query(FIELDS.LAT) String latitude,
@Query(FIELDS.LON) String longitude);

@GET("forecast")
Call<ForecastResponseModel> getCityForcast(@Query("appid") String appid,
@Query("q") String city);
Call<ForecastResponseModel> getCityForcast(@Query(FIELDS.Q) String city);

@GET("forecast")
Call<ForecastResponseModel> getLocationForecast(@Query("appid") String appid,
@Query("lat") String latitude,
@Query("lon") String longitude);
Call<ForecastResponseModel> getLocationForecast(@Query(FIELDS.LAT) String latitude,
@Query(FIELDS.LON) String longitude);

interface FIELDS {
String APPID = "appid";
String Q = "q";
String LAT = "lat";
String LON = "lon";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package github.vatsal.easyweather.retrofit.api;

import android.app.Activity;
import android.content.Context;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand All @@ -11,16 +8,6 @@
* Created by nuhbye on 07/03/16.
*/
public abstract class WeatherRetrofitCallback<S> implements Callback {
Activity activity;
Context context;

public WeatherRetrofitCallback(Activity activity) {
this.activity = activity;
}

public WeatherRetrofitCallback(Context context) {
this.context = context;
}

@Override
public void onResponse(Call call, Response response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public void setCountry(String country) {

@Override
public String toString() {
return "ClassPojo [coord = " + coord + ", id = " + id + ", sys = " + sys + ", name = " + name + ", population = " + population + ", country = " + country + "]";
return String.format("ClassPojo [coord = %s, id = %s, sys = %s, name = %s, population = %s, country = %s]", coord, id, sys, name, population, country);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public void setAll(String all) {

@Override
public String toString() {
return "ClassPojo [all = " + all + "]";
return String.format("ClassPojo [all = %s]", all);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public void setLat(String lat) {

@Override
public String toString() {
return "ClassPojo [lon = " + lon + ", lat = " + lat + "]";
return String.format("ClassPojo [lon = %s, lat = %s]", lon, lat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public void setCity(City city) {

@Override
public String toString() {
return "ClassPojo [message = " + message + ", cnt = " + cnt + ", cod = " + cod + ", list = " + list + ", city = " + city + "]";
return String.format("ClassPojo [message = %s, cnt = %s, cod = %s, list = %s, city = %s]", message, cnt, cod, list, city);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ public void setMain(Main main) {

@Override
public String toString() {
return "ClassPojo [clouds = " + clouds + ", dt = " + dt + ", wind = " + wind + ", sys = " + sys + ", weather = " + weather + ", dt_txt = " + dt_txt + ", rain = " + rain + ", main = " + main + "]";
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public void setTemp(String temp) {

@Override
public String toString() {
return "ClassPojo [humidity = " + humidity + ", pressure = " + pressure + ", temp_max = " + temp_max + ", temp_min = " + temp_min + ", temp = " + temp + "]";
return String.format("ClassPojo [humidity = %s, pressure = %s, temp_max = %s, temp_min = %s, temp = %s]", humidity, pressure, temp_max, temp_min, temp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ public void setCountry(String country) {

@Override
public String toString() {
return "ClassPojo [message = " + message + ", id = " + id + ", sunset = " + sunset + ", sunrise = " + sunrise + ", type = " + type + ", country = " + country + "]";
return String.format("ClassPojo [message = %s, id = %s, sunset = %s, sunrise = %s, type = %s, country = %s]", message, id, sunset, sunrise, type, country);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* --OpenWeatherMapDemo
*/
public class Weather {
private static final String HTTP_OPENWEATHERMAP_ORG_IMG_W = "http://openweathermap.org/img/w/";

private String id;

private String icon;
Expand All @@ -30,7 +32,7 @@ public String getIcon() {
}

public String getIconLink() {
return "http://openweathermap.org/img/w/" + icon + ".png";
return String.format("%s%s.png", HTTP_OPENWEATHERMAP_ORG_IMG_W, icon);
}

public void setIcon(String icon) {
Expand All @@ -55,6 +57,6 @@ public void setMain(String main) {

@Override
public String toString() {
return "ClassPojo [id = " + id + ", icon = " + icon + ", description = " + description + ", main = " + main + "]";
return String.format("ClassPojo [id = %s, icon = %s, description = %s, main = %s]", id, icon, description, main);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github.vatsal.easyweather.retrofit.models;

import java.io.Serializable;
import java.util.Arrays;

/**
* Created by
Expand Down Expand Up @@ -134,6 +135,7 @@ public void setMain(Main main) {

@Override
public String toString() {
return "ClassPojo [id = " + id + ", dt = " + dt + ", clouds = " + clouds + ", coord = " + coord + ", wind = " + wind + ", cod = " + cod + ", sys = " + sys + ", name = " + name + ", base = " + base + ", weather = " + weather + ", rain = " + rain + ", main = " + main + "]";
return String.format("ClassPojo [id = %s, dt = %s, clouds = %s, coord = %s, wind = %s, cod = %s, sys = %s, name = %s, base = %s, weather = %s, rain = %s, main = %s]",
id, dt, clouds, coord, wind, cod, sys, name, base, Arrays.toString(weather), rain, main);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public void setDeg(String deg) {

@Override
public String toString() {
return "ClassPojo [speed = " + speed + ", deg = " + deg + "]";
return String.format("ClassPojo [speed = %s, deg = %s]", speed, deg);
}
}