Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit c6a933930631af9cc4b47f6d20cd5a132227d5ae 2 parents 09ca8d2 + c813fe6
Marc authored
1  android/PredictionMarket/.settings/org.eclipse.core.resources.prefs
... ... @@ -1,2 +1,3 @@
1 1 eclipse.preferences.version=1
  2 +encoding//src/com/bitcoin/predictionmarket/activity/ContractDetailsActivity.java=UTF-8
2 3 encoding//src/com/bitcoin/predictionmarket/adapter/ContractListAdapter.java=UTF-8
5 android/PredictionMarket/AndroidManifest.xml
@@ -21,6 +21,11 @@
21 21 <category android:name="android.intent.category.LAUNCHER" />
22 22 </intent-filter>
23 23 </activity>
  24 + <activity
  25 + android:name="com.bitcoin.predictionmarket.activity.ContractDetailsActivity"
  26 + android:theme="@style/Theme.Sherlock.Light.DarkActionBar"
  27 + android:label="@string/contractDetailsTitle">
  28 + </activity>
24 29 </application>
25 30
26 31 </manifest>
1  android/PredictionMarket/project.properties
@@ -13,3 +13,4 @@
13 13 # Project target.
14 14 target=android-17
15 15 android.library.reference.1=../ActionBarSherlock-Forked/library
  16 +android.library.reference.2=../GraphView
27 android/PredictionMarket/res/layout/contract_details.xml
... ... @@ -0,0 +1,27 @@
  1 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2 + android:layout_width="fill_parent"
  3 + android:layout_height="fill_parent"
  4 + android:orientation="vertical"
  5 + android:padding="8dp" >
  6 +
  7 + <TextView
  8 + android:id="@+id/contractName"
  9 + android:layout_width="wrap_content"
  10 + android:layout_height="wrap_content"
  11 + android:textAppearance="?android:attr/textAppearanceLarge" />
  12 +
  13 + <TextView
  14 + android:id="@+id/contractExpiration"
  15 + android:layout_width="wrap_content"
  16 + android:layout_height="wrap_content"
  17 + android:ellipsize="end"
  18 + android:singleLine="true"
  19 + android:textAppearance="?android:attr/textAppearanceMedium" />
  20 +
  21 + <LinearLayout
  22 + android:id="@+id/graphRoot"
  23 + android:layout_width="fill_parent"
  24 + android:layout_height="0dp"
  25 + android:layout_weight="1" />
  26 +
  27 +</LinearLayout>
3  android/PredictionMarket/res/values/strings.xml
... ... @@ -1,7 +1,8 @@
1 1 <resources>
2 2
3   - <string name="app_name">Prediction Market</string>
  3 + <string name="app_name">Bit Predictor</string>
4 4 <string name="contracts">Contracts</string>
  5 + <string name="contractDetailsTitle">Contract Details</string>
5 6 <string name="noContractsFound">No contracts found</string>
6 7 <string name="loading">Loading...</string>
7 8 <string name="expiresOn">Expires on %s</string>
117 android/PredictionMarket/src/com/bitcoin/predictionmarket/activity/ContractDetailsActivity.java
... ... @@ -0,0 +1,117 @@
  1 +package com.bitcoin.predictionmarket.activity;
  2 +
  3 +import java.text.DateFormat;
  4 +import java.text.DecimalFormat;
  5 +import java.text.SimpleDateFormat;
  6 +import java.util.Random;
  7 +
  8 +import android.content.Context;
  9 +import android.content.Intent;
  10 +import android.graphics.Color;
  11 +import android.os.Bundle;
  12 +import android.widget.LinearLayout;
  13 +import android.widget.TextView;
  14 +
  15 +import com.actionbarsherlock.app.ActionBar;
  16 +import com.actionbarsherlock.app.SherlockFragmentActivity;
  17 +import com.actionbarsherlock.view.MenuItem;
  18 +import com.bitcoin.predictionmarket.R;
  19 +import com.bitcoin.predictionmarket.model.Contract;
  20 +import com.jjoe64.graphview.GraphView;
  21 +import com.jjoe64.graphview.GraphView.GraphViewData;
  22 +import com.jjoe64.graphview.GraphViewSeries;
  23 +import com.jjoe64.graphview.GraphViewStyle;
  24 +import com.jjoe64.graphview.LineGraphView;
  25 +
  26 +public class ContractDetailsActivity extends SherlockFragmentActivity {
  27 + private static final String BUNDLE_CONTRACT = "BUNDLE_CONTRACT";
  28 +
  29 + private final DateFormat dateFormatter = SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL);
  30 + private final DecimalFormat doubleFormatter = new DecimalFormat("฿ #.##");
  31 + private final DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM);
  32 +
  33 + public static void launch(Context context, Contract contract) {
  34 + Intent intent = new Intent(context, ContractDetailsActivity.class);
  35 + intent.putExtra(BUNDLE_CONTRACT, contract);
  36 + context.startActivity(intent);
  37 + }
  38 +
  39 + @Override
  40 + protected void onCreate(Bundle savedInstanceState) {
  41 + super.onCreate(savedInstanceState);
  42 + setContentView(R.layout.contract_details);
  43 +
  44 + ActionBar actionBar = getSupportActionBar();
  45 + actionBar.setDisplayHomeAsUpEnabled(true);
  46 +
  47 + Contract contract = (Contract) getIntent().getSerializableExtra(BUNDLE_CONTRACT);
  48 + ((TextView)findViewById(R.id.contractName)).setText(contract.name);
  49 + ((TextView)findViewById(R.id.contractExpiration)).setText(getString(R.string.expiresOn, dateFormatter.format(contract.expiration)));
  50 +
  51 + GraphView graphView = new LineGraphView(this, "example") {
  52 + @Override
  53 + protected String formatLabel(double value, boolean isValueX) {
  54 + if (isValueX) {
  55 + return timeFormatter.format(value);
  56 + } else {
  57 + return doubleFormatter.format(value);
  58 + }
  59 + }
  60 + };
  61 + graphView.addSeries(genRandomWalkDataForDay()); // data
  62 + graphView.setScalable(true);
  63 + graphView.setGraphViewStyle(new GraphViewStyle(Color.rgb(0, 0, 0), Color.rgb(0, 0, 0), Color.rgb(96, 96, 96)));
  64 +
  65 + LinearLayout layout = (LinearLayout) findViewById(R.id.graphRoot);
  66 + layout.addView(graphView);
  67 + }
  68 +
  69 + @Override
  70 + protected void onResume() {
  71 + super.onResume();
  72 +
  73 + // Apparently works around a bug that manifests itself on some Android
  74 + // versions.
  75 + getSherlock().dispatchInvalidateOptionsMenu();
  76 + }
  77 +
  78 + @Override
  79 + public boolean onOptionsItemSelected(MenuItem item) {
  80 + if (item.getItemId() == android.R.id.home) {
  81 + MainActivity.launchFromChild(this);
  82 + finish();
  83 + return true;
  84 + } else {
  85 + return super.onOptionsItemSelected(item);
  86 + }
  87 + }
  88 +
  89 + private GraphViewSeries genRandomWalkDataForDay() {
  90 + final Random random = new Random();
  91 + final int size = 240;
  92 + final double min = 0;
  93 + final double max = 10;
  94 + final double range = max - min;
  95 + final double walkPerTick = range / 1000.0;
  96 + double current = min + random.nextDouble() * range;
  97 +
  98 + final long now = System.currentTimeMillis();
  99 + final long dayMs = 60 * 60 * 24 * 1000;
  100 + final long interval = dayMs / size;
  101 +
  102 + GraphViewData[] graphViewData = new GraphViewData[size];
  103 +
  104 + for (int i = 0; i < size; i++) {
  105 + graphViewData[i] = new GraphViewData(now - interval * (size - i), current);
  106 + double delta = (random.nextDouble() - 0.5) * walkPerTick;
  107 + current += delta;
  108 + current = clamp(current, min, max);
  109 + }
  110 +
  111 + return new GraphViewSeries(graphViewData);
  112 + }
  113 +
  114 + private double clamp(double val, double min, double max) {
  115 + return Math.max(min, Math.min(val, max));
  116 + }
  117 +}
8 android/PredictionMarket/src/com/bitcoin/predictionmarket/activity/MainActivity.java
@@ -4,6 +4,8 @@
4 4 package com.bitcoin.predictionmarket.activity;
5 5
6 6 import android.annotation.SuppressLint;
  7 +import android.content.Context;
  8 +import android.content.Intent;
7 9 import android.os.Bundle;
8 10 import android.support.v4.view.ViewPager;
9 11
@@ -16,6 +18,12 @@
16 18 public class MainActivity extends SherlockFragmentActivity {
17 19 private ViewPager viewPager;
18 20 private TabsAdapter tabsAdapter;
  21 +
  22 + public static void launchFromChild(Context context) {
  23 + Intent intent = new Intent(context, MainActivity.class);
  24 + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  25 + context.startActivity(intent);
  26 + }
19 27
20 28 @SuppressLint("NewApi")
21 29 @Override
45 android/PredictionMarket/src/com/bitcoin/predictionmarket/adapter/ContractListAdapter.java
@@ -21,6 +21,7 @@
21 21
22 22 import com.bitcoin.predictionmarket.R;
23 23 import com.bitcoin.predictionmarket.adapter.ContractListAdapter.SyncWithServer.SettableList;
  24 +import com.bitcoin.predictionmarket.model.Contract;
24 25
25 26 public class ContractListAdapter extends BaseAdapter {
26 27 private final DecimalFormat doubleFormatter = new DecimalFormat("฿ #.##");
@@ -30,11 +31,11 @@
30 31 private final LayoutInflater layoutInflater;
31 32 private final ListView listView;
32 33
33   - private List<ListItem> listData = new ArrayList<ListItem>();
  34 + private List<Contract> listData = new ArrayList<Contract>();
34 35
35 36 private final SettableList settableList = new SettableList() {
36 37 @Override
37   - public void setList(List<ListItem> newListData) {
  38 + public void setList(List<Contract> newListData) {
38 39 ContractListAdapter.this.setList(newListData);
39 40 }
40 41 };
@@ -45,16 +46,6 @@ public void setList(List<ListItem> newListData) {
45 46 TextView contractExpiration;
46 47 TextView contractPrice;
47 48 }
48   -
49   - public abstract static class ListItem {
50   -
51   - }
52   -
53   - public static class ContractListItem extends ListItem {
54   - public String contractName;
55   - public Date contractExpiration;
56   - public double contractPrice;
57   - }
58 49
59 50 public ContractListAdapter(Context context, ListView listView) {
60 51 this.context = context;
@@ -77,11 +68,11 @@ public long getItemId(int position) {
77 68 return position;
78 69 }
79 70
80   - public List<ListItem> getList() {
  71 + public List<Contract> getList() {
81 72 return listData;
82 73 }
83 74
84   - void setList(List<ListItem> newListData) {
  75 + void setList(List<Contract> newListData) {
85 76 listData = newListData;
86 77 notifyDataSetChanged();
87 78 }
@@ -100,12 +91,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
100 91 convertView.setTag(contractViewHolder);
101 92 }
102 93
103   - final ContractListItem listItem = (ContractListItem) listData.get(position);
  94 + final Contract contract = (Contract) listData.get(position);
104 95 final ContractViewHolder viewHolder = (ContractViewHolder) convertView.getTag();
105 96
106   - viewHolder.contractName.setText(listItem.contractName);
107   - viewHolder.contractExpiration.setText(context.getString(R.string.expiresOn, dateFormatter.format(listItem.contractExpiration)));
108   - viewHolder.contractPrice.setText(doubleFormatter.format(listItem.contractPrice));
  97 + viewHolder.contractName.setText(contract.name);
  98 + viewHolder.contractExpiration.setText(context.getString(R.string.expiresOn, dateFormatter.format(contract.expiration)));
  99 + viewHolder.contractPrice.setText(doubleFormatter.format(contract.price));
109 100
110 101 return convertView;
111 102 }
@@ -118,9 +109,9 @@ public void syncWithServer(OnSyncCompleteListener listener) {
118 109 void onSyncComplete();
119 110 }
120 111
121   - static class SyncWithServer extends AsyncTask<Void, Void, List<ListItem>> {
  112 + static class SyncWithServer extends AsyncTask<Void, Void, List<Contract>> {
122 113 interface SettableList {
123   - void setList(List<ListItem> newListData);
  114 + void setList(List<Contract> newListData);
124 115 }
125 116
126 117 private final ListView listView;
@@ -140,20 +131,20 @@ protected void onPreExecute() {
140 131 }
141 132
142 133 @Override
143   - protected List<ListItem> doInBackground(Void... params) {
144   - final List<ListItem> result = new ArrayList<ListItem>();
  134 + protected List<Contract> doInBackground(Void... params) {
  135 + final List<Contract> result = new ArrayList<Contract>();
145 136
146   - ContractListItem a = new ContractListItem();
147   - a.contractName = "Dow Jones above 25000";
148   - a.contractExpiration = new Date(1388606400000L);
149   - a.contractPrice = 4.50;
  137 + Contract a = new Contract();
  138 + a.name = "Dow Jones above 25000";
  139 + a.expiration = new Date(1388606400000L);
  140 + a.price = 4.50;
150 141 result.add(a);
151 142
152 143 return result;
153 144 }
154 145
155 146 @Override
156   - protected void onPostExecute(List<ListItem> result) {
  147 + protected void onPostExecute(List<Contract> result) {
157 148 if (result.isEmpty()) {
158 149 ((TextView) listView.getEmptyView()).setText(R.string.noContractsFound);
159 150 }
6 android/PredictionMarket/src/com/bitcoin/predictionmarket/fragment/ContractListFragment.java
@@ -11,9 +11,10 @@
11 11
12 12 import com.actionbarsherlock.app.SherlockListFragment;
13 13 import com.bitcoin.predictionmarket.R;
  14 +import com.bitcoin.predictionmarket.activity.ContractDetailsActivity;
14 15 import com.bitcoin.predictionmarket.adapter.ContractListAdapter;
15   -import com.bitcoin.predictionmarket.adapter.ContractListAdapter.ListItem;
16 16 import com.bitcoin.predictionmarket.adapter.ContractListAdapter.OnSyncCompleteListener;
  17 +import com.bitcoin.predictionmarket.model.Contract;
17 18
18 19 public class ContractListFragment extends SherlockListFragment implements OnSyncCompleteListener {
19 20 public static final String TAG = "ContractListFragment";
@@ -56,6 +57,7 @@ public void onSyncComplete() {
56 57
57 58 @Override
58 59 public void onListItemClick(ListView l, View v, int position, long id) {
59   - final ListItem item = (ListItem) contractListAdapter.getItem(position);
  60 + final Contract contract = (Contract) contractListAdapter.getItem(position);
  61 + ContractDetailsActivity.launch(getSherlockActivity(), contract);
60 62 }
61 63 }
12 android/PredictionMarket/src/com/bitcoin/predictionmarket/model/Contract.java
... ... @@ -0,0 +1,12 @@
  1 +package com.bitcoin.predictionmarket.model;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.Date;
  5 +
  6 +public class Contract implements Serializable {
  7 + private static final long serialVersionUID = 1L;
  8 +
  9 + public String name;
  10 + public Date expiration;
  11 + public double price;
  12 +}

0 comments on commit c6a9339

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