Skip to content

Commit

Permalink
Tree building of groups fixed
Browse files Browse the repository at this point in the history
- Couldn't do it the cheater way I had to build a breath first search and then run through it until all the children found their parent
- Fixed loading types even if there are groups available in current group
- Fixed regions not properly updating
  • Loading branch information
w9jds committed Mar 4, 2016
1 parent fd63c76 commit 8749830
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 33 deletions.
31 changes: 22 additions & 9 deletions app/src/main/java/com/w9jds/marketbot/Activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,28 @@ public void onDataLoaded(Object data) {
public void updateSelectedParentGroup(MarketGroup group) {
currentParent = group;

if (group.children.size() > 0) {
adapter.updateCollection(group.children.values());
}
else if (group.items.size() < 1) {
adapter.clear();
dataManager.loadGroupTypes(group.getTypesLocation());
}
else {
if (group.items.size() > 0) {
adapter.updateCollection(group.items.values());
}

if (currentParent != null) {
if (group.children.size() > 0) {
if (group.items.size() > 0) {
adapter.addAndResort(group.children.values());
}
else {
adapter.clear();
adapter.updateCollection(group.children.values());
}
} else {
if (group.items.size() < 1) {
adapter.clear();
}

dataManager.loadGroupTypes(currentParent.getTypesLocation());
}
}

layoutManager.scrollToPosition(0);
}

Expand All @@ -110,7 +121,9 @@ public void onBackPressed() {

@Override
public void dataStartedLoading() {
progressBar.setVisibility(View.VISIBLE);
if (adapter.getAdapterSize() < 1) {
progressBar.setVisibility(View.VISIBLE);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MarketGroupsAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
private final @Nullable DataLoadingSubject dataLoading;

private onMarketGroupChanged groupChangedListener;
private List<? extends MarketItemBase> items;
private List<? extends MarketItemBase> items = new ArrayList<>();

public interface onMarketGroupChanged {
void updateSelectedParentGroup(MarketGroup group);
Expand Down Expand Up @@ -168,9 +168,14 @@ public Object getItem(int position) {
return items.get(position);
}

public int getAdapterSize() {
return items.size();
}

private void bindMarketGroup(final MarketGroup group, MarketGroupHolder holder) {
holder.title.setText(group.getName());

holder.subtitle.setVisibility(View.VISIBLE);
if (group.getDescription().equals("")) {
holder.subtitle.setVisibility(View.GONE);
}
Expand All @@ -190,10 +195,17 @@ private void bindMarketType(final Type type, MarketTypeHolder holder) {

public void addAndResort(Collection<? extends MarketItemBase> newItems) {
ArrayList<MarketItemBase> groups = new ArrayList<>(newItems);
groups.addAll(items);
int oldSize = items.size();
Collections.sort(groups, new Comparitor());

items = groups;

notifyItemRangeInserted(0, items.size());
if (items.size() > 0) {
notifyItemRangeChanged(0, oldSize);
}

notifyItemRangeInserted(oldSize, items.size() - oldSize);
}

public void updateCollection(Collection<? extends MarketItemBase> newChildren) {
Expand All @@ -219,13 +231,22 @@ public void updateCollection(Collection<? extends MarketItemBase> newChildren) {
}

public void clear() {
int oldSize = items.size();

items.clear();
notifyDataSetChanged();
notifyItemRangeRemoved(0, oldSize);
}

private class Comparitor implements Comparator<MarketItemBase> {
@Override
public int compare(MarketItemBase lhs, MarketItemBase rhs) {
if (lhs instanceof MarketGroup && rhs instanceof Type) {
return -1;
}
if (lhs instanceof Type && rhs instanceof MarketGroup) {
return 1;
}

return lhs.getName().compareTo(rhs.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ public void updateCollection(Collection<MarketOrder> newChildren) {
}
}

public void clear() {
int oldSize = orders.size();

orders.clear();
notifyItemRangeRemoved(0, oldSize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void updateOrdersList(Region region, Type type) {
currentRegion = region;
currentType = type;

adapter.clear();
switch(position) {
case 1:
dataManager.loadSellOrders(region, type);
Expand Down
108 changes: 87 additions & 21 deletions eveapi/src/main/java/com/w9jds/eveapi/Client/Crest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.w9jds.eveapi.Client;

import android.os.AsyncTask;

import com.w9jds.eveapi.Callback;
import com.w9jds.eveapi.Models.MarketGroup;
import com.w9jds.eveapi.Models.MarketOrder;
import com.w9jds.eveapi.Models.MarketItemBase;
import com.w9jds.eveapi.Models.OrderType;
import com.w9jds.eveapi.Models.Type;
import com.w9jds.eveapi.Models.TypeInfo;
Expand All @@ -14,6 +16,9 @@

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import retrofit2.Call;
import retrofit2.Response;
Expand Down Expand Up @@ -82,27 +87,8 @@ public void getMarketGroups(final Callback<Hashtable<Long, MarketGroup>> callbac
call.enqueue(new retrofit2.Callback<MarketGroups>() {
@Override
public void onResponse(Call<MarketGroups> call, Response<MarketGroups> response) {
Hashtable<Long, MarketGroup> tree = new Hashtable<>();

for (MarketGroup group : response.body().groups) {
tree.put(group.getId(), group);
}

for (int i = response.body().groups.size() - 1; i > 0; i--) {
MarketGroup group = response.body().groups.get(i);

if (group.hasParent()) {
long parentId = group.getParentGroupId();
if (tree.containsKey(parentId)) {
tree.get(parentId).children.put(group.getId(), group);
}

tree.remove(group.getId());
}
}


callback.success(tree);
new buildMarketGroupTree(response.body().groups, callback).execute();
}

@Override
Expand Down Expand Up @@ -157,6 +143,86 @@ public void onFailure(Call<MarketOrders> call, Throwable t) {
});
}

private class buildMarketGroupTree extends AsyncTask<Void, Void, Hashtable<Long, MarketGroup>> {

Hashtable<Long, MarketGroup> tree = new Hashtable<>();
Hashtable<Long, MarketGroup> children = new Hashtable<>();
final ArrayList<MarketGroup> groups;
final Callback<Hashtable<Long, MarketGroup>> callback;

public buildMarketGroupTree(ArrayList<MarketGroup> groups, Callback<Hashtable<Long, MarketGroup>> callback) {
this.groups = groups;
this.callback = callback;
}

@Override
protected Hashtable<Long, MarketGroup> doInBackground(Void... params) {
for (MarketGroup group : groups) {
if (!group.hasParent()) {
tree.put(group.getId(), group);
}
else {
children.put(group.getId(), group);
}
}

while (children.values().size() > 0) {
for (Iterator<MarketGroup> iterator = children.values().iterator(); iterator.hasNext(); ) {
MarketGroup group = iterator.next();
if (children.containsKey(group.getParentGroupId())) {
children.get(group.getParentGroupId()).children.put(group.getId(), group);
iterator.remove();
}
else {
boolean found = bfsForParent(group, tree);

if (found) {
iterator.remove();
}
}
}
}

return tree;
}

@Override
protected void onPostExecute(Hashtable<Long, MarketGroup> tree) {
callback.success(tree);
super.onPostExecute(tree);
}
}

private boolean bfsForParent(MarketItemBase base, Hashtable<Long, MarketGroup> tree) {
boolean parentFound = false;
MarketGroup kid = (MarketGroup) base;
Queue queue = new LinkedList();

for (MarketItemBase group : tree.values()) {
if (parentFound) {
break;
}

queue.add(group);

while(!queue.isEmpty()) {
MarketGroup node = (MarketGroup)queue.remove();
if (node.getId() == kid.getParentGroupId()) {
node.children.put(base.getId(), (MarketGroup)base);
parentFound = true;
queue.clear();
}
else {
for (MarketGroup child : node.children.values()) {
queue.add(child);
}
}
}
}

return parentFound;
}


public static class Builder {

Expand Down

0 comments on commit 8749830

Please sign in to comment.