ref: v3.0.1
app/src/main/java/xyz/apiote/bimba/czwek/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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
package xyz.apiote.bimba.czwek.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.content.edit 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 xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.Line import xyz.apiote.bimba.czwek.api.QueryableV1 import xyz.apiote.bimba.czwek.api.StopV1 import xyz.apiote.bimba.czwek.dashboard.ui.home.HomeFragment import xyz.apiote.bimba.czwek.dashboard.ui.map.MapFragment import xyz.apiote.bimba.czwek.dashboard.ui.voyage.VoyageFragment import xyz.apiote.bimba.czwek.databinding.ActivityMainBinding import xyz.apiote.bimba.czwek.departures.DeparturesActivity import xyz.apiote.bimba.czwek.search.ResultsActivity import xyz.apiote.bimba.czwek.settings.ServerChooserActivity import xyz.apiote.bimba.czwek.settings.feeds.FeedChooserActivity class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private lateinit var locationPermissionRequest: ActivityResultLauncher<Array<String>> private lateinit var permissionAsker: Fragment override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) getSharedPreferences("shp", MODE_PRIVATE).edit(true) { putBoolean("firstRun", false) } supportFragmentManager.registerFragmentLifecycleCallbacks( object : FragmentLifecycleCallbacks() { override fun onFragmentViewCreated( fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle? ) { setNavbarIcons(f) super.onFragmentViewCreated(fm, f, v, savedInstanceState) } }, true ) binding.navigationDrawer.setNavigationItemSelectedListener { when (it.itemId) { R.id.drawer_servers -> { startActivity(Intent(this, ServerChooserActivity::class.java)) } R.id.drawer_cities -> { startActivity(Intent(this, FeedChooserActivity::class.java)) } } false } 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 -> { when (permissionAsker) { is HomeFragment -> { showResults(ResultsActivity.Mode.MODE_LOCATION) } is MapFragment -> { (permissionAsker as MapFragment).showLocation() } } } else -> { // todo(ux,ui) dialog Toast.makeText(this, "No location access given", Toast.LENGTH_SHORT).show() } } } } @Suppress( "OVERRIDE_DEPRECATION", "DEPRECATION" ) // fixme 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(fragment: Fragment) { when (PackageManager.PERMISSION_GRANTED) { ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) -> { when (fragment) { is HomeFragment -> { showResults(ResultsActivity.Mode.MODE_LOCATION) } is MapFragment -> { fragment.showLocation() } } } else -> { permissionAsker = fragment locationPermissionRequest.launch( arrayOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION ) ) } } } fun onSuggestionClicked(queryable: QueryableV1) { when (queryable) { is StopV1 -> { val intent = Intent(this, DeparturesActivity::class.java).apply { putExtra("code", queryable.code) putExtra("name", queryable.name) } startActivity(intent) } is Line -> { TODO("[3.1] start line graph activity") } } } fun onSearchClicked(text: CharSequence?) { showResults(ResultsActivity.Mode.MODE_SEARCH, text.toString()) } private fun showResults(mode: ResultsActivity.Mode, query: String = "") { /* todo [3.1] (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) { // todo [voyage-planning] // 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) } } } } |