package cgeo.geocaching.maps;

import cgeo.geocaching.storage.DataStore;
import cgeo.geocaching.utils.AndroidRxUtils;

import android.location.Location;

import java.util.ArrayList;

import io.reactivex.schedulers.Schedulers;

/**
 * Map trail history
 */
public class PositionHistory {

    /**
     * minimum distance between two recorded points of the trail
     */
    private static final double MINIMUM_DISTANCE_METERS = 10.0;

    /**
     * maximum number of positions to remember
     */
    private static final int MAX_POSITIONS = 700;

    private ArrayList<Location> history = new ArrayList<>();

    // load data from permanent storage
    public PositionHistory() {
        history = DataStore.loadTrailHistory();
    }

    // save current coords to permanent storage
    private void saveToStorage(final Location coordinates) {
        AndroidRxUtils.andThenOnUi(Schedulers.io(), new Runnable() {
            @Override
            public void run() {
                DataStore.saveTrailpoint(coordinates);
            }
        }, new Runnable() {
            @Override
            public void run() {
                // nothing to do UI-wise
            }
        });
    }

    // clear position history (in memory and on permanent storage)
    public void reset() {
        AndroidRxUtils.andThenOnUi(Schedulers.io(), new Runnable() {
            @Override
            public void run() {
                DataStore.clearTrailHistory();
                history.clear();
            }
        }, new Runnable() {
            @Override
            public void run() {
                // nothing to do UI-wise
            }
        });
    }

    /**
     * Adds the current position to the trail history to be able to show the trail on the map.
     */
    public void rememberTrailPosition(final Location coordinates) {
        if (coordinates.getAccuracy() >= 50f) {
            return;
        }
        if (coordinates.getLatitude() == 0.0 && coordinates.getLongitude() == 0.0) {
            return;
        }
        if (history.isEmpty()) {
            saveToStorage(coordinates);
            history.add(coordinates);
            return;
        }

        final Location historyRecent = history.get(history.size() - 1);
        if (historyRecent.distanceTo(coordinates) <= MINIMUM_DISTANCE_METERS) {
            return;
        }

        saveToStorage(coordinates);
        history.add(coordinates);

        // avoid running out of memory
        final int itemsToRemove = getHistory().size() - MAX_POSITIONS;
        if (itemsToRemove > 0) {
            for (int i = 0; i < itemsToRemove; i++) {
                getHistory().remove(0);
            }
        }
    }

    public ArrayList<Location> getHistory() {
        return history;
    }

    public void setHistory(final ArrayList<Location> history) {
        this.history = history;
    }

}

@@ -69,53 +69,8 @@
 ore;
-%0Aimport cgeo.geocaching.utils.AndroidRxUtils;
 %0A%0Aim
@@ -805,52 +805,39 @@
     
-AndroidRxUtils.andThenOnUi(Schedulers.io(), 
+Schedulers.io().scheduleDirect(
 new 
@@ -976,145 +976,8 @@
   %7D%0A
-        %7D, new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                // nothing to do UI-wise%0A            %7D%0A
     
@@ -1096,52 +1096,39 @@
     
-AndroidRxUtils.andThenOnUi(Schedulers.io(), 
+Schedulers.io().scheduleDirect(
 new 
@@ -1237,24 +1237,24 @@
 lHistory();%0A
+
             
@@ -1292,145 +1292,8 @@
   %7D%0A
-        %7D, new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                // nothing to do UI-wise%0A            %7D%0A
     

switch to Schedulers.io().scheduleDirect from AndroidRxUtils.andThenOnUi