package com.dollearn.student.ui.home
|
|
import android.annotation.SuppressLint
|
import android.app.Activity
|
import android.content.Intent
|
import androidx.core.os.bundleOf
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.utils.myToast
|
import cn.sinata.xldutils.utils.showAllowingStateLoss
|
import com.dollearn.student.R
|
import com.dollearn.student.dialog.PayDialog
|
import com.dollearn.student.dialog.TipDialog
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.Place
|
import com.dollearn.student.network.requestByF
|
import com.dollearn.student.ui.course.MyReservationActivity
|
import com.dollearn.student.ui.home.adapter.MyPlaceAdapter
|
import com.dollearn.student.utils.Const
|
import com.dollearn.student.utils.Const.EventCode.CHANGE_PAY_SUCCESS
|
import com.dollearn.student.utils.event.InfoEvent
|
import com.dollearn.student.utils.interfaces.StringCallback
|
import com.dollearn.student.utils.pay.PayListener
|
import com.dollearn.student.utils.pay.PayUtil
|
import kotlinx.android.synthetic.main.layout_common_list.*
|
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.Subscribe
|
import org.jetbrains.anko.support.v4.runOnUiThread
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
import java.util.*
|
|
class MyPlaceFragment : BaseFragment(), PayListener {
|
override fun contentViewId() = R.layout.layout_common_list
|
|
private val type by lazy { arguments?.getInt("type", TYPE_ALL) ?: TYPE_ALL }
|
private var page = 1
|
private val list = arrayListOf<Place>()
|
private val callback = object : MyPlaceAdapter.Callback {
|
override fun onPay(position: Int) {
|
PayDialog.show(childFragmentManager, false, object : StringCallback {
|
override fun onResult(rst: String) {
|
pay(list[position].id, rst.toInt())
|
}
|
}, list[position].payMoney, list[position].playPaiCoin)
|
}
|
}
|
|
private val adapter = MyPlaceAdapter(list, callback)
|
private var timer: Timer? = null
|
private var task: TimerTask? = null
|
|
override fun onFirstVisibleToUser() {
|
rv_list.layoutManager = LinearLayoutManager(requireContext())
|
rv_list.adapter = adapter
|
refreshLayout.setOnRefreshListener {
|
page = 1
|
getData()
|
}
|
refreshLayout.setOnLoadMoreListener {
|
page++
|
getData()
|
}
|
getData()
|
adapter.setOnItemClickListener { view, position ->
|
startActivityForResult<MyReservationActivity>(1, "id" to list[position].id)
|
}
|
EventBus.getDefault().register(this)
|
}
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
@Subscribe
|
fun onEvent(e: InfoEvent) {
|
if (e.code == Const.EventCode.CHANGE_CANCEL_SUCCESS) {
|
val item = e.info as String
|
for (b in list.indices) {
|
if (list[b].id == item) {
|
list[b].status = 5
|
}
|
}
|
adapter.notifyDataSetChanged()
|
|
} else if (e.code == CHANGE_PAY_SUCCESS) {
|
refreshLayout.autoRefresh()
|
}
|
}
|
|
private fun getData() {
|
HttpManager.queryMySite(page, if (type == -1) null else type)
|
.requestByF(this, success = { _, data ->
|
if (page == 1)
|
list.clear()
|
list.addAll(data ?: arrayListOf())
|
adapter.notifyDataSetChanged()
|
if (list.isEmpty())
|
refreshLayout.finishRefreshWithNoMoreData()
|
else if (data.isNullOrEmpty())
|
refreshLayout.finishLoadMoreWithNoMoreData()
|
else if (page == 1)
|
refreshLayout.finishRefresh()
|
else
|
refreshLayout.finishLoadMore()
|
if (!list.filter { it.status == 0 }.isNullOrEmpty() && timer == null) {
|
timer = Timer()
|
task = object : TimerTask() {
|
override fun run() {
|
runOnUiThread {
|
adapter.notifyDataSetChanged()
|
}
|
}
|
}
|
timer?.schedule(task, 1000L, 1000L)
|
}
|
}) { _, _ ->
|
if (page == 1)
|
refreshLayout.finishRefresh(false)
|
else
|
refreshLayout.finishLoadMore(false)
|
page--
|
}
|
}
|
|
private fun cancelOrder(position: Int) {
|
val tipDialog = TipDialog()
|
tipDialog.arguments = bundleOf("msg" to "确认取消本次预约吗?")
|
tipDialog.setCallback(object : TipDialog.OnClickCallback {
|
override fun onOk() {
|
HttpManager.cancelMySite(list[position].id)
|
.requestByF(this@MyPlaceFragment) { _, data ->
|
myToast("取消成功")
|
list[position].status = 5
|
adapter.notifyItemChanged(position)
|
}
|
}
|
|
override fun onCancel() {
|
}
|
})
|
tipDialog.showAllowingStateLoss(childFragmentManager, "cancel")
|
}
|
|
private fun pay(id: String, payType: Int) {
|
HttpManager.continuePaymentMySite(id, payType).requestByF(this, success = { _, data ->
|
if (payType == 2) {
|
PayUtil.aliPay(requireContext(), data?.orderInfo ?: "")
|
} else if (payType == 3) {
|
onPaySuccess()
|
} else {
|
PayUtil.weChatPay(data!!)
|
}
|
}) { _, msg ->
|
}
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (requestCode == Activity.RESULT_OK)
|
refreshLayout.autoRefresh()
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
EventBus.getDefault().unregister(this)
|
timer?.cancel()
|
timer = null
|
task = null
|
PayUtil.removePayListener(this)
|
}
|
|
companion object {
|
//0=待支付,1=待核销,2=已核销,3=已完成,4=已过期,5=已取消
|
const val TYPE_ALL = -1
|
const val TYPE_UN_PAY = 0
|
const val TYPE_UN_USE = 1
|
const val TYPE_USING = 2
|
const val TYPE_EXPIRE = 4
|
const val TYPE_CANCELED = 5
|
fun newInstance(type: Int): MyPlaceFragment {
|
val myPlaceFragment = MyPlaceFragment()
|
myPlaceFragment.arguments = bundleOf("type" to type)
|
return myPlaceFragment
|
}
|
}
|
|
override fun onPaySuccess() {
|
refreshLayout.autoRefresh()
|
}
|
|
override fun onPayCancel() {
|
}
|
|
override fun onPayError(msg: String) {
|
myToast(msg)
|
}
|
}
|