package mx.trackermap.TrackerMap.android.details import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.DetailsActivityBinding import mx.trackermap.TrackerMap.android.devices.Action class DetailsActivity : AppCompatActivity() { private var _binding: DetailsActivityBinding? = null private val binding get() = _binding!! private lateinit var adapter: UnitDetailsAdapter private var deviceId: Int = 0 companion object { const val DEVICE_ID_EXTRA = "device_id" const val ACTION_EXTRA = "action" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = DetailsActivityBinding.inflate(layoutInflater) setContentView(binding.root) initialize() } override fun onDestroy() { super.onDestroy() _binding = null } private fun initialize() { deviceId = intent.getIntExtra(DEVICE_ID_EXTRA, 0) val initialSection = intent.getSerializableExtra(ACTION_EXTRA) as Action Log.d("DetailsActivity", "Device ID - $deviceId") Log.d("DetailsActivity", "Initial Section - $initialSection") adapter = UnitDetailsAdapter(this, deviceId) binding.detailsPager.adapter = adapter TabLayoutMediator(binding.detailsTabs, binding.detailsPager) { tab, position -> tab.text = when (position) { 0 -> getString(R.string.unit_details) 1 -> getString(R.string.unit_reports) else -> getString(R.string.unit_commands) } }.attach() binding.detailsPager.setCurrentItem( when (initialSection) { Action.DETAILS -> 0 Action.REPORTS -> 1 else -> 2 }, false ) binding.detailsTabs.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab?) { binding.detailsPager.isUserInputEnabled = when (tab?.position) { 0 -> true 1 -> false else -> true } } override fun onTabUnselected(tab: TabLayout.Tab?) {} override fun onTabReselected(tab: TabLayout.Tab?) {} }) } }