MainActivity.kt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package vn.hongyegroup.yybusiness.ui
  2. import android.os.Build
  3. import android.os.Bundle
  4. import android.view.View
  5. import android.view.WindowManager
  6. import android.view.inputmethod.EditorInfo
  7. import androidx.recyclerview.widget.LinearLayoutManager
  8. import com.android.basiclib.base.activity.BaseVVDActivity
  9. import com.android.basiclib.engine.dialog.PopupType
  10. import com.android.basiclib.engine.dialog.showPopup
  11. import com.android.basiclib.engine.toast.toast
  12. import com.android.basiclib.ext.click
  13. import com.android.basiclib.ext.gotoActivity
  14. import com.android.basiclib.ext.timeStampFormat2Date
  15. import com.android.basiclib.utils.CheckUtil
  16. import com.android.basiclib.utils.CommUtils
  17. import com.android.basiclib.utils.KeyboardUtils
  18. import com.android.basiclib.utils.StatusBarUtils
  19. import com.android.basiclib.utils.log.MyLogUtils
  20. import com.android.basiclib.view.gloading.Gloading
  21. import dagger.hilt.android.AndroidEntryPoint
  22. import vn.hongyegroup.yybusiness.R
  23. import vn.hongyegroup.yybusiness.adapter.AttendanceAdapter
  24. import vn.hongyegroup.yybusiness.databinding.ActivityMainBinding
  25. import vn.hongyegroup.yybusiness.databinding.PopopAreYouSureBinding
  26. import vn.hongyegroup.yybusiness.databinding.PopopSignCheckInOutBinding
  27. import vn.hongyegroup.yybusiness.entity.AttendanceBean
  28. import vn.hongyegroup.yybusiness.mvvm.MainViewModel
  29. import vn.hongyegroup.yybusiness.widget.AreYouSureUtil
  30. import vn.hongyegroup.yybusiness.widget.ShowCheckInUtil
  31. /**
  32. * 应用的首页
  33. */
  34. @AndroidEntryPoint
  35. class MainActivity : BaseVVDActivity<MainViewModel, ActivityMainBinding>() {
  36. protected lateinit var mGLoadingHolder: Gloading.Holder
  37. override fun init(savedInstanceState: Bundle?) {
  38. //沉浸式
  39. StatusBarUtils.immersive(this)
  40. //允许 window 的内容可以上移到刘海屏状态栏
  41. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  42. val lp = window.attributes
  43. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
  44. window.setAttributes(lp)
  45. }
  46. StatusBarUtils.setMargin(mContext, mBinding.statusBarView)
  47. initLoadingView()
  48. showSelectedDate(false)
  49. initRV()
  50. initData()
  51. initListener()
  52. }
  53. private fun initLoadingView() {
  54. mGLoadingHolder = Gloading.getDefault().wrap(mBinding.flLoadBox).withRetry {
  55. onGoadingRetry()
  56. }
  57. }
  58. private fun onGoadingRetry() {
  59. initData()
  60. }
  61. private fun initData() {
  62. //请求接口
  63. mViewModel.fetchAppliedList().observe(this) {
  64. // mBinding.refreshLayout.isRefreshing = false
  65. hideStateProgress()
  66. }
  67. }
  68. private fun initRV() {
  69. mBinding.indexableLayout.setLayoutManager(LinearLayoutManager(this))
  70. // 设置数据适配器
  71. mViewModel.mAdapter = AttendanceAdapter(this)
  72. mBinding.indexableLayout.setAdapter(mViewModel.mAdapter)
  73. // 设置右侧Index的滚动风格
  74. mBinding.indexableLayout.setOverlayStyle_MaterialDesign(CommUtils.getColor(R.color.button_box_bg))
  75. mBinding.indexableLayout.setStickyEnable(false)
  76. // 设置Item监听
  77. mViewModel.mAdapter.setOnAttendanceListener(object : AttendanceAdapter.OnAttendanceListener {
  78. override fun onCheckIn(entity: AttendanceBean) {
  79. showCheckPopup(true, entity)
  80. }
  81. override fun onCheckOut(entity: AttendanceBean) {
  82. showCheckPopup(false, entity)
  83. }
  84. })
  85. // mViewModel.mAdapter.setOnItemContentClickListener { view, originalPosition, currentPosition, entity ->
  86. // if (originalPosition >= 0) {
  87. // MyLogUtils.w("选中:" + entity.staff_name + " 当前位置:" + currentPosition + " 原始所在数组位置:" + originalPosition +" view id:${view.id}")
  88. //
  89. // //点击切换
  90. // when (view.id) {
  91. // R.id.fl_sign_in_box -> {
  92. // toast("点击签到")
  93. // }
  94. //
  95. // R.id.fl_sign_out_box -> {
  96. // toast("点击签出")
  97. // }
  98. //
  99. // R.id.ll_name_box -> {
  100. //
  101. // }
  102. // }
  103. //
  104. // } else {
  105. // MyLogUtils.w("选中Header/Footer:" + entity.staff_name + " 当前位置:" + currentPosition)
  106. // }
  107. //
  108. // }
  109. //
  110. // mViewModel.mAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
  111. // MyLogUtils.w("选中:$indexTitle 当前位置:$currentPosition")
  112. // }
  113. }
  114. /**
  115. * 展示签到签出的弹窗
  116. */
  117. private fun showCheckPopup(isCheckIn: Boolean, entity: AttendanceBean) {
  118. ShowCheckInUtil.showPopup(mActivity) { path ->
  119. if (!CheckUtil.isEmpty(path)) {
  120. doCheckInOut(isCheckIn, entity, path)
  121. }
  122. }
  123. }
  124. //调用接口签到签出
  125. private fun doCheckInOut(checkIn: Boolean, entity: AttendanceBean, path: String?) {
  126. mViewModel.requestCheckInOut(checkIn, entity.applied_id, path).observe(this) {
  127. if (it != null) {
  128. if (checkIn) {
  129. entity.check_in_img = it.check_img
  130. entity.check_in_time = it.check_time
  131. } else {
  132. entity.check_out_img = it.check_img
  133. entity.check_out_time = it.check_time
  134. }
  135. mViewModel.mAdapter.notifyDataSetChanged()
  136. }
  137. }
  138. }
  139. //其他的监听
  140. private fun initListener() {
  141. // mBinding.refreshLayout.setOnRefreshListener {
  142. // onRefresh()
  143. // }
  144. //退出登录事件
  145. mBinding.btnLogout.click {
  146. AreYouSureUtil.showPopup(mActivity, "Are you sure you need to exit the system?") {
  147. doLogout()
  148. }
  149. }
  150. //筛选开始时间和结束时间
  151. mBinding.tvPickStart.click {
  152. mViewModel.pickerDate(true, mActivity)
  153. }
  154. mBinding.tvPickEnd.click {
  155. mViewModel.pickerDate(false, mActivity)
  156. }
  157. //搜索相关
  158. mBinding.etSearch.setOnEditorActionListener { v, actionId, event ->
  159. if (actionId == EditorInfo.IME_ACTION_SEARCH) {
  160. // 执行搜索操作
  161. performSearch()
  162. true
  163. } else {
  164. false
  165. }
  166. }
  167. mBinding.ivSearch.click {
  168. performSearch()
  169. }
  170. //重置
  171. mBinding.btnReset.click {
  172. AreYouSureUtil.showPopup(mActivity, "Are you sure you want to reset the filtering options?") {
  173. resetFilteringOption()
  174. }
  175. }
  176. }
  177. override fun startObserve() {
  178. super.startObserve()
  179. mViewModel.pickTimeLD.observe(this) { date ->
  180. if (date != null) {
  181. showSelectedDate(true)
  182. }
  183. }
  184. }
  185. //重置筛选选项
  186. private fun resetFilteringOption() {
  187. mViewModel.mKeyword = null
  188. mBinding.etSearch.text = null
  189. mViewModel.mStartDateTimeStamp = System.currentTimeMillis()
  190. mViewModel.mEndDateTimeStamp = System.currentTimeMillis()
  191. showSelectedDate(true)
  192. }
  193. //执行搜索
  194. private fun performSearch() {
  195. KeyboardUtils.hideSoftInput(this)
  196. val query = mBinding.etSearch.text.toString()
  197. MyLogUtils.w("performSearch query:$query")
  198. if (!CheckUtil.isEmpty(query)) {
  199. mViewModel.mKeyword = query
  200. onRefresh()
  201. }
  202. }
  203. //展示选中的日期
  204. private fun showSelectedDate(needRefresh: Boolean) {
  205. mBinding.tvPickStart.text = mViewModel.mStartDateTimeStamp.timeStampFormat2Date()
  206. mBinding.tvPickEnd.text = mViewModel.mEndDateTimeStamp.timeStampFormat2Date()
  207. if (needRefresh) onRefresh()
  208. }
  209. private fun onRefresh() {
  210. // mBinding.refreshLayout.isRefreshing = true
  211. showStateProgress()
  212. initData()
  213. }
  214. //执行登出系统
  215. private fun doLogout() {
  216. mViewModel.doLogout().observe(this) {
  217. if (it != null && it) {
  218. gotoActivity<LoginActivity>()
  219. finish()
  220. }
  221. }
  222. }
  223. override fun showStateLoading() {
  224. mGLoadingHolder.showLoading()
  225. mBinding.indexableLayout.visibility = View.GONE
  226. }
  227. override fun showStateSuccess() {
  228. mGLoadingHolder.showLoadSuccess()
  229. mBinding.indexableLayout.visibility = View.VISIBLE
  230. }
  231. override fun showStateError(message: String?) {
  232. mGLoadingHolder.showLoadFailed(message)
  233. mBinding.indexableLayout.visibility = View.GONE
  234. }
  235. override fun showStateNoData() {
  236. mGLoadingHolder.showEmpty()
  237. mBinding.indexableLayout.visibility = View.GONE
  238. }
  239. }