diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java index e97ee79..977a323 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java @@ -25,7 +25,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { private static DatabaseHelper instance; private static final String DB_NAME = "openbikesharing.sqlite"; - private static final int DB_VERSION = 1; + private static final int DB_VERSION = 2; public static final String STATIONS_TABLE_NAME = "stations"; public static final String STATIONS_COLUMN_ID = "id"; @@ -39,6 +39,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { public static final String STATIONS_COLUMN_BANKING = "banking"; public static final String STATIONS_COLUMN_BONUS = "bonus"; public static final String STATIONS_COLUMN_STATUS = "status"; + public static final String STATIONS_COLUMN_EBIKES = "ebikes"; public static final String FAV_STATIONS_TABLE_NAME = "fav_stations"; public static final String FAV_STATIONS_COLUMN_ID = "id"; @@ -61,7 +62,8 @@ public void onCreate(SQLiteDatabase db) { + "(id TEXT PRIMARY KEY, name TEXT NOT NULL, last_update TEXT NOT NULL, " + "latitude NUMERIC NOT NULL, longitude NUMERIC NOT NULL, " + "free_bikes INTEGER NOT NULL, empty_slots INTEGER NOT NULL, " - + "address TEXT, banking INTEGER, bonus INTEGER, status TEXT)" + + "address TEXT, banking INTEGER, bonus INTEGER, status TEXT, " + + "ebikes INTEGER) " ); db.execSQL("CREATE TABLE " + FAV_STATIONS_TABLE_NAME + "(id TEXT PRIMARY KEY)" @@ -71,6 +73,9 @@ public void onCreate(SQLiteDatabase db) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion < 2) { + db.execSQL("ALTER TABLE " + STATIONS_TABLE_NAME + " ADD COLUMN ebikes INTEGER;"); + } } } diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java index db1f84d..7a3dc85 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java @@ -57,7 +57,9 @@ public void storeStations(ArrayList stations) { values.put(DatabaseHelper.STATIONS_COLUMN_BONUS, station.isBonus() ? 1 : 0); if (station.getStatus() != null) values.put(DatabaseHelper.STATIONS_COLUMN_STATUS, station.getStatus().name()); - + if (station.getEBikes() != null) { + values.put(DatabaseHelper.STATIONS_COLUMN_EBIKES, String.valueOf(station.getEBikes())); + } db.insert(DatabaseHelper.STATIONS_TABLE_NAME, null, values); } db.setTransactionSuccessful(); @@ -75,7 +77,7 @@ public ArrayList getStations() { SQLiteDatabase db = dbHelper.getReadableDatabase(); ArrayList stations = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.STATIONS_TABLE_NAME, null); try { @@ -97,7 +99,7 @@ public Station getStation(String id) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.STATIONS_TABLE_NAME + " " + "WHERE id = ?", new String[] { id }); try { @@ -127,7 +129,7 @@ public ArrayList getFavoriteStations() { SQLiteDatabase db = dbHelper.getReadableDatabase(); ArrayList favStations = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT sta.id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.FAV_STATIONS_TABLE_NAME + " sta " + "INNER JOIN " + DatabaseHelper.STATIONS_TABLE_NAME + " fav " + "ON sta.id = fav.id", null); @@ -181,6 +183,9 @@ private Station toStation(Cursor cursor) { if (!cursor.isNull(10)) { station.setStatus(StationStatus.valueOf(cursor.getString(10))); // status } + if (!cursor.isNull(11)) { + station.setEBikes(cursor.getInt(11)); // ebikes + } return station; } diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java index 0fbf0cf..e64c638 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java @@ -35,6 +35,7 @@ public class Station implements Serializable, Comparable { private Boolean banking; private Boolean bonus; private StationStatus status; + private Integer eBikes; public Station(String id, String name, String lastUpdate, double latitude, double longitude, int freeBikes, int emptySlots) { this.id = id; @@ -49,6 +50,7 @@ public Station(String id, String name, String lastUpdate, double latitude, doubl this.banking = null; this.bonus = null; this.status = null; + this.eBikes = null; } public String getId() { @@ -95,6 +97,10 @@ public Boolean isBonus() { return bonus; } + public Integer getEBikes() { + return eBikes; + } + public StationStatus getStatus() { return status; } @@ -111,6 +117,10 @@ public void setBonus(boolean bonus) { this.bonus = bonus; } + public void setEBikes(int eBikes) { + this.eBikes = eBikes; + } + @Override public int compareTo(Station another) { return name.compareToIgnoreCase(another.getName()) > 0 ? 1 : diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java index f4d1bb3..3c2b89d 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java @@ -145,6 +145,11 @@ public BikeNetworkParser(String toParse, boolean stripIdFromStationName) throws station.setStatus(StationStatus.OPEN); } } + + /* electric bikes */ + if (rawExtra.has("ebikes")) { + station.setEBikes(rawExtra.getInt("ebikes")); + } } stations.add(station); }