From a8fbe5853bc0adf83a993212088bf95bdbc3a6ac Mon Sep 17 00:00:00 2001 From: Mateusz Polok Date: Tue, 28 May 2019 14:35:00 +0200 Subject: [PATCH] fixed calling --- .../ApartmentDetailsFragment.kt | 52 +++++++++++++------ .../ApartmentDetailsViewModel.kt | 1 - 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsFragment.kt b/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsFragment.kt index 1f2c95a..7801017 100644 --- a/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsFragment.kt +++ b/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsFragment.kt @@ -1,10 +1,13 @@ package com.jakdor.apapp.ui.apartmentDetails -import android.Manifest +import android.content.Intent +import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast +import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment @@ -18,10 +21,6 @@ import com.jakdor.apapp.ui.MainActivity import com.jakdor.apapp.utils.GlideApp import kotlinx.android.synthetic.main.fragment_apartment_details.* import javax.inject.Inject -import android.content.Intent -import android.content.pm.PackageManager -import android.net.Uri -import androidx.core.app.ActivityCompat class ApartmentDetailsFragment : Fragment(), InjectableFragment { @@ -33,6 +32,7 @@ class ApartmentDetailsFragment : Fragment(), InjectableFragment { private lateinit var binding: FragmentApartmentDetailsBinding private var apartmentId: Int = -1 private var phoneNumber: String = "" + private var getPhoneNumber = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -57,9 +57,30 @@ class ApartmentDetailsFragment : Fragment(), InjectableFragment { super.onViewCreated(view, savedInstanceState) call_button.setOnClickListener { - val callIntent = Intent(Intent.ACTION_DIAL) - callIntent.data = Uri.parse("tel:$phoneNumber") - startActivity(callIntent) + if(getPhoneNumber) { + viewModel?.getApartmentPhoneNumber(apartmentId) + getPhoneNumber = false + } + + val builder = AlertDialog.Builder(activity as MainActivity) + builder.setTitle("Wybierz akcję") + builder.setItems(arrayOf("Zadzwoń", "Wyślij SMS")) { dialog, which -> + when(which){ + 0 -> { val callIntent = Intent(Intent.ACTION_DIAL) + callIntent.data = Uri.parse("tel:$phoneNumber") + phoneNumber = "" + startActivity(callIntent) + } + 1 -> { + val smsIntent = Intent(Intent.ACTION_VIEW) + smsIntent.type = "vnd.android-dir/mms-sms" + smsIntent.putExtra("address", phoneNumber) + startActivity(smsIntent) + } + } + getPhoneNumber = true + } + builder.show() } apartment_map_fab.setOnClickListener { @@ -97,15 +118,14 @@ class ApartmentDetailsFragment : Fragment(), InjectableFragment { apartment_img_pager_tab_indicator.setupWithViewPager(apartment_img_pager, true) } - viewModel?.getApartmentPhoneNumber(apartmentId) - - val userPhoneNumberObserver = Observer { phoneNumber -> - if(phoneNumber!= null && phoneNumber.trim().isNotEmpty()){ - this.phoneNumber = phoneNumber - call_button.isEnabled = true - call_button.isFocusable = true - call_button.isClickable = true + viewModel?.observeApartmentPhoneNumber() + val userPhoneNumberObserver = Observer { tempPhoneNumber -> + if(tempPhoneNumber.trim().isNotEmpty()){ + this.phoneNumber = tempPhoneNumber + getPhoneNumber = true + }else if(tempPhoneNumber.trim().isEmpty()){ + Toast.makeText(activity,"Brak numeru telefonu", Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsViewModel.kt b/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsViewModel.kt index 9bb37b8..5b9f924 100644 --- a/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsViewModel.kt +++ b/app/src/main/java/com/jakdor/apapp/ui/apartmentDetails/ApartmentDetailsViewModel.kt @@ -35,7 +35,6 @@ class ApartmentDetailsViewModel } fun getApartmentPhoneNumber(id: Int){ - observeApartmentPhoneNumber() apartmentRepository.getApartmentPhoneNumber(id) }