package com.example.jetpacksample

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mayi.fastdevelop.base.BaseActivity
import com.mayi.fastdevelop.base.BaseVMActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseVMActivity<MainViewModel>() {

//    private val viewmodel: MainViewModel by viewModels<MainViewModel>()
    private var itemAdapter: ItemAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        itemAdapter = ItemAdapter()
        list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        list.adapter = itemAdapter
        viewmodel.liveData.observe(this, Observer {
            if (it != null) {
                dismssLoadingDialog()
                if (it.ok) {
                    it!!.data?.let { it1 ->
                        itemAdapter!!.list!!.addAll(it1)
                        itemAdapter!!.notifyDataSetChanged()
                    }
                } else {
                    showToast(it.msg)
                }
            }
        })
        bottom.setOnClickListener {
            showLoadingDialog()
            viewmodel!!.requestData("1")
        }
    }
}