lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
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
package com.kuanzhai.user.ui.crosscity
 
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.visible
import com.kuanzhai.user.R
import com.kuanzhai.user.network.HttpManager
import com.kuanzhai.user.network.requestByF
import com.kuanzhai.user.ui.MainActivity
import com.kuanzhai.user.ui.trip.BuyCardActivity
import com.kuanzhai.user.utils.Const
import com.kuanzhai.user.utils.event.BaseEvent
import kotlinx.android.synthetic.main.fragment_cross_city.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.support.v4.startActivityForResult
 
class CrossCityFragment:BaseFragment() {
    override fun contentViewId() = R.layout.fragment_cross_city
 
    private var startId:Int? = null //出发站点
    private var endId:Int? = null //到达站点
    private var startCity = ""
    private var endCity = ""
    private var receiver:BroadcastReceiver? = null
 
    var type = Const.OrderType.TYPE_CROSS_CITY //默认跨城
 
    override fun onFirstVisibleToUser() {
        initClick()
        EventBus.getDefault().register(this)
        if ((activity as MainActivity).isShowCard){
            tv_buy_card_2.visible()
        }else{
            tv_buy_card_2.gone()
        }
        receiver = object :BroadcastReceiver(){
            override fun onReceive(context: Context?, intent: Intent?) {
                if (intent?.action == "crossOrderSuccess"){
                    tv_start.text = ""
                    tv_end.text = ""
                    startId = null
                    endId = null
                    startCity = ""
                    endCity = ""
                    tv_action.gone()
                }
            }
        }
        activity?.registerReceiver(receiver, IntentFilter("crossOrderSuccess"))
        iv_img.setImageResource(if (type == Const.OrderType.TYPE_AIRBUS) R.mipmap.banner_air else R.mipmap.banner_cross)
    }
 
    private fun initClick() {
        tv_start.onClick {
            startActivityForResult<ChooseCityActivity>(Const.RequestCode.START_SITE,"type" to type)
        }
        tv_end.onClick {
            if (startId != null)
                startActivityForResult<ChooseCityActivity>(Const.RequestCode.END_SITE,"startId" to startId,"type" to type)
            else
                myToast("请先选择起点")
        }
        tv_action.onClick {
            if ((activity as MainActivity).checkLogin()){
                tv_action.isEnabled = false
                HttpManager.queryLines(startId!!,endId!!).requestByF(this@CrossCityFragment,success = {_,data->
                    tv_action.isEnabled = true
                    data?.apply {
                        when(size){
                            0-> myToast("没有可用线路")
                            1-> startActivity<ShiftActivity>("data" to this[0],"startCity" to startCity,"endCity" to endCity,"startId" to startId,"endId" to endId,"type" to type)
                            else->{
                                startActivity<ChooseLineActivity>("data" to this,"startCity" to startCity,"endCity" to endCity,"startId" to startId,"endId" to endId,"type" to type)
                            }
                        }
                    }
                }){_,_->
                    tv_action.isEnabled = true
                }
            }
        }
        tv_buy_card_2.setOnClickListener {
            if ((activity as MainActivity).checkLogin()){
                startActivity<BuyCardActivity>("type" to Const.OrderType.TYPE_CROSS_CITY)
            }
        }
    }
 
    fun changeType(type: Int) {
        if (this.type!= type){
            this.type = type
            val intent = Intent("crossOrderSuccess")
            requireActivity().sendBroadcast(intent)
            iv_img.setImageResource(if (type == Const.OrderType.TYPE_AIRBUS) R.mipmap.banner_air else R.mipmap.banner_cross)
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK&&data!=null){
            if (requestCode == Const.RequestCode.START_SITE){
                startId = data.getIntExtra("id",0)
                startCity = data.getStringExtra("city")?:""
                tv_start.text = data.getStringExtra("name")
                endId = null
                tv_end.text = ""
                tv_action.gone()
            }else{
                endId = data.getIntExtra("id",0)
                endCity = data.getStringExtra("city")?:""
                tv_end.text = data.getStringExtra("name")
//                iv_exchange.visible() //刘矩:站点不双向,不要交换按钮
                tv_action.visible()
            }
        }
    }
 
    @Subscribe
    fun onEvent(e: BaseEvent){
        if (e.type == Const.Event.EVENT_CARD_SHOW){
            if (e.obj as Boolean){
                tv_buy_card_2.visible()
            }else{
                tv_buy_card_2.gone()
            }
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (receiver!=null)
            activity?.unregisterReceiver(receiver)
        EventBus.getDefault().unregister(this)
    }
}