riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package cn.flightfeather.thirdapp.module.task
 
import android.app.Activity
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import android.widget.AdapterView
import cn.flightfeather.thirdapp.R
import cn.flightfeather.thirdapp.activity.MapActivity
import cn.flightfeather.thirdapp.adapter.AllListViewAdapter
import cn.flightfeather.thirdapp.bean.entity.*
import cn.flightfeather.thirdapp.model.event.SceneEvent
import cn.flightfeather.thirdapp.module.base.BaseActivity
import cn.flightfeather.thirdapp.util.Domain
import cn.flightfeather.thirdapp.util.UUIDGenerator
import kotlinx.android.synthetic.main.activity_scense_detail.*
import kotlinx.android.synthetic.main.layout_actionbar.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.toast
import java.text.DecimalFormat
import java.util.*
 
/**
 * @author riku
 * Date: 2019/8/6
 * 场景详情界面,新建场景或修改场景
 */
const val START_LOCATION = 1
const val NO_CHOICE = "请选择"
 
class SceneDetailActivity : BaseActivity(), View.OnClickListener {
 
    override fun getLayoutId(): Int = R.layout.activity_scense_detail
 
    lateinit var viewModel: SceneDetailViewModel
 
    lateinit var provinceAdapter: AllListViewAdapter<Province>
    lateinit var cityAdapter: AllListViewAdapter<City>
    lateinit var districtAdapter: AllListViewAdapter<District>
    lateinit var townAdapter: AllListViewAdapter<Town>
    lateinit var sceneTypeAdapter: AllListViewAdapter<Domainitem>
 
    //模式(0:新增场景信息;1:修改场景信息)
    private var mode = 0
    private var scene: Scense? = null
    private var title = "新建场景"
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
 
        viewModel = ViewModelProviders.of(this).get(SceneDetailViewModel::class.java)
 
        mode = intent.getIntExtra("mode", 0)
        scene = intent.getSerializableExtra("updateScene") as Scense?
 
        initUI()
        scene?.let {
            title = "修改场景信息"
            setSceneDetail(it)
        }
 
        viewModel.provinceList.observe(this, Observer {
            provinceAdapter.notifyDataSetChanged()
        })
 
        viewModel.allSceneTypeList.observe(this, Observer {
            sceneTypeAdapter.notifyDataSetChanged()
            scene?.let { setSceneType(it.type) }
        })
 
        viewModel.getData()
        viewModel.getSceneType()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == START_LOCATION && resultCode == Activity.RESULT_OK) {
            data?.let {
                val longitude = it.getSerializableExtra("Longitude") as Double
                val latitude = it.getSerializableExtra("Latitude") as Double
                val df = DecimalFormat("0.000000")
                et_lng.setText( df.format(longitude))
                et_lat.setText(df.format(latitude))
 
                img_location.setImageResource(R.drawable.icon_mark_green)
                tv_location.text = "定位完成"
            }
 
        }
    }
 
    private fun initUI() {
        provinceAdapter = object : AllListViewAdapter<Province>(viewModel.provinceList.value, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder?, obj: Province?) {
                holder?.setText(R.id.tv_item, obj?.provincename)
            }
        }
        cityAdapter = object : AllListViewAdapter<City>(viewModel.cityList, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder?, obj: City?) {
                holder?.setText(R.id.tv_item, obj?.cityname)
            }
        }
        districtAdapter = object : AllListViewAdapter<District>(viewModel.districtList, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder?, obj: District?) {
                holder?.setText(R.id.tv_item, obj?.districtname)
            }
        }
        townAdapter = object : AllListViewAdapter<Town>(viewModel.townList, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder?, obj: Town?) {
                holder?.setText(R.id.tv_item, obj?.townname)
            }
        }
        sceneTypeAdapter = object : AllListViewAdapter<Domainitem>(viewModel.allSceneTypeList.value, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder?, obj: Domainitem?) {
                holder?.setText(R.id.tv_item, obj?.text)
            }
        }
 
        sp_province.apply {
            adapter = provinceAdapter
            onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onNothingSelected(parent: AdapterView<*>?) = Unit
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    viewModel.cityList.apply {
                        clear()
                        val pId = (sp_province.selectedItem as Province).provinceid
                        viewModel.allCityList.forEach {
                            if (it.pronvinceid == pId) {
                                add(it)
                            }
                        }
                    }
 
                    cityAdapter.notifyDataSetChanged()
                }
            }
        }
 
        sp_city.apply {
            adapter = cityAdapter
            onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onNothingSelected(parent: AdapterView<*>?) = Unit
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    viewModel.districtList.apply {
                        clear()
                        add(District().apply { districtname = NO_CHOICE })
                        val cId = (sp_city.selectedItem as City).cityId
                        viewModel.allDistrictList.forEach {
                            if (it.cityid == cId) {
                                add(it)
                            }
                        }
                    }
 
                    districtAdapter.notifyDataSetChanged()
                }
            }
        }
 
        sp_district.apply {
            adapter = districtAdapter
            onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onNothingSelected(parent: AdapterView<*>?) = Unit
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    viewModel.townList.apply {
                        clear()
                        add(Town().apply { townname = NO_CHOICE })
                        val dId = (sp_district.selectedItem as District).districtid
                        viewModel.allTownList.forEach {
                            if (it.districtid == dId) {
                                add(it)
                            }
                        }
                    }
 
                    townAdapter.notifyDataSetChanged()
 
                    scene?.let { setLocation(it) }
                }
            }
        }
 
        sp_town.apply {
            adapter = townAdapter
        }
 
        sp_type.apply {
            adapter = sceneTypeAdapter
        }
 
        img_location.setOnClickListener(this)
        tv_save.setOnClickListener(this)
 
        img_left.setOnClickListener(this)
        img_right.visibility = View.GONE
        spinner_topclass_task.visibility = View.GONE
 
    }
 
    private fun submit() {
        if (check()) {
            AlertDialog.Builder(this).apply {
                setTitle("确认是否提交?")
                setPositiveButton(R.string.yes) {dialog, which ->
                    save(mode)
                }
                setNegativeButton(R.string.cancel, null)
            }.show()
        } else {
            application.toast("请将信息填写完整!")
        }
    }
 
    private fun setSceneDetail(scene: Scense) {
        actionbar_title.text = title
        et_scene_name.setText(scene.name)
        et_scene_address.setText(scene.location)
        et_scene_contact.setText(scene.contacts)
        et_scene_telephone.setText(scene.contactst)
        et_scene_wechat.setText(scene.contactswx)
        et_lng.apply {
            setText(scene.longitude?.toString())
            isEnabled = false
        }
        et_lat.apply {
            setText(scene.latitude?.toString())
            isEnabled = false
        }
        img_location.apply {
            setImageResource(R.drawable.icon_mark_green)
            setOnClickListener(null)
        }
        tv_location.text = "无法修改"
    }
 
    private fun setSceneType(typeName: String?) {
        typeName?.let {
            for (i in 0 until sceneTypeAdapter.count) {
                if (sceneTypeAdapter.getItem(i).text == it) {
                    sp_type.setSelection(i)
                }
            }
        }
    }
 
    private fun setLocation(scene: Scense) {
        scene.provincename?.let {
            for (i in 0 until provinceAdapter.count) {
                if (provinceAdapter.getItem(i).provincename == it) {
                    sp_province.setSelection(i)
                }
            }
        }
        scene.cityname?.let {
            for (i in 0 until cityAdapter.count) {
                if (cityAdapter.getItem(i).cityname == it) {
                    sp_city.setSelection(i)
                }
            }
        }
        scene.districtname?.let {
            for (i in 0 until districtAdapter.count) {
                if (districtAdapter.getItem(i).districtname == it) {
                    sp_district.setSelection(i)
                }
            }
        }
        scene.townname?.let {
            for (i in 0 until townAdapter.count) {
                if (townAdapter.getItem(i).townname == it) {
                    sp_town.setSelection(i)
                }
            }
        }
    }
 
    private fun check():Boolean {
        if (et_scene_name.text.toString().isEmpty()) return false
        if (et_scene_address.text.toString().isEmpty()) return false
        if (et_scene_contact.text.toString().isEmpty()) return false
        if (et_scene_telephone.text.toString().isEmpty()) return false
        if ((sp_district.selectedItem as District).districtname == NO_CHOICE) return false
        if ((sp_town.selectedItem as Town).townname == NO_CHOICE) return false
        if (et_lng.text.toString().isEmpty()) return false
        if (et_lat.text.toString().isEmpty()) return false
        return true
    }
 
    private fun save(mode: Int) {
        when (mode) {
            //新增
            0 -> {
                val newScene = Scense().apply {
                    guid = UUIDGenerator.generate16ShortUUID()
                    name = et_scene_name.text.toString()
 
                    (sp_type.selectedItem as Domainitem).let {
                        typeid = it.value.toByte()
                        type = it.text
                    }
                    location = et_scene_address.text.toString()
                    longitude = et_lng.text.toString().toDouble()
                    latitude = et_lat.text.toString().toDouble()
                    (sp_province.selectedItem as Province).let {
                        provincecode = it.provincecode
                        provincename = it.provincename
                    }
                    (sp_city.selectedItem as City).let {
                        citycode = it.citycode
                        cityname = it.cityname
                    }
                    (sp_district.selectedItem as District).let {
                        districtcode = it.districtcode
                        districtname = it.districtname
                    }
                    (sp_town.selectedItem as Town).let {
                        towncode = it.towncode
                        townname = it.townname
                    }
                    contactst = et_scene_telephone.text.toString()
                    contactswx = et_scene_wechat.text.toString()
                    contacts = et_scene_contact.text.toString()
                    createdate = Date()
                    updatedate = Date()
                    extension1 = Domain.SCENSE_AVAILABLE
                }
 
                viewModel.putScene(newScene)
            }
            //修改
            1 -> {
                scene?.apply {
                    name = et_scene_name.text.toString()
 
                    (sp_type.selectedItem as Domainitem).let {
                        typeid = it.value.toByte()
                        type = it.text
                    }
                    location = et_scene_address.text.toString()
                    longitude = et_lng.text.toString().toDouble()
                    latitude = et_lat.text.toString().toDouble()
                    (sp_province.selectedItem as Province).let {
                        provincecode = it.provincecode
                        provincename = it.provincename
                    }
                    (sp_city.selectedItem as City).let {
                        citycode = it.citycode
                        cityname = it.cityname
                    }
                    (sp_district.selectedItem as District).let {
                        districtcode = it.districtcode
                        districtname = it.districtname
                    }
                    (sp_town.selectedItem as Town).let {
                        towncode = it.towncode
                        townname = it.townname
                    }
                    contactst = et_scene_telephone.text.toString()
                    contactswx = et_scene_wechat.text.toString()
                    contacts = et_scene_contact.text.toString()
 
                    updatedate = Date()
                }
 
                viewModel.updateScene(scene)
            }
        }
    }
 
    @Subscribe
    fun onSubmitDone(sceneEvent: SceneEvent) {
        finish()
    }
 
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.img_left -> {
                finish()
            }
            R.id.img_location -> {
                startActivityForResult(Intent(this, MapActivity::class.java), START_LOCATION)
            }
            R.id.tv_save -> {
                submit()
            }
        }
    }
}