use of im.tny.segvault.disturbances.model.Trip in project underlx by underlx.
the class StatsActivity method getTripsFor.
private static RealmQuery<Trip> getTripsFor(Realm realm, int days) {
if (days == 0) {
return realm.where(Trip.class);
} else {
RealmResults<StationUse> uses = realm.where(StationUse.class).greaterThan("entryDate", new Date(new Date().getTime() - TimeUnit.DAYS.toMillis(days))).findAll().where().equalTo("type", "NETWORK_ENTRY").or().equalTo("type", "VISIT").findAll();
// now we have all station uses that **might** be part of editable trips
// get all trips that contain these uses and which are yet to be confirmed
RealmQuery<Trip> tripsQuery = realm.where(Trip.class);
if (uses.size() > 0) {
// first item detached from the others because otherwise "Missing left-hand side of OR" might happen
// https://github.com/realm/realm-java/issues/1014#issuecomment-107235374
tripsQuery = tripsQuery.equalTo("path.station.id", uses.get(0).getStation().getId()).equalTo("path.entryDate", uses.get(0).getEntryDate());
for (int i = 1; i < uses.size(); i++) {
tripsQuery = tripsQuery.or().equalTo("path.station.id", uses.get(i).getStation().getId()).equalTo("path.entryDate", uses.get(i).getEntryDate());
}
return tripsQuery;
}
// https://github.com/realm/realm-java/issues/4011
return tripsQuery.equalTo("id", "NEVER_BE_TRUE");
}
}
use of im.tny.segvault.disturbances.model.Trip in project underlx by underlx.
the class TripCorrectionActivity method populateUI.
private void populateUI() {
Realm realm = Application.getDefaultRealmInstance(this);
trip = realm.where(Trip.class).equalTo("id", tripId).findFirst();
if (trip == null) {
finish();
return;
}
trip = realm.copyFromRealm(trip);
realm.close();
originalPath = trip.toConnectionPath(network);
if (!trip.canBeCorrected()) {
finish();
return;
}
List<Station> stations = new ArrayList<>(network.getStations());
startPicker.setStations(stations);
endPicker.setStations(stations);
startPicker.setAllStationsSortStrategy(new StationPickerView.DistanceSortStrategy(network, originalPath.getStartVertex()));
endPicker.setAllStationsSortStrategy(new StationPickerView.DistanceSortStrategy(network, originalPath.getEndVertex()));
startPicker.setWeakSelection(originalPath.getStartVertex().getStation());
endPicker.setWeakSelection(originalPath.getEndVertex().getStation());
StationPickerView.OnStationSelectedListener onStationSelectedListener = new StationPickerView.OnStationSelectedListener() {
@Override
public void onStationSelected(Station station) {
redrawPath();
}
};
startPicker.setOnStationSelectedListener(onStationSelectedListener);
endPicker.setOnStationSelectedListener(onStationSelectedListener);
StationPickerView.OnSelectionLostListener onSelectionLostListener = new StationPickerView.OnSelectionLostListener() {
@Override
public void onSelectionLost() {
redrawPath();
}
};
startPicker.setOnSelectionLostListener(onSelectionLostListener);
endPicker.setOnSelectionLostListener(onSelectionLostListener);
redrawPath();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveChanges();
}
});
}
use of im.tny.segvault.disturbances.model.Trip in project underlx by underlx.
the class TripFragment method refreshUI.
private void refreshUI() {
Network network = this.network;
if (network == null) {
return;
}
Realm realm = Application.getDefaultRealmInstance(getContext());
Trip trip = realm.where(Trip.class).equalTo("id", tripId).findFirst();
if (trip == null) {
return;
}
Path path = trip.toConnectionPath(network);
Station origin = path.getStartVertex().getStation();
Station dest = path.getEndVertex().getStation();
SpannableStringBuilder builder = new SpannableStringBuilder();
if (path.getEdgeList().size() == 0) {
builder.append("#");
builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_beenhere_black_24dp), builder.length() - 1, builder.length(), 0);
builder.append(" " + origin.getName());
} else {
builder.append(origin.getName() + " ").append("#");
builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_arrow_forward_black_24dp), builder.length() - 1, builder.length(), 0);
builder.append(" " + dest.getName());
}
stationNamesView.setText(builder);
dateView.setText(DateUtils.formatDateTime(getContext(), path.getEntryTime(0).getTime(), DateUtils.FORMAT_SHOW_DATE));
int length = path.getTimeablePhysicalLength();
long time = path.getMovementMilliseconds();
if (length == 0 || time == 0) {
statsLayout.setVisibility(View.GONE);
} else {
statsView.setText(String.format(getString(R.string.frag_trip_stats), length, DateUtils.formatElapsedTime(time / 1000), (((double) length / (double) (time / 1000)) * 3.6)));
}
populatePathView(getContext(), inflater, path, layoutRoute, true);
if (!trip.canBeCorrected()) {
correctButton.setVisibility(View.GONE);
}
realm.close();
}
use of im.tny.segvault.disturbances.model.Trip in project underlx by underlx.
the class Synchronizer method sync.
private void sync() {
Log.d("sync", "Waiting for lock");
synchronized (lock) {
Log.d("sync", "Lock acquired");
if (!Connectivity.isConnected(context)) {
Log.d("sync", "Not connected, abort");
return;
}
if (lastSync != null && new Date().getTime() - lastSync.getTime() < TimeUnit.SECONDS.toMillis(2)) {
Log.d("sync", "Last sync was right now, abort");
// avoid looping on ACTION_TRIP_REALM_UPDATED broadcasts
return;
}
Realm realm = Application.getDefaultRealmInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Trip> unsyncedTrips = realm.where(Trip.class).equalTo("synced", false).findAll();
for (Trip t : unsyncedTrips) {
API.TripRequest request = tripToAPIRequest(t);
try {
if (t.isSubmitted()) {
// submit update, if possible
if (t.canBeCorrected()) {
API.getInstance().putTrip(request);
t.setSynced(true);
t.setSubmitted(true);
}
} else {
// submit new
API.getInstance().postTrip(request);
t.setSynced(true);
t.setSubmitted(true);
}
} catch (APIException e) {
t.registerSyncFailure();
if (t.isFailedToSync()) {
// give up on this one
t.setSynced(true);
}
}
}
RealmResults<im.tny.segvault.disturbances.model.Feedback> unsyncedFeedback = realm.where(im.tny.segvault.disturbances.model.Feedback.class).equalTo("synced", false).findAll();
for (im.tny.segvault.disturbances.model.Feedback f : unsyncedFeedback) {
API.Feedback request = feedbackToAPIRequest(f);
try {
API.getInstance().postFeedback(request);
f.setSynced(true);
} catch (APIException e) {
e.printStackTrace();
}
}
}
});
realm.close();
lastSync = new Date();
Log.d("sync", "Sync done");
}
}
use of im.tny.segvault.disturbances.model.Trip in project underlx by underlx.
the class TripFragment method deleteTrip.
private void deleteTrip() {
Realm realm = Application.getDefaultRealmInstance(getContext());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Trip trip = realm.where(Trip.class).equalTo("id", tripId).findFirst();
if (trip != null) {
trip.getPath().deleteAllFromRealm();
trip.deleteFromRealm();
}
}
});
realm.close();
dismiss();
}
Aggregations