diff options
5 files changed, 63 insertions, 23 deletions
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 808808a..cdb7102 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -26,20 +26,20 @@ class TrackerApp : Application() { val appModule = module { single { "https://etbsa.net/api" } - single { SessionApi(get()) } - single { DevicesApi(get()) } - single { PositionsApi(get()) } - single { CommandsApi(get()) } - single { ReportsApi(get()) } - single { GeofencesApi(get()) } + factory { SessionApi(get()) } + factory { DevicesApi(get()) } + factory { PositionsApi(get()) } + factory { CommandsApi(get()) } + factory { ReportsApi(get()) } + factory { GeofencesApi(get()) } - single { UnitsController(get(), get()) } - single { GeofencesController(get()) } + factory { UnitsController(get(), get()) } + factory { GeofencesController(get()) } viewModel { LoginViewModel(get(), get()) } viewModel { UnitInformationViewModel(get()) } viewModel { UnitCommandsViewModel(get()) } - single { UnitsViewModel(get()) } + viewModel { UnitsViewModel(get()) } viewModel { UnitReportsViewModel(get(), get()) } viewModel { UserInformationViewModel(get()) } } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt index bd3ab67..6bee413 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt @@ -11,18 +11,17 @@ import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.databinding.DevicesFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity +import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi -class DevicesFragment : Fragment() { +class DevicesFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { private var _binding: DevicesFragmentBinding? = null private val binding get() = _binding!! - private val unitsViewModel: UnitsViewModel by viewModel() - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -42,9 +41,20 @@ class DevicesFragment : Fragment() { super.onViewCreated(view, savedInstanceState) setupList() + } + + override fun onResume() { + super.onResume() + setupObservers() } + override fun onPause() { + super.onPause() + + removeObservers() + } + private fun setupList() { binding.devicesList.layoutManager = LinearLayoutManager( context, LinearLayoutManager.VERTICAL, @@ -65,6 +75,11 @@ class DevicesFragment : Fragment() { } } + private fun removeObservers() { + Log.d("DevicesFragment", "removeObservers()") + unitsViewModel.units.removeObservers(viewLifecycleOwner) + } + private fun itemAction(unit: UnitInformation, action: Action) { when (action) { Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index fc417a0..30e3cea 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -1,29 +1,23 @@ package mx.trackermap.TrackerMap.android.map import android.content.Intent -import android.graphics.Color 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 kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitMapFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity import mx.trackermap.TrackerMap.android.devices.Action import mx.trackermap.TrackerMap.android.devices.UnitRenderData +import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation -import mx.trackermap.TrackerMap.utils.Formatter -import mx.trackermap.TrackerMap.utils.SpeedUnit -import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi -class UnitMapFragment : Fragment() { - - private val unitsViewModel: UnitsViewModel by viewModel() +class UnitMapFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { private var _binding: UnitMapFragmentBinding? = null private val binding get() = _binding!! @@ -42,9 +36,20 @@ class UnitMapFragment : Fragment() { super.onViewCreated(view, savedInstanceState) initializeMap() + } + + override fun onResume() { + super.onResume() + setupObservers() } + override fun onPause() { + super.onPause() + + removeObservers() + } + private fun initializeMap() { unitsMapFragment = childFragmentManager.findFragmentById(R.id.unitsMap) as MapFragment unitsMapFragment.markerCallback = unitsViewModel::selectUnitWith @@ -93,6 +98,13 @@ class UnitMapFragment : Fragment() { } } + private fun removeObservers() { + Log.d("MapFragment", "removeObservers()") + unitsViewModel.units.removeObservers(viewLifecycleOwner) + unitsViewModel.selectedUnit.removeObservers(viewLifecycleOwner) + unitsViewModel.geofences.removeObservers(viewLifecycleOwner) + } + private fun itemAction(unit: UnitInformation, action: Action) { when (action) { Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt new file mode 100644 index 0000000..ab3afc1 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitFragment.kt @@ -0,0 +1,8 @@ +package mx.trackermap.TrackerMap.android.units + +import androidx.fragment.app.Fragment +import kotlinx.coroutines.DelicateCoroutinesApi + +open class UnitFragment @DelicateCoroutinesApi constructor( + private val unitsViewModel: UnitsViewModel +) : Fragment()
\ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt index 7e5f162..e198ce0 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt @@ -24,6 +24,12 @@ class UnitsActivity : AppCompatActivity() { private val binding get() = _binding!! private val unitsViewModel: UnitsViewModel by viewModel() + private val mapFragment: UnitMapFragment by lazy { + UnitMapFragment(unitsViewModel) + } + private val devicesFragment: DevicesFragment by lazy { + DevicesFragment(unitsViewModel) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -73,9 +79,8 @@ class UnitsActivity : AppCompatActivity() { val newFragment = when (displayMode) { - UnitsViewModel.UnitsDisplayMode.LIST -> DevicesFragment() - UnitsViewModel.UnitsDisplayMode.MAP -> UnitMapFragment() - else -> DevicesFragment() + UnitsViewModel.UnitsDisplayMode.LIST -> devicesFragment + UnitsViewModel.UnitsDisplayMode.MAP -> mapFragment } supportFragmentManager.commit { replace(R.id.displayContainer, newFragment) |