罗明文
2024-06-16 9673bcd57c6100ad9fdfbee728ef078104511fc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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)
    }
}