Skip to content

Commit

Permalink
Do not load quotes when instantiating portfolio stock quote repositor…
Browse files Browse the repository at this point in the history
…y, since we only need to load them when in the Portfolio Activity so load them at that time; Save quotes for use when no data as JSON instead of as a single string (no need to worry about separator characters and escaping)
  • Loading branch information
niteshpatel committed Aug 27, 2016
1 parent ef71bfc commit 178df27
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 70 deletions.
10 changes: 6 additions & 4 deletions src/nitezh/ministock/activities/PortfolioActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ of this software and associated documentation files (the "Software"), to deal

import nitezh.ministock.DialogTools;
import nitezh.ministock.Storage;
import nitezh.ministock.utils.StorageCache;
import nitezh.ministock.PreferenceStorage;
import nitezh.ministock.R;
import nitezh.ministock.activities.widget.WidgetProviderBase;
Expand All @@ -70,15 +69,18 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Storage storage = PreferenceStorage.getInstance(this);
AndroidWidgetRepository widgetRepository = new AndroidWidgetRepository(this);

this.portfolioRepository = new PortfolioStockRepository(
storage,
new StorageCache(storage),
new AndroidWidgetRepository(this)
widgetRepository
);

this.portfolioRepository.updateStocksQuotes();

this.refreshView();
}


void refreshView() {
setContentView(R.layout.portfolio);

Expand Down
28 changes: 9 additions & 19 deletions src/nitezh/ministock/activities/widget/WidgetView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,19 @@ of this software and associated documentation files (the "Software"), to deal
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.RemoteViews;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import nitezh.ministock.PreferenceStorage;
import nitezh.ministock.R;
import nitezh.ministock.Storage;
import nitezh.ministock.utils.StorageCache;
import nitezh.ministock.WidgetProvider;
import nitezh.ministock.domain.AndroidWidgetRepository;
import nitezh.ministock.domain.PortfolioStock;
import nitezh.ministock.domain.PortfolioStockRepository;
import nitezh.ministock.domain.StockQuote;
import nitezh.ministock.domain.Widget;
import nitezh.ministock.domain.WidgetRepository;
import nitezh.ministock.domain.WidgetStock;
import nitezh.ministock.domain.*;
import nitezh.ministock.utils.CurrencyTools;
import nitezh.ministock.utils.NumberTools;
import nitezh.ministock.utils.ReflectionTools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static nitezh.ministock.activities.widget.WidgetProviderBase.UpdateType;
import static nitezh.ministock.activities.widget.WidgetProviderBase.ViewType;

Expand Down Expand Up @@ -83,9 +74,8 @@ public WidgetView(Context context, int appWidgetId, UpdateType updateMode,
this.updateMode = updateMode;
this.symbols = widget.getSymbols();

Storage storage = PreferenceStorage.getInstance(context);
this.portfolioStocks = new PortfolioStockRepository(PreferenceStorage.getInstance(context),
new StorageCache(storage), widgetRepository).getStocksForSymbols(symbols);
this.portfolioStocks = new PortfolioStockRepository(
PreferenceStorage.getInstance(context), widgetRepository).getStocksForSymbols(symbols);
this.hasPortfolioData = !portfolioStocks.isEmpty();

this.remoteViews = this.getBlankRemoteViews(this.widget, context.getPackageName());
Expand Down Expand Up @@ -354,7 +344,7 @@ private void updateWidgetRowWithDefaults(WidgetRow widgetRow, WidgetStock widget
}

private void updateWidgetRowWithNoData(WidgetRow widgetRow) {
widgetRow.setHasNoData(true);
widgetRow.setHasNoData();
if (this.widget.isNarrow()) {
widgetRow.setPrice("no");
widgetRow.setPriceColor(Color.GRAY);
Expand Down
32 changes: 17 additions & 15 deletions src/nitezh/ministock/domain/PortfolioStockRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,55 @@ of this software and associated documentation files (the "Software"), to deal

import android.content.Context;

import nitezh.ministock.utils.StorageCache;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import nitezh.ministock.DialogTools;
import nitezh.ministock.Storage;
import nitezh.ministock.UserData;
import nitezh.ministock.utils.Cache;
import nitezh.ministock.utils.CurrencyTools;
import nitezh.ministock.utils.NumberTools;

public class PortfolioStockRepository {
public static final String PORTFOLIO_JSON = "portfolioJson";
public static final String WIDGET_JSON = "widgetJson";
public HashMap<String, StockQuote> stocksQuotes = new HashMap<>();

public HashMap<String, StockQuote> stocksQuotes = new HashMap<>();
public HashMap<String, PortfolioStock> portfolioStocksInfo = new HashMap<>();
public Set<String> widgetsStockSymbols = new HashSet<>();

private static final HashMap<String, PortfolioStock> mPortfolioStocks = new HashMap<>();
private static boolean mDirtyPortfolioStockMap = true;

private final WidgetRepository widgetRepository;
private Storage mAppStorage;

public PortfolioStockRepository(Storage appStorage, Cache cache, WidgetRepository widgetRepository) {
public PortfolioStockRepository(Storage appStorage, WidgetRepository widgetRepository) {
this.mAppStorage = appStorage;
this.widgetRepository = widgetRepository;

this.widgetsStockSymbols = widgetRepository.getWidgetsStockSymbols();
this.portfolioStocksInfo = getPortfolioStocksInfo(widgetsStockSymbols);
this.stocksQuotes = getStocksQuotes(appStorage, cache, widgetRepository);
}

private HashMap<String, StockQuote> getStocksQuotes(Storage appStorage, Cache cache, WidgetRepository widgetRepository) {
public void updateStocksQuotes() {
if (this.stocksQuotes.isEmpty())
this.stocksQuotes = getStocksQuotes();
}

private HashMap<String, StockQuote> getStocksQuotes() {
StockQuoteRepository stockQuoteRepository = new StockQuoteRepository(
this.mAppStorage, new StorageCache(this.mAppStorage), this.widgetRepository);

Set<String> symbolSet = portfolioStocksInfo.keySet();

return new StockQuoteRepository(appStorage, cache, widgetRepository)
return stockQuoteRepository
.getQuotes(Arrays.asList(symbolSet.toArray(new String[symbolSet.size()])), false);
}

Expand Down
80 changes: 50 additions & 30 deletions src/nitezh/ministock/domain/StockQuoteRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ of this software and associated documentation files (the "Software"), to deal
package nitezh.ministock.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.*;

import nitezh.ministock.utils.Cache;
import nitezh.ministock.Storage;
import nitezh.ministock.dataaccess.FxChangeRepository;
import nitezh.ministock.dataaccess.GoogleStockQuoteRepository;
import nitezh.ministock.dataaccess.YahooStockQuoteRepository;
import org.json.JSONException;
import org.json.JSONObject;


public class StockQuoteRepository {
Expand Down Expand Up @@ -108,7 +105,7 @@ public HashMap<String, StockQuote> getQuotes(List<String> symbols, boolean noCac
Set<String> widgetSymbols = this.widgetRepository.getWidgetsStockSymbols();
widgetSymbols.add("^DJI");
widgetSymbols.addAll(new PortfolioStockRepository(
this.appStorage, this.appCache, this.widgetRepository).getStocks().keySet());
this.appStorage, this.widgetRepository).getStocks().keySet());
quotes = getLiveQuotes(new ArrayList<>(widgetSymbols));
}

Expand All @@ -128,25 +125,38 @@ public HashMap<String, StockQuote> getQuotes(List<String> symbols, boolean noCac
}

private HashMap<String, StockQuote> loadQuotes() {
if (mCachedQuotes != null) {
if (mCachedQuotes != null && !mCachedQuotes.isEmpty()) {
return mCachedQuotes;
}

HashMap<String, StockQuote> quotes = new HashMap<>();
String savedQuotes = this.appStorage.getString("savedQuotes", "");
String savedQuotesString = this.appStorage.getString("savedQuotes", "");
String timeStamp = this.appStorage.getString("savedQuotesTime", "");
if (!savedQuotes.equals("")) {

if (!savedQuotesString.equals("")) {
JSONObject savedQuotes = new JSONObject();
try {
savedQuotes = new JSONObject(savedQuotesString);
} catch (JSONException ignored) {
}

try {
for (String line : savedQuotes.split("\n")) {
String[] values = line.split(";");
quotes.put(values[0], new StockQuote(
values[0],
values[1],
values[2],
values[3],
values[4],
values[5],
values[6]));
String key;
JSONObject details;

for (Iterator iter = savedQuotes.keys(); iter.hasNext(); ) {
key = (String) iter.next();
details = savedQuotes.getJSONObject(key);

quotes.put(key, new StockQuote(
details.getString("symbol"),
details.getString("price"),
details.getString("change"),
details.getString("percent"),
details.getString("exchange"),
details.getString("volume"),
details.getString("name")
));
}
} catch (Exception e) {
quotes = new HashMap<>();
Expand All @@ -166,19 +176,29 @@ private void saveQuotes(HashMap<String, StockQuote> quotes, String timeStamp) {
mCachedQuotes = quotes;
mTimeStamp = timeStamp;

StringBuilder savedQuotes = new StringBuilder();
JSONObject savedQuotes = new JSONObject();
for (String symbol : quotes.keySet()) {
try {
savedQuotes.put(symbol, new JSONObject());

} catch (JSONException ignored) {
}

StockQuote quote = quotes.get(symbol);
savedQuotes.append(String.format("%s;%s;%s;%s;%s;%s;%s\n",
quote.getSymbol() != null ? quote.getSymbol() : "",
quote.getPrice() != null ? quote.getPrice() : "",
quote.getChange() != null ? quote.getChange() : "",
quote.getPercent() != null ? quote.getPercent() : "",
quote.getExchange() != null ? quote.getExchange() : "",
quote.getVolume() != null ? quote.getVolume() : "",
quote.getName() != null ? quote.getName() : ""));
try {
savedQuotes.getJSONObject(symbol).put("symbol", quote.getSymbol());
savedQuotes.getJSONObject(symbol).put("price", quote.getPrice());
savedQuotes.getJSONObject(symbol).put("change", quote.getChange());
savedQuotes.getJSONObject(symbol).put("percent", quote.getPercent());
savedQuotes.getJSONObject(symbol).put("exchange", quote.getExchange());
savedQuotes.getJSONObject(symbol).put("volume", quote.getVolume());
savedQuotes.getJSONObject(symbol).put("name", quote.getName());

} catch (JSONException ignored) {
}
}
this.appStorage.putString("savedQuotes", savedQuotes.toString().trim());

this.appStorage.putString("savedQuotes", savedQuotes.toString());
this.appStorage.putString("savedQuotesTime", timeStamp);
this.appStorage.apply();
}
Expand Down
2 changes: 0 additions & 2 deletions src/nitezh/ministock/tests/PortfolioStockRepositoryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ of this software and associated documentation files (the "Software"), to deal
import junit.framework.TestCase;
import nitezh.ministock.domain.PortfolioStock;
import nitezh.ministock.domain.PortfolioStockRepository;
import nitezh.ministock.tests.mocks.MockCache;
import nitezh.ministock.tests.mocks.MockStorage;
import nitezh.ministock.tests.mocks.MockWidgetRepository;

Expand All @@ -39,7 +38,6 @@ public class PortfolioStockRepositoryTests extends TestCase {
public void setUp() {
this.stockRepository = new PortfolioStockRepository(
new MockStorage(),
new MockCache(),
new MockWidgetRepository());
}

Expand Down

0 comments on commit 178df27

Please sign in to comment.