aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt
blob: 6bc7aab1592df650baa8ccf10089b9a33880f373 (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
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.UnitMapFragment
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.icon_map
                    UnitsViewModel.UnitsDisplayMode.MAP -> R.drawable.icon_list
                    else -> R.drawable.icon_map
                }
            )
            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 -> UnitMapFragment()
                    else -> DevicesFragment()
                }
            supportFragmentManager.commit {
                replace(R.id.displayContainer, newFragment)
            }
        }
    }
}