use of ch.hsr.sa.radiotour.dataaccess.models.Rider in project app by TourLive.
the class RaceGroupRepository method updateRaceGroupRiders.
public void updateRaceGroupRiders(RaceGroup raceGroup, final RealmList<Rider> newRiders, OnUpdateRaceGroupCallBack callback) {
Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
RiderRepository riderRepository = new RiderRepository();
RealmList<Rider> riders = new RealmList<>();
riders.addAll(newRiders);
realm.beginTransaction();
RaceGroup realmRemoveGroup = realm.where(RaceGroup.class).equalTo("riders.id", newRiders.get(0).getId()).findFirst();
if (realmRemoveGroup != null) {
for (Rider r : riders) {
realmRemoveGroup.removeRider(r);
}
}
realm.commitTransaction();
if (raceGroup.getType() == RaceGroupType.FELD) {
Iterator<Rider> iterator = riders.iterator();
while (iterator.hasNext()) {
Rider r = iterator.next();
if (r.isUnknown()) {
riderRepository.removeRider(r, null);
iterator.remove();
}
}
}
realm.beginTransaction();
RaceGroup realmRaceGroup = realm.where(RaceGroup.class).equalTo("type", raceGroup.getType().toString()).equalTo("position", raceGroup.getPosition()).findFirst();
if (!riders.isEmpty())
realmRaceGroup.appendRiders(riders);
realm.commitTransaction();
if (realmRemoveGroup != null && realmRemoveGroup.getRiders().isEmpty()) {
deleteRaceGroup(realmRemoveGroup);
}
if (callback != null) {
callback.onSuccess(realmRaceGroup);
}
}
use of ch.hsr.sa.radiotour.dataaccess.models.Rider in project app by TourLive.
the class RaceGroupRepository method addRaceGroup.
@Override
public void addRaceGroup(RaceGroup raceGroup, OnSaveRaceGroupCallback callback) {
Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
realm.beginTransaction();
RealmResults<RaceGroup> rGtoUpdate = realm.where(RaceGroup.class).greaterThanOrEqualTo("position", raceGroup.getPosition()).findAllSorted("position");
for (RaceGroup rG : rGtoUpdate) {
rG.setPosition(rG.getPosition() + 1);
rG.setActualGapTime(rG.getActualGapTime() + 1);
if (rG.getType() == RaceGroupType.LEAD) {
rG.setType(RaceGroupType.NORMAL);
}
}
realm.commitTransaction();
for (Rider r : raceGroup.getRiders()) {
RealmResults<RaceGroup> resRG = realm.where(RaceGroup.class).equalTo("riders.id", r.getId()).findAll();
if (!resRG.isEmpty()) {
Iterator<RaceGroup> iterator = resRG.iterator();
while (iterator.hasNext()) {
RaceGroup rG = iterator.next();
realm.beginTransaction();
rG.removeRider(r);
realm.commitTransaction();
if (rG.getRiders().isEmpty())
deleteRaceGroup(rG);
}
}
}
realm.beginTransaction();
RaceGroup realmRaceGroup = realm.createObject(RaceGroup.class, UUID.randomUUID().toString());
realmRaceGroup.setType(raceGroup.getType());
realmRaceGroup.setActualGapTime(raceGroup.getActualGapTime());
realmRaceGroup.setHistoryGapTime(0);
realmRaceGroup.setPosition(raceGroup.getPosition());
RealmList<Rider> res = new RealmList<>();
for (Rider r : raceGroup.getRiders()) {
RealmResults<Rider> temp = realm.where(Rider.class).equalTo("startNr", r.getStartNr()).findAll();
res.addAll(temp);
}
realmRaceGroup.setRiders(res);
realm.commitTransaction();
if (callback != null) {
callback.onSuccess(realmRaceGroup);
}
}
use of ch.hsr.sa.radiotour.dataaccess.models.Rider in project app by TourLive.
the class RiderRepository method getAllUnknownRidersReturned.
@Override
public RealmList<Rider> getAllUnknownRidersReturned() {
Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
RealmResults<Rider> results = realm.where(Rider.class).equalTo("isUnknown", true).findAll().sort("startNr");
RealmList<Rider> res = new RealmList<>();
res.addAll(results);
return res;
}
use of ch.hsr.sa.radiotour.dataaccess.models.Rider in project app by TourLive.
the class MainActivity method initViewsAndHandlers.
private void initViewsAndHandlers() {
uiHandler = new Handler();
timerForRace = new Timer();
heightView = findViewById(R.id.txtHeightValue);
topFieldActualGapView = findViewById(R.id.txtTopFieldActualGap);
topRadioTourActualGapView = findViewById(R.id.txtTopRadioTourActualGap);
stageView = findViewById(R.id.txtStageValue);
velocityView = findViewById(R.id.txtVelocityValue);
raceKilometerView = findViewById(R.id.txtRacekilometerValue);
raceTimeView = findViewById(R.id.txtRacetimeValue);
startStopView = findViewById(R.id.btnStartStopRace);
resetView = findViewById(R.id.btnReset);
if (StagePresenter.getInstance().getStage() != null)
updateStageInfo(StagePresenter.getInstance().getStage());
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 0);
}
startStopView.setOnClickListener((View click) -> {
if (!raceInProgress) {
raceInProgress = true;
startStopView.setBackgroundColor(getColor(R.color.colorOlive));
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsAltitude();
locationManager.addNmeaListener((OnNmeaMessageListener) (message, timestamp) -> {
if (message.startsWith("$")) {
String[] tokens = message.split(",");
String type = tokens[0];
if (type.startsWith("$GPGGA") && !tokens[11].isEmpty()) {
correctionHeight = Double.parseDouble(tokens[11]);
}
}
});
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (actualLocation != null) {
distanceInMeter += actualLocation.distanceTo(location);
}
actualLocation = location;
uiHandler.post(() -> {
heightView.setText(getString(R.string.header_prefix_m, (actualLocation.getAltitude() - correctionHeight)));
raceKilometerView.setText(getString(R.string.header_prefix_km, distanceInMeter / 1000f, wholeDistanceInKm));
double seconds = TimeUnit.MILLISECONDS.toSeconds(raceTime.getTime());
double averageSpeed = (distanceInMeter / seconds) * 3.6;
velocityView.setText(getString(R.string.header_prefix_kmh, averageSpeed));
});
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Has to be implemented, but not needed
}
@Override
public void onProviderEnabled(String provider) {
// Has to be implemented, but not needed
}
@Override
public void onProviderDisabled(String provider) {
// Has to be implemented, but not needed
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateTime, minDistanceChange, locationListener);
timerForRace = new Timer();
timerTaskForRace = new TimerTask() {
@Override
public void run() {
raceTime.setTime(raceTime.getTime() + updateTimeForRace);
uiHandler.post(() -> raceTimeView.setText(convertLongToTimeString(raceTime.getTime())));
timeInRaceGroupCounter++;
if (timeInRaceGroupCounter == 60) {
timeInRaceGroupCounter = 0;
for (Rider r : RaceGroupPresenter.getInstance().getLeadRaceGroup().getRiders()) {
RiderStageConnectionPresenter.getInstance().appendTimeInLeadGroup(r.getRiderStages().first(), 1);
}
}
}
};
timerForRace.schedule(timerTaskForRace, delayZero, updateTimeForRace);
}
} else {
raceInProgress = false;
locationManager.removeUpdates(locationListener);
startStopView.setBackgroundColor(getColor(R.color.colorPrimaryUltraLight));
timerForRace.cancel();
}
});
resetView.setOnClickListener(event -> {
raceInProgress = false;
raceTime.setTime(0);
distanceInMeter = 0;
if (locationListener != null)
locationManager.removeUpdates(locationListener);
actualLocation = null;
startStopView.setBackgroundColor(getColor(R.color.colorPrimaryUltraLight));
raceTimeView.setText(convertLongToTimeString(raceTime.getTime()));
timerForRace.cancel();
});
raceTimeView.setOnClickListener((View event) -> {
TimePickerDialog dialog = new TimePickerDialog(this, (view, hourOfDay, minute) -> {
raceTime.setTime(new Time(TimeUnit.HOURS.toMillis(view.getHour()) + TimeUnit.MINUTES.toMillis(view.getMinute())).getTime());
raceTimeView.setText(convertLongToTimeString(raceTime.getTime()));
}, 0, 0, true);
dialog.setTitle(R.string.header_select_time);
dialog.show();
});
timerForUpdate = new Timer();
timerTaskForUpdate = new TimerTask() {
@Override
public void run() {
updateUIInfos();
}
};
timerForUpdate.schedule(timerTaskForUpdate, delayTime, updateTime);
}
use of ch.hsr.sa.radiotour.dataaccess.models.Rider in project app by TourLive.
the class RaceGroupRepositoryInstrumentedTest method updateRaceGroupRiders.
@Test
public void updateRaceGroupRiders() {
RaceGroup raceGroupOne = new RaceGroup();
raceGroupOne.setType(RaceGroupType.LEAD);
raceGroupOne.setPosition(0);
raceGroupOne.setRiders(riders);
RealmList<Rider> newRiders = new RealmList<>();
for (int i = 3; i < 6; i++) {
Rider rider = new Rider();
rider.setTeamShortName("T");
rider.setTeamName("TEST");
rider.setCountry("T");
rider.setName("TEST " + i);
rider.setStartNr(i);
newRiders.add(rider);
synchronized (this) {
riderRepository.addRider(rider, onSaveRiderCallback);
}
}
RaceGroup raceGroupTwo = new RaceGroup();
raceGroupTwo.setType(RaceGroupType.NORMAL);
raceGroupTwo.setPosition(1);
raceGroupTwo.setRiders(newRiders);
synchronized (this) {
raceGroupRepository.addRaceGroup(raceGroupOne, onSaveRaceGroupCallback);
}
synchronized (this) {
raceGroupRepository.addRaceGroup(raceGroupTwo, onSaveRaceGroupCallback);
}
RealmResults<RaceGroup> res = realm.where(RaceGroup.class).findAll();
RealmList<Rider> riderToTransfer = new RealmList<>();
riderToTransfer.add(res.get(0).getRiders().first());
assertEquals(3, res.get(0).getRiders().size());
assertEquals(3, res.get(1).getRiders().size());
synchronized (this) {
raceGroupRepository.updateRaceGroupRiders(res.get(1), riderToTransfer, onUpdateRaceGroupCallBack);
}
RealmResults<RaceGroup> endRes = realm.where(RaceGroup.class).findAll();
assertEquals(2, res.get(0).getRiders().size());
assertEquals(4, res.get(1).getRiders().size());
}
Aggregations