package vn.hongyegroup.yybusiness.ui import android.os.Build import android.os.Bundle import android.view.View import android.view.WindowManager import android.view.inputmethod.EditorInfo import androidx.recyclerview.widget.LinearLayoutManager import com.android.basiclib.base.activity.BaseVVDActivity import com.android.basiclib.engine.dialog.PopupType import com.android.basiclib.engine.dialog.showPopup import com.android.basiclib.engine.toast.toast import com.android.basiclib.ext.click import com.android.basiclib.ext.gotoActivity import com.android.basiclib.ext.timeStampFormat2Date import com.android.basiclib.utils.CheckUtil import com.android.basiclib.utils.CommUtils import com.android.basiclib.utils.KeyboardUtils import com.android.basiclib.utils.StatusBarUtils import com.android.basiclib.utils.log.MyLogUtils import com.android.basiclib.view.gloading.Gloading import dagger.hilt.android.AndroidEntryPoint import vn.hongyegroup.yybusiness.R import vn.hongyegroup.yybusiness.adapter.AttendanceAdapter import vn.hongyegroup.yybusiness.databinding.ActivityMainBinding import vn.hongyegroup.yybusiness.databinding.PopopAreYouSureBinding import vn.hongyegroup.yybusiness.databinding.PopopSignCheckInOutBinding import vn.hongyegroup.yybusiness.entity.AttendanceBean import vn.hongyegroup.yybusiness.mvvm.MainViewModel import vn.hongyegroup.yybusiness.widget.AreYouSureUtil import vn.hongyegroup.yybusiness.widget.ShowCheckInUtil /** * 应用的首页 */ @AndroidEntryPoint class MainActivity : BaseVVDActivity() { protected lateinit var mGLoadingHolder: Gloading.Holder override fun init(savedInstanceState: Bundle?) { //沉浸式 StatusBarUtils.immersive(this) //允许 window 的内容可以上移到刘海屏状态栏 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val lp = window.attributes lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES window.setAttributes(lp) } StatusBarUtils.setMargin(mContext, mBinding.statusBarView) initLoadingView() showSelectedDate(false) initRV() initData() initListener() } private fun initLoadingView() { mGLoadingHolder = Gloading.getDefault().wrap(mBinding.flLoadBox).withRetry { onGoadingRetry() } } private fun onGoadingRetry() { initData() } private fun initData() { //请求接口 mViewModel.fetchAppliedList().observe(this) { // mBinding.refreshLayout.isRefreshing = false hideStateProgress() } } private fun initRV() { mBinding.indexableLayout.setLayoutManager(LinearLayoutManager(this)) // 设置数据适配器 mViewModel.mAdapter = AttendanceAdapter(this) mBinding.indexableLayout.setAdapter(mViewModel.mAdapter) // 设置右侧Index的滚动风格 mBinding.indexableLayout.setOverlayStyle_MaterialDesign(CommUtils.getColor(R.color.button_box_bg)) mBinding.indexableLayout.setStickyEnable(false) // 设置Item监听 mViewModel.mAdapter.setOnAttendanceListener(object : AttendanceAdapter.OnAttendanceListener { override fun onCheckIn(entity: AttendanceBean) { showCheckPopup(true, entity) } override fun onCheckOut(entity: AttendanceBean) { showCheckPopup(false, entity) } }) // mViewModel.mAdapter.setOnItemContentClickListener { view, originalPosition, currentPosition, entity -> // if (originalPosition >= 0) { // MyLogUtils.w("选中:" + entity.staff_name + " 当前位置:" + currentPosition + " 原始所在数组位置:" + originalPosition +" view id:${view.id}") // // //点击切换 // when (view.id) { // R.id.fl_sign_in_box -> { // toast("点击签到") // } // // R.id.fl_sign_out_box -> { // toast("点击签出") // } // // R.id.ll_name_box -> { // // } // } // // } else { // MyLogUtils.w("选中Header/Footer:" + entity.staff_name + " 当前位置:" + currentPosition) // } // // } // // mViewModel.mAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle -> // MyLogUtils.w("选中:$indexTitle 当前位置:$currentPosition") // } } /** * 展示签到签出的弹窗 */ private fun showCheckPopup(isCheckIn: Boolean, entity: AttendanceBean) { ShowCheckInUtil.showPopup(mActivity) { path -> if (!CheckUtil.isEmpty(path)) { doCheckInOut(isCheckIn, entity, path) } } } //调用接口签到签出 private fun doCheckInOut(checkIn: Boolean, entity: AttendanceBean, path: String?) { mViewModel.requestCheckInOut(checkIn, entity.applied_id, path).observe(this) { if (it != null) { if (checkIn) { entity.check_in_img = it.check_img entity.check_in_time = it.check_time } else { entity.check_out_img = it.check_img entity.check_out_time = it.check_time } mViewModel.mAdapter.notifyDataSetChanged() } } } //其他的监听 private fun initListener() { // mBinding.refreshLayout.setOnRefreshListener { // onRefresh() // } mBinding.llRefreshBox.click { onRefresh() } //退出登录事件 mBinding.btnLogout.click { AreYouSureUtil.showPopup(mActivity, "Are you sure you need to exit the system?") { doLogout() } } //筛选开始时间和结束时间 mBinding.tvPickStart.click { mViewModel.pickerDate(true, mActivity) } mBinding.tvPickEnd.click { mViewModel.pickerDate(false, mActivity) } //搜索相关 mBinding.etSearch.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 执行搜索操作 performSearch() true } else { false } } mBinding.ivSearch.click { performSearch() } //重置 mBinding.btnReset.click { resetFilteringOption() } } override fun startObserve() { super.startObserve() mViewModel.pickTimeLD.observe(this) { date -> if (date != null) { showSelectedDate(true) } } } //重置筛选选项 private fun resetFilteringOption() { mViewModel.mKeyword = null mBinding.etSearch.text = null mViewModel.mStartDateTimeStamp = System.currentTimeMillis() mViewModel.mEndDateTimeStamp = System.currentTimeMillis() showSelectedDate(true) } //执行搜索 private fun performSearch() { KeyboardUtils.hideSoftInput(this) val query = mBinding.etSearch.text.toString() MyLogUtils.w("performSearch query:$query") if (!CheckUtil.isEmpty(query)) { mViewModel.mKeyword = query onRefresh() } } //展示选中的日期 private fun showSelectedDate(needRefresh: Boolean) { mBinding.tvPickStart.text = mViewModel.mStartDateTimeStamp.timeStampFormat2Date() mBinding.tvPickEnd.text = mViewModel.mEndDateTimeStamp.timeStampFormat2Date() if (needRefresh) onRefresh() } private fun onRefresh() { // mBinding.refreshLayout.isRefreshing = true showStateProgress() initData() } //执行登出系统 private fun doLogout() { mViewModel.doLogout().observe(this) { if (it != null && it) { gotoActivity() finish() } } } override fun showStateLoading() { mGLoadingHolder.showLoading() mBinding.indexableLayout.visibility = View.GONE } override fun showStateSuccess() { mGLoadingHolder.showLoadSuccess() mBinding.indexableLayout.visibility = View.VISIBLE } override fun showStateError(message: String?) { mGLoadingHolder.showLoadFailed(message) mBinding.indexableLayout.visibility = View.GONE } override fun showStateNoData() { mGLoadingHolder.showEmpty() mBinding.indexableLayout.visibility = View.GONE } }