Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.82 KB | None | 0 0
  1. import android.content.Intent
  2. import android.net.Uri
  3. import android.view.MotionEvent.ACTION_DOWN
  4. import android.view.View
  5. import android.widget.ProgressBar
  6. import android.widget.Toast
  7. import androidx.core.net.toUri
  8. import androidx.lifecycle.Observer
  9. import androidx.navigation.fragment.findNavController
  10. import androidx.navigation.fragment.navArgs
  11. import kotlinx.android.synthetic.main.fragment_trainers_details.*
  12. import ua.com.fitness.R
  13. import ua.com.fitness.common.contracts.BottomNavigationContract
  14. import ua.com.fitness.common.contracts.ToolbarContract
  15. import ua.com.fitness.extensions.getPhotoUrl
  16. import ua.com.fitness.extensions.loadImage
  17. import ua.com.fitness.models.Trainer
  18. import ua.com.fitness.ui.base.BaseFragment
  19.  
  20. class TrainersDetailsFragment : BaseFragment<TrainersDetailsViewModel>() {
  21.  
  22.     private val args by navArgs<TrainersDetailsFragmentArgs>()
  23.  
  24.     override fun onViewModelCreated() {
  25.         observeLiveData()
  26.  
  27.         viewModel.downloadTrainerData(args.trainerUuid)
  28.     }
  29.  
  30.     private fun observeLiveData() {
  31.         viewModel.errorLiveData.observe(viewLifecycleOwner, Observer {
  32.             showToast(it)
  33.         })
  34.  
  35.         viewModel.trainersLiveData.observe(viewLifecycleOwner, Observer {
  36.             setupUI(it)
  37.         })
  38.     }
  39.  
  40.     private fun setupUI(trainer: Trainer) {
  41.         ivTrainerImage.loadImage(trainer.photo.getPhotoUrl())
  42.         tvTrainerDescription.text = trainer.description
  43.         tvTrainerName.text = trainer.name
  44.  
  45.         setInformationVisibility(trainer)
  46.         ivTrainerArrowBack.setOnClickListener { findNavController().navigateUp() }
  47.         ivTrainerToolbarBack.setOnClickListener { findNavController().navigateUp() }
  48.  
  49.         llEmailContainer.setOnClickListener { sendEmail(trainer.email ?: "") }
  50.         llPhoneContainer.setOnClickListener { call(trainer.phone) }
  51.  
  52.  
  53.         trainersContentContainer.setOnTouchListener { v, event ->
  54.             if (event.action == ACTION_DOWN) {
  55.                 if (trainerDetailsMotionLayout.currentState == R.id.end_layout_image_trainer) {
  56.                     trainerDetailsMotionLayout.setTransition(
  57.                         R.id.end_layout_image_trainer,
  58.                         R.id.start_layout_trainer
  59.                     )
  60.                     trainerDetailsMotionLayout.setTransitionDuration(100)
  61.                     trainerDetailsMotionLayout.transitionToEnd()
  62.                     trainerDetailsMotionLayout.setTransition(
  63.                         R.id.start_layout_trainer,
  64.                         R.id.end_layout_trainer
  65.                     )
  66.                 }
  67.  
  68.             }
  69.  
  70.             true
  71.         }
  72.  
  73.     }
  74.  
  75.     private fun sendEmail(email: String) {
  76.         val intent = Intent(Intent.ACTION_SENDTO)
  77.         intent.data = Uri.parse("mailto:$email")
  78.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  79.         if (intent.resolveActivity(activity?.packageManager!!) != null) {
  80.             startActivity(intent)
  81.         }else {
  82.             showToast(getString(R.string.app_not_found))
  83.         }
  84.     }
  85.  
  86.     private fun call(phone: String) {
  87.         val phoneIntent = Intent(Intent.ACTION_DIAL)
  88.         phoneIntent.data = "tel:$phone".toUri()
  89.         phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  90.         if (phoneIntent.resolveActivity(activity?.packageManager!!) != null) {
  91.             startActivity(phoneIntent)
  92.         }else {
  93.             showToast(getString(R.string.app_not_found))
  94.         }
  95.     }
  96.  
  97.     private fun setInformationVisibility(trainer: Trainer) {
  98.         if (trainer.email.isNullOrBlank()) {
  99.             llEmailContainer.visibility = View.GONE
  100.         } else {
  101.             llEmailContainer.visibility = View.VISIBLE
  102.             tvTrainerEmail.text = trainer.email
  103.         }
  104.  
  105.         if (trainer.facebook.isNullOrBlank()) {
  106.             llFacebookContainer.visibility = View.GONE
  107.         } else {
  108.             llFacebookContainer.visibility = View.VISIBLE
  109.             tvTrainerFacebook.text = trainer.facebook
  110.         }
  111.  
  112.         if (trainer.phone.isBlank()) {
  113.             llPhoneContainer.visibility = View.GONE
  114.         } else {
  115.             llPhoneContainer.visibility = View.VISIBLE
  116.             tvTrainerPhone.text = trainer.phone
  117.         }
  118.  
  119.     }
  120.  
  121.     override fun setupToolbar(toolbarContract: ToolbarContract) {
  122.         toolbarContract.setStatusBarColor(android.R.color.transparent)
  123.         toolbarContract.setToolbarVisibility(false)
  124.     }
  125.  
  126.     override fun setupBottomBar(bottomNavigationContract: BottomNavigationContract) {
  127.         bottomNavigationContract.hideBottomNavigation()
  128.     }
  129.  
  130.     override fun getViewModelClass() = TrainersDetailsViewModel::class.java
  131.  
  132.     override fun getLayout() = R.layout.fragment_trainers_details
  133.  
  134.     override fun isNeedToShowProgressBar() = true
  135.  
  136.     override fun getProgressBar(): ProgressBar = progressBarTrainersDetails
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement