Skip to content
Browse files

Merge branch 'master' of https://github.com/jackquack/BitPredictor

  • Loading branch information...
2 parents 09ca8d2 + c813fe6 commit c6a933930631af9cc4b47f6d20cd5a132227d5ae @jackquack committed Mar 23, 2013
View
1 android/PredictionMarket/.settings/org.eclipse.core.resources.prefs
@@ -1,2 +1,3 @@
eclipse.preferences.version=1
+encoding//src/com/bitcoin/predictionmarket/activity/ContractDetailsActivity.java=UTF-8
encoding//src/com/bitcoin/predictionmarket/adapter/ContractListAdapter.java=UTF-8
View
5 android/PredictionMarket/AndroidManifest.xml
@@ -21,6 +21,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+ <activity
+ android:name="com.bitcoin.predictionmarket.activity.ContractDetailsActivity"
+ android:theme="@style/Theme.Sherlock.Light.DarkActionBar"
+ android:label="@string/contractDetailsTitle">
+ </activity>
</application>
</manifest>
View
1 android/PredictionMarket/project.properties
@@ -13,3 +13,4 @@
# Project target.
target=android-17
android.library.reference.1=../ActionBarSherlock-Forked/library
+android.library.reference.2=../GraphView
View
27 android/PredictionMarket/res/layout/contract_details.xml
@@ -0,0 +1,27 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:orientation="vertical"
+ android:padding="8dp" >
+
+ <TextView
+ android:id="@+id/contractName"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/contractExpiration"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:ellipsize="end"
+ android:singleLine="true"
+ android:textAppearance="?android:attr/textAppearanceMedium" />
+
+ <LinearLayout
+ android:id="@+id/graphRoot"
+ android:layout_width="fill_parent"
+ android:layout_height="0dp"
+ android:layout_weight="1" />
+
+</LinearLayout>
View
3 android/PredictionMarket/res/values/strings.xml
@@ -1,7 +1,8 @@
<resources>
- <string name="app_name">Prediction Market</string>
+ <string name="app_name">Bit Predictor</string>
<string name="contracts">Contracts</string>
+ <string name="contractDetailsTitle">Contract Details</string>
<string name="noContractsFound">No contracts found</string>
<string name="loading">Loading...</string>
<string name="expiresOn">Expires on %s</string>
View
117 ...d/PredictionMarket/src/com/bitcoin/predictionmarket/activity/ContractDetailsActivity.java
@@ -0,0 +1,117 @@
+package com.bitcoin.predictionmarket.activity;
+
+import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.Random;
+
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import com.actionbarsherlock.app.ActionBar;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.MenuItem;
+import com.bitcoin.predictionmarket.R;
+import com.bitcoin.predictionmarket.model.Contract;
+import com.jjoe64.graphview.GraphView;
+import com.jjoe64.graphview.GraphView.GraphViewData;
+import com.jjoe64.graphview.GraphViewSeries;
+import com.jjoe64.graphview.GraphViewStyle;
+import com.jjoe64.graphview.LineGraphView;
+
+public class ContractDetailsActivity extends SherlockFragmentActivity {
+ private static final String BUNDLE_CONTRACT = "BUNDLE_CONTRACT";
+
+ private final DateFormat dateFormatter = SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL);
+ private final DecimalFormat doubleFormatter = new DecimalFormat("฿ #.##");
+ private final DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM);
+
+ public static void launch(Context context, Contract contract) {
+ Intent intent = new Intent(context, ContractDetailsActivity.class);
+ intent.putExtra(BUNDLE_CONTRACT, contract);
+ context.startActivity(intent);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.contract_details);
+
+ ActionBar actionBar = getSupportActionBar();
+ actionBar.setDisplayHomeAsUpEnabled(true);
+
+ Contract contract = (Contract) getIntent().getSerializableExtra(BUNDLE_CONTRACT);
+ ((TextView)findViewById(R.id.contractName)).setText(contract.name);
+ ((TextView)findViewById(R.id.contractExpiration)).setText(getString(R.string.expiresOn, dateFormatter.format(contract.expiration)));
+
+ GraphView graphView = new LineGraphView(this, "example") {
+ @Override
+ protected String formatLabel(double value, boolean isValueX) {
+ if (isValueX) {
+ return timeFormatter.format(value);
+ } else {
+ return doubleFormatter.format(value);
+ }
+ }
+ };
+ graphView.addSeries(genRandomWalkDataForDay()); // data
+ graphView.setScalable(true);
+ graphView.setGraphViewStyle(new GraphViewStyle(Color.rgb(0, 0, 0), Color.rgb(0, 0, 0), Color.rgb(96, 96, 96)));
+
+ LinearLayout layout = (LinearLayout) findViewById(R.id.graphRoot);
+ layout.addView(graphView);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ // Apparently works around a bug that manifests itself on some Android
+ // versions.
+ getSherlock().dispatchInvalidateOptionsMenu();
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == android.R.id.home) {
+ MainActivity.launchFromChild(this);
+ finish();
+ return true;
+ } else {
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
+ private GraphViewSeries genRandomWalkDataForDay() {
+ final Random random = new Random();
+ final int size = 240;
+ final double min = 0;
+ final double max = 10;
+ final double range = max - min;
+ final double walkPerTick = range / 1000.0;
+ double current = min + random.nextDouble() * range;
+
+ final long now = System.currentTimeMillis();
+ final long dayMs = 60 * 60 * 24 * 1000;
+ final long interval = dayMs / size;
+
+ GraphViewData[] graphViewData = new GraphViewData[size];
+
+ for (int i = 0; i < size; i++) {
+ graphViewData[i] = new GraphViewData(now - interval * (size - i), current);
+ double delta = (random.nextDouble() - 0.5) * walkPerTick;
+ current += delta;
+ current = clamp(current, min, max);
+ }
+
+ return new GraphViewSeries(graphViewData);
+ }
+
+ private double clamp(double val, double min, double max) {
+ return Math.max(min, Math.min(val, max));
+ }
+}
View
8 android/PredictionMarket/src/com/bitcoin/predictionmarket/activity/MainActivity.java
@@ -4,6 +4,8 @@
package com.bitcoin.predictionmarket.activity;
import android.annotation.SuppressLint;
+import android.content.Context;
+import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
@@ -16,6 +18,12 @@
public class MainActivity extends SherlockFragmentActivity {
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
+
+ public static void launchFromChild(Context context) {
+ Intent intent = new Intent(context, MainActivity.class);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ context.startActivity(intent);
+ }
@SuppressLint("NewApi")
@Override
View
45 android/PredictionMarket/src/com/bitcoin/predictionmarket/adapter/ContractListAdapter.java
@@ -21,6 +21,7 @@
import com.bitcoin.predictionmarket.R;
import com.bitcoin.predictionmarket.adapter.ContractListAdapter.SyncWithServer.SettableList;
+import com.bitcoin.predictionmarket.model.Contract;
public class ContractListAdapter extends BaseAdapter {
private final DecimalFormat doubleFormatter = new DecimalFormat("฿ #.##");
@@ -30,11 +31,11 @@
private final LayoutInflater layoutInflater;
private final ListView listView;
- private List<ListItem> listData = new ArrayList<ListItem>();
+ private List<Contract> listData = new ArrayList<Contract>();
private final SettableList settableList = new SettableList() {
@Override
- public void setList(List<ListItem> newListData) {
+ public void setList(List<Contract> newListData) {
ContractListAdapter.this.setList(newListData);
}
};
@@ -45,16 +46,6 @@ public void setList(List<ListItem> newListData) {
TextView contractExpiration;
TextView contractPrice;
}
-
- public abstract static class ListItem {
-
- }
-
- public static class ContractListItem extends ListItem {
- public String contractName;
- public Date contractExpiration;
- public double contractPrice;
- }
public ContractListAdapter(Context context, ListView listView) {
this.context = context;
@@ -77,11 +68,11 @@ public long getItemId(int position) {
return position;
}
- public List<ListItem> getList() {
+ public List<Contract> getList() {
return listData;
}
- void setList(List<ListItem> newListData) {
+ void setList(List<Contract> newListData) {
listData = newListData;
notifyDataSetChanged();
}
@@ -100,12 +91,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
convertView.setTag(contractViewHolder);
}
- final ContractListItem listItem = (ContractListItem) listData.get(position);
+ final Contract contract = (Contract) listData.get(position);
final ContractViewHolder viewHolder = (ContractViewHolder) convertView.getTag();
- viewHolder.contractName.setText(listItem.contractName);
- viewHolder.contractExpiration.setText(context.getString(R.string.expiresOn, dateFormatter.format(listItem.contractExpiration)));
- viewHolder.contractPrice.setText(doubleFormatter.format(listItem.contractPrice));
+ viewHolder.contractName.setText(contract.name);
+ viewHolder.contractExpiration.setText(context.getString(R.string.expiresOn, dateFormatter.format(contract.expiration)));
+ viewHolder.contractPrice.setText(doubleFormatter.format(contract.price));
return convertView;
}
@@ -118,9 +109,9 @@ public void syncWithServer(OnSyncCompleteListener listener) {
void onSyncComplete();
}
- static class SyncWithServer extends AsyncTask<Void, Void, List<ListItem>> {
+ static class SyncWithServer extends AsyncTask<Void, Void, List<Contract>> {
interface SettableList {
- void setList(List<ListItem> newListData);
+ void setList(List<Contract> newListData);
}
private final ListView listView;
@@ -140,20 +131,20 @@ protected void onPreExecute() {
}
@Override
- protected List<ListItem> doInBackground(Void... params) {
- final List<ListItem> result = new ArrayList<ListItem>();
+ protected List<Contract> doInBackground(Void... params) {
+ final List<Contract> result = new ArrayList<Contract>();
- ContractListItem a = new ContractListItem();
- a.contractName = "Dow Jones above 25000";
- a.contractExpiration = new Date(1388606400000L);
- a.contractPrice = 4.50;
+ Contract a = new Contract();
+ a.name = "Dow Jones above 25000";
+ a.expiration = new Date(1388606400000L);
+ a.price = 4.50;
result.add(a);
return result;
}
@Override
- protected void onPostExecute(List<ListItem> result) {
+ protected void onPostExecute(List<Contract> result) {
if (result.isEmpty()) {
((TextView) listView.getEmptyView()).setText(R.string.noContractsFound);
}
View
6 android/PredictionMarket/src/com/bitcoin/predictionmarket/fragment/ContractListFragment.java
@@ -11,9 +11,10 @@
import com.actionbarsherlock.app.SherlockListFragment;
import com.bitcoin.predictionmarket.R;
+import com.bitcoin.predictionmarket.activity.ContractDetailsActivity;
import com.bitcoin.predictionmarket.adapter.ContractListAdapter;
-import com.bitcoin.predictionmarket.adapter.ContractListAdapter.ListItem;
import com.bitcoin.predictionmarket.adapter.ContractListAdapter.OnSyncCompleteListener;
+import com.bitcoin.predictionmarket.model.Contract;
public class ContractListFragment extends SherlockListFragment implements OnSyncCompleteListener {
public static final String TAG = "ContractListFragment";
@@ -56,6 +57,7 @@ public void onSyncComplete() {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
- final ListItem item = (ListItem) contractListAdapter.getItem(position);
+ final Contract contract = (Contract) contractListAdapter.getItem(position);
+ ContractDetailsActivity.launch(getSherlockActivity(), contract);
}
}
View
12 android/PredictionMarket/src/com/bitcoin/predictionmarket/model/Contract.java
@@ -0,0 +1,12 @@
+package com.bitcoin.predictionmarket.model;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class Contract implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ public String name;
+ public Date expiration;
+ public double price;
+}

0 comments on commit c6a9339

Please sign in to comment.
Something went wrong with that request. Please try again.