Guest User

Untitled

a guest
Nov 2nd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.97 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() , PlaceSelectionListener
  2. {
  3.  
  4. companion object {
  5. private const val PLACE_PICKER_REQUEST = 1
  6. }
  7.  
  8. /*Оперируемые переменные*/
  9. private var Provider_value:String = "Поставщик не выбран"
  10. private var Coal_value:String = "Уголь не выбран"
  11. private var Coal_price_value:Int = 0
  12. private var Coal_mass_value:Int = 0
  13. private var Address_value:String = ""
  14. private var Distance_value:Int = 0
  15. private var Delivery_price_value:Int = 0
  16. private var Total_price_value:Int = 0
  17. private var Start_ll:LatLng = LatLng(53.716, 91.429)
  18. private var End_ll: LatLng?=null
  19. private var Check: Boolean = false
  20. private var CheckArray: Boolean = true
  21. private var Provider_array = StartArray()
  22.  
  23. override fun onCreate(savedInstanceState: Bundle?) {
  24. super.onCreate(savedInstanceState)
  25. setContentView(R.layout.activity_main)
  26. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  27. initializeUI()
  28. }
  29.  
  30. fun initializeUI() {
  31.  
  32. val autocompleteFragment = fragmentManager.findFragmentById(R.id.autocomplete_fragment) as PlaceAutocompleteFragment
  33. autocompleteFragment.setOnPlaceSelectedListener(this)
  34. btnPickAddress.setOnClickListener { PlacePickerStart() }
  35. OneSpinnerContent(Provider_array)
  36. spinnerListener()
  37. MassTextListener()
  38.  
  39. }
  40.  
  41. override fun onPlaceSelected(p0: Place?) {
  42. if (p0 != null) {
  43. End_ll = p0.latLng
  44. Address_value = p0.address as String
  45. End_ll?.latitude?.let { End_ll?.longitude?.let { it1 -> LatLng(it, it1) } }?.let { urlRead(LatLng(Start_ll.latitude, Start_ll.longitude), it, "DRIVING") }
  46. }
  47. }
  48.  
  49. override fun onError(p0: Status?) {
  50. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  51. }
  52.  
  53. fun OneSpinnerContent(arg:Array<coalProvider>) {
  54. val adapter = ArrayAdapter<coalProvider>(applicationContext, android.R.layout.simple_spinner_item, arg)
  55. adapter.setDropDownViewResource(android.R.layout.simple_spinner_item)
  56. Provider_Vive_element_text.adapter = adapter
  57. }
  58.  
  59. fun TwoSpinnerContent(Coal_array: Array<coalProvider.Coal>) {
  60. val adapterSpTwo = ArrayAdapter<coalProvider.Coal>(applicationContext, android.R.layout.simple_spinner_item, Coal_array)
  61. adapterSpTwo.setDropDownViewResource(android.R.layout.simple_spinner_item)
  62. Coal_Vive_element_text.adapter = adapterSpTwo
  63. }
  64.  
  65. fun spinnerListener(){
  66. Provider_Vive_element_text.setOnTouchListener { v, event ->
  67. if (event.getAction() === MotionEvent.ACTION_UP) {
  68. if(CheckArray){
  69. Provider_array=arrayOfcoalProvider()
  70. OneSpinnerContent(Provider_array)
  71. CheckArray=false
  72. }
  73. }
  74. false
  75. }
  76. Provider_Vive_element_text.onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
  77. override fun onItemSelected(parent:AdapterView<*>, view:View, position:Int, id:Long)
  78. {
  79. val spinner_one_selected_item:coalProvider = Provider_Vive_element_text.selectedItem as coalProvider
  80. spinner_one_selected_item.Provider_latlng?.let { Start_ll = it }
  81. spinner_one_selected_item.Provider_coal[0].Coal_price?.let { Coal_price_value = it }
  82. TwoSpinnerContent(spinner_one_selected_item.Provider_coal)
  83. Provider_value = spinner_one_selected_item.Provider_name.toString()
  84. if(End_ll !=null){
  85. End_ll?.latitude?.let { End_ll?.longitude?.let { it1 -> LatLng(it, it1) } }?.let { urlRead(LatLng(Start_ll.latitude, Start_ll.longitude), it, "DRIVING") }
  86. }
  87. resUpdate()
  88. /*Предзданное значение на первую позицию при смене поставщика*/
  89. OnPiceParceUpdate(spinner_one_selected_item.Provider_coal[0].Coal_price)
  90. }
  91. override fun onNothingSelected(parent:AdapterView<*>) {
  92. }
  93. }
  94. Coal_Vive_element_text.onItemSelectedListener = object: AdapterView.OnItemSelectedListener
  95. {
  96. override fun onItemSelected(parent:AdapterView<*>, view:View, position:Int, id:Long) {
  97. val spinner_two_selected_item:coalProvider.Coal = Coal_Vive_element_text.selectedItem as coalProvider.Coal
  98. spinner_two_selected_item.Coal_price?.let { Coal_price_value = it }
  99.  
  100. Coal_value = spinner_two_selected_item.Coal_name.toString()
  101. resUpdate()
  102. Mass_price_Vive_element_text.text = Coal_price_value.toString()
  103. }
  104. override fun onNothingSelected(parent:AdapterView<*>) {
  105. }
  106. }
  107. }
  108.  
  109.  
  110. fun MassTextListener() {
  111. Coal_mass_Vive_element_text.addTextChangedListener(object: TextWatcher
  112. {
  113. override fun afterTextChanged(s: Editable) {}
  114. override fun beforeTextChanged(s:CharSequence, start:Int, count:Int, after:Int) {}
  115. override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int)
  116. {
  117. if (s.isNotBlank()) {
  118. when (s.toString().toInt()) {
  119. 0 -> Coal_mass_Vive_element_text.setText("1")
  120. !in 1..40 -> Coal_mass_Vive_element_text.setText("40")
  121. }
  122. Coal_mass_value = Coal_mass_Vive_element_text.text.toString().toInt()
  123. resUpdate()
  124. }
  125.  
  126. }
  127. })
  128. }
  129.  
  130. fun PlacePickerStart() {
  131.  
  132. val builder = PlacePicker.IntentBuilder()
  133. val latLng = LatLng(53.716, 91.429)
  134. builder.setLatLngBounds(LatLngBounds.builder().include(latLng).build())
  135. startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST)
  136. }
  137.  
  138. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  139. if (requestCode === PLACE_PICKER_REQUEST) {
  140. if (resultCode === AppCompatActivity.RESULT_OK) {
  141. val place = PlacePicker.getPlace(data, this)
  142. place.latLng?.let { End_ll = it }
  143. Address_value = place.address as String
  144. //urlRead(LatLng(0.0,0.0), LatLng(0.0,0.0),"DRIVING")
  145. End_ll?.latitude?.let { End_ll?.longitude?.let { it1 -> LatLng(it, it1) } }?.let { urlRead(LatLng(Start_ll.latitude, Start_ll.longitude), it, "DRIVING") }
  146. }
  147. }
  148.  
  149. }
  150.  
  151. fun urlRead (start: LatLng, end: LatLng, mode: String):LatLng {
  152. val url = ("http://maps.googleapis.com/maps/api/directions/json?"
  153. + "origin=" + start.latitude + "," + start.longitude
  154. + "&destination=" + end.latitude + "," + end.longitude
  155. + "&sensor=false&units=metric&mode=" + mode)
  156. url.httpGet().responseString { request, response, result ->
  157. when (result) {
  158. is Result.Failure -> {
  159. }
  160.  
  161. is Result.Success -> {
  162. if(jsonParse(result.value).isNotBlank()){
  163. Distance_value = jsonParse(result.value).toInt() / 1000
  164. resUpdate()
  165. Distance_Vive_element_text.text=Distance_value.toString()
  166. }
  167. else{
  168. Toast.makeText(this, "Нет пути", Toast.LENGTH_SHORT).show()
  169. }
  170.  
  171. }
  172. }
  173. }
  174. return end
  175. }
  176.  
  177. fun jsonParse(res: String):String{
  178. println("Result: $res")
  179. val parser: Parser = Parser()
  180. val stringBuilder: StringBuilder = StringBuilder(res)
  181. val json: JsonObject = parser.parse(stringBuilder) as JsonObject
  182. var jojo = json.lookup<String?>("routes.legs.distance.value").value.toString()
  183. return jojo.replace("[", "").replace("]", "")
  184. }
  185.  
  186. fun arrayOfcoalProvider(): Array<coalProvider> {
  187. val Provider_one = coalProvider("Разрез Аршановский", 0,LatLng(53.402971, 91.083748), Provider_coal = arrayOf(coalProvider.Coal("ДМСШ 0-25/1", 1500),coalProvider.Coal("ДО 25-50", 1700),coalProvider.Coal("ДПК 50-200", 1900),coalProvider.Coal("ДР 0-300 ", 1200)))
  188. val Provider_two = coalProvider("Разрез Белоярский", 1, LatLng(53.529799, 91.410684),Provider_coal = arrayOf(coalProvider.Coal("ДМСШ 0-25/2", 1501),coalProvider.Coal("ДО 25-50", 1701),coalProvider.Coal("ДПК 50-200", 1901),coalProvider.Coal("ДР 0-300 ", 1201)))
  189. val Provider_Three = coalProvider( "Разрез Черногорский", 2, LatLng(53.759367, 91.061604),Provider_coal = arrayOf(coalProvider.Coal("ДМСШ 0-25/3", 1502),coalProvider.Coal("ДО 25-50", 1702),coalProvider.Coal("ДПК 50-200", 1902),coalProvider.Coal("ДР 0-300 ", 1202)))
  190. val Provider_four = coalProvider( "Разрез Восточнобейский", 3, LatLng(53.326586, 91.361016),Provider_coal = arrayOf(coalProvider.Coal("ДМСШ 0-25/4", 1503),coalProvider.Coal("ДО 25-50", 1703),coalProvider.Coal("ДПК 50-200", 1903),coalProvider.Coal("ДР 0-300 ", 1203)))
  191. val Provider_Five = coalProvider( "Разрез Изыхский", 4, LatLng(53.630114, 91.436063),Provider_coal = arrayOf(coalProvider.Coal("ДМСШ 0-25/5", 1504),coalProvider.Coal("ДО 25-50", 1704),coalProvider.Coal("ДПК 50-200", 1904),coalProvider.Coal("ДР 0-300 ", 1204)))
  192. return arrayOf(Provider_one,Provider_two,Provider_Three,Provider_four,Provider_Five)
  193. }
  194. fun StartArray(): Array<coalProvider> {
  195. val Provider_Start = coalProvider("Выюерите поставщика", 0,LatLng(0.0, 0.0), Provider_coal = arrayOf(coalProvider.Coal("Поставщик не выбран", null)))
  196. return arrayOf(Provider_Start)
  197. }
  198.  
  199. fun resUpdate(){
  200. if(Address_value.isNotBlank()){
  201. findViewById<EditText>(R.id.place_autocomplete_search_input).setText(Address_value)
  202. }
  203. if(Distance_value != 0 && Coal_mass_value != 0){
  204. when (Coal_mass_value) {
  205. in 1..3 -> Delivery_price_value =(Distance_value * 10)
  206. in 4..7 -> Delivery_price_value =(Distance_value * 15)
  207. in 8..20 -> Delivery_price_value =(Distance_value * 35)
  208. in 21..40 -> Delivery_price_value =(Distance_value * 80)
  209. else -> 0f
  210. }
  211. Delivery_price_Vive_element_text.text=Delivery_price_value.toString()
  212. }
  213.  
  214. if(Coal_mass_value!=0 && Distance_value!=0 && Coal_price_value!=0){
  215. Total_price_value = Coal_mass_value * Coal_price_value + Delivery_price_value
  216. Total_price_Vive_element_text.text = Total_price_value.toString()
  217. Check = true
  218. }
  219.  
  220. }
  221.  
  222. fun OnPiceParceUpdate(PriceValue: Int?){
  223.  
  224. Mass_price_Vive_element_text.text = PriceValue.toString()
  225.  
  226. }
  227.  
  228. fun sendMessage(view:View) {
  229. resUpdate()
  230. if(Check){
  231. val intent = Intent(this, miActivity::class.java)
  232. intent.putExtra(Provider.t, Provider_value.toString())
  233. intent.putExtra(Coal.t, Coal_value.toString())
  234. intent.putExtra(Coal_price.t, Coal_price_value.toString())
  235. intent.putExtra(Coal_mass.t, Coal_mass_value.toString())
  236. intent.putExtra(Address.t, Address_value.toString())
  237. intent.putExtra(Delivery_price.t, Delivery_price_value.toString())
  238. intent.putExtra(Distance.t, Distance_value.toString())
  239. intent.putExtra(Total_price.t, Total_price_value.toString())
  240. startActivity(intent)
  241. }
  242. }
  243. }
  244.  
  245. class miActivity : AppCompatActivity() {
  246.  
  247. private var PLC: Int? =null
  248. private var Status:String =""
  249.  
  250. private var Login: String= "_________"
  251. private var pass: String="___________"
  252. private var PhoneNumProvider: Long = 79131816542
  253. private var PhoneNumUser: String = ""
  254.  
  255. private var Provider_value :String= ""
  256. private var Coal_value :String= ""
  257. private var Coal_price_value :String= ""
  258. private var Coal_mass_value :String= ""
  259. private var Address_value :String= ""
  260. private var Delivery_price_value :String= ""
  261. private var Distance_value :String= ""
  262. private var Total_price_value :String= ""
  263.  
  264. override fun onCreate(savedInstanceState: Bundle?) {
  265. super.onCreate(savedInstanceState)
  266. setContentView(R.layout.missage)
  267.  
  268. val intent = intent
  269. Provider_value = intent.getStringExtra(Provider.t)
  270. Coal_value = intent.getStringExtra(Coal.t)
  271. Coal_price_value = intent.getStringExtra(Coal_price.t)
  272. Coal_mass_value = intent.getStringExtra(Coal_mass.t)
  273. Address_value = intent.getStringExtra(Address.t)
  274. Delivery_price_value = intent.getStringExtra(Delivery_price.t)
  275. Distance_value = intent.getStringExtra(Distance.t)
  276. Total_price_value = intent.getStringExtra(Total_price.t)
  277. bindVive()
  278. alDig()
  279. phoneNum()
  280. }
  281.  
  282. fun alDig(){
  283. Order_Button_element.setOnClickListener {
  284. Show_Alert()
  285. }
  286. }
  287.  
  288. private fun Show_Alert() {
  289. alert("Вы уверенны что хотите совершить заказ? ","Подтверждение заказа") {
  290. positiveButton("Да") {
  291. toast("You clicked on YES")
  292. PLC = callNumber(VerificationCode())
  293. println(PLC.toString())
  294. VerCode()
  295. }
  296. negativeButton("Нет") {
  297. toast("You clicked on NO")
  298. }
  299. }.show()
  300. }
  301.  
  302. fun callNumber (code:Int): Int {
  303. val url = ("https://smsc.ru/sys/send.php?"
  304. + "login=" + "$Login"
  305. + "&psw=" + "$pass"
  306. + "&phones=" + "$PhoneNumUser"
  307. +"&mes="+"$code")
  308. url.httpGet().responseString { request, response, result ->
  309. when (result) {
  310. is Result.Failure -> {
  311. }
  312.  
  313. is Result.Success -> {
  314. }
  315. }
  316. }
  317. return code
  318. }
  319.  
  320. private fun VerCode() {
  321. alert {
  322. customView {
  323. verticalLayout {
  324. //Dialog Title
  325. toolbar {
  326. lparams(width = matchParent, height = wrapContent)
  327. title = "Код подтверждение"
  328. }
  329. val task = editText {
  330. hint = "Code hire "
  331. padding = dip(20)
  332. }
  333. positiveButton("Подтвердить") {
  334. if(task.text.toString() == PLC.toString() ){
  335. sendMissage()
  336. Status="Успешно"
  337. }
  338. else{
  339. Status="Введен не верный код"
  340. }
  341. alert("$Status","Подтверждение заказа") {
  342. positiveButton("Закрыть") {
  343. }
  344. }.show()
  345. }
  346. }
  347. }
  348. }.show()
  349. }
  350.  
  351. fun VerificationCode(): Int {
  352. fun Random.nextInt(range: IntRange): Int {
  353. return range.start + nextInt(range.last - range.start)
  354. }
  355. val random = Random()
  356. val verificationCode = random.nextInt(10000..999999)
  357. return verificationCode
  358. }
  359.  
  360. fun sendMissage (): Int {
  361. val url = ("https://smsc.ru/sys/send.php?"
  362. + "login=" + "$Login"
  363. + "&psw=" + "$pass"
  364. + "&phones=" + "$PhoneNumProvider"
  365. +"&mes="+"" +
  366. "Поставщик $Provider_valuen" +
  367. "Уголь $Coal_valuen" +
  368. "Цена $Coal_price_valuen" +
  369. "Масс $Coal_mass_valuen" +
  370. "Адрес $Address_valuen" +
  371. "Цена доставки $Delivery_price_valuen" +
  372. "Дистанция $Distance_valuen" +
  373. "Общая стоимость $Total_price_value")+"&charset="+"utf-8"
  374. url.httpGet().responseString { request, response, result ->
  375. when (result) {
  376. is Result.Failure -> {
  377. }
  378.  
  379. is Result.Success -> {
  380. }
  381. }
  382. }
  383. return end
  384. }
  385.  
  386. fun phoneNum(){
  387. Phone_number_Vive_element_text.addTextChangedListener(object : TextWatcher {
  388. override fun afterTextChanged(p0: Editable?) {}
  389.  
  390. override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
  391.  
  392. override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
  393. val p0 = p0.toString()
  394. PhoneNumUser = Phone_number_Vive_element_text.text.toString()
  395. }
  396. })
  397. }
  398.  
  399. fun bindVive(){
  400. Provider_Vive_element_text.text = Provider_value
  401. Coal_Vive_element_text.text = Coal_value
  402. Mass_price_Vive_element.text = Coal_price_value
  403. Coal_mass_Vive_element_text.text = Coal_mass_value
  404. Address_Vive_element_text.text = Address_value
  405. Distance_Vive_element_text.text = Distance_value
  406. Delivery_price_Vive_element_text.text = Delivery_price_value
  407. Total_price_Vive_element_text.text = Total_price_value
  408. }
  409.  
  410. }
Add Comment
Please, Sign In to add comment