-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bd390b
commit 5136eda
Showing
9 changed files
with
236 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
.../java/com/survivingwithandroid/weather/lib/demo15/fragment/HistoricalWeatherFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package com.survivingwithandroid.weather.lib.demo15.fragment; | ||
|
||
|
||
import android.app.Fragment; | ||
import android.os.Bundle; | ||
|
||
|
||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.survivingwithandroid.weather.lib.WeatherClient; | ||
import com.survivingwithandroid.weather.lib.WeatherConfig; | ||
import com.survivingwithandroid.weather.lib.demo15.R; | ||
import com.survivingwithandroid.weather.lib.exception.WeatherLibException; | ||
import com.survivingwithandroid.weather.lib.exception.WeatherProviderInstantiationException; | ||
import com.survivingwithandroid.weather.lib.model.HistoricalHourWeather; | ||
import com.survivingwithandroid.weather.lib.model.HistoricalWeather; | ||
import com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType; | ||
import com.survivingwithandroid.weather.lib.request.WeatherRequest; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
|
||
public class HistoricalWeatherFragment extends Fragment { | ||
|
||
private RecyclerView rv; | ||
private WeatherClient client; | ||
private RecyclerWeatherAdapter rwa; | ||
|
||
public HistoricalWeatherFragment() { | ||
} | ||
|
||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
WeatherClient.ClientBuilder builder = new WeatherClient.ClientBuilder(); | ||
WeatherConfig config = new WeatherConfig(); | ||
|
||
try { | ||
client = builder.attach(getActivity()) | ||
.provider(new OpenweathermapProviderType()) | ||
.httpClient(com.survivingwithandroid.weather.lib.client.volley.WeatherClientDefault.class) | ||
.config(config) | ||
.build(); | ||
} catch (WeatherProviderInstantiationException wpie) { | ||
wpie.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
View v = inflater.inflate(R.layout.fragment_historical_weather, container, false); | ||
rv = (RecyclerView) v.findViewById(R.id.weather_recycler_view); | ||
rv.setLayoutManager(new LinearLayoutManager(getActivity())); | ||
rwa = new RecyclerWeatherAdapter(null); | ||
rv.setAdapter(rwa); | ||
return v; | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
|
||
Calendar c1 = Calendar.getInstance(); | ||
c1.clear(); | ||
//c1.set(2014,Calendar.JANUARY,1,0,0,0); | ||
c1.set(2014, Calendar.JULY, 6,0,0,0); | ||
|
||
Date d1 = c1.getTime(); | ||
|
||
Calendar c2 = Calendar.getInstance(); | ||
c2.clear(); | ||
// c2.set(2014,Calendar.JANUARY, 2,0,0,0); | ||
c2.set(2014, Calendar.JULY, 7,0,0,0); | ||
Date d2 = c2.getTime(); | ||
|
||
//client.getHistoricalWeather(new WeatherRequest("2885679"), d1, d2, new WeatherClient.HistoricalWeatherEventListener() { | ||
client.getHistoricalWeather(new WeatherRequest(56.0499F, 12.7067F), d1, d2, new WeatherClient.HistoricalWeatherEventListener() { | ||
@Override | ||
public void onWeatherRetrieved(HistoricalWeather histWeather) { | ||
|
||
List<HistoricalHourWeather> histData = histWeather.getHoistoricalData(); | ||
Log.d("Hist", "Data ["+histData+"] - Size ["+histData.size()+"]"); | ||
rwa.setData(histData); | ||
rwa.notifyDataSetChanged(); | ||
|
||
rv.setAdapter(rwa); | ||
} | ||
|
||
@Override | ||
public void onWeatherError(WeatherLibException wle) { | ||
Toast.makeText(getActivity(), "Error parsing the response", Toast.LENGTH_SHORT).show(); | ||
wle.printStackTrace(); | ||
} | ||
|
||
@Override | ||
public void onConnectionError(Throwable t) { | ||
Toast.makeText(getActivity(), "Connection error", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
|
||
} | ||
|
||
|
||
public static class RecyclerWeatherAdapter extends RecyclerView.Adapter<RecyclerWeatherAdapter.WeatherViewHolder> { | ||
|
||
private List<HistoricalHourWeather> histData; | ||
private SimpleDateFormat sdf = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm"); | ||
|
||
public RecyclerWeatherAdapter(List<HistoricalHourWeather> histData) { | ||
this.histData = histData; | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(WeatherViewHolder weatherViewHolder, int i) { | ||
HistoricalHourWeather hhw = histData.get(i); | ||
Calendar c = Calendar.getInstance(); | ||
c.setTimeInMillis(hhw.timestamp); | ||
String d = sdf.format(c.getTime()); | ||
weatherViewHolder.tvDate.setText(d); | ||
weatherViewHolder.tvMinTemp.setText(String.valueOf(hhw.weather.temperature.getMinTemp())); | ||
weatherViewHolder.tvMaxTemp.setText(String.valueOf(hhw.weather.temperature.getMaxTemp())); | ||
} | ||
|
||
@Override | ||
public WeatherViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { | ||
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hist_row, null); | ||
WeatherViewHolder wvh = new WeatherViewHolder(v); | ||
return wvh; | ||
} | ||
|
||
public static class WeatherViewHolder extends RecyclerView.ViewHolder { | ||
protected TextView tvDate; | ||
protected TextView tvMinTemp; | ||
protected TextView tvMaxTemp; | ||
|
||
public WeatherViewHolder(View v) { | ||
super(v); | ||
this.tvDate = (TextView) v.findViewById(R.id.hist_date); | ||
this.tvMinTemp = (TextView) v.findViewById(R.id.hist_min_temp); | ||
this.tvMaxTemp = (TextView) v.findViewById(R.id.hist_max_temp); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public int getItemCount() { | ||
return histData == null ? 0 : histData.size(); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return histData == null ? 0 : histData.get(position).hashCode(); | ||
} | ||
|
||
public void setData(List<HistoricalHourWeather> histData) { | ||
this.histData = histData; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@android:id/text1"/> |
13 changes: 13 additions & 0 deletions
13
demo15/src/main/res/layout/fragment_historical_weather.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".fragment.HistoricalWeather"> | ||
|
||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/weather_recycler_view" | ||
android:scrollbars="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
|
||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/hist_date" | ||
android:textColor="@android:color/white" | ||
android:background="@android:color/holo_blue_dark"/> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/hist_min_temp" | ||
android:textColor="@android:color/holo_green_dark" | ||
android:layout_below="@id/hist_date" | ||
android:layout_marginTop="5dp"/> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/hist_max_temp" | ||
android:textColor="@android:color/holo_red_dark" | ||
android:layout_below="@id/hist_date" | ||
android:layout_marginLeft="5dp" | ||
android:layout_toRightOf="@id/hist_min_temp" | ||
android:layout_alignBaseline="@id/hist_min_temp"/> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="AppTheme" parent="android:Theme.Material.Light"> | ||
</style> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters