Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.comtrade.aicapone.Activities
- import android.content.Intent
- import android.graphics.Bitmap
- import android.os.Bundle
- import android.preference.PreferenceManager
- import android.util.Log
- import android.view.View
- import android.widget.Button
- import android.widget.ImageView
- import android.widget.TextView
- import com.comtrade.aicapone.LightClient
- import com.comtrade.aicapone.MapsService
- import com.comtrade.aicapone.Models.Job
- import com.comtrade.aicapone.Models.Role
- import com.comtrade.aicapone.Models.Status
- import com.comtrade.aicapone.Models.User
- import com.comtrade.aicapone.R
- import com.comtrade.aicapone.Services.JobService
- import com.comtrade.aicapone.Services.StorageService
- import com.comtrade.aicapone.Util.Message
- import com.comtrade.aicapone.Util.MessageType
- import com.google.android.gms.common.ConnectionResult
- import com.google.android.gms.maps.CameraUpdateFactory
- import com.google.android.gms.maps.GoogleMap
- import com.google.android.gms.maps.model.*
- import com.google.maps.model.TravelMode
- import kotlinx.android.synthetic.main.load_board_job_details.*
- import kotlinx.android.synthetic.main.load_board_jobs.*
- import retrofit2.Call
- import retrofit2.Callback
- import retrofit2.Response
- import android.graphics.BitmapFactory
- import java.io.ByteArrayInputStream
- class LoadBoardDetailsActivity : MapActivity() {
- var mStartPos : TextView?=null
- var mDestination : TextView?=null
- var mJobDescription : TextView?=null
- var mJobDistance : TextView?=null
- var mJobLoadLimit : TextView?=null
- var mJobValue : TextView?=null
- var mContact : TextView?=null
- var mTakeJobBtn: Button?=null
- var mCompleteJobBtn: Button?=null
- var job : Job? = null
- var mJobId: Long? = null
- var jobService: JobService? = null
- private var mMap: GoogleMap? = null
- private val mapService = MapsService(this)
- var markerOptions1: MarkerOptions? = null
- var markerOptions2: MarkerOptions? = null
- private var marker1: Marker? = null
- private var marker2: Marker? = null
- var mUser: User? = null
- var mStorageService: StorageService? = null
- var mToken: String? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- jobService = JobService(getString(R.string.apiBaseURL))
- setContentView(R.layout.load_board_job_details)
- mStorageService = StorageService()
- mToken = mStorageService!!.getToken(this)
- mStartPos = findViewById(R.id.txtStart)
- mDestination = findViewById(R.id.txtDestination)
- mJobDescription = findViewById(R.id.job_desc)
- mJobLoadLimit = findViewById(R.id.job_loadLimit)
- mJobDistance = findViewById(R.id.job_distance)
- mJobValue = findViewById(R.id.job_value)
- mContact = findViewById(R.id.contactID)
- mTakeJobBtn = findViewById(R.id.btnTakeJob)
- mCompleteJobBtn = findViewById(R.id.btnCompleteJob)
- if (intent.extras["load_job_id"] != null) {
- mJobId = intent.extras["load_job_id"] as Long?
- if (mJobId != null) {
- loadJob()
- }
- } else {
- job = getIntent().getExtras().getSerializable("key_job") as? Job
- Log.d("CLICK_ON_ITEM", "Intent: " + (job?.dropAddress))
- displayJob()
- Log.e("QR bin",""+job!!.QRImg)
- }
- }
- fun getJobQR()
- {
- jobService!!.getQR(mJobId!!, mToken).enqueue(object : Callback<String> {
- override fun onFailure(call: Call<String>?, t: Throwable?) {
- Log.d("retrofit failure", "message" + t!!.message)
- Message.showMessage(getString(R.string.job_take_err_msg), MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- override fun onResponse(call: Call<String>?, response: Response<String>?) {
- if (response != null) {
- Log.d("retrofit response", response.toString())
- if (response.isSuccessful) {
- Log.e("QR Response",response.body()!!)
- job!!.QRImg = response.body()!!
- Log.e("QR bin",""+job!!.QRImg)
- } else {
- Log.d("retrofit response", "Job load failed")
- }
- }
- }
- })
- }
- fun displayJob() {
- mStartPos?.text = job?.loadAddress
- mDestination?.text = job?.dropAddress
- getJobQR()
- fill(this.job!!)
- val latLngAdr1 = mapService.getLatLongFromString(job?.loadAddress!!)
- val latLngAdr2 = mapService.getLatLongFromString(job?.dropAddress!!)
- markerOptions1 = mapService.createMarkerOptions(latLngAdr1!!, job?.loadAddress!!, BitmapDescriptorFactory.fromResource(R.drawable.ic_truck))
- markerOptions2 = mapService.createMarkerOptions(latLngAdr2!!, job?.dropAddress!!, BitmapDescriptorFactory.fromResource(R.drawable.ic_box5))
- mapService.getMapAsync(R.id.load_board_det_map)
- mCompleteJobBtn?.setOnClickListener{ completeJob() }
- mTakeJobBtn?.setOnClickListener {
- if(mStorageService!!.getActiveVehicle(this)!=null&&mStorageService!!.getActiveVehicle(this)?.LoadLimit!! > this.job!!.weight!!)
- takeJob()
- else {
- Message.showMessage(getString(R.string.truck_limit_err_msg), MessageType.DANGER, this)
- val i = Intent(this, UserVehicleActivity::class.java)
- startActivity(i)
- }
- }
- mUser = mStorageService!!.getUser(this)
- if (mUser!!.roleId == Role.BROKER.value || job!!.shipperId != null) {
- mTakeJobBtn!!.visibility = View.GONE
- }
- if(job!!.shipperId != mUser!!.id || job!!.statusId != Status.INPROGRESS.value) {
- mCompleteJobBtn!!.visibility = View.GONE
- }
- }
- fun loadJob() {
- jobService!!.getById(mJobId!!, mToken).enqueue(object : Callback<Job> {
- override fun onFailure(call: Call<Job>?, t: Throwable?) {
- Log.d("retrofit failure", "message" + t!!.message)
- Message.showMessage(getString(R.string.job_take_err_msg), MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- override fun onResponse(call: Call<Job>?, response: Response<Job>?) {
- if (response != null) {
- Log.d("retrofit response", response.toString())
- if (response.isSuccessful) {
- job = response.body()!!
- displayJob()
- Log.e("QR bin",""+job!!.QRImg)
- } else {
- Log.d("retrofit response", "Job load failed")
- }
- }
- }
- })
- }
- fun takeJob() {
- val preferences = PreferenceManager.getDefaultSharedPreferences(this@LoadBoardDetailsActivity)
- val walletName = preferences.getString("account_wallet_name", null)
- var password: String? = preferences.getString("account_wallet_password", null)
- if (walletName == null) {
- //ovde ispisati poruku da je potrebno kreirati wallet pre uzimanja posla i izaci iz funkcije
- }
- jobService!!.takeJob(job?.id!!, mToken).enqueue(object : Callback<Unit> {
- override fun onFailure(call: Call<Unit>?, t: Throwable?) {
- Log.d("retrofit failure", "message " + t!!.message)
- Message.showMessage(getString(R.string.something_went_wrong_msg), MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- override fun onResponse(call: Call<Unit>?, response: Response<Unit>?) {
- if (response != null) {
- Log.d("retrofit response", response.toString())
- if (response.isSuccessful) {
- if (walletName != null) {
- //if treba skloniti kad se gore ubaci poruka
- var lightClient: LightClient = LightClient(this@LoadBoardDetailsActivity)
- lightClient.TakeDelivery(job!!.id!!, password!!, walletName)
- }
- Message.showMessage(getString(R.string.job_taken_smg), MessageType.SUCCESS, this@LoadBoardDetailsActivity)
- mapService.drawRoute(marker1?.title!!, marker2?.title!!, TravelMode.DRIVING)
- mTakeJobBtn!!.visibility = View.GONE
- mCompleteJobBtn!!.visibility = View.VISIBLE
- } else {
- Message.showMessage("Something went wrong, please try again.", MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- }
- }
- })
- }
- fun completeJob() {
- var jobNameTextField = textViewJobName
- jobService!!.complete(job?.id!!, mToken).enqueue(object : Callback<Unit> {
- override fun onFailure(call: Call<Unit>?, t: Throwable?) {
- Log.d("retrofit failure", "message " + t!!.message)
- Message.showMessage(getString(R.string.something_went_wrong_msg), MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- override fun onResponse(call: Call<Unit>?, response: Response<Unit>?) {
- if (response != null) {
- Log.d("retrofit response", response.toString())
- if (response.isSuccessful) {
- val preferences = PreferenceManager.getDefaultSharedPreferences(this@LoadBoardDetailsActivity)
- val fromAddress = preferences.getString("account_wallet_name", null)
- val password = preferences.getString("account_wallet_password", null)
- var lightClient: LightClient = LightClient(this@LoadBoardDetailsActivity)
- var out = lightClient.CompleteDelivery(job?.id!!, password, fromAddress)
- Message.showMessage(getString(R.string.job_completed_msg), MessageType.SUCCESS, this@LoadBoardDetailsActivity)
- mapService.drawRoute(marker1?.title!!, marker2?.title!!, TravelMode.DRIVING)
- mTakeJobBtn!!.visibility = View.GONE
- mCompleteJobBtn!!.visibility = View.GONE
- jobNameTextField.setText(jobNameTextField.text.toString().plus(" (Completed) "))
- } else {
- Message.showMessage(getString(R.string.something_went_wrong_msg), MessageType.DANGER, this@LoadBoardDetailsActivity)
- }
- }
- }
- })
- }
- override fun onMapReady(googleMap: GoogleMap) {
- mMap = googleMap
- marker1 = mapService.addSingleMarkerToMap(mMap!!, markerOptions1!!)
- marker2 = mapService.addSingleMarkerToMap(mMap!!, markerOptions2!!)
- mapService.setMap(googleMap)
- mMap!!.setOnMapLoadedCallback {
- mMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(mapService.getLatLongFromString(job?.loadAddress!!), 10f))
- mapService.drawRoute(marker1?.title!!, marker2?.title!!, TravelMode.DRIVING)
- }
- }
- fun fill(job: Job) {
- mStartPos?.text = job.loadAddress
- mDestination?.text = job.dropAddress
- mJobDescription?.text = job.description
- mJobLoadLimit?.text = job.weight.toString()
- mJobValue?.text = job.price.toString()
- mContact?.text = job.contactPerson
- textViewJobName.text = job.jobName!!.capitalize()
- }
- override fun onConnected(p0: Bundle?) {
- println("ONCONNECED CALLBACK")
- }
- override fun onConnectionFailed(connectionResult: ConnectionResult) {
- println("ONCONNECTION FAILED: ")
- }
- override fun onConnectionSuspended(i: Int) {
- println("ONCONNECTION SUSPENDED: ")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement