aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt
blob: 24d0e4b97f1600d763cf14de58ba86f5a3269ad1 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package mx.trackermap.TrackerMap.android.units

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
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 mx.trackermap.TrackerMap.android.session.UserInformationActivity
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()
    private val mapFragment: UnitMapFragment by lazy {
        UnitMapFragment(unitsViewModel)
    }
    private val devicesFragment: DevicesFragment by lazy {
        DevicesFragment(unitsViewModel)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        _binding = UnitsActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupEvents()
    }

    override fun onResume() {
        super.onResume()

        setupObservers()
    }

    override fun onPause() {
        super.onPause()

        removeObservers()
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }

    private fun setupEvents() {
        binding.userButton.setOnClickListener {
            val intent = Intent(applicationContext, UserInformationActivity::class.java)
            startActivity(intent)
        }
        binding.displayModeToggle.setOnClickListener {
            unitsViewModel.toggleDisplayMode()
        }
        binding.mapLayerToggle.setOnClickListener {
            showLayersPopUp(it)
        }
        binding.searchInput.doAfterTextChanged {
            Log.d("UnitsActivity", "Search changed to ${it.toString()}")
            /*binding.searchInput.setCompoundDrawablesRelativeWithIntrinsicBounds(
                null, null,
                if (it?.isEmpty() == true) null
                else ContextCompat.getDrawable(this, R.drawable.icon_back),
                null,
            )*/
            binding.searchInput
            unitsViewModel.search(it.toString())
        }
    }

    private fun showLayersPopUp(view: View) {
        val popOver = PopupMenu(this, view)
        popOver.menuInflater.inflate(R.menu.map_layers, popOver.menu)
        popOver.setOnMenuItemClickListener { item ->
            unitsViewModel.setMapLayer(
                when (item.itemId) {
                    R.id.layerStreets -> getString(R.string.maps_streets_tile_url)
                    R.id.layerSatellite -> getString(R.string.maps_satellite_url)
                    R.id.layerHybrid -> getString(R.string.maps_hybrid_url)
                    else -> getString(R.string.maps_streets_tile_url)
                }
            )
            true
        }
        popOver.show()
    }

    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
                }
            )

            binding.mapLayerToggle.visibility =
                when (displayMode) {
                    UnitsViewModel.UnitsDisplayMode.LIST -> View.GONE
                    UnitsViewModel.UnitsDisplayMode.MAP -> View.VISIBLE
                    else -> View.GONE
                }

            val newFragment =
                when (displayMode) {
                    UnitsViewModel.UnitsDisplayMode.LIST -> devicesFragment
                    UnitsViewModel.UnitsDisplayMode.MAP -> mapFragment
                    else -> devicesFragment
                }
            supportFragmentManager.commit {
                replace(R.id.displayContainer, newFragment)
            }
        }
    }

    private fun removeObservers() {
        unitsViewModel.unitsDisplayMode.removeObservers(this)
    }
}