Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bananeweizen committed Jun 3, 2015
2 parents 05d3354 + ac60329 commit 5a4efb0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 61 deletions.
1 change: 0 additions & 1 deletion main/res/values/changelog_release.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
· New: Undo available for some actions\n
· New: Copy coords from waypoints to clipboard\n
· New: Catch more http links to GPX files from the browser\n
· New: Menu in cache details to put cache on ignore list (but not to remove)\n
· New: Only turn off GPS in some views if \"Low power mode\" is active in settings\n
· New: Refresh menu option for trackables\n
· New: Can import TerraCaching.com GPX files\n
Expand Down
9 changes: 7 additions & 2 deletions main/src/cgeo/geocaching/CacheDetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cgeo.geocaching.connector.capability.IgnoreCapability;
import cgeo.geocaching.connector.gc.GCConnector;
import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.connector.trackable.TrackableBrand;
import cgeo.geocaching.connector.trackable.TrackableConnector;
import cgeo.geocaching.enumerations.CacheAttribute;
import cgeo.geocaching.enumerations.LoadFlags;
Expand Down Expand Up @@ -196,6 +197,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
private Subscription geoDataSubscription = Subscriptions.empty();

private List<TrackableConnector> trackablesConnectors;
private final EnumSet<TrackableBrand> processedBrands = EnumSet.noneOf(TrackableBrand.class);

@Override
public Loader<List<Trackable>> onCreateLoader(final int id, final Bundle bundle) {
Expand Down Expand Up @@ -343,6 +345,7 @@ public void call() {
// Load Generic trackables connectors Async
trackablesConnectors = ConnectorFactory.getGenericTrackablesConnectors();
for (final TrackableConnector connector: trackablesConnectors) {
processedBrands.add(connector.getBrand());
getSupportLoaderManager().initLoader(connector.getCacheInventoryLoaderId(), null, this).forceLoad();
}

Expand Down Expand Up @@ -563,7 +566,9 @@ public boolean onPrepareOptionsMenu(final Menu menu) {
if (cache != null) {
final IConnector connector = ConnectorFactory.getConnector(cache);
if (connector instanceof IgnoreCapability) {
menu.findItem(R.id.menu_ignore).setVisible(((IgnoreCapability) connector).canIgnoreCache(cache));
// for release only
// menu.findItem(R.id.menu_ignore).setVisible(((IgnoreCapability) connector).canIgnoreCache(cache));
menu.findItem(R.id.menu_ignore).setVisible(false);
}
}
return super.onPrepareOptionsMenu(menu);
Expand Down Expand Up @@ -1839,7 +1844,7 @@ public ListView getDispatchedView(final ViewGroup parentView) {
// TODO: fix layout, then switch back to Android-resource and delete copied one
// this copy is modified to respect the text color
final TrackableListAdapter adapterTrackables = new TrackableListAdapter(CacheDetailActivity.this);
cache.mergeInventory(genericTrackables);
cache.mergeInventory(genericTrackables, processedBrands);

// Todo: don't hesitate to use addAll() on adapter, once API Level reach 11 (as of today level is 9)
//adapterTrackables.addAll(cache.getInventory());
Expand Down
46 changes: 20 additions & 26 deletions main/src/cgeo/geocaching/Geocache.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.connector.gc.Tile;
import cgeo.geocaching.connector.gc.UncertainProperty;
import cgeo.geocaching.connector.trackable.TrackableBrand;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
Expand Down Expand Up @@ -68,9 +69,9 @@
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -343,8 +344,7 @@ public boolean gatherMissingFrom(final Geocache other) {
// "don't know" or "0 items". Since we cannot distinguish
// them here, only populate inventoryItems from
// old data when we have to do it for inventory.
inventory = other.inventory;
inventoryItems = other.inventoryItems;
setInventory(other.inventory);
}
if (logCounts.isEmpty()) {
logCounts = other.logCounts;
Expand Down Expand Up @@ -1000,33 +1000,27 @@ public void setInventory(final List<Trackable> newInventory) {
*
* @param newTrackables to be added to the Geocache
*/
public void mergeInventory(final List<Trackable> newTrackables) {
if (inventory != null) {
// Remove non existing Trackable from the same Brand
final Iterator<Trackable> iterator = inventory.iterator();
while (iterator.hasNext()) {
final Trackable trackable = iterator.next();
boolean removeTrackable = true;
boolean hasBrand = false;
for (final Trackable newTrackable : newTrackables) {
if (trackable.getBrand() == newTrackable.getBrand()) {
hasBrand = true;
if (trackable.getUniqueID().equals(newTrackable.getUniqueID())) {
removeTrackable = false;
break;
}
public void mergeInventory(final List<Trackable> newTrackables, final EnumSet<TrackableBrand> processedBrands) {

final List<Trackable> mergedTrackables = new ArrayList<>(newTrackables);

for (final Trackable trackable : ListUtils.emptyIfNull(inventory)) {
if (processedBrands.contains(trackable.getBrand())) {
final ListIterator<Trackable> iterator = mergedTrackables.listIterator();
while (iterator.hasNext()) {
final Trackable newTrackable = iterator.next();
if (trackable.getUniqueID().equals(newTrackable.getUniqueID())) {
// Respect the merge order. New Values replace existing values.
trackable.mergeTrackable(newTrackable);
iterator.set(trackable);
break;
}
}
if (hasBrand && removeTrackable) {
iterator.remove();
}
} else {
mergedTrackables.add(trackable);
}
}
// Add new trackables
for (final Trackable newTrackable : newTrackables) {
addInventoryItem(newTrackable);
}
inventoryItems = CollectionUtils.size(inventory);
setInventory(mergedTrackables);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion main/src/cgeo/geocaching/connector/gc/GCParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ static private ImmutablePair<StatusCode, Geocache> parseCacheFromText(final Stri
if (matcherInventory.groupCount() > 1) {
final String inventoryPre = matcherInventory.group(2);

final ArrayList<Trackable> inventory = new ArrayList<>();
if (StringUtils.isNotBlank(inventoryPre)) {
final MatcherWrapper matcherInventoryInside = new MatcherWrapper(GCConstants.PATTERN_INVENTORYINSIDE, inventoryPre);

Expand All @@ -658,10 +659,11 @@ static private ImmutablePair<StatusCode, Geocache> parseCacheFromText(final Stri
inventoryItem.setGuid(matcherInventoryInside.group(1));
inventoryItem.setName(matcherInventoryInside.group(2));

cache.addInventoryItem(inventoryItem);
inventory.add(inventoryItem);
}
}
}
cache.mergeInventory(inventory, EnumSet.of(TrackableBrand.TRAVELBUG));
}
}
} catch (final RuntimeException e) {
Expand Down
3 changes: 2 additions & 1 deletion main/src/cgeo/geocaching/connector/oc/OkapiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cgeo.geocaching.connector.oc.OCApiConnector.ApiSupport;
import cgeo.geocaching.connector.oc.OCApiConnector.OAuthLevel;
import cgeo.geocaching.connector.oc.UserInfo.UserInfoStatus;
import cgeo.geocaching.connector.trackable.TrackableBrand;
import cgeo.geocaching.enumerations.CacheAttribute;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
Expand Down Expand Up @@ -400,7 +401,7 @@ private static Geocache parseCache(final ObjectNode response) {
//cache.setLicense(response.getString("attribution_note"));
cache.setWaypoints(parseWaypoints((ArrayNode) response.path(CACHE_WPTS)), false);

cache.mergeInventory(parseTrackables((ArrayNode) response.path(CACHE_TRACKABLES)));
cache.mergeInventory(parseTrackables((ArrayNode) response.path(CACHE_TRACKABLES)), EnumSet.of(TrackableBrand.GEOKRETY));

if (response.has(CACHE_IS_WATCHED)) {
cache.setOnWatchlist(response.get(CACHE_IS_WATCHED).asBoolean());
Expand Down
84 changes: 54 additions & 30 deletions tests/src/cgeo/geocaching/GeocacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;

public class GeocacheTest extends CGeoTestCase {
Expand Down Expand Up @@ -380,17 +381,20 @@ public static void testMergeInventory() {

final List<Trackable> inventory1 = new ArrayList<>(4);

// TB to be updated
// TB to be cleared
final Trackable trackable1 = new Trackable();
trackable1.setGeocode("TB1234");
trackable1.setName("TB 1234");
trackable1.setTrackingcode("TRACK 1");
trackable1.forceSetBrand(TrackableBrand.TRAVELBUG);
inventory1.add(trackable1);

// TB to be updated
final Trackable trackable2 = new Trackable();
trackable2.setGeocode("GK1234");
trackable2.setName("GK 1234");
trackable2.setGeocode("GK1234"); // must be kept intact
trackable2.setName("GK 1234 OLD NAME"); // must be overridden
trackable2.setDistance(100F); // must be overridden
trackable2.setTrackingcode("TRACK 2"); // must be kept intact
trackable2.forceSetBrand(TrackableBrand.GEOKRETY);
inventory1.add(trackable2);

Expand All @@ -410,8 +414,16 @@ public static void testMergeInventory() {
assertThat(cache.getInventory()).hasSize(4);
assertThat(cache.getInventoryItems()).isEqualTo(4);

// new TB
final EnumSet<TrackableBrand> processedBrands = EnumSet.noneOf(TrackableBrand.class);
processedBrands.add(TrackableBrand.SWAGGIE);
processedBrands.add(TrackableBrand.GEOKRETY);
// This brand must be cleared from result as it don't appear in "inventory2"
processedBrands.add(TrackableBrand.TRAVELBUG);
//deliberatly not declare TrackableBrand.UNKNOWN. They must not be removed from merge result

final List<Trackable> inventory2 = new ArrayList<>(3);

// new TB
final Trackable trackable5 = new Trackable();
trackable5.setGeocode("SW1234");
trackable5.setName("SW 1234");
Expand All @@ -422,6 +434,7 @@ public static void testMergeInventory() {
// TB updater
final Trackable trackable6 = new Trackable();
trackable6.setGeocode("GK1234");
trackable2.setName("GK 1234 _NEW_ NAME");
trackable6.setDistance(200F);
trackable6.forceSetBrand(TrackableBrand.GEOKRETY);
inventory2.add(trackable6);
Expand All @@ -434,45 +447,56 @@ public static void testMergeInventory() {
trackable7.forceSetBrand(TrackableBrand.GEOKRETY);
inventory2.add(trackable7);

cache.mergeInventory(inventory2);
cache.mergeInventory(inventory2, processedBrands);

assertThat(cache.getInventory()).hasSize(5);
assertThat(cache.getInventoryItems()).isEqualTo(5);
assertThat(cache.getInventory()).hasSize(4);
assertThat(cache.getInventoryItems()).isEqualTo(4);

assertThat(cache.getInventory().get(0)).isEqualTo(trackable1);
assertThat(cache.getInventory().get(0).getGeocode()).isEqualTo("TB1234");
assertThat(cache.getInventory().get(0).getName()).isEqualTo("TB 1234");
assertThat(cache.getInventory().get(0).getDistance()).isEqualTo(-1F);
assertThat(cache.getInventory().get(0)).isEqualTo(trackable5);
assertThat(cache.getInventory().get(0).getGeocode()).isEqualTo("SW1234");
assertThat(cache.getInventory().get(0).getName()).isEqualTo("SW 1234");
assertThat(cache.getInventory().get(0).getDistance()).isEqualTo(100F);
assertThat(cache.getInventory().get(0).getOwner()).isNull();
assertThat(cache.getInventory().get(0).getBrand()).isEqualTo(TrackableBrand.TRAVELBUG);
assertThat(cache.getInventory().get(0).getBrand()).isEqualTo(TrackableBrand.SWAGGIE);

assertThat(cache.getInventory().get(1)).isEqualTo(trackable2);
assertThat(cache.getInventory().get(1).getGeocode()).isEqualTo("GK1234");
assertThat(cache.getInventory().get(1).getName()).isEqualTo("GK 1234");
assertThat(cache.getInventory().get(1).getName()).isEqualTo("GK 1234 _NEW_ NAME");
assertThat(cache.getInventory().get(1).getDistance()).isEqualTo(200F);
assertThat(cache.getInventory().get(1).getTrackingcode()).isEqualTo("TRACK 2");
assertThat(cache.getInventory().get(1).getOwner()).isNull();
assertThat(cache.getInventory().get(1).getBrand()).isEqualTo(TrackableBrand.GEOKRETY);

assertThat(cache.getInventory().get(2)).isEqualTo(trackable4);
assertThat(cache.getInventory().get(2).getGeocode()).isEqualTo("UN0000");
assertThat(cache.getInventory().get(2).getName()).isEqualTo("");
assertThat(cache.getInventory().get(2).getDistance()).isEqualTo(-1F);
assertThat(cache.getInventory().get(2)).isEqualTo(trackable7);
assertThat(cache.getInventory().get(2).getGeocode()).isEqualTo("GK4321");
assertThat(cache.getInventory().get(2).getName()).isEqualTo("GK 4321");
assertThat(cache.getInventory().get(2).getDistance()).isEqualTo(300F);
assertThat(cache.getInventory().get(2).getOwner()).isNull();
assertThat(cache.getInventory().get(2).getBrand()).isEqualTo(TrackableBrand.UNKNOWN);
assertThat(cache.getInventory().get(2).getBrand()).isEqualTo(TrackableBrand.GEOKRETY);

assertThat(cache.getInventory().get(3)).isEqualTo(trackable5);
assertThat(cache.getInventory().get(3).getGeocode()).isEqualTo("SW1234");
assertThat(cache.getInventory().get(3).getName()).isEqualTo("SW 1234");
assertThat(cache.getInventory().get(3).getDistance()).isEqualTo(100F);
assertThat(cache.getInventory().get(3)).isEqualTo(trackable4);
assertThat(cache.getInventory().get(3).getGeocode()).isEqualTo("UN0000");
assertThat(cache.getInventory().get(3).getName()).isEqualTo("");
assertThat(cache.getInventory().get(3).getDistance()).isEqualTo(-1F);
assertThat(cache.getInventory().get(3).getOwner()).isNull();
assertThat(cache.getInventory().get(3).getBrand()).isEqualTo(TrackableBrand.SWAGGIE);

assertThat(cache.getInventory().get(4)).isEqualTo(trackable7);
assertThat(cache.getInventory().get(4).getGeocode()).isEqualTo("GK4321");
assertThat(cache.getInventory().get(4).getName()).isEqualTo("GK 4321");
assertThat(cache.getInventory().get(4).getDistance()).isEqualTo(300F);
assertThat(cache.getInventory().get(4).getOwner()).isNull();
assertThat(cache.getInventory().get(4).getBrand()).isEqualTo(TrackableBrand.GEOKRETY);
assertThat(cache.getInventory().get(3).getBrand()).isEqualTo(TrackableBrand.UNKNOWN);

// test null inventory
final Geocache cache1 = new Geocache();
final List<Trackable> inventory3 = Collections.singletonList(trackable1);
assertThat(cache1.getInventory()).isNull();

cache1.mergeInventory(inventory3, EnumSet.of(TrackableBrand.TRAVELBUG));

assertThat(cache1.getInventory()).hasSize(1);
assertThat(cache1.getInventoryItems()).isEqualTo(1);

assertThat(cache1.getInventory().get(0)).isEqualTo(trackable1);
assertThat(cache1.getInventory().get(0).getGeocode()).isEqualTo("TB1234");
assertThat(cache1.getInventory().get(0).getName()).isEqualTo("TB 1234");
assertThat(cache1.getInventory().get(0).getDistance()).isEqualTo(-1F);
assertThat(cache1.getInventory().get(0).getOwner()).isNull();
assertThat(cache1.getInventory().get(0).getBrand()).isEqualTo(TrackableBrand.TRAVELBUG);
}

public static void testAddInventoryItem() {
Expand Down

0 comments on commit 5a4efb0

Please sign in to comment.