Bimba.git

ref: 2f14b9ea523c41743d6e0ed0da2d2f3cba903e26

app/src/main/java/ml/adamsprogs/bimba/dashboard/MainActivity.kt


  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ml.adamsprogs.bimba.dashboard

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.Item
import ml.adamsprogs.bimba.api.Line
import ml.adamsprogs.bimba.api.Stop
import ml.adamsprogs.bimba.databinding.ActivityMainBinding
import ml.adamsprogs.bimba.search.ResultsActivity
import ml.adamsprogs.bimba.dashboard.ui.home.HomeFragment
import ml.adamsprogs.bimba.dashboard.ui.map.MapFragment
import ml.adamsprogs.bimba.dashboard.ui.voyage.VoyageFragment
import ml.adamsprogs.bimba.departures.DeparturesActivity


class MainActivity : AppCompatActivity() {
	private lateinit var binding: ActivityMainBinding
	private lateinit var locationPermissionRequest: ActivityResultLauncher<Array<String>>

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		binding = ActivityMainBinding.inflate(layoutInflater)
		setContentView(binding.root)

		supportFragmentManager.registerFragmentLifecycleCallbacks(
			object : FragmentLifecycleCallbacks() {
				override fun onFragmentViewCreated(
					fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?
				) {
					setNavbarIcons(f)
					super.onFragmentViewCreated(fm, f, v, savedInstanceState)
				}
			}, true
		)

		WindowCompat.setDecorFitsSystemWindows(window, false)
		val navView: BottomNavigationView = binding.bottomNavigation
		val navHostFragment =
			supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
		val navController = navHostFragment.navController
		navView.setupWithNavController(navController)

		locationPermissionRequest = registerForActivityResult(
			ActivityResultContracts.RequestMultiplePermissions()
		) { permissions ->
			when {
				permissions[Manifest.permission.ACCESS_FINE_LOCATION] ?: false ||
								permissions[Manifest.permission.ACCESS_COARSE_LOCATION] ?: false -> {
					showResults(ResultsActivity.Mode.MODE_LOCATION)
				}
				else -> {
					// todo(ux,ui) dialog
					Toast.makeText(this, "No location access given", Toast.LENGTH_SHORT).show()
				}
			}
		}
	}

	@Suppress(
		"OVERRIDE_DEPRECATION",
		"DEPRECATION"
	)  // fix_later https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher
	override fun onBackPressed() {
		if (binding.container.isDrawerOpen(binding.navigationDrawer)) {
			binding.container.closeDrawer(binding.navigationDrawer)
		} else {
			super.onBackPressed()
		}
	}

	fun onNavigationClicked() {
		if (binding.container.isDrawerOpen(binding.navigationDrawer)) {
			binding.container.closeDrawer(binding.navigationDrawer)
		} else {
			binding.container.openDrawer(binding.navigationDrawer)
		}
	}

	fun onGpsClicked(fab: View) {
		when (PackageManager.PERMISSION_GRANTED) {
			ContextCompat.checkSelfPermission(
				this,
				Manifest.permission.ACCESS_COARSE_LOCATION
			) -> {
				/* todo(ux,low) animation
					https://developer.android.com/guide/fragments/animate
					https://github.com/raheemadamboev/fab-explosion-animation-app
				*/
				showResults(ResultsActivity.Mode.MODE_LOCATION)
			}
			else -> {
				locationPermissionRequest.launch(
					arrayOf(
						Manifest.permission.ACCESS_FINE_LOCATION,
						Manifest.permission.ACCESS_COARSE_LOCATION
					)
				)
			}
		}
	}

	fun onSuggestionClicked(item: Item) {
		when (item) {
			is Stop -> {
				val intent = Intent(this, DeparturesActivity::class.java).apply {
					putExtra("code", item.code)
					putExtra("name", item.name)
				}
				startActivity(intent)
			}
			is Line -> {
				TODO("start line graph actvity")
			}
		}
	}

	fun onSearchClicked(text: CharSequence?) {
		showResults(ResultsActivity.Mode.MODE_SEARCH, text.toString())
	}

	private fun showResults(mode: ResultsActivity.Mode, query: String = "") {
		/* todo(ux,low) animation
			https://developer.android.com/guide/fragments/animate
			https://github.com/raheemadamboev/fab-explosion-animation-app
		*/
		val intent = Intent(this, ResultsActivity::class.java).apply {
			putExtra("mode", mode)
			putExtra("query", query)
		}
		startActivity(intent)
	}

	private fun setNavbarIcons(f: Fragment) {
		binding.bottomNavigation.menu[2].setIcon(R.drawable.voyage_outline)
		binding.bottomNavigation.menu[1].setIcon(R.drawable.home_outline)
		binding.bottomNavigation.menu[0].setIcon(R.drawable.map_outline)
		when (f) {
			is HomeFragment -> {
				binding.bottomNavigation.menu[1].setIcon(R.drawable.home_black)
			}
			is VoyageFragment -> {
				binding.bottomNavigation.menu[2].setIcon(R.drawable.voyage_black)
			}
			is MapFragment -> {
				binding.bottomNavigation.menu[0].setIcon(R.drawable.map_black)
			}
			else -> {
				binding.bottomNavigation.menu[1].setIcon(R.drawable.home_black)
			}
		}
	}
}