Search in sources :

Example 1 with RaceGroup

use of ch.hsr.sa.radiotour.dataaccess.models.RaceGroup in project app by TourLive.

the class RaceGroupRepository method updateRaceGroupGapTime.

@Override
public void updateRaceGroupGapTime(RaceGroup raceGroup, long timeStamp, OnUpdateRaceGroupCallBack callback) {
    Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
    long timeBefore = realm.where(RaceGroup.class).equalTo("id", raceGroup.getId()).findFirst().getActualGapTime();
    realm.beginTransaction();
    RaceGroup res = realm.where(RaceGroup.class).equalTo("id", raceGroup.getId()).findFirst();
    res.setHistoryGapTime(res.getActualGapTime());
    res.setActualGapTime(timeStamp);
    realm.commitTransaction();
    if (callback != null) {
        RiderStageConnectionPresenter.getInstance().updateRiderStageConnectionTime(timeBefore, timeStamp, res);
        callback.onSuccess(res);
    }
}
Also used : RaceGroup(ch.hsr.sa.radiotour.dataaccess.models.RaceGroup) Realm(io.realm.Realm)

Example 2 with RaceGroup

use of ch.hsr.sa.radiotour.dataaccess.models.RaceGroup in project app by TourLive.

the class RaceGroupRepository method deleteRaceGroup.

@Override
public void deleteRaceGroup(RaceGroup raceGroup) {
    int pos;
    Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
    realm.beginTransaction();
    RaceGroup realmRemoveGroup = realm.where(RaceGroup.class).equalTo("id", raceGroup.getId()).findFirst();
    pos = realmRemoveGroup.getPosition();
    realmRemoveGroup.deleteFromRealm();
    realm.commitTransaction();
    realm.beginTransaction();
    RealmResults<RaceGroup> rGtoUpdate = realm.where(RaceGroup.class).greaterThanOrEqualTo("position", pos).findAllSorted("position");
    for (RaceGroup rG : rGtoUpdate) {
        rG.setPosition(rG.getPosition() - 1);
        if (rG.getPosition() == 0 && rG.getType() != RaceGroupType.FELD) {
            rG.setType(RaceGroupType.LEAD);
        }
    }
    realm.commitTransaction();
}
Also used : RaceGroup(ch.hsr.sa.radiotour.dataaccess.models.RaceGroup) Realm(io.realm.Realm)

Example 3 with RaceGroup

use of ch.hsr.sa.radiotour.dataaccess.models.RaceGroup 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);
    }
}
Also used : RealmList(io.realm.RealmList) RaceGroup(ch.hsr.sa.radiotour.dataaccess.models.RaceGroup) Realm(io.realm.Realm) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider)

Example 4 with RaceGroup

use of ch.hsr.sa.radiotour.dataaccess.models.RaceGroup 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);
    }
}
Also used : RaceGroup(ch.hsr.sa.radiotour.dataaccess.models.RaceGroup) RealmList(io.realm.RealmList) Realm(io.realm.Realm) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider)

Example 5 with RaceGroup

use of ch.hsr.sa.radiotour.dataaccess.models.RaceGroup in project app by TourLive.

the class RaceGroupRepositoryInstrumentedTest method updateRaceGroupPosition.

@Test
public void updateRaceGroupPosition() {
    RaceGroup raceGroup = new RaceGroup();
    raceGroup.setType(RaceGroupType.NORMAL);
    raceGroup.setPosition(1);
    raceGroup.setRiders(riders);
    synchronized (this) {
        raceGroupRepository.addRaceGroup(raceGroup, onSaveRaceGroupCallback);
    }
    RealmResults<RaceGroup> res = realm.where(RaceGroup.class).findAll();
    raceGroupRepository.updateRaceGroupPosition(res.get(0), 0);
    RealmResults<RaceGroup> endRes = realm.where(RaceGroup.class).findAll();
    assertEquals(1, res.size());
    assertEquals(0, endRes.get(0).getPosition());
    assertEquals(RaceGroupType.LEAD, res.get(0).getType());
}
Also used : RaceGroup(ch.hsr.sa.radiotour.dataaccess.models.RaceGroup) Test(org.junit.Test)

Aggregations

RaceGroup (ch.hsr.sa.radiotour.dataaccess.models.RaceGroup)17 Rider (ch.hsr.sa.radiotour.dataaccess.models.Rider)9 Realm (io.realm.Realm)7 RealmList (io.realm.RealmList)7 Test (org.junit.Test)6 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 TextView (android.widget.TextView)1 JSONException (org.json.JSONException)1