From 3f96fb09330cae93b5f0f509d18076a7d1674878 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Fri, 3 Jan 2020 18:17:20 +0000 Subject: [PATCH] Drawer buttons are now disabled till their commands are fully executed --- .../com/rtbishop/look4sat/MainViewModel.kt | 10 ++--- .../com/rtbishop/look4sat/ui/MainActivity.kt | 37 ++++++++++++++++--- app/src/main/res/values/strings.xml | 6 +++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt index a6e30c7d..d542e385 100644 --- a/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt @@ -120,7 +120,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { apply() } _gsp.postValue(GroundStationPosition(lat, lon, alt)) - _debugMessage.postValue("Location was updated") + _debugMessage.postValue(app.getString(R.string.updateLocSuccess)) } } @@ -145,9 +145,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } tleMainList = tleList tleSelection = mutableListOf() - _debugMessage.postValue("TLE file was updated") + _debugMessage.postValue(app.getString(R.string.updateTleSuccess)) } catch (exception: IOException) { - _debugMessage.postValue("Couldn't update TLE file") + _debugMessage.postValue(app.getString(R.string.updateTleFailure)) } } } @@ -156,9 +156,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { viewModelScope.launch(Dispatchers.IO) { try { repository.updateTransmittersDatabase() - _debugMessage.postValue("Transmitters were updated") + _debugMessage.postValue(app.getString(R.string.updateTransSuccess)) } catch (exception: IOException) { - _debugMessage.postValue("Couldn't update transmitters") + _debugMessage.postValue(app.getString(R.string.updateTransFailure)) } } } diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt index bd7fb19b..a83b7415 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt @@ -121,8 +121,13 @@ class MainActivity : AppCompatActivity() { } private fun setupObservers() { - viewModel.debugMessage.observe(this, Observer { debug_message -> - Toast.makeText(this, debug_message, Toast.LENGTH_SHORT).show() + viewModel.debugMessage.observe(this, Observer { message -> + Toast.makeText(this, message, Toast.LENGTH_SHORT).show() + when (message) { + getString(R.string.updateLocSuccess) -> drawerBtnLoc.enable() + getString(R.string.updateTleSuccess), getString(R.string.updateTleFailure) -> drawerBtnTle.enable() + getString(R.string.updateTransSuccess), getString(R.string.updateTransFailure) -> drawerBtnTrans.enable() + } }) viewModel.gsp.observe(this, Observer { gsp -> @@ -132,11 +137,21 @@ class MainActivity : AppCompatActivity() { } private fun setupDrawer() { - drawerBtnLoc.setOnClickListener { requestLocationUpdate() } - drawerBtnTle.setOnClickListener { viewModel.updateAndSaveTleFile() } - drawerBtnTrans.setOnClickListener { viewModel.updateTransmittersDatabase() } + drawerBtnLoc.setOnClickListener { + drawerBtnLoc.disable() + requestLocationUpdate() + } + drawerBtnTle.setOnClickListener { + drawerBtnTle.disable() + viewModel.updateAndSaveTleFile() + } + drawerBtnTrans.setOnClickListener { + drawerBtnTrans.disable() + viewModel.updateTransmittersDatabase() + } drawerBtnGithub.setOnClickListener { - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl))) + val githubIntent = Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl)) + startActivity(githubIntent) } drawerBtnExit.setOnClickListener { finish() } } @@ -167,4 +182,14 @@ class MainActivity : AppCompatActivity() { val navController = findNavController(R.id.nav_host) return navController.navigateUp(appBarConfig) || super.onSupportNavigateUp() } +} + +fun ImageButton.enable() { + this.isEnabled = true + this.alpha = 1.0f +} + +fun ImageButton.disable() { + this.isEnabled = false + this.alpha = 0.25f } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5e933ab0..5a433f1d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -100,4 +100,10 @@ GSP Nasa Map Placeholder + + Location was updated + TLE file was updated + Could not update TLE file + Transmitters database was updated + Could not update transmitters