package cn.muchinfo.rma.view.base.future.holds

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.toArrayList
import cn.muchinfo.rma.view.base.BaseFragment

/**
 * 持仓列表界面
 */
class HoldFragment : BaseFragment<HoldViewModel>() {

    companion object {
        fun getInstance() = HoldFragment()
    }
//region views
    private var _view: View? = null
    private var idHoldList: RecyclerView? = null // 持仓列表
//endregion
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _view = inflater.inflate(R.layout.hold_fragment, container, false)
        initViews()
        viewModel.holdLiveData.bindOptional(context!!) { data ->
            (idHoldList?.adapter as HoldViewModel.HoldAdapter).update(data?.toArrayList())
        }
        return _view
    }

    private fun initViews() {
        idHoldList = _view?.findViewById(R.id.id_hold_list)

        val layoutManager = LinearLayoutManager(context)
        idHoldList?.layoutManager = layoutManager
        idHoldList?.adapter = viewModel.getAdapter()
    }

    override fun onResume() {
        super.onResume()
        viewModel.queryHold()
    }
}