egslava

Kotlin properties

Mar 21st, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. open class MyFragment : NavigationFragment() {
  2.  
  3.     val log = Logger("MyFragment")
  4.     val api: WebSocketService? get() = a().api
  5.     val token: String get() = a().token()
  6.     val cache by lazy { Cache(context) }
  7.     val buy = BuyOptionFragment()
  8.     val recentDeals = RecentDealsFragment()
  9.  
  10.     var asset: Asset = Asset()  // UNDEFINED BEHAVIOR!!! see #onViewCreated!
  11.         get() = field
  12.         set(value) {
  13.             if (field == value)return;
  14.             unsubscribe(field)
  15.             assetHistory(value) { story ->
  16.                 plot.renderer.plot.clear()
  17.                 story.sortBy { it.time() }
  18.                 plot.renderer.plot.addPoints( story.times(), story.values())
  19.                 subscribe(value)
  20.             }
  21.  
  22.             initExpirations(value)
  23.             if (buy.isAdded) buy.refund.max = value.max_refund ?: 100
  24.             field = value
  25.         }
  26.     fun subscribe(asset: Asset) = api?.net(subscribe(asset.id!!, token, null ))
  27.     fun unsubscribe(asset: Asset) = api?.net(unsubscribe(asset.id!!, token, null ))
  28.     fun assetHistory(asset: Asset, callback: (AssetStoryPoints) -> Unit) =
  29.         api?.net(getAssetHistory(token, asset.id!!) { request, response -> callback(response.of(asset)) })
  30.  
  31.     companion object {
  32.         val EXP_DATE = getTimeInstance(MEDIUM)
  33.     }
  34.     fun Long.asExpTime() = EXP_DATE.format( Date(this) ).removeSuffix(":00")
  35.  
  36.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = inflater.inflate(R.layout.fragment_trade, container, false)
  37.     override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
  38.         super.onViewCreated(view, savedInstanceState)
  39.  
  40.         asset = cache.assets.best()!!   // Choosing the best asset! Don't remove this string
  41.  
  42.         pager.adapter = StaticPagerAdapter(context, childFragmentManager, R.array.title_pager_trade_options, buy, recentDeals)
  43.         tabs.setupWithViewPager(pager)
  44.  
  45.          // run only after fragments was initialized
  46.         initAssetsPopup()
  47.         initBalance()
  48.         initExpirations(cache.assets.best())
  49.     }
  50.  
  51.     override fun onNetMessage(action: String?, message: Any?) {
  52.         super.onNetMessage(action, message)
  53.         when (action){
  54.             AssetsAction.ACTION -> initAssetsPopup()
  55.             ProfileAction.ACTION -> runOnUiThread { initBalance() }
  56.             PointAction.ACTION -> {
  57.                 log.v("PointAction")
  58.                 val point = (message as PointAction).points[0]
  59.                 log.v("message as PointAction")
  60.                 if (point.assetid == asset.id) {
  61.                     plot.renderer.plot.addPoint(point.stime, point.rate)
  62.                     log.v("condition is true")
  63.                 }
  64.                 log.v("point is added")
  65. //                {"action":"point","message":{"points":[{"assetid":35,"rate":3.203368,"stime":1457307341.7}]}}
  66.             }
  67. //            AssetsAction.ACTION -> (message as AssetsAction).
  68.         }
  69.     }
  70.  
  71.     fun initAssetsPopup() {
  72.         if(cache.assets.isEmpty()) {
  73.             choose_assets.invisible()
  74.             return
  75.         }
  76.         choose_assets.visible()
  77.         TradePopupHelper(choose_assets, cache.assets ?: listOf()) {
  78.             val result = result ?: return@TradePopupHelper
  79.             choose_assets.text = result.name
  80.             asset = result
  81.         }
  82.     }
  83.  
  84.     fun initExpirations(asset: Asset?) {
  85.         if (asset == null) {
  86.             expiration_times.invisible()
  87.             return
  88.         }
  89. //        this.asset = asset
  90.         expiration_times.visible()
  91.         expiration_times.onClick {
  92.             ListPopupWindow(context).apply {
  93.                 isModal = true
  94.                 val times = asset.times()
  95.                 setAdapter(ArrayAdapter(context, R.layout.item_exptime_popup, times.map { it.asExpTime() }.dropLast(1)))
  96.  
  97.                 anchorView = expiration_times
  98.                 horizontalOffset = -resources.getDimensionPixelOffset(R.dimen.std_margin)
  99.                 verticalOffset = resources.getDimensionPixelOffset(R.dimen.std_margin)
  100.                 setOnItemClickListener{ adapterView, view, position, id ->
  101.                     expiration_times.text = times[position].asExpTime()
  102.                     plot.renderer.plot.setExpTime(times[position] / 1000.0, times[position + 1] / 1000.0)
  103.                     runDelayedOnUiThread(150) { dismiss() }   // without delay there's no click animation
  104.                 }
  105.                 show()
  106.             }
  107.         }
  108. //        expiration_times.adapter =
  109.         log.v( "expiration step: ${asset.expiration_step}")
  110.  
  111.     }
  112.  
  113.     fun initBalance() { balance.text = cache.addCurrency(cache.profile.balanceToShow) }
  114.  
  115.     val firstTimeQueries = object : BroadcastReceiver(){
  116.         override fun onReceive(context: Context?, intent: Intent?) {
  117.             subscribe(asset)
  118.         }
  119.     }
  120.     override fun onStart() {
  121.         super.onStart()
  122.         context.registerReceiver(firstTimeQueries, IntentFilter(ON_RECONNECT))
  123.     }
  124.     override fun onStop() {
  125.         super.onStop()
  126.         context.unregisterReceiver(firstTimeQueries)
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment