Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open class MyFragment : NavigationFragment() {
- val log = Logger("MyFragment")
- val api: WebSocketService? get() = a().api
- val token: String get() = a().token()
- val cache by lazy { Cache(context) }
- val buy = BuyOptionFragment()
- val recentDeals = RecentDealsFragment()
- var asset: Asset = Asset() // UNDEFINED BEHAVIOR!!! see #onViewCreated!
- get() = field
- set(value) {
- if (field == value)return;
- unsubscribe(field)
- assetHistory(value) { story ->
- plot.renderer.plot.clear()
- story.sortBy { it.time() }
- plot.renderer.plot.addPoints( story.times(), story.values())
- subscribe(value)
- }
- initExpirations(value)
- if (buy.isAdded) buy.refund.max = value.max_refund ?: 100
- field = value
- }
- fun subscribe(asset: Asset) = api?.net(subscribe(asset.id!!, token, null ))
- fun unsubscribe(asset: Asset) = api?.net(unsubscribe(asset.id!!, token, null ))
- fun assetHistory(asset: Asset, callback: (AssetStoryPoints) -> Unit) =
- api?.net(getAssetHistory(token, asset.id!!) { request, response -> callback(response.of(asset)) })
- companion object {
- val EXP_DATE = getTimeInstance(MEDIUM)
- }
- fun Long.asExpTime() = EXP_DATE.format( Date(this) ).removeSuffix(":00")
- override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = inflater.inflate(R.layout.fragment_trade, container, false)
- override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- asset = cache.assets.best()!! // Choosing the best asset! Don't remove this string
- pager.adapter = StaticPagerAdapter(context, childFragmentManager, R.array.title_pager_trade_options, buy, recentDeals)
- tabs.setupWithViewPager(pager)
- // run only after fragments was initialized
- initAssetsPopup()
- initBalance()
- initExpirations(cache.assets.best())
- }
- override fun onNetMessage(action: String?, message: Any?) {
- super.onNetMessage(action, message)
- when (action){
- AssetsAction.ACTION -> initAssetsPopup()
- ProfileAction.ACTION -> runOnUiThread { initBalance() }
- PointAction.ACTION -> {
- log.v("PointAction")
- val point = (message as PointAction).points[0]
- log.v("message as PointAction")
- if (point.assetid == asset.id) {
- plot.renderer.plot.addPoint(point.stime, point.rate)
- log.v("condition is true")
- }
- log.v("point is added")
- // {"action":"point","message":{"points":[{"assetid":35,"rate":3.203368,"stime":1457307341.7}]}}
- }
- // AssetsAction.ACTION -> (message as AssetsAction).
- }
- }
- fun initAssetsPopup() {
- if(cache.assets.isEmpty()) {
- choose_assets.invisible()
- return
- }
- choose_assets.visible()
- TradePopupHelper(choose_assets, cache.assets ?: listOf()) {
- val result = result ?: return@TradePopupHelper
- choose_assets.text = result.name
- asset = result
- }
- }
- fun initExpirations(asset: Asset?) {
- if (asset == null) {
- expiration_times.invisible()
- return
- }
- // this.asset = asset
- expiration_times.visible()
- expiration_times.onClick {
- ListPopupWindow(context).apply {
- isModal = true
- val times = asset.times()
- setAdapter(ArrayAdapter(context, R.layout.item_exptime_popup, times.map { it.asExpTime() }.dropLast(1)))
- anchorView = expiration_times
- horizontalOffset = -resources.getDimensionPixelOffset(R.dimen.std_margin)
- verticalOffset = resources.getDimensionPixelOffset(R.dimen.std_margin)
- setOnItemClickListener{ adapterView, view, position, id ->
- expiration_times.text = times[position].asExpTime()
- plot.renderer.plot.setExpTime(times[position] / 1000.0, times[position + 1] / 1000.0)
- runDelayedOnUiThread(150) { dismiss() } // without delay there's no click animation
- }
- show()
- }
- }
- // expiration_times.adapter =
- log.v( "expiration step: ${asset.expiration_step}")
- }
- fun initBalance() { balance.text = cache.addCurrency(cache.profile.balanceToShow) }
- val firstTimeQueries = object : BroadcastReceiver(){
- override fun onReceive(context: Context?, intent: Intent?) {
- subscribe(asset)
- }
- }
- override fun onStart() {
- super.onStart()
- context.registerReceiver(firstTimeQueries, IntentFilter(ON_RECONNECT))
- }
- override fun onStop() {
- super.onStop()
- context.unregisterReceiver(firstTimeQueries)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment