Skip to content

Commit

Permalink
Implemented RecyclerView for Hourly Forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Henigan committed Apr 17, 2018
1 parent e9fd75b commit a993c75
Show file tree
Hide file tree
Showing 18 changed files with 664 additions and 31 deletions.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.10.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.android.support:recyclerview-v7:26.1.0'
}
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="com.example.rhenigan.stormy">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand All @@ -12,14 +12,17 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
<activity
android:name=".UI.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UI.DailyForecastActivity" />
<activity android:name=".HourlyForecastActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.rhenigan.stormy;

import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.rhenigan.stormy.UI.MainActivity;
import com.example.rhenigan.stormy.adapters.HourAdapter;
import com.example.rhenigan.stormy.weather.Hour;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HourlyForecastActivity extends AppCompatActivity {

private Hour[] mHours;

@BindView(R.id.recyclerView) RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly_forecast);

ButterKnife.bind(this);

Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
mHours = Arrays.copyOf(parcelables, parcelables.length, Hour[].class);

HourAdapter adapter = new HourAdapter(mHours);
mRecyclerView.setAdapter(adapter);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);

mRecyclerView.setHasFixedSize(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.rhenigan.stormy;
package com.example.rhenigan.stormy.UI;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;

import com.example.rhenigan.stormy.R;

/**
* Created by henig on 4/4/2018.
* ------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.rhenigan.stormy;
package com.example.rhenigan.stormy.UI;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
Expand All @@ -13,13 +14,22 @@
import android.widget.TextView;
import android.widget.Toast;

import com.example.rhenigan.stormy.HourlyForecastActivity;
import com.example.rhenigan.stormy.R;
import com.example.rhenigan.stormy.weather.Current;
import com.example.rhenigan.stormy.weather.Day;
import com.example.rhenigan.stormy.weather.Forecast;
import com.example.rhenigan.stormy.weather.Hour;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand All @@ -29,8 +39,9 @@
public class MainActivity extends AppCompatActivity {

public static final String TAG = "MAIN ACTIVITY";
public static final String HOURLY_FORECAST = "HOURLY_FORECAST";

private CurrentWeather mCurrentWeather;
private Forecast mForecast;

@BindView(R.id.timeLabel) TextView mTimeLabel;
@BindView(R.id.temperatureLabel) TextView mTemperatureLabel;
Expand All @@ -42,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
@BindView(R.id.progressBar) ProgressBar mProgressBar;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -101,7 +113,7 @@ public void run() {
try {
String jsonData = response.body().string();
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
mForecast = parseForecastDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -136,34 +148,93 @@ private void toggleRefresh() {
}

private void updateDisplay() {
mTemperatureLabel.setText(mCurrentWeather.getTemp() + "");
mTimeLabel.setText("At " + mCurrentWeather.getFormattedTime() + " it will be");
mHumidityValue.setText(mCurrentWeather.getHum() + "");
mPrecipValue.setText(mCurrentWeather.getPrecip() + "%");
mSummaryLabel.setText(mCurrentWeather.getSummary() + "");
Drawable drawable = getResources().getDrawable(mCurrentWeather.getIconId());
mTemperatureLabel.setText(mForecast.getCurrent().getTemp() + "");
mTimeLabel.setText("At " + mForecast.getCurrent().getFormattedTime() + " it will be");
mHumidityValue.setText(mForecast.getCurrent().getHum() + "");
mPrecipValue.setText(mForecast.getCurrent().getPrecip() + "%");
mSummaryLabel.setText(mForecast.getCurrent().getSummary() + "");
Drawable drawable = getResources().getDrawable(mForecast.getCurrent().getIconId());
mIconImageView.setImageDrawable(drawable);
}

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
private Forecast parseForecastDetails(String jsonData) throws JSONException{
Forecast forecast = new Forecast();

forecast.setCurrent(getCurrentDetails(jsonData));
forecast.setHourlyForecast(getHourlyForecast(jsonData));
forecast.setDailyForecast(getDailyForecast(jsonData));

return forecast;
}

private Day[] getDailyForecast(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");

JSONObject daily = forecast.getJSONObject("daily");
JSONArray data = daily.getJSONArray("data");

Day[] days = new Day[data.length()];

for(int i = 0; i < data.length(); i++) {
JSONObject jsonDay = data.getJSONObject(i);

Day day = new Day();

day.setSummary(jsonDay.getString("summary"));
day.setIcon(jsonDay.getString("icon"));
day.setTempMax(jsonDay.getDouble("temperatureMax"));
day.setTime(jsonDay.getLong("time"));
day.setTimeZone(timezone);

days[i] = day;
}
return days;
}

private Hour[] getHourlyForecast(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject hourly = forecast.getJSONObject("hourly");
JSONArray data = hourly.getJSONArray("data");

Hour[] hours = new Hour[data.length()];

for(int i = 0; i < data.length(); i++) {
JSONObject jsonHour = data.getJSONObject(i);

Hour hour = new Hour();

hour.setSummary(jsonHour.getString("summary"));
hour.setTemp(jsonHour.getDouble("temperature"));
hour.setIcon(jsonHour.getString("icon"));
hour.setTime(jsonHour.getLong("time"));
hour.setTimeZone(timezone);

hours[i] = hour;
}
return hours;
}

private Current getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
Log.i(TAG, "From Json: " + timezone);

JSONObject currently = forecast.getJSONObject("currently");

CurrentWeather currentWeather = new CurrentWeather();
currentWeather.setHum(currently.getDouble("humidity"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setIcon(currently.getString("icon"));
currentWeather.setPrecip(currently.getDouble("precipProbability"));
currentWeather.setSummary(currently.getString("summary"));
currentWeather.setTemp(currently.getDouble("temperature"));
currentWeather.setTimeZone(timezone);
Current current = new Current();
current.setHum(currently.getDouble("humidity"));
current.setTime(currently.getLong("time"));
current.setIcon(currently.getString("icon"));
current.setPrecip(currently.getDouble("precipProbability"));
current.setSummary(currently.getString("summary"));
current.setTemp(currently.getDouble("temperature"));
current.setTimeZone(timezone);

Log.d(TAG, currentWeather.getFormattedTime());
Log.d(TAG, current.getFormattedTime());

return currentWeather;
return current;
}

private boolean isNetworkAvailable() {
Expand All @@ -180,4 +251,11 @@ private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}

@OnClick (R.id.HourlyButton)
public void startHourlyActivity(View view) {
Intent intent = new Intent(this, HourlyForecastActivity.class);
intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast());
startActivity(intent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.example.rhenigan.stormy.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.rhenigan.stormy.R;
import com.example.rhenigan.stormy.weather.Hour;

/**
* Created by henig on 4/16/2018.
* ------------------------------
*/
public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {

private Hour[] mHours;

public HourAdapter(Hour[] hours) {
mHours = hours;
}

public class HourViewHolder extends RecyclerView.ViewHolder {

public TextView mTimeLabel;
public TextView mSummaryLabel;
public TextView mTempLabel;
public ImageView mIconImageView;

public HourViewHolder(View itemView) {
super(itemView);

mTimeLabel = itemView.findViewById(R.id.timeLabel);
mSummaryLabel = itemView.findViewById(R.id.summaryLabel);
mTempLabel = itemView.findViewById(R.id.tempLabel);
mIconImageView = itemView.findViewById(R.id.iconImageView);
}

public void bindHour(Hour hour) {
mTimeLabel.setText(hour.getHour());
mSummaryLabel.setText(hour.getSummary());
mTempLabel.setText(hour.getTemp() + "");
mIconImageView.setImageResource(hour.getIconId());
}
}

@Override
public HourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hourly_list_item, parent, false);
HourViewHolder viewHolder = new HourViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(HourViewHolder holder, int position) {
holder.bindHour(mHours[position]);
}

@Override
public int getItemCount() {
return mHours.length;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.example.rhenigan.stormy;
package com.example.rhenigan.stormy.weather;

import com.example.rhenigan.stormy.R;

import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -9,7 +11,7 @@
* ------------------------------
*/

public class CurrentWeather {
public class Current {
private String mIcon;
private Long mTime;
private double mTemp;
Expand Down
Loading

0 comments on commit a993c75

Please sign in to comment.