This commit is contained in:
aminecmi 2015-07-08 21:10:24 +02:00
parent 62f8455e5d
commit 95655c3880
8 changed files with 157 additions and 3 deletions

View File

@ -4,6 +4,15 @@ import android.os.Bundle;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.amine.myterio.app.api.WeatherAdapters;
import com.amine.myterio.app.api.WeatherApis;
import com.amine.myterio.app.model.City;
import com.amine.myterio.app.model.Forecast;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class DetailsActivity extends ActionBarActivity { public class DetailsActivity extends ActionBarActivity {
@ -12,6 +21,49 @@ public class DetailsActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details); setContentView(R.layout.activity_details);
String cityName = getIntent().getExtras().getString("city_name");
WeatherAdapters adapters = new WeatherAdapters();
final Forecast[] f = {null};
if (cityName != null) {
WeatherApis.WeatherDailyForecastLocationApi s = adapters.getWeatherForecastLocationAdapter();
s.cityForecast(cityName, new Callback<Forecast>() {
@Override
public void success(Forecast forecast, Response response) {
f[0] = forecast;
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Pas de prévisions pour cette ville.", Toast.LENGTH_SHORT).show();
}
});
} else {
City city = getIntent().getExtras().getParcelable("city");
cityName = city.getName();
WeatherApis.WeatherDailyForecastApi s = adapters.getWeatherForecastAdapter();
s.cityForecast(city.getCityIdentifier(), new Callback<Forecast>() {
@Override
public void success(Forecast forecast, Response response) {
f[0] = forecast;
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Pas de prévisions pour cette ville.", Toast.LENGTH_SHORT).show();
}
});
}
TextView name = (TextView) findViewById(R.id.cityName);
name.setText(cityName);
} }

View File

@ -47,6 +47,8 @@ public class SearchActivity extends AppCompatActivity {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(SearchActivity.this, DetailsActivity.class); Intent intent = new Intent(SearchActivity.this, DetailsActivity.class);
intent.putExtra("city_name", query);
startActivity(intent); startActivity(intent);
return false; return false;
} }

View File

@ -1,6 +1,7 @@
package com.amine.myterio.app.adapters; package com.amine.myterio.app.adapters;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -8,6 +9,7 @@ import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.amine.myterio.app.DetailsActivity;
import com.amine.myterio.app.R; import com.amine.myterio.app.R;
import com.amine.myterio.app.api.WeatherAdapters; import com.amine.myterio.app.api.WeatherAdapters;
import com.amine.myterio.app.api.WeatherApis; import com.amine.myterio.app.api.WeatherApis;
@ -91,6 +93,10 @@ public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// Go to details activity // Go to details activity
final int position = getLayoutPosition();
Intent intent = new Intent(c, DetailsActivity.class);
intent.putExtra("city", mDataset.get(position));
c.startActivity(intent);
} }
} }
} }

View File

@ -23,4 +23,9 @@ public class WeatherAdapters {
public WeatherApis.WeatherDailyForecastApi getWeatherForecastAdapter() { public WeatherApis.WeatherDailyForecastApi getWeatherForecastAdapter() {
return restAdapter.create(WeatherApis.WeatherDailyForecastApi.class); return restAdapter.create(WeatherApis.WeatherDailyForecastApi.class);
} }
public WeatherApis.WeatherDailyForecastLocationApi getWeatherForecastLocationAdapter() {
return restAdapter.create(WeatherApis.WeatherDailyForecastLocationApi.class);
}
} }

View File

@ -19,4 +19,9 @@ public class WeatherApis {
@GET("/data/2.5/forecast/daily?units=metric&cnt=7") @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, 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);
}
} }

View File

@ -1,9 +1,23 @@
package com.amine.myterio.app.model; package com.amine.myterio.app.model;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.List; import java.util.List;
public class City { public class City implements Parcelable {
public static final Parcelable.Creator<City> CREATOR = new Parcelable.Creator<City>() {
@Override
public City createFromParcel(Parcel source) {
return new City(source);
}
@Override
public City[] newArray(int size) {
return new City[size];
}
};
public String name; public String name;
public int id; public int id;
public MainInfo main; public MainInfo main;
@ -24,6 +38,12 @@ public class City {
this.name = description; this.name = description;
} }
public City(Parcel source) {
this.name = source.readString();
this.id = source.readInt();
this.main = source.readParcelable(MainInfo.class.getClassLoader());
}
public String getName() { public String getName() {
return name; return name;
@ -64,4 +84,16 @@ public class City {
public void setWind(Wind wind) { public void setWind(Wind wind) {
this.wind = wind; this.wind = wind;
} }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeInt(this.id);
dest.writeParcelable(this.main, flags);
}
} }

View File

@ -1,10 +1,30 @@
package com.amine.myterio.app.model; package com.amine.myterio.app.model;
public class MainInfo { import android.os.Parcel;
import android.os.Parcelable;
public class MainInfo implements Parcelable {
public static final Parcelable.Creator<MainInfo> CREATOR = new Parcelable.Creator<MainInfo>() {
@Override
public MainInfo createFromParcel(Parcel source) {
return new MainInfo(source);
}
@Override
public MainInfo[] newArray(int size) {
return new MainInfo[size];
}
};
public float temp; public float temp;
public float temp_max; public float temp_max;
public float temp_min; public float temp_min;
public MainInfo(Parcel source) {
this.temp = source.readFloat();
this.temp_max = source.readFloat();
this.temp_min = source.readFloat();
}
public int getTemp() { public int getTemp() {
return Math.round(temp); return Math.round(temp);
} }
@ -28,4 +48,16 @@ public class MainInfo {
public void setTemp_min(float temp_min) { public void setTemp_min(float temp_min) {
this.temp_min = temp_min; this.temp_min = temp_min;
} }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(this.temp);
dest.writeFloat(this.temp_max);
dest.writeFloat(this.temp_min);
}
} }

View File

@ -9,8 +9,28 @@
tools:context="com.amine.myterio.app.DetailsActivity"> tools:context="com.amine.myterio.app.DetailsActivity">
<TextView <TextView
android:id="@+id/cityName"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/hello_world"/> android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="New Text"/>
<ImageView
android:id="@+id/weatherImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cityName"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<ListView
android:id="@+id/weekForecast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/weatherImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"/>
</RelativeLayout> </RelativeLayout>