This commit is contained in:
aminecmi 2015-07-12 13:00:16 +02:00
parent 6fee719c3b
commit 65b45bc19c
27 changed files with 101 additions and 115 deletions

View File

@ -12,6 +12,7 @@ import android.widget.Toast;
import com.amine.myterio.app.adapters.ForecastAdapter;
import com.amine.myterio.app.api.WeatherAdapters;
import com.amine.myterio.app.api.WeatherApis;
import com.amine.myterio.app.config.Config;
import com.amine.myterio.app.model.City;
import com.amine.myterio.app.model.Forecast;
import retrofit.Callback;
@ -39,7 +40,7 @@ public class DetailsActivity extends AppCompatActivity {
if (cityName != null) {
WeatherApis.WeatherDailyForecastLocationApi s = adapters.getWeatherForecastLocationAdapter();
s.cityForecast(cityName, new Callback<Forecast>() {
s.cityForecast(cityName, Config.country, new Callback<Forecast>() {
@Override
public void success(Forecast forecast, Response response) {
f[0] = forecast;
@ -49,12 +50,12 @@ public class DetailsActivity extends AppCompatActivity {
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Pas de pr<70>visions pour cette ville.", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show();
}
});
WeatherApis.WeatherLocationApi sWeather = adapters.getWeatherLocationAdapter();
sWeather.locationWeather(cityName, new Callback<City>() {
sWeather.locationWeather(cityName, Config.country, new Callback<City>() {
@Override
public void success(City c, Response response) {
ImageView image = (ImageView) findViewById(R.id.weatherImage);
@ -63,7 +64,7 @@ public class DetailsActivity extends AppCompatActivity {
@Override
public void failure(RetrofitError error) {
Toast.makeText(DetailsActivity.this, "Hello toast!", Toast.LENGTH_SHORT).show();
Toast.makeText(DetailsActivity.this, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show();
}
});
} else {
@ -71,7 +72,7 @@ public class DetailsActivity extends AppCompatActivity {
cityName = city.getName();
WeatherApis.WeatherDailyForecastApi s = adapters.getWeatherForecastAdapter();
s.cityForecast(city.getCityIdentifier(), new Callback<Forecast>() {
s.cityForecast(city.getCityIdentifier(), Config.country, new Callback<Forecast>() {
@Override
public void success(Forecast forecast, Response response) {
f[0] = forecast;
@ -80,7 +81,7 @@ public class DetailsActivity extends AppCompatActivity {
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Pas de pr<70>visions pour cette ville.", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), getString(R.string.weather_not_available), Toast.LENGTH_SHORT).show();
}
});
@ -94,7 +95,7 @@ public class DetailsActivity extends AppCompatActivity {
@Override
public void failure(RetrofitError error) {
Toast.makeText(DetailsActivity.this, "Hello toast!", Toast.LENGTH_SHORT).show();
Toast.makeText(DetailsActivity.this, getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show();
}
});
}

View File

@ -1,5 +1,6 @@
package com.amine.myterio.app;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
@ -7,10 +8,12 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.amine.myterio.app.adapters.CitiesAdapter;
import com.amine.myterio.app.config.Config;
import com.amine.myterio.app.db.CityDAO;
import com.amine.myterio.app.model.City;
import com.melnykov.fab.FloatingActionButton;
@ -20,33 +23,23 @@ import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleView();
/* WeatherAdapters adapters = new WeatherAdapters();
WeatherApis.WeatherDailyForecastApi s = adapters.getWeatherForecastAdapter();
s.cityForecast(1851632, new Callback<Forecast>() {
@Override
public void success(Forecast forecast, Response response) {
Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show();
}
});*/
private void saveUserCountry() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Config.country = tm.getSimCountryIso().toLowerCase();
if (Config.country == null || Config.country.equals("") || Config.country.isEmpty())
Config.country = MainActivity.this.getResources().getConfiguration().locale.getCountry().toLowerCase();
}
private void handleView() {
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.list);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list);
saveUserCountry();
// Google way to test is tablet
boolean isTablet = ((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
@ -62,18 +55,12 @@ public class MainActivity extends AppCompatActivity {
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
ArrayList<City> cities = new ArrayList<City>();
ArrayList<City> cities;
CityDAO dao = CityDAO.getInstance(this);
City c = new City("Cairns", 2172797);
City c1 = new City("Moscow", 524901);
dao.insertCity(c);
dao.insertCity(c1);
cities = dao.getAllCities();
mAdapter = new CitiesAdapter(cities, this);
RecyclerView.Adapter mAdapter = new CitiesAdapter(cities, this);
mRecyclerView.setAdapter(mAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

View File

@ -9,9 +9,9 @@ import android.view.View;
import android.widget.*;
import com.amine.myterio.app.api.CitiesAdapters;
import com.amine.myterio.app.api.CitiesApi;
import com.amine.myterio.app.api.Predictions;
import com.amine.myterio.app.config.Config;
import com.amine.myterio.app.model.Countries;
import com.amine.myterio.app.model.Predictions;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

View File

@ -13,6 +13,7 @@ import com.amine.myterio.app.DetailsActivity;
import com.amine.myterio.app.R;
import com.amine.myterio.app.api.WeatherAdapters;
import com.amine.myterio.app.api.WeatherApis;
import com.amine.myterio.app.config.Config;
import com.amine.myterio.app.model.City;
import retrofit.Callback;
import retrofit.RetrofitError;
@ -21,19 +22,18 @@ import retrofit.client.Response;
import java.util.ArrayList;
public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder> {
private static ArrayList<City> mDataset;
private static Context c;
private ArrayList<City> mDataset;
private Context c;
public CitiesAdapter(ArrayList<City> cities, Context c) {
mDataset = cities;
this.mDataset = cities;
this.c = c;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.city_card_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
return new ViewHolder(v);
}
@Override
@ -44,7 +44,7 @@ public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder
WeatherAdapters adapters = new WeatherAdapters();
WeatherApis.WeatherCityApi s = adapters.getWeatherCityAdapter();
s.cityWeather(currentCity[0].getCityIdentifier(), new Callback<City>() {
s.cityWeather(currentCity[0].getCityIdentifier(), Config.country, new Callback<City>() {
@Override
public void success(City city, Response response) {
currentCity[0] = city;
@ -71,7 +71,7 @@ public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder
@Override
public void failure(RetrofitError error) {
Toast.makeText(c, "Hello toast!", Toast.LENGTH_SHORT).show();
Toast.makeText(c, c.getString(R.string.weather_get_error), Toast.LENGTH_SHORT).show();
}
});
}
@ -82,7 +82,7 @@ public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder
}
public class ViewHolder extends RecyclerView.ViewHolder implements RecyclerView.OnClickListener {
public View mCardView;
public final View mCardView;
public ViewHolder(View itemView) {
super(itemView);

View File

@ -15,9 +15,8 @@ import java.text.SimpleDateFormat;
import java.util.Date;
public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHolder> {
private static Forecast forecast;
private static Context c;
private Forecast forecast;
private Context c;
public ForecastAdapter(Context c, Forecast forecast) {
this.forecast = forecast;
this.c = c;
@ -27,8 +26,7 @@ public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHo
@Override
public ForecastAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
return new ViewHolder(v);
}
@Override
@ -52,7 +50,7 @@ public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHo
}
public class ViewHolder extends RecyclerView.ViewHolder {
public View itemView;
public final View itemView;
public ViewHolder(View itemView) {
super(itemView);

View File

@ -3,7 +3,7 @@ package com.amine.myterio.app.api;
import retrofit.RestAdapter;
public class CitiesAdapters {
RestAdapter restAdapter;
private final RestAdapter restAdapter;
public CitiesAdapters() {
this.restAdapter = new RestAdapter.Builder()

View File

@ -1,5 +1,6 @@
package com.amine.myterio.app.api;
import com.amine.myterio.app.model.Predictions;
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;

View File

@ -3,7 +3,7 @@ package com.amine.myterio.app.api;
import retrofit.RestAdapter;
public class WeatherAdapters {
RestAdapter restAdapter;
private final RestAdapter restAdapter;
public WeatherAdapters() {

View File

@ -9,19 +9,19 @@ import retrofit.http.Query;
public class WeatherApis {
public interface WeatherLocationApi {
@GET("/data/2.5/weather")
void locationWeather(@Query("q") String location, Callback<City> cb);
void locationWeather(@Query("q") String location, @Query("lang") String lang, Callback<City> cb);
}
public interface WeatherCityApi {
@GET("/data/2.5/weather")
void cityWeather(@Query("id") int city, Callback<City> cb);
void cityWeather(@Query("id") int city, @Query("lang") String lang, Callback<City> cb);
}
public interface WeatherDailyForecastApi {
@GET("/data/2.5/forecast/daily?units=metric&cnt=7")
void cityForecast(@Query("id") int city, Callback<Forecast> cb);
void cityForecast(@Query("id") int city, @Query("lang") String lang, Callback<Forecast> cb);
}
public interface WeatherDailyForecastLocationApi {
@GET("/data/2.5/forecast/daily?units=metric&cnt=7")
void cityForecast(@Query("q") String location, Callback<Forecast> cb);
void cityForecast(@Query("q") String location, @Query("lang") String lang, Callback<Forecast> cb);
}
}

View File

@ -1,5 +1,6 @@
package com.amine.myterio.app.config;
public class Config {
public static String placesApiKey = "AIzaSyDxSXMcZK1Ybjkz-IuN2EhhFCwRSbaianE";
public static final String placesApiKey = "AIzaSyDxSXMcZK1Ybjkz-IuN2EhhFCwRSbaianE";
public static String country = "FR";
}

View File

@ -37,11 +37,11 @@ public class CityDAO {
}
}
public void open() {
private void open() {
bdd = maBaseSQLite.getWritableDatabase();
}
public void close() {
private void close() {
bdd.close();
}

View File

@ -4,7 +4,7 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CityDB extends SQLiteOpenHelper {
class CityDB extends SQLiteOpenHelper {
private static final String TABLE_CITIES = "cities";
private static final String COL_ID = "ID";

View File

@ -18,11 +18,11 @@ public class City implements Parcelable {
return new City[size];
}
};
public String name;
public int id;
public MainInfo main;
public List<Weather> weather;
public Wind wind;
private String name;
private int id;
private MainInfo main;
private List<Weather> weather;
private Wind wind;
public City(String name, int id) {
this.name = name;
@ -38,7 +38,7 @@ public class City implements Parcelable {
this.name = description;
}
public City(Parcel source) {
private City(Parcel source) {
this.name = source.readString();
this.id = source.readInt();
this.main = source.readParcelable(MainInfo.class.getClassLoader());

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
public class Countries {
public List<Country> countries;
private List<Country> countries;
public Countries() {
countries = new ArrayList<Country>();

View File

@ -3,9 +3,9 @@ package com.amine.myterio.app.model;
import java.util.ArrayList;
import java.util.List;
public class Country {
public String name;
public List<City> cities;
class Country {
private String name;
private List<City> cities;
public Country() {
cities = new ArrayList<City>();

View File

@ -3,8 +3,8 @@ package com.amine.myterio.app.model;
import java.util.List;
public class Forecast {
public City city;
public List<ForecastWeather> list;
private City city;
private List<ForecastWeather> list;
public City getCity() {
return city;

View File

@ -3,10 +3,10 @@ package com.amine.myterio.app.model;
import java.util.List;
public class ForecastWeather {
public TempInfo temp;
public List<Weather> weather;
public float speed;
public int dt;
private TempInfo temp;
private List<Weather> weather;
private float speed;
private int dt;
public TempInfo getTemp() {
return temp;

View File

@ -15,9 +15,9 @@ public class MainInfo implements Parcelable {
return new MainInfo[size];
}
};
public float temp;
public float temp_max;
public float temp_min;
private float temp;
private float temp_max;
private float temp_min;
public MainInfo(Parcel source) {
this.temp = source.readFloat();

View File

@ -1,9 +1,7 @@
package com.amine.myterio.app.api;
import com.amine.myterio.app.model.City;
package com.amine.myterio.app.model;
public class PredictionCities {
public String description;
private String description;
public String getDescription() {
return description;

View File

@ -1,10 +1,10 @@
package com.amine.myterio.app.api;
package com.amine.myterio.app.model;
import java.util.ArrayList;
import java.util.List;
public class Predictions {
public List<PredictionCities> predictions;
private List<PredictionCities> predictions;
public List<PredictionCities> getPredictions() {
return predictions;

View File

@ -1,12 +1,12 @@
package com.amine.myterio.app.model;
public class TempInfo {
public float day;
public float min;
public float max;
public float night;
public float eve;
public float morn;
private float day;
private float min;
private float max;
private float night;
private float eve;
private float morn;
public float getDay() {
return day;

View File

@ -4,10 +4,10 @@ import android.content.Context;
import android.graphics.drawable.Drawable;
public class Weather {
public String description;
public String icon;
public int id;
public String main;
private String description;
private String icon;
private int id;
private String main;
public String getDescription() {
return description;
@ -17,10 +17,14 @@ public class Weather {
this.description = description;
}
public String getIcon() {
private String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Drawable getIconDrawable(Context c, String packageName) {
String uri = "@mipmap/";
@ -77,10 +81,6 @@ public class Weather {
return c.getResources().getDrawable(imageResource);
}
public void setIcon(String icon) {
this.icon = icon;
}
public int getId() {
return id;
}

View File

@ -1,8 +1,8 @@
package com.amine.myterio.app.model;
public class Wind {
public float deg;
public float speed;
private float deg;
private float speed;
public float getDeg() {
return deg;

View File

@ -19,6 +19,7 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout
@ -35,7 +36,6 @@
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
@ -45,7 +45,6 @@
android:layout_alignLeft="@+id/max_temp"
android:layout_alignStart="@+id/max_temp"
android:layout_alignTop="@+id/imageView2"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
@ -55,7 +54,6 @@
android:layout_alignTop="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<ImageView
@ -86,7 +84,6 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<ImageView
@ -118,7 +115,6 @@
android:layout_below="@+id/weather_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
@ -128,7 +124,6 @@
android:layout_above="@+id/weather_image"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<ImageView

View File

@ -0,0 +1,9 @@
<resources>
<string name="app_name">Myterio</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_search">SearchActivity</string>
<string name="title_activity_details">DetailsActivity</string>
<string name="weather_not_available">This location dosn\'t have any weather.</string>
<string name="weather_get_error">Can\'t get the weather from the API.</string>
</resources>

View File

@ -1,6 +0,0 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -1,7 +1,9 @@
<resources>
<string name="app_name">Myterio</string>
<string name="app_name" translatable="false">Myterio</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_search">SearchActivity</string>
<string name="title_activity_details">DetailsActivity</string>
<string name="title_activity_search" translatable="false">SearchActivity</string>
<string name="title_activity_details" translatable="false">DetailsActivity</string>
<string name="weather_not_available">Pas de prévisions pour cette ville.</string>
<string name="weather_get_error">Erreur lors de la récupération des prévisions</string>
</resources>