From 99c88e6a9af709e4260fed4b65be3fb42ac7ef2d Mon Sep 17 00:00:00 2001 From: whipp_alexander Date: Thu, 7 Apr 2016 19:46:10 -0400 Subject: [PATCH] Item Margins Fork * Added ISK Margins for items * That being said this is purely calculation as it still displays in its native format with ISK for the margin percentage. * Next stage would be to make the front-end changes --- .../marketbot/adapters/OrdersTabAdapter.java | 6 +- .../marketbot/data/loader/DataManager.java | 89 ++++++++++++++++++- .../marketbot/ui/fragments/OrdersTab.java | 5 ++ app/src/main/res/values/strings.xml | 1 + .../com/w9jds/eveapi/Models/MarketOrder.java | 9 +- 5 files changed, 105 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/w9jds/marketbot/adapters/OrdersTabAdapter.java b/app/src/main/java/com/w9jds/marketbot/adapters/OrdersTabAdapter.java index 5308781..ea4c5d6 100644 --- a/app/src/main/java/com/w9jds/marketbot/adapters/OrdersTabAdapter.java +++ b/app/src/main/java/com/w9jds/marketbot/adapters/OrdersTabAdapter.java @@ -15,13 +15,15 @@ /** * Created by Jeremy on 3/1/2016. + * + * Modified by Alexander Whipp on 4/7/2016. */ public final class OrdersTabAdapter extends FragmentStatePagerAdapter { private Resources resources; private long typeId; - final int PAGE_COUNT = 3; + final int PAGE_COUNT = 4; public OrdersTabAdapter(FragmentManager fragmentManager, Context context, long typeId) { super(fragmentManager); @@ -44,6 +46,8 @@ public CharSequence getPageTitle(int position) { return resources.getString(R.string.sell_orders); case 2: return resources.getString(R.string.buy_orders); + case 3: + return resources.getString(R.string.margins); default: return ""; } diff --git a/app/src/main/java/com/w9jds/marketbot/data/loader/DataManager.java b/app/src/main/java/com/w9jds/marketbot/data/loader/DataManager.java index e20f428..a6aa851 100644 --- a/app/src/main/java/com/w9jds/marketbot/data/loader/DataManager.java +++ b/app/src/main/java/com/w9jds/marketbot/data/loader/DataManager.java @@ -7,12 +7,13 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; -import android.provider.ContactsContract; import com.w9jds.eveapi.Callback; import com.w9jds.eveapi.Client.Crest; import com.w9jds.eveapi.Models.MarketGroup; +import com.w9jds.eveapi.Models.MarketOrder; import com.w9jds.eveapi.Models.OrderType; +import com.w9jds.eveapi.Models.Reference; import com.w9jds.eveapi.Models.Region; import com.w9jds.eveapi.Models.ServerInfo; import com.w9jds.eveapi.Models.Type; @@ -28,12 +29,15 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import javax.inject.Inject; import javax.inject.Named; /** * Created by Jeremy Shore on 2/19/16. + * + * Modified by Alexander Whipp on 4/7/2016 */ public abstract class DataManager extends BaseDataManager { @@ -218,7 +222,7 @@ private void updateMarketTypes() { } public void loadNextPageTypes(String targetLocation) { - publicCrest.getMarketTypes(targetLocation, marketTypeCallback); + publicCrest.getMarketTypes(targetLocation, marketTypeCallback); } private void updateRegions() { @@ -340,6 +344,87 @@ public void failure(String error) { }); } + public void loadMarginOrders(final Region region, final Type type){ + loadStarted(); + incrementLoadingCount(); + + publicCrest.getOrders(region.getId(), type.getHref(), OrderType.buy, new Callback() { + @Override + public void success(final MarketOrders marketBuyOrders) { + if (marketBuyOrders != null) { + + publicCrest.getOrders(region.getId(), type.getHref(), OrderType.sell, new Callback() { + @Override + public void success(MarketOrders marketSellOrders) { + if (marketSellOrders != null) { + onDataLoaded(combineOrders(marketBuyOrders, marketSellOrders).orders); + } + decrementLoadingCount(); + loadFinished(); + } + + @Override + public void failure(String error) { + decrementLoadingCount(); + loadFinished(); + } + }); + } + } + + @Override + public void failure(String error) { + decrementLoadingCount(); + loadFinished(); + } + }); + + } + + private MarketOrders combineOrders(MarketOrders buyOrders, MarketOrders sellOrders){ + HashMap buyOrderMaximums = new HashMap<>(); + for(MarketOrder m: buyOrders.orders){ + if(buyOrderMaximums.containsKey(m.getLocation().getName())){ + if(buyOrderMaximums.get(m.getLocation().getName()).getPrice() < m.getPrice()){ + buyOrderMaximums.put(m.getLocation().getName(), m); + } + }else{ + buyOrderMaximums.put(m.getLocation().getName(), m); + } + } + + HashMap sellOrderMinimums = new HashMap<>(); + for(MarketOrder m: sellOrders.orders){ + if(sellOrderMinimums.containsKey(m.getLocation().getName())){ + if(sellOrderMinimums.get(m.getLocation().getName()).getPrice() > m.getPrice()){ + sellOrderMinimums.put(m.getLocation().getName(), m); + } + }else{ + sellOrderMinimums.put(m.getLocation().getName(), m); + } + } + + ArrayList finalOrderList = new ArrayList<>(); + + for(String station_name : buyOrderMaximums.keySet()){ + if(sellOrderMinimums.containsKey(station_name)){ + MarketOrder buyOrder = buyOrderMaximums.get(station_name); + MarketOrder sellOrder = sellOrderMinimums.get(station_name); + //(sales price - taxes and fees - purchase price) / purchase price + double price = (sellOrder.getPrice() - buyOrder.getPrice()) / buyOrder.getPrice(); + buyOrder.setPrice(price); + finalOrderList.add(buyOrder); + } + } + + MarketOrders finalOrders = new MarketOrders(); + finalOrders.orders = finalOrderList; + finalOrders.size = finalOrderList.size(); + + return finalOrders; + + } + public void searchMarketTypes(String queryString) { loadStarted(); incrementLoadingCount(); diff --git a/app/src/main/java/com/w9jds/marketbot/ui/fragments/OrdersTab.java b/app/src/main/java/com/w9jds/marketbot/ui/fragments/OrdersTab.java index 2c6d83a..7ce866b 100644 --- a/app/src/main/java/com/w9jds/marketbot/ui/fragments/OrdersTab.java +++ b/app/src/main/java/com/w9jds/marketbot/ui/fragments/OrdersTab.java @@ -29,6 +29,8 @@ /** * Created by Jeremy on 3/1/2016. + * + * Modified by Alexander Whipp on 4/7/2016. */ public final class OrdersTab extends Fragment implements BaseDataManager.DataLoadingCallbacks { @@ -137,6 +139,9 @@ public void updateOrdersList(Region region, Type type) { case 2: dataManager.loadBuyOrders(region, type); break; + case 3: + dataManager.loadMarginOrders(region, type); + break; } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8d3355c..e018dbf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,7 @@ Market Bot + Margins Sell Orders Buy Orders Info diff --git a/eveapi/src/main/java/com/w9jds/eveapi/Models/MarketOrder.java b/eveapi/src/main/java/com/w9jds/eveapi/Models/MarketOrder.java index aa956bc..5c3f749 100644 --- a/eveapi/src/main/java/com/w9jds/eveapi/Models/MarketOrder.java +++ b/eveapi/src/main/java/com/w9jds/eveapi/Models/MarketOrder.java @@ -2,11 +2,12 @@ import com.google.gson.annotations.SerializedName; -import java.util.Date; - /** * Created by Jeremy on 3/1/2016. + * + * Modified by Alexander Whipp on 4/7/2016. */ + public final class MarketOrder extends MarketItemBase { @SerializedName("buy") @@ -52,6 +53,8 @@ public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } + public int getDuration() { return duration; } @@ -79,4 +82,6 @@ public int getVolumeStart() { public boolean isBuyOrder() { return isBuyOrder; } + + }