From 505ae28d2b35f39b0e17a178b688dcf70a5d7e12 Mon Sep 17 00:00:00 2001 From: F8Full Date: Tue, 7 May 2019 17:52:54 -0400 Subject: [PATCH] URGENT FIX !! App closes when clicking directions buttons -launching activity from model was a bad idea -replicate startActivityForResult pattern in activity model and activity -but in trip model and trip fragment --- .../findmybikes/ui/trip/TripDetailsFragment.kt | 7 +++++++ .../ui/trip/TripFragmentViewModel.kt | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripDetailsFragment.kt b/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripDetailsFragment.kt index 53c3440..9d8a555 100644 --- a/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripDetailsFragment.kt +++ b/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripDetailsFragment.kt @@ -105,6 +105,13 @@ class TripDetailsFragment : Fragment() { } }) + fragmentModel.lastStartActivityIntent.observe(this, Observer { + it?.let { intent -> + fragmentModel.clearLastStartActivityRequest() + startActivity(intent) + } + }) + inflatedView.findViewById(R.id.trip_details_directions_loc_to_a).setOnClickListener { fragmentModel.locToStationADirectionsFabClick() } diff --git a/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripFragmentViewModel.kt b/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripFragmentViewModel.kt index 4657016..3eb7d25 100644 --- a/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripFragmentViewModel.kt +++ b/app/src/main/java/com/ludoscity/findmybikes/ui/trip/TripFragmentViewModel.kt @@ -43,6 +43,18 @@ class TripFragmentViewModel(app: Application, private val aToBGoogleMapDirectionsIntent = MutableLiveData() private val bToDestGoogleMapDirectionsIntent = MutableLiveData() + private val lastStartActData = MutableLiveData() + val lastStartActivityIntent: LiveData + get() = lastStartActData + + fun requestStartActivity(intent: Intent) { + lastStartActData.value = intent + } + + fun clearLastStartActivityRequest() { + lastStartActData.value = null + } + val locToStationAText: LiveData get() = locToStationADurationString @@ -196,15 +208,15 @@ class TripFragmentViewModel(app: Application, } fun locToStationADirectionsFabClick() { - getApplication().startActivity(locToAGoogleMapDirectionsIntent.value) + lastStartActData.value = locToAGoogleMapDirectionsIntent.value } fun stationAToStationBDirectionsFabClick() { - getApplication().startActivity(aToBGoogleMapDirectionsIntent.value) + lastStartActData.value = aToBGoogleMapDirectionsIntent.value } fun stationBTofinalDestinationDirectionsFabClick() { - getApplication().startActivity(bToDestGoogleMapDirectionsIntent.value) + lastStartActData.value = bToDestGoogleMapDirectionsIntent.value } private fun recalculateTripTotal(statBToFinal: Int?, statAToStatB: Int?, locToA: Int?) {