From 84c797ac104c0fff96ba6793cefaa5ba0b4887fc Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Thu, 16 Dec 2021 02:08:57 -0600 Subject: Updates unit details fragment to fetch the information for the given ID --- .../mx/trackermap/TrackerMap/android/TrackerApp.kt | 2 ++ .../TrackerMap/android/details/DetailsActivity.kt | 3 +-- .../details/information/UnitInformationFragment.kt | 20 +++++++++++++++-- .../information/UnitInformationViewModel.kt | 26 ++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt (limited to 'androidApp/src/main/java/mx') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt index e23c0de..7929832 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -1,6 +1,7 @@ package mx.trackermap.TrackerMap.android import android.app.Application +import mx.trackermap.TrackerMap.android.details.information.UnitInformationViewModel import mx.trackermap.TrackerMap.android.session.LoginViewModel import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.apis.DevicesApi @@ -27,6 +28,7 @@ class TrackerApp : Application() { single { UnitsController(get(), get()) } viewModel { LoginViewModel(get(), get()) } + viewModel { UnitInformationViewModel() } single { UnitsViewModel(get()) } } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt index 3e8f00b..12ab79e 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt @@ -12,7 +12,6 @@ class DetailsActivity: AppCompatActivity() { private val binding get() = _binding!! private lateinit var adapter: UnitDetailsAdapter private var deviceId: Int = 0 - private lateinit var initialSection: Action companion object { val DEVICE_ID_EXTRA = "device_id" @@ -36,7 +35,7 @@ class DetailsActivity: AppCompatActivity() { private fun initialize() { deviceId = intent.getIntExtra(DEVICE_ID_EXTRA, 0) - initialSection = intent.getSerializableExtra(ACTION_EXTRA) as Action + val initialSection = intent.getSerializableExtra(ACTION_EXTRA) as Action Log.d("DetailsActivity", "Device ID - $deviceId") Log.d("DetailsActivity", "Initial Section - $initialSection") diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt index 3b8e7d5..7a0ed16 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationFragment.kt @@ -1,18 +1,22 @@ package mx.trackermap.TrackerMap.android.details.information import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import mx.trackermap.TrackerMap.android.databinding.UnitDetailsInformationBinding import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter +import org.koin.androidx.viewmodel.ext.android.viewModel class UnitInformationFragment : Fragment() { private var _binding: UnitDetailsInformationBinding? = null private val binding get() = _binding!! + private val unitInformationViewModel: UnitInformationViewModel by viewModel() + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -24,12 +28,24 @@ class UnitInformationFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) - binding.unitInformationText.text = "INFORMATION for ID - $id" + + setupObservers() + fetchInformation() } override fun onDestroyView() { super.onDestroyView() _binding = null } + + private fun setupObservers() { + unitInformationViewModel.unit.observe(this) { unitInformation -> + Log.d("UnitInformationFragment", "Unit Information Fetched - $unitInformation") + } + } + + private fun fetchInformation() { + val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) + unitInformationViewModel.fetchUnit(id ?: 0) + } } \ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt new file mode 100644 index 0000000..e7100e1 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt @@ -0,0 +1,26 @@ +package mx.trackermap.TrackerMap.android.details.information + +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.models.UnitInformation +import mx.trackermap.TrackerMap.controllers.UnitsController +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject + +class UnitInformationViewModel: ViewModel(), KoinComponent { + @DelicateCoroutinesApi + private val unitsController: UnitsController by inject() + var unit = MutableLiveData() + + init { + Log.d("UnitInformationVM", "Initializing Unit Information View Model") + } + + fun fetchUnit(deviceId: Int) { + unit.postValue(unitsController.getUnit(deviceId)) + } +} \ No newline at end of file -- cgit v1.2.3