Search in sources :

Example 1 with Rider

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);
    }
}
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 2 with Rider

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);
    }
}
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 3 with Rider

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

Example 4 with Rider

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);
}
Also used : R(ch.hsr.sa.radiotour.R) Bundle(android.os.Bundle) Time(java.sql.Time) PackageManager(android.content.pm.PackageManager) Date(java.util.Date) Timer(java.util.Timer) RaceFragment(ch.hsr.sa.radiotour.presentation.fragments.RaceFragment) LocationListener(android.location.LocationListener) Manifest(android.Manifest) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) RiderStageConnectionPresenter(ch.hsr.sa.radiotour.business.presenter.RiderStageConnectionPresenter) Handler(android.os.Handler) View(android.view.View) TimerTask(java.util.TimerTask) Log(android.util.Log) UrlLink(ch.hsr.sa.radiotour.controller.api.UrlLink) RewardPresenter(ch.hsr.sa.radiotour.business.presenter.RewardPresenter) RiderRaceGroupFragment(ch.hsr.sa.radiotour.presentation.fragments.RiderRaceGroupFragment) TimeZone(java.util.TimeZone) StagePresenter(ch.hsr.sa.radiotour.business.presenter.StagePresenter) Fragment(android.support.v4.app.Fragment) MaillotPresenter(ch.hsr.sa.radiotour.business.presenter.MaillotPresenter) RiderPresenter(ch.hsr.sa.radiotour.business.presenter.RiderPresenter) Stage(ch.hsr.sa.radiotour.dataaccess.models.Stage) ActivityCompat(android.support.v4.app.ActivityCompat) AppCompatActivity(android.support.v7.app.AppCompatActivity) ImportFragment(ch.hsr.sa.radiotour.presentation.fragments.ImportFragment) TextView(android.widget.TextView) JudgmentPresenter(ch.hsr.sa.radiotour.business.presenter.JudgmentPresenter) Pattern(java.util.regex.Pattern) Location(android.location.Location) LocationManager(android.location.LocationManager) Context(android.content.Context) VirtualClassFragment(ch.hsr.sa.radiotour.presentation.fragments.VirtualClassFragment) TimePickerDialog(android.app.TimePickerDialog) SpecialFragment(ch.hsr.sa.radiotour.presentation.fragments.SpecialFragment) ViewPager(android.support.v4.view.ViewPager) SimpleDateFormat(java.text.SimpleDateFormat) OnNmeaMessageListener(android.location.OnNmeaMessageListener) TabLayout(android.support.design.widget.TabLayout) RaceGroupPresenter(ch.hsr.sa.radiotour.business.presenter.RaceGroupPresenter) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider) MaillotsFragment(ch.hsr.sa.radiotour.presentation.fragments.MaillotsFragment) JudgmentRiderConnectionPresenter(ch.hsr.sa.radiotour.business.presenter.JudgmentRiderConnectionPresenter) ViewPageAdapter(ch.hsr.sa.radiotour.controller.adapter.ViewPageAdapter) TimeUnit(java.util.concurrent.TimeUnit) APIClient(ch.hsr.sa.radiotour.controller.api.APIClient) JSONArray(org.json.JSONArray) Bundle(android.os.Bundle) Handler(android.os.Handler) TimePickerDialog(android.app.TimePickerDialog) Time(java.sql.Time) View(android.view.View) TextView(android.widget.TextView) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider) Timer(java.util.Timer) TimerTask(java.util.TimerTask) LocationListener(android.location.LocationListener) Location(android.location.Location)

Example 5 with Rider

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

Aggregations

Rider (ch.hsr.sa.radiotour.dataaccess.models.Rider)46 RealmList (io.realm.RealmList)17 Realm (io.realm.Realm)13 RaceGroup (ch.hsr.sa.radiotour.dataaccess.models.RaceGroup)9 Test (org.junit.Test)9 RiderStageConnection (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection)5 TextView (android.widget.TextView)3 Judgement (ch.hsr.sa.radiotour.dataaccess.models.Judgement)3 JSONException (org.json.JSONException)3 Fragment (android.support.v4.app.Fragment)2 View (android.view.View)2 JudgmentRiderConnection (ch.hsr.sa.radiotour.dataaccess.models.JudgmentRiderConnection)2 RiderRanking (ch.hsr.sa.radiotour.dataaccess.models.RiderRanking)2 Manifest (android.Manifest)1 AlertDialog (android.app.AlertDialog)1 TimePickerDialog (android.app.TimePickerDialog)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 PackageManager (android.content.pm.PackageManager)1 Location (android.location.Location)1