Skip to content

Commit

Permalink
Added #882 - Weather feature overlay
Browse files Browse the repository at this point in the history
Signed-off-by: Amr Hossam <[email protected]>
  • Loading branch information
amrhossamdev authored and aaronbrethorst committed Mar 22, 2024
1 parent e36d188 commit 3df18c7
Show file tree
Hide file tree
Showing 32 changed files with 752 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.onebusaway.android.io.test;

import static androidx.test.InstrumentationRegistry.getTargetContext;
import static junit.framework.Assert.assertNotNull;

import android.content.ContentResolver;
import android.net.Uri;

import org.junit.Test;
import org.onebusaway.android.R;
import org.onebusaway.android.io.elements.ObaRegion;
import org.onebusaway.android.io.request.ObaRegionsRequest;
import org.onebusaway.android.io.request.ObaRegionsResponse;
import org.onebusaway.android.io.request.weather.ObaWeatherRequest;
import org.onebusaway.android.io.request.weather.models.ObaWeatherResponse;


public class RegionWeatherTest {
@Test
public void testRequest() {

final Uri.Builder builder = new Uri.Builder();
builder.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE);
builder.authority(getTargetContext().getPackageName());
builder.path(String.valueOf(R.raw.regions_v3));

ObaRegionsRequest regionsRequest = ObaRegionsRequest.newRequest(getTargetContext(), builder.build());
ObaRegionsResponse regionsResponse = regionsRequest.call();

final ObaRegion[] list = regionsResponse.getRegions();

for (ObaRegion region : list) {
ObaWeatherRequest weatherRequest= ObaWeatherRequest.newRequest(region.getId());
ObaWeatherResponse weatherResponse = weatherRequest.call();
assertNotNull(weatherResponse);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.onebusaway.android.region.ObaRegionsTask;
import org.onebusaway.android.ui.HomeActivity;
import org.onebusaway.android.ui.LayersSpeedDialAdapter;
import org.onebusaway.android.ui.weather.RegionCallback;
import org.onebusaway.android.util.LocationHelper;
import org.onebusaway.android.util.LocationUtils;
import org.onebusaway.android.util.PermissionUtils;
Expand Down Expand Up @@ -721,6 +722,8 @@ public void showStops(List<ObaStop> stops, ObaReferences refs) {
// Make sure that the stop overlay has been successfully initialized
if (setupStopOverlay() && stops != null) {
mStopOverlay.populateStops(stops, refs);
// When we have stops that means we have a valid region to get the weather
checkRegionWeather(false);
}
}

Expand All @@ -744,12 +747,13 @@ public void notifyOutOfRange() {
//Otherwise, its premature since we don't know the device's relationship to
//available OBA regions or the manually set API region
String serverName = Application.get().getCustomApiUrl();
if (mWarnOutOfRange && (Application.get().getCurrentRegion() != null || !TextUtils
.isEmpty(serverName))) {
if (mWarnOutOfRange && (Application.get().getCurrentRegion() != null || !TextUtils.isEmpty(serverName))) {
if (mRunning && canManageDialog(getActivity())) {
showDialog(MapDialogFragment.OUTOFRANGE_DIALOG);
}
}
// Notify weather view that we are out of range
checkRegionWeather(true);
}

//
Expand All @@ -775,6 +779,7 @@ public void onRegionTaskFinished(boolean currentRegionChanged) {
setMyLocation(true, false);
} else {
zoomToRegion();
checkRegionWeather(false);
}
}
}
Expand Down Expand Up @@ -909,6 +914,21 @@ public void zoomToRegion() {
mMap.animateCamera((CameraUpdateFactory.newLatLngBounds(b, width, height, padding)));
}
}
private RegionCallback regionCallback;

public void setRegionCallback(RegionCallback callback) {
this.regionCallback = callback;
}

public void checkRegionWeather(boolean isOutOfRange) {
// If we have a valid region, callback to home activity to get the weather.
ObaRegion region = Application.get().getCurrentRegion();
boolean isValid = (region != null && mMap != null && !isOutOfRange);

if (regionCallback != null) {
regionCallback.onValidRegion(isValid);
}
}

@Override
public Location getSouthWest() {
Expand Down Expand Up @@ -1339,6 +1359,7 @@ private Dialog createOutOfRangeDialog() {
(dialog, which) -> {
if (mMapFragment != null && mMapFragment.isAdded()) {
mMapFragment.zoomToRegion();
mMapFragment.checkRegionWeather(false);
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.onebusaway.android.io.request.weather;

import android.net.Uri;

import androidx.annotation.NonNull;

import org.onebusaway.android.R;
import org.onebusaway.android.app.Application;
import org.onebusaway.android.io.request.RequestBase;
import org.onebusaway.android.io.request.weather.models.ObaWeatherResponse;

import java.util.concurrent.Callable;


public final class ObaWeatherRequest extends RequestBase implements Callable<ObaWeatherResponse> {

private ObaWeatherRequest(Uri uri) {
super(uri);
}

public static class Builder {

private static Uri URI = null;

public Builder(long regionId) {
String weatherAPIURL = Application.get().getResources().getString(R.string.weather_api_url);

// Replacing param regionID with our current region id.
weatherAPIURL = weatherAPIURL.replace("regionID",String.valueOf(regionId));
URI = Uri.parse(weatherAPIURL);
}

public ObaWeatherRequest build() {
return new ObaWeatherRequest(URI);
}
}

public static ObaWeatherRequest newRequest(long regionId) {
return new Builder(regionId).build();
}

@Override
public ObaWeatherResponse call() {
return call(ObaWeatherResponse.class);
}

@NonNull
@Override
public String toString() {
return "ObaWeatherRequest [mUri=" + mUri + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.onebusaway.android.io.request.weather;

import org.onebusaway.android.io.request.weather.models.ObaWeatherResponse;

public interface WeatherRequestListener {
void onWeatherResponseReceived(ObaWeatherResponse response);
void onWeatherRequestFailed();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.onebusaway.android.io.request.weather;

import android.os.AsyncTask;
import android.util.Log;

import org.onebusaway.android.io.request.weather.models.ObaWeatherResponse;

public class WeatherRequestTask extends AsyncTask<ObaWeatherRequest, Void, ObaWeatherResponse> {
private static final String TAG = "Weather Request";
private WeatherRequestListener mListener;

public WeatherRequestTask(WeatherRequestListener listener) {
mListener = listener;
}

@Override
protected ObaWeatherResponse doInBackground(ObaWeatherRequest... requests) {
try {
return requests[0].call();
} catch (Exception e) {
Log.e(TAG, "Error executing weather request", e);
return null;
}
}

@Override
protected void onPostExecute(ObaWeatherResponse response) {
if (response != null) {
mListener.onWeatherResponseReceived(response);
} else {
mListener.onWeatherRequestFailed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.onebusaway.android.io.request.weather.models;

public class CurrentForecast {
public String icon;
public double precip_per_hour;
public double precip_probability;
public String summary;
public double temperature;
public double temperature_feels_like;
public int time;
public double wind_speed;

public String getIcon() {
return icon;
}

public double getPrecip_per_hour() {
return precip_per_hour;
}

public double getPrecip_probability() {
return precip_probability;
}

public String getSummary() {
return summary;
}

public double getTemperature() {
return temperature;
}

public double getTemperature_feels_like() {
return temperature_feels_like;
}

public int getTime() {
return time;
}

public double getWind_speed() {
return wind_speed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.onebusaway.android.io.request.weather.models;

public class HourlyForecast {

private String icon;
private double precip_per_hour;
private double precip_probability;
private String summary;
private double temperature;
private double temperature_feels_like;
private int time;
private double wind_speed;

public String getIcon() {
return icon;
}

public double getPrecip_per_hour() {
return precip_per_hour;
}

public double getPrecip_probability() {
return precip_probability;
}

public String getSummary() {
return summary;
}

public double getTemperature() {
return temperature;
}

public double getTemperature_feels_like() {
return temperature_feels_like;
}

public int getTime() {
return time;
}

public double getWind_speed() {
return wind_speed;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.onebusaway.android.io.request.weather.models;

import java.util.List;

public class ObaWeatherResponse {

private CurrentForecast current_forecast;
private List<HourlyForecast> hourly_forecast;
private double latitude;
private double longitude;
private int region_identifier;
private String region_name;
private String retrieved_at;
private String today_summary;
private String units;

public CurrentForecast getCurrent_forecast() {
return current_forecast;
}

public List<HourlyForecast> getHourly_forecast() {
return hourly_forecast;
}


public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

public int getRegion_identifier() {
return region_identifier;
}


public String getRegion_name() {
return region_name;
}


public String getRetrieved_at() {
return retrieved_at;
}


public String getToday_summary() {
return today_summary;
}


public String getUnits() {
return units;
}

@Override
public String toString() {
return "ObaWeatherResponse{" +
"current_forecast=" + current_forecast +
", hourly_forecast=" + hourly_forecast +
", latitude=" + latitude +
", longitude=" + longitude +
", region_identifier=" + region_identifier +
", region_name='" + region_name + '\'' +
", retrieved_at='" + retrieved_at + '\'' +
", today_summary='" + today_summary + '\'' +
", units='" + units + '\'' +
'}';
}
}
Loading

0 comments on commit 3df18c7

Please sign in to comment.