Skip to content

Commit

Permalink
Merge pull request #31 from dymbe/johan-comment
Browse files Browse the repository at this point in the history
Implemented most of of the requested changes
  • Loading branch information
mvanmeerten authored Apr 14, 2020
2 parents 1b76978 + 0502e87 commit ca92e65
Show file tree
Hide file tree
Showing 10 changed files with 99,515 additions and 200,016 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum class AppDefinition(
),
TRUSTCHAIN_TRADER(
R.drawable.ic_device_hub_black_24dp,
"Trader",
"AI trading bot",
R.color.blue,
TrustChainTraderActivity::class.java
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mattskala.itemadapter.ItemAdapter
import kotlinx.android.synthetic.main.fragment_payload.*
import kotlinx.coroutines.*
Expand Down Expand Up @@ -81,7 +82,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
val payloadSerializable =
createPayloadSerializable(amount, price, type)
val payload = createPayload(amount, price, type)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(payload)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
marketCommunity.broadcast(payloadSerializable)
}

Expand Down Expand Up @@ -175,7 +177,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
"New ask came in! They are selling ${payload.amount} ${payload.primaryCurrency}. The price is ${payload.price} ${payload.secondaryCurrency} per ${payload.primaryCurrency}"
)
if (!(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.contains(payload)) {
TrustChainPayloadGeneratorActivity.payloads.add(payload)
TrustChainPayloadGeneratorActivity.payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
}
}
private fun bidListener(payload: TradePayload) {
Expand All @@ -184,7 +187,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
"New bid came in! They are asking ${payload.amount} ${payload.primaryCurrency}. The price is ${payload.price} ${payload.secondaryCurrency} per ${payload.primaryCurrency}"
)
if (!(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.contains(payload)) {
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(payload)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="parent"
tools:layout_editor_absoluteY="1dp" />


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
android:id="@+id/action_payloadCreateFragment_to_payloadFragment"
app:destination="@id/payloadFragment"
app:enterAnim="@anim/fragment_close_enter"
app:exitAnim="@anim/fragment_close_exit" />
app:exitAnim="@anim/fragment_close_exit"
app:popUpTo="@id/payloadFragment"
app:popUpToInclusive="true" />
</fragment>
</navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mattskala.itemadapter.ItemAdapter
import kotlinx.android.synthetic.main.fragment_trader.*
import kotlinx.coroutines.*
Expand Down Expand Up @@ -64,7 +65,7 @@ class TraderFragment : BaseFragment(R.layout.fragment_trader) {
}
loadCurrentPayloads((TrustChainTraderActivity.acceptedPayloads), "accepted")
loadCurrentPayloads((TrustChainTraderActivity.declinedPayloads), "declined")
ai = NaiveBayes(resources.openRawResource(R.raw.training_5600_mean1))
ai = NaiveBayes(resources.openRawResource(R.raw.ai_trading_data))
}

private fun loadCurrentPayloads(
Expand All @@ -86,9 +87,17 @@ class TraderFragment : BaseFragment(R.layout.fragment_trader) {
}
}
if (adapterString == "accepted") {
val adapterCount = adapterAccepted.itemCount
adapterAccepted.updateItems(items)
if (adapterCount != adapterAccepted.itemCount) {
acceptedPayloads.layoutManager!!.smoothScrollToPosition(acceptedPayloads, RecyclerView.State(), 0)
}
} else if (adapterString == "declined") {
val adapterCount = adapterDeclined.itemCount
adapterDeclined.updateItems(items)
if (adapterCount != adapterDeclined.itemCount) {
declinedPayloads.layoutManager!!.smoothScrollToPosition(declinedPayloads, RecyclerView.State(), 0)
}
}

binding.imgEmpty.isVisible = items.isEmpty() && (TrustChainTraderActivity.acceptedPayloads).isEmpty() && (TrustChainTraderActivity.declinedPayloads).isEmpty()
Expand All @@ -110,10 +119,14 @@ class TraderFragment : BaseFragment(R.layout.fragment_trader) {
} else if (type == 2) {
val price = round(payload.price!!.roundToInt() / payload.amount!!.roundToInt())
if (ai.predict(price) == 1) {
Log.d(
"PayloadFragment::onViewCreated",
"Accepted!"
)
accept(payload, 1)
}
} else {
(TrustChainTraderActivity.PayloadsList).declinedPayloads.add(payload)
(TrustChainTraderActivity.PayloadsList).declinedPayloads.add(0, payload)
}
}
}
Expand All @@ -133,7 +146,7 @@ class TraderFragment : BaseFragment(R.layout.fragment_trader) {
accept(payload, 0)
}
} else {
(TrustChainTraderActivity.PayloadsList).declinedPayloads.add(payload)
(TrustChainTraderActivity.PayloadsList).declinedPayloads.add(0, payload)
}
}
}
Expand All @@ -156,7 +169,7 @@ class TraderFragment : BaseFragment(R.layout.fragment_trader) {
// trustchain.createAcceptTxProposalBlock(payload.primaryCurrency,payload.secondaryCurrency,
// payload.amount?.toFloat(),payload.price?.toFloat(),payload.type, payload.publicKey)

(TrustChainTraderActivity.PayloadsList).acceptedPayloads.add(payload)
(TrustChainTraderActivity.PayloadsList).acceptedPayloads.add(0, payload)

if (type == 0) {
updateWallet(payload.amount!!, payload.price!!, type)
Expand Down
7 changes: 5 additions & 2 deletions trustchain-trader/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
app:layout_constraintEnd_toStartOf="@id/bottomNavigation"
app:navGraph="@navigation/nav_graph_trader" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:background="@color/light_gray"
app:menu="@menu/bottom_navigation_menu_trader"
app:layout_constraintBottom_toBottomOf="parent" />
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/navHostFragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 3 additions & 2 deletions trustchain-trader/src/main/res/layout/fragment_trader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@
<LinearLayout
android:id="@+id/Transactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/titlePayloadList"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/titlePayloadList"
app:layout_constraintBottom_toBottomOf="parent"
android:weightSum="2"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -160,4 +160,5 @@
app:layout_constraintBottom_toBottomOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit ca92e65

Please sign in to comment.