ref: e4d7079147c16b0d67cd39e35f5d943644d24c68
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.dashboard import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.view.View import androidx.activity.OnBackPressedCallback import androidx.activity.enableEdgeToEdge import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.content.edit import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.get import androidx.core.view.updatePadding import androidx.drawerlayout.widget.DrawerLayout.DrawerListener 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 androidx.preference.PreferenceManager import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.openlocationcode.OpenLocationCode import xyz.apiote.bimba.czwek.AboutActivity import xyz.apiote.bimba.czwek.R 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.onboarding.FirstRunActivity import xyz.apiote.bimba.czwek.search.ResultsActivity import xyz.apiote.bimba.czwek.settings.DownloadCitiesWorker import xyz.apiote.bimba.czwek.settings.ServerChooserActivity import xyz.apiote.bimba.czwek.settings.SettingsActivity 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 private var locationPermissionDialogShown = false override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) FirstRunActivity.setFirstRunDone(this) ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) val l = windowInsets.displayCutout?.safeInsetLeft?.takeIf { it > 0 } ?: insets.left binding.navigationDrawer.updatePadding(left = l) windowInsets } supportFragmentManager.registerFragmentLifecycleCallbacks( object : FragmentLifecycleCallbacks() { override fun onFragmentViewCreated( fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle? ) { setNavbarIcons(f) super.onFragmentViewCreated(fm, f, v, savedInstanceState) } }, true ) val onBackPressedCallback = object : OnBackPressedCallback(binding.container.isDrawerOpen(binding.navigationDrawer)) { override fun handleOnBackPressed() { binding.container.closeDrawer(binding.navigationDrawer) } } onBackPressedDispatcher.addCallback(onBackPressedCallback) binding.container.addDrawerListener(object : DrawerListener { override fun onDrawerSlide(drawerView: View, slideOffset: Float) {} override fun onDrawerOpened(drawerView: View) { onBackPressedCallback.isEnabled = true } override fun onDrawerClosed(drawerView: View) { onBackPressedCallback.isEnabled = false } override fun onDrawerStateChanged(newState: Int) {} }) 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)) } R.id.drawer_settings -> { startActivity(Intent(this, SettingsActivity::class.java)) } R.id.drawer_about -> { startActivity(Intent(this, AboutActivity::class.java)) } } 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 -> { if (locationPermissionDialogShown) { return@registerForActivityResult } MaterialAlertDialogBuilder(this).setIcon( AppCompatResources.getDrawable( this, R.drawable.error_gps ) ) .setTitle(getString(R.string.no_location_access)) .setMessage(getString(R.string.no_location_message)) .setPositiveButton(R.string.ok) { _, _ -> } .show() locationPermissionDialogShown = true } } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { val notificationPermissionAsked = PreferenceManager.getDefaultSharedPreferences(this).getBoolean( NOTIFICATION_PERMISSION_ASKED, false ) if (ActivityCompat.checkSelfPermission( this, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED && !notificationPermissionAsked ) { requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1) PreferenceManager.getDefaultSharedPreferences(this).edit { putBoolean(NOTIFICATION_PERMISSION_ASKED, true) } } } } 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 onSearchClicked(text: CharSequence?) { if (OpenLocationCode.isValidCode(text.toString())) { val olc = OpenLocationCode(text.toString()) if (!olc.isFull) { showResults(ResultsActivity.Mode.MODE_SHORT_CODE_LOCATION, text.toString()) } else { val area = olc.decode() showResults(olc.code, area.centerLatitude, area.centerLongitude) } } else if (OpenLocationCode.isValidCode( text.toString().trim().split(" ").first().trim(',').trim() ) ) { if (PreferenceManager.getDefaultSharedPreferences(applicationContext) .getLong(DownloadCitiesWorker.LAST_UPDATE_KEY, -1) < 0 ) { if (!PreferenceManager.getDefaultSharedPreferences(applicationContext) .getBoolean(NO_GEOCODING_DATA_SHOWN, false) ) { MaterialAlertDialogBuilder(this) .setIcon(R.drawable.geocoding) .setTitle(R.string.no_geocoding_data) .setMessage(R.string.no_geocoding_data_description) .setPositiveButton(R.string.ok) { _, _ -> showResults( ResultsActivity.Mode.MODE_SEARCH, text.toString() ) } .show() PreferenceManager.getDefaultSharedPreferences(applicationContext).edit{ putBoolean(NO_GEOCODING_DATA_SHOWN, true) } } } else { showResults(ResultsActivity.Mode.MODE_SHORT_CODE, text.toString()) } } else { showResults(ResultsActivity.Mode.MODE_SEARCH, text.toString()) } } private fun showResults(query: String, centerLatitude: Double, centerLongitude: Double) { /* todo [3.2] (ux,low) animation https://developer.android.com/guide/fragments/animate https://github.com/raheemadamboev/fab-explosion-animation-app */ startActivity(ResultsActivity.getIntent(this, ResultsActivity.Mode.MODE_POSITION, query, centerLatitude, centerLongitude)) } private fun showResults(mode: ResultsActivity.Mode, query: String = "") { /* todo [3.2] (ux,low) animation https://developer.android.com/guide/fragments/animate https://github.com/raheemadamboev/fab-explosion-animation-app */ startActivity(ResultsActivity.getIntent(this, mode, query)) } 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) } } } companion object { const val NOTIFICATION_PERMISSION_ASKED = "notificationPermissionAsked" const val NO_GEOCODING_DATA_SHOWN = "no_geocoding_data_shown" } } |