aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt
blob: c6d260b3cb819e4f76aeed534fb5287aa9c77885 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package mx.trackermap.TrackerMap.android.details

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.DetailsActivityBinding
import mx.trackermap.TrackerMap.android.shared.UnitRenderData

@DelicateCoroutinesApi
class DetailsActivity : AppCompatActivity() {

    private var _binding: DetailsActivityBinding? = null
    private val binding get() = _binding!!
    private lateinit var adapter: UnitDetailsAdapter
    private var deviceId: Int = 0
    private var deviceName: String = ""

    companion object {
        const val DEVICE_ID_EXTRA = "device_id"
        const val DEVICE_NAME_EXTRA = "device_name"
        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)
        deviceName = intent.getStringExtra(DEVICE_NAME_EXTRA) ?: ""
        val initialSection = intent.getSerializableExtra(ACTION_EXTRA) as UnitRenderData.Action
        Log.d("DetailsActivity", "Device ID - $deviceId")
        Log.d("DetailsActivity", "Initial Section - $initialSection")

        adapter = UnitDetailsAdapter(this, deviceId, deviceName)
        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) {
                UnitRenderData.Action.DETAILS -> 0
                UnitRenderData.Action.REPORTS -> 1
                else -> 2
            }, false
        )
        binding.detailsPager.isUserInputEnabled = when (initialSection) {
            UnitRenderData.Action.DETAILS -> true
            UnitRenderData.Action.REPORTS -> false
            else -> true
        }
        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?) {}
        })
        binding.closeButton.setOnClickListener {
            finish()
        }

        requestPermission {}
    }

    private fun requestPermission (callback: (Boolean) -> Unit) {
        val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
        val launcher = registerForActivityResult(
            ActivityResultContracts.RequestPermission(), callback)

        when {
            ActivityCompat.checkSelfPermission(this, permission)
                    == PackageManager.PERMISSION_GRANTED -> {
                Log.d("DetailsActivity", "$permission = PackageManager.PERMISSION_GRANTED")
            }
            ActivityCompat.shouldShowRequestPermissionRationale(this, permission) -> {
                Log.d("DetailsActivity", "shouldShowRequestPermissionRationale")
            }
            else -> {
                Log.d("DetailsActivity", "Requesting $permission permission")
                launcher.launch(permission)
            }
        }
    }
}