Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Japan example app #108

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, OrderInputFragment.newInstance())
.replace(R.id.container, OrderInputFragment.newInstance("8704"))
.commitNow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class OrderInputFragment : Fragment() {

companion object {
fun newInstance() = OrderInputFragment()
fun newInstance(symbol: String): OrderInputFragment {
val args = Bundle()
args.putString("symbol", symbol)
val fragment = OrderInputFragment()
fragment.arguments = args
return fragment
}
}

private lateinit var viewModel: OrderInputViewModel
Expand All @@ -29,6 +36,9 @@ class OrderInputFragment : Fragment() {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(activity!!).get(OrderInputViewModel::class.java)
// TODO: Use the ViewModel
arguments?.getString("symbol")?.let {
viewModel.init(it)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To init a ViewModel you need to create custom factory and use it with ViewModelProviders: https://android.jlelse.eu/android-viewmodel-with-custom-arguments-d0ff0fba29e1

}
viewModel.getOrderModel().observe(this, Observer { orderForm ->
orderForm?.run {
tvSymbolName.text = symbol.name
Expand Down Expand Up @@ -71,6 +81,7 @@ class OrderInputFragment : Fragment() {
btLimit.isChecked = false
Copy link

@falnatsheh falnatsheh Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts on having btLimit and btMarket boolean in the ViewModel vs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are actually the button itself, here we're make it selected or not. however it can be made more concise or nature if keep the status in viewModel and linked them together. I'll try

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the model turns out a better solution. I've got a OrderType in my OrderInfo already.

btMarket.isChecked = true
togglePriceType()
viewModel.resetPrice()
}
btLimit.setOnClickListener {
btLimit.isChecked = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ class OrderInputViewModel : ViewModel() {
}
}

fun init(symbol: String?) {
// TODO need a cleaner way to initialize the OrderForm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!this::orderForm.isInitialized is needed? since this is the init of the class I assume this::orderForm.isInitialized will return false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when screen rotates, Fragment will call init again. however we don't need to init the ViewModel since we should reuse the model in this case. I'm still looking if there is a way to call this init only once and when it's needed. (need to dig into how Fragment works for this)

Copy link

@falnatsheh falnatsheh Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When configuration changes the fragment is destroyed and then recreated by the fragment manager. However the ViewModel survives configuration changes so when the activity is recreated and you call the ViewModelProviders here you actually get the same instance. https://developer.android.com/topic/libraries/architecture/viewmodel#lifecycle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the view model factory works well. removed the init method now.

if (symbol != null) {
if (!this::orderForm.isInitialized || (this::orderForm.isInitialized && symbol != orderForm.value?.symbol?.symbol)) {
orderForm = MutableLiveData()
orderForm.value = OrderForm(
TradeItSDKHolder.getSymbolProvider().getJapanSymbol(symbol),
TradeItSDKHolder.getBuyingPower())
}
}
}

fun resetPrice() {
val value = orderForm.value
orderForm.value = value?.apply {
orderInfo = orderInfo.copy(limitPrice = symbol.price)
}
}
}

class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
Expand Down Expand Up @@ -94,13 +112,13 @@ class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {

//TODO temp solutions below
interface JapanSymbolProvider {
fun getJapanSymbol(symol: String): JapanSymbol
fun getJapanSymbol(symbol: String): JapanSymbol
}

class SampleJapanSymbol : JapanSymbolProvider {
override fun getJapanSymbol(symol: String): JapanSymbol {
override fun getJapanSymbol(symbol: String): JapanSymbol {
return JapanSymbol("カブドットコム証券(株)",
"8703", "東証1部", 386.0,
symbol, "東証1部", 386.0,
384.0, 286.0, 486.0, 100)
}
}
Expand Down
13 changes: 7 additions & 6 deletions exampleAppJapan/src/main/res/layout/order_input_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@

<android.support.v7.widget.LinearLayoutCompat
android:id="@+id/orderInfo"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toEndOf="@+id/quantityInput"
app:layout_constraintStart_toStartOf="@+id/quantityInput"
app:layout_constraintTop_toBottomOf="@+id/divider3">


Expand Down Expand Up @@ -342,8 +343,8 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="@+id/orderInfo"
app:layout_constraintStart_toStartOf="@+id/orderInfo"
app:layout_constraintEnd_toEndOf="@+id/quantityInput"
app:layout_constraintStart_toStartOf="@+id/quantityInput"
app:layout_constraintTop_toBottomOf="@+id/divider4">

<android.support.v7.widget.LinearLayoutCompat
Expand Down Expand Up @@ -417,8 +418,8 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="@+id/orderInfo"
app:layout_constraintStart_toStartOf="@+id/orderInfo"
app:layout_constraintEnd_toEndOf="@+id/quantityInput"
app:layout_constraintStart_toStartOf="@+id/quantityInput"
app:layout_constraintTop_toBottomOf="@+id/divider5">

<android.support.v7.widget.LinearLayoutCompat
Expand Down