package mx.trackermap.TrackerMap.android.units import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.commit import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitsActivityBinding import mx.trackermap.TrackerMap.android.devices.DevicesFragment import mx.trackermap.TrackerMap.android.map.MapFragment import org.koin.androidx.viewmodel.ext.android.viewModel @DelicateCoroutinesApi class UnitsActivity : AppCompatActivity() { private var _binding: UnitsActivityBinding? = null private val binding get() = _binding!! private val unitsViewModel: UnitsViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = UnitsActivityBinding.inflate(layoutInflater) setContentView(binding.root) setupEvents() setupObservers() } override fun onDestroy() { super.onDestroy() _binding = null } private fun setupEvents() { binding.profileButton.setOnClickListener { Log.d("UnitsActivity", "Profile tapped") } binding.displayModeToggle.setOnClickListener { Log.d("UnitsActivity", "Units display toggle tapped") unitsViewModel.toggleDisplayMode() } binding.searchInput.doAfterTextChanged { Log.d("UnitsActivity", "Search changed to ${it.toString()}") unitsViewModel.search(it.toString()) } } private fun setupObservers() { unitsViewModel.unitsDisplayMode.observe(this) { displayMode -> binding.displayModeToggle.setImageResource( when (displayMode) { UnitsViewModel.UnitsDisplayMode.LIST -> R.drawable.ic_baseline_map_24 UnitsViewModel.UnitsDisplayMode.MAP -> R.drawable.ic_baseline_list_24 else -> R.drawable.ic_baseline_map_24 } ) binding.displayModeToggle.contentDescription = getString( when (displayMode) { UnitsViewModel.UnitsDisplayMode.LIST -> R.string.toggle_map UnitsViewModel.UnitsDisplayMode.MAP -> R.string.toggle_list else -> R.string.toggle_map } ) val newFragment = when (displayMode) { UnitsViewModel.UnitsDisplayMode.LIST -> DevicesFragment() UnitsViewModel.UnitsDisplayMode.MAP -> MapFragment() else -> DevicesFragment() } supportFragmentManager.commit { replace(R.id.displayContainer, newFragment) } } } }