aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt
blob: 27d8756a86681a3e17b75e8cdfc6da26ff7cd87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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.TabLayoutMediator
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 -> "Details"
                1 -> "Reports"
                else -> "Commands"
            }
        }.attach()
        binding.detailsPager.setCurrentItem(
            when (initialSection) {
                Action.DETAILS -> 0
                Action.REPORTS -> 1
                else -> 2
            }, false
        )
        binding.detailsPager.isUserInputEnabled = false
    }
}