Attached action to button in the drawer header. Added LiveData debug toasts. Libraries update.

This commit is contained in:
Arty Bishop committed 2019-11-05 21:08:22 +01:00
1 parent dc7642bc52
commit 52fa9ab603
3 files changed
+56 -27

No files matched your search

@@ -10,6 +10,7 @@ import android.os.CountDownTimer
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.ImageButton
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@@ -18,7 +19,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp import androidx.navigation.ui.navigateUp
@@ -44,6 +45,11 @@ class MainActivity : AppCompatActivity() {
private lateinit var drawerLat: TextView private lateinit var drawerLat: TextView
private lateinit var drawerLon: TextView private lateinit var drawerLon: TextView
private lateinit var drawerHeight: TextView private lateinit var drawerHeight: TextView
private lateinit var drawerBtnLoc: ImageButton
private lateinit var drawerBtnTle: ImageButton
private lateinit var drawerBtnTrans: ImageButton
private lateinit var drawerBtnGithub: ImageButton
private lateinit var drawerBtnExit: ImageButton
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -51,11 +57,10 @@ class MainActivity : AppCompatActivity() {
setupComponents() setupComponents()
setupDrawer() setupDrawer()
setupTimer() setupTimer()
updateLocation()
} }
private fun setupComponents() { private fun setupComponents() {
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
timerLayout = findViewById(R.id.timer_layout) timerLayout = findViewById(R.id.timer_layout)
timeToAos = findViewById(R.id.time_to_aos) timeToAos = findViewById(R.id.time_to_aos)
@@ -70,6 +75,12 @@ class MainActivity : AppCompatActivity() {
drawerLon = header.findViewById(R.id.drawer_lon_value) drawerLon = header.findViewById(R.id.drawer_lon_value)
drawerHeight = header.findViewById(R.id.drawer_height_value) drawerHeight = header.findViewById(R.id.drawer_height_value)
drawerBtnLoc = header.findViewById(R.id.drawer_btn_loc)
drawerBtnTle = header.findViewById(R.id.drawer_btn_tle)
drawerBtnTrans = header.findViewById(R.id.drawer_btn_trans)
drawerBtnGithub = header.findViewById(R.id.drawer_btn_github)
drawerBtnExit = header.findViewById(R.id.drawer_btn_exit)
val navController = findNavController(R.id.nav_host) val navController = findNavController(R.id.nav_host)
appBarConfig = AppBarConfiguration(setOf(R.id.nav_sky, R.id.nav_single_sat), drawerLayout) appBarConfig = AppBarConfiguration(setOf(R.id.nav_sky, R.id.nav_single_sat), drawerLayout)
setupActionBarWithNavController(navController, appBarConfig) setupActionBarWithNavController(navController, appBarConfig)
@@ -77,7 +88,7 @@ class MainActivity : AppCompatActivity() {
navController.addOnDestinationChangedListener { _, destination, _ -> navController.addOnDestinationChangedListener { _, destination, _ ->
when { when {
destination.id == R.id.nav_sky -> { destination.id == R.id.nav_sky || destination.id == R.id.nav_single_sat -> {
this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
toolbar.visibility = View.VISIBLE toolbar.visibility = View.VISIBLE
timerLayout.visibility = View.VISIBLE timerLayout.visibility = View.VISIBLE
@@ -94,11 +105,26 @@ class MainActivity : AppCompatActivity() {
} }
private fun setupDrawer() { private fun setupDrawer() {
viewModel.debugMessage.observe(this, androidx.lifecycle.Observer { debug_message ->
if (debug_message != "") {
Toast.makeText(this, debug_message, Toast.LENGTH_SHORT).show()
}
})
viewModel.gsp.observe(this, androidx.lifecycle.Observer { gsp -> viewModel.gsp.observe(this, androidx.lifecycle.Observer { gsp ->
drawerLat.text = String.format("%.4f", gsp.latitude) drawerLat.text = String.format("%.4f", gsp.latitude)
drawerLon.text = String.format("%.4f", gsp.longitude) drawerLon.text = String.format("%.4f", gsp.longitude)
drawerHeight.text = String.format("%.1fm", gsp.heightAMSL) drawerHeight.text = String.format("%.1fm", gsp.heightAMSL)
}) })
drawerBtnLoc.setOnClickListener { updateLocation() }
drawerBtnTle.setOnClickListener { viewModel.updateTwoLineElementFile() }
drawerBtnTrans.setOnClickListener { viewModel.updateTransmittersDatabase() }
drawerBtnGithub.setOnClickListener {
val intentGitHub = Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl))
startActivity(intentGitHub)
}
drawerBtnExit.setOnClickListener { finish() }
} }
private fun setupTimer() { private fun setupTimer() {
@@ -139,15 +165,6 @@ class MainActivity : AppCompatActivity() {
} }
} }
private fun goToGithub() {
val intentGitHub = Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl))
startActivity(intentGitHub)
}
private fun exit() {
finish()
}
override fun onRequestPermissionsResult(reqCode: Int, perms: Array<out String>, res: IntArray) { override fun onRequestPermissionsResult(reqCode: Int, perms: Array<out String>, res: IntArray) {
when (reqCode) { when (reqCode) {
permLocationCode -> { permLocationCode -> {
@@ -4,7 +4,6 @@ import android.app.Application
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.location.Location import android.location.Location
import android.widget.Toast
import androidx.core.content.edit import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
@@ -21,25 +20,38 @@ import java.io.IOException
class MainViewModel(application: Application) : AndroidViewModel(application) { class MainViewModel(application: Application) : AndroidViewModel(application) {
private val keyLat = "LATITUDE"
private val keyLon = "LONGITUDE"
private val keyHeight = "HEIGHT"
private val repository: Repository = Injector.provideRepository(application) private val repository: Repository = Injector.provideRepository(application)
private val preferences = PreferenceManager.getDefaultSharedPreferences(application) private val preferences = PreferenceManager.getDefaultSharedPreferences(application)
private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(application) private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(application)
private val _gsp = MutableLiveData<GroundStationPosition>() private val _debugMessage = MutableLiveData("")
val debugMessage: LiveData<String> = _debugMessage
private val _gsp = MutableLiveData<GroundStationPosition>(
GroundStationPosition(
preferences.getDouble(keyLat, 0.0),
preferences.getDouble(keyLon, 0.0),
preferences.getDouble(keyHeight, 0.0)
)
)
val gsp: LiveData<GroundStationPosition> = _gsp val gsp: LiveData<GroundStationPosition> = _gsp
fun updateLocation() { fun updateLocation() {
fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? -> fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
val lat = location?.latitude ?: 51.5074 val lat = location?.latitude ?: 0.0
val lon = location?.longitude ?: 0.1278 val lon = location?.longitude ?: 0.0
val height = location?.altitude ?: 48.0 val height = location?.altitude ?: 0.0
preferences.edit { preferences.edit {
putDouble("LATITUDE", lat) putDouble(keyLat, lat)
putDouble("LONGITUDE", lon) putDouble(keyLon, lon)
putDouble("HEIGHT", height) putDouble(keyHeight, height)
apply() apply()
} }
_gsp.postValue(GroundStationPosition(lat, lon, height)) _gsp.postValue(GroundStationPosition(lat, lon, height))
_debugMessage.postValue("Location was updated")
} }
} }
@@ -50,9 +62,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(fileName, Context.MODE_PRIVATE).use { getApplication<Application>().openFileOutput(fileName, Context.MODE_PRIVATE).use {
it.write(repository.fetchTleStream().readBytes()) it.write(repository.fetchTleStream().readBytes())
} }
Toast.makeText(getApplication(), "TLE file was updated", Toast.LENGTH_SHORT).show() _debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) { } catch (exception: IOException) {
Toast.makeText(getApplication(), "Could not update TLE", Toast.LENGTH_SHORT).show() _debugMessage.postValue("Couldn't update TLE file")
} }
} }
} }
@@ -61,9 +73,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
repository.updateTransmittersDatabase() repository.updateTransmittersDatabase()
Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT).show() _debugMessage.postValue("Transmitters were updated")
} catch (exception: IOException) { } catch (exception: IOException) {
Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT).show() _debugMessage.postValue("Couldn't update transmitters")
} }
} }
} }
+2 -2
View File
@@ -8,7 +8,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.5.1' classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }
@@ -33,7 +33,7 @@ ext {
preference = '1.1.0' preference = '1.1.0'
navigation = '2.1.0' navigation = '2.1.0'
lifecycle = '2.1.0' lifecycle = '2.2.0-rc01'
work = '2.2.0' work = '2.2.0'
room = '2.2.1' room = '2.2.1'