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 @@ -14,7 +14,7 @@
import com.androidsx.rainnotifications.backgroundservice.util.NotificationHelper;
import com.androidsx.rainnotifications.backgroundservice.util.UserLocationFetcher;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientException;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientResponseListener;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientHourlyResponseListener;
import com.androidsx.rainnotifications.model.Alert;
import com.androidsx.rainnotifications.model.Day;
import com.androidsx.rainnotifications.model.DayTemplate;
Expand Down Expand Up @@ -59,7 +59,7 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
UserLocationFetcher.getUserLocation(this, new UserLocationFetcher.UserLocationResultListener() {
@Override
public void onLocationSuccess(Location location) {
WeatherClientFactory.requestForecastForLocation(getApplicationContext(), location.getLatitude(), location.getLongitude(), new WeatherClientResponseListener() {
WeatherClientFactory.requestHourlyForecastForLocation(getApplicationContext(), location.getLatitude(), location.getLongitude(), new WeatherClientHourlyResponseListener() {
@Override
public void onForecastSuccess(ForecastTable forecastTable) {
if (intent != null && intent.getIntExtra(Constants.Extras.EXTRA_DAY_ALARM, 0) == Constants.Alarms.DAY_ALARM_ID) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
Expand All @@ -28,10 +30,12 @@
import com.androidsx.rainnotifications.backgroundservice.util.UserLocationFetcher;
import com.androidsx.rainnotifications.dailyclothes.R;
import com.androidsx.rainnotifications.dailyclothes.model.Clothes;
import com.androidsx.rainnotifications.dailyclothes.model.MockDailyForecast;
import com.androidsx.rainnotifications.dailyclothes.ui.widget.customfont.CustomTextView;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientDailyResponseListener;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientException;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientResponseListener;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientHourlyResponseListener;
import com.androidsx.rainnotifications.model.DailyForecast;
import com.androidsx.rainnotifications.model.DailyForecastTable;
import com.androidsx.rainnotifications.model.Day;
import com.androidsx.rainnotifications.model.DayTemplate;
import com.androidsx.rainnotifications.model.DayTemplateLoaderFactory;
Expand All @@ -45,6 +49,7 @@
import com.squareup.picasso.Picasso;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.Duration;

import java.lang.reflect.Field;
Expand All @@ -63,6 +68,7 @@ public class HomeActivity extends FragmentActivity {
private static final long HEART_BUTTON_ANIMATION_DURATION = 200;
private static final int MAX_FORECAST_ITEMS = 24;
private static final int COLOR_TRANSITION_DURATION = 100;
private static final int WEEK_FORECAST_DAYS = 5;

private enum ForecastDataState {LOADING, ERROR_LOCATION, ERROR_FORECAST, LOADED, DONE};

Expand All @@ -86,6 +92,8 @@ public float getScrollValue() {
private ForecastTable forecastTable;
private Day day;
private String forecastSummaryMessage;
private String city;
private DailyForecastTable dailyForecastTable;

private boolean activityDestroyed = false; // Panic mode. It's used for not modify any view.
private View frameLoading;
Expand All @@ -105,6 +113,9 @@ public float getScrollValue() {
private LinearLayout hourlyLinear;
private HorizontalScrollView hourlyScroll;

private DailyForecastAdapter dailyAdapter;
private CustomTextView cityLabel;

private Integer todayCollapsedBackgroundColor;
private Integer todayCollapsedPrimaryColor;
private Integer todayCollapsedSecondaryColor;
Expand Down Expand Up @@ -180,14 +191,21 @@ private void setForecastDataState(ForecastDataState newState) {
private void getForecastData() {
// FIXME: we do exactly the same in the weather service. grr..
UserLocationFetcher.getUserLocation(this, new UserLocationFetcher.UserLocationResultListener() {

private boolean hourlyDone = false;
private boolean dailyDone = false;

@Override
public void onLocationSuccess(final Location location) {
WeatherClientFactory.requestForecastForLocation(HomeActivity.this, location.getLatitude(), location.getLongitude(), new WeatherClientResponseListener() {

// FIXME: This happens on UI Thread and skips frames.
HomeActivity.this.city = UserLocationFetcher.getLocationAddress(HomeActivity.this, location.getLatitude(), location.getLongitude());

WeatherClientFactory.requestHourlyForecastForLocation(HomeActivity.this, location.getLatitude(), location.getLongitude(), new WeatherClientHourlyResponseListener() {
@Override
public void onForecastSuccess(ForecastTable forecastTable) {

// FIXME: This happens on UI Thread and skips frames.

HomeActivity.this.forecastTable = forecastTable;
HomeActivity.this.forecastTableTime = new DateTime();
HomeActivity.this.day = new Day(forecastTable);
Expand All @@ -199,12 +217,28 @@ public void onForecastSuccess(ForecastTable forecastTable) {
HomeActivity.this.forecastSummaryMessage = template.resolveMessage(HomeActivity.this, HomeActivity.this.day);
}

setForecastDataState(ForecastDataState.LOADED);
hourlyDone = true;
checkBothRequestDone();
}

@Override
public void onForecastFailure(WeatherClientException exception) {
Timber.e(exception, "Failed to get the forecast");
Timber.e(exception, "Failed to get hourly forecast");
setForecastDataState(ForecastDataState.ERROR_FORECAST);
}
});

WeatherClientFactory.requestDailyForecastForLocation(HomeActivity.this, location.getLatitude(), location.getLongitude(), new WeatherClientDailyResponseListener() {
@Override
public void onForecastSuccess(DailyForecastTable dailyForecastTable) {
HomeActivity.this.dailyForecastTable = dailyForecastTable;
dailyDone = true;
checkBothRequestDone();
}

@Override
public void onForecastFailure(WeatherClientException weatherClientException) {
Timber.e(weatherClientException, "Failed to get daily forecast");
setForecastDataState(ForecastDataState.ERROR_FORECAST);
}
});
Expand All @@ -215,6 +249,12 @@ public void onLocationFailure(UserLocationFetcher.UserLocationException exceptio
Timber.e(exception, "Failed to get the location");
setForecastDataState(ForecastDataState.ERROR_LOCATION);
}

private void checkBothRequestDone() {
if(hourlyDone && dailyDone) {
setForecastDataState(ForecastDataState.LOADED);
}
}
});
}

Expand All @@ -237,6 +277,7 @@ private void setupUI() {
heartButton = findViewById(R.id.heart_button);
hourlyLinear = (LinearLayout) findViewById(R.id.hourly_forecast);
hourlyScroll = (HorizontalScrollView) findViewById(R.id.hourly_scroll);
cityLabel = (CustomTextView) findViewById(R.id.week_forecast_city);

todayDivider = findViewById(R.id.today_forecast_divider);
todayMinTemperatureIcon = (ImageView) findViewById(R.id.today_min_temp_icon);
Expand Down Expand Up @@ -302,7 +343,8 @@ private void setupClothesViewPager() {

private void setupWeekForecastList() {
ListView weekList = (ListView) findViewById(R.id.week_forecast_list_view);
weekList.setAdapter(new DailyForecastAdapter(getLayoutInflater(), MockDailyForecast.getMockList()));
dailyAdapter = new DailyForecastAdapter(getLayoutInflater(), null);
weekList.setAdapter(dailyAdapter);
weekList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Expand Down Expand Up @@ -351,7 +393,14 @@ private void updateUI() {
maxTemperature.setText(temperatureFormat.format(day.getMaxTemperature().getWeatherWrapper().getTemperature(localeScale)));
slidingPanelSummary.setText(forecastSummaryMessage);
updateHourlyForecastList();
demoPanel();
updateDailyForecastList();

slidingPanel.postDelayed(new Runnable() {
@Override
public void run() {
demoPanel();
}
}, FORECAST_DATA_DONE_DELAY/2);

slidingPanel.postDelayed(new Runnable() {
@Override
Expand Down Expand Up @@ -404,6 +453,11 @@ private void updateHourlyForecastList() {
}
}

private void updateDailyForecastList() {
cityLabel.setText(city.toUpperCase());
dailyAdapter.updateForecast(dailyForecastTable.getDailyForecastList());
}

private int getWeatherIcon(WeatherType type) {
// TODO: Pensar que hacer con las versiones night.
switch (type) {
Expand Down Expand Up @@ -505,7 +559,7 @@ private void showPanel() {

private void demoPanel() {
animateColors(PanelScrollValue.EXPANDED);
slidingPanel.expandPanel();
slidingPanel.expandPanel(PanelScrollValue.EXPANDED.getScrollValue());
}

private void hidePanel() {
Expand Down Expand Up @@ -582,6 +636,90 @@ public void onPageScrollStateChanged(int state) {
}
}

private class DailyForecastAdapter extends BaseAdapter {
private List<DailyForecast> dailyForecasts;
private LayoutInflater inflater;

public DailyForecastAdapter(LayoutInflater inflater, List<DailyForecast> dailyForecasts) {
this.inflater = inflater;
this.dailyForecasts = dailyForecasts;
}

public void updateForecast(List<DailyForecast> dailyForecasts) {
this.dailyForecasts = dailyForecasts;
notifyDataSetChanged();
}

@Override
public int getCount() {
return dailyForecasts != null ? Math.min(WEEK_FORECAST_DAYS, dailyForecasts.size() - 1) : 0; // First is today
}

@Override
public Object getItem(int position) {
return dailyForecasts.get(position + 1); // First is today
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_daily_forecast, null);
convertView.setTag(new DailyForecastHolder(convertView));
}

((DailyForecastHolder) convertView.getTag()).update(dailyForecasts.get(position + 1)); // First is today

return convertView;
}

private class DailyForecastHolder {
private ImageView icon;
private TextView day;
private TextView minTemperature;
private TextView maxTemperature;

public DailyForecastHolder(View v) {
icon = (ImageView) v.findViewById(R.id.daily_forecast_icon);
day = (TextView) v.findViewById(R.id.daily_forecast_day);
minTemperature = (TextView) v.findViewById(R.id.daily_forecast_min_temperature);
maxTemperature = (TextView) v.findViewById(R.id.daily_forecast_max_temperature);
}

public void update(DailyForecast dailyForecast) {
icon.setImageResource(getWeatherIcon(dailyForecast.getWeatherWrapper().getWeatherType()));
day.setText(getDayOfWeek(dailyForecast.getDay()));
minTemperature.setText(temperatureFormat.format(dailyForecast.getWeatherWrapper().getMinTemperature(localeScale)));
maxTemperature.setText(temperatureFormat.format(dailyForecast.getWeatherWrapper().getMaxTemperature(localeScale)));
}
}
}

private String getDayOfWeek(DateTime day) {
switch (day.getDayOfWeek()) {
case DateTimeConstants.MONDAY:
return getString(R.string.day_monday);
case DateTimeConstants.TUESDAY:
return getString(R.string.day_tuesday);
case DateTimeConstants.WEDNESDAY:
return getString(R.string.day_wednesday);
case DateTimeConstants.THURSDAY:
return getString(R.string.day_thursday);
case DateTimeConstants.FRIDAY:
return getString(R.string.day_friday);
case DateTimeConstants.SATURDAY:
return getString(R.string.day_saturday);
case DateTimeConstants.SUNDAY:
return getString(R.string.day_sunday);
default:
return ""; // Impossible
}
}

/** Linked from the XML. */
public void onErrorRetry(View v) {
if(dataState.equals(ForecastDataState.ERROR_LOCATION)) {
Expand Down
8 changes: 8 additions & 0 deletions dailyclothes/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
<string name="default_day_message">Ops! There has been an error. Please check your internet connection and if the problem persists contact us at weatherup-support@androidsx.com</string>
<string name="week_forecast_title">5-DAY FORECAST</string>
<string name="temperature_symbol">°</string>

<string name="day_monday">MONDAY</string>
<string name="day_tuesday">TUESDAY</string>
<string name="day_wednesday">WEDNESDAY</string>
<string name="day_thursday">THURSDAY</string>
<string name="day_friday">FRIDAY</string>
<string name="day_saturday">SATURDAY</string>
<string name="day_sunday">SUNDAY</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import com.androidsx.rainnotifications.R;
import com.androidsx.rainnotifications.alert.AlertGenerator;
import com.androidsx.rainnotifications.alert.DayTemplateGenerator;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientHourlyResponseListener;
import com.androidsx.rainnotifications.model.DayTemplateLoaderFactory;
import com.androidsx.rainnotifications.backgroundservice.util.UserLocationFetcher;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientException;
import com.androidsx.rainnotifications.forecastapislibrary.WeatherClientResponseListener;
import com.androidsx.rainnotifications.model.ForecastTable;
import com.androidsx.rainnotifications.ui.debug.DebugActivity;
import com.androidsx.rainnotifications.ui.welcome.BaseWelcomeActivity;
Expand Down Expand Up @@ -66,7 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {
UserLocationFetcher.getUserLocation(this, new UserLocationFetcher.UserLocationResultListener() {
@Override
public void onLocationSuccess(final Location location) {
WeatherClientFactory.requestForecastForLocation(MainMobileActivity.this, location.getLatitude(), location.getLongitude(), new WeatherClientResponseListener() {
WeatherClientFactory.requestHourlyForecastForLocation(MainMobileActivity.this, location.getLatitude(), location.getLongitude(), new WeatherClientHourlyResponseListener() {
@Override
public void onForecastSuccess(ForecastTable forecastTable) {
final String locationAddress = UserLocationFetcher.getLocationAddress(
Expand Down
Loading