diff --git a/app/src/main/java/com/rtbishop/lookingsat/ui/MainActivity.kt b/app/src/main/java/com/rtbishop/lookingsat/ui/MainActivity.kt index 0d05866f..3b396342 100644 --- a/app/src/main/java/com/rtbishop/lookingsat/ui/MainActivity.kt +++ b/app/src/main/java/com/rtbishop/lookingsat/ui/MainActivity.kt @@ -10,6 +10,7 @@ import android.os.CountDownTimer import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.ImageButton import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @@ -18,7 +19,7 @@ import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.drawerlayout.widget.DrawerLayout -import androidx.lifecycle.ViewModelProviders +import androidx.lifecycle.ViewModelProvider import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.navigateUp @@ -44,6 +45,11 @@ class MainActivity : AppCompatActivity() { private lateinit var drawerLat: TextView private lateinit var drawerLon: 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?) { super.onCreate(savedInstanceState) @@ -51,11 +57,10 @@ class MainActivity : AppCompatActivity() { setupComponents() setupDrawer() setupTimer() - updateLocation() } private fun setupComponents() { - viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) + viewModel = ViewModelProvider(this).get(MainViewModel::class.java) timerLayout = findViewById(R.id.timer_layout) timeToAos = findViewById(R.id.time_to_aos) @@ -70,6 +75,12 @@ class MainActivity : AppCompatActivity() { drawerLon = header.findViewById(R.id.drawer_lon_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) appBarConfig = AppBarConfiguration(setOf(R.id.nav_sky, R.id.nav_single_sat), drawerLayout) setupActionBarWithNavController(navController, appBarConfig) @@ -77,7 +88,7 @@ class MainActivity : AppCompatActivity() { navController.addOnDestinationChangedListener { _, destination, _ -> 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 toolbar.visibility = View.VISIBLE timerLayout.visibility = View.VISIBLE @@ -94,11 +105,26 @@ class MainActivity : AppCompatActivity() { } 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 -> drawerLat.text = String.format("%.4f", gsp.latitude) drawerLon.text = String.format("%.4f", gsp.longitude) 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() { @@ -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, res: IntArray) { when (reqCode) { permLocationCode -> { diff --git a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt index e7327ac8..ea35daf7 100644 --- a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt +++ b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt @@ -4,7 +4,6 @@ import android.app.Application import android.content.Context import android.content.SharedPreferences import android.location.Location -import android.widget.Toast import androidx.core.content.edit import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData @@ -21,25 +20,38 @@ import java.io.IOException 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 preferences = PreferenceManager.getDefaultSharedPreferences(application) private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(application) - private val _gsp = MutableLiveData() + private val _debugMessage = MutableLiveData("") + val debugMessage: LiveData = _debugMessage + private val _gsp = MutableLiveData( + GroundStationPosition( + preferences.getDouble(keyLat, 0.0), + preferences.getDouble(keyLon, 0.0), + preferences.getDouble(keyHeight, 0.0) + ) + ) val gsp: LiveData = _gsp fun updateLocation() { fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? -> - val lat = location?.latitude ?: 51.5074 - val lon = location?.longitude ?: 0.1278 - val height = location?.altitude ?: 48.0 + val lat = location?.latitude ?: 0.0 + val lon = location?.longitude ?: 0.0 + val height = location?.altitude ?: 0.0 preferences.edit { - putDouble("LATITUDE", lat) - putDouble("LONGITUDE", lon) - putDouble("HEIGHT", height) + putDouble(keyLat, lat) + putDouble(keyLon, lon) + putDouble(keyHeight, height) apply() } _gsp.postValue(GroundStationPosition(lat, lon, height)) + _debugMessage.postValue("Location was updated") } } @@ -50,9 +62,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { getApplication().openFileOutput(fileName, Context.MODE_PRIVATE).use { 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) { - 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) { try { repository.updateTransmittersDatabase() - Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT).show() + _debugMessage.postValue("Transmitters were updated") } catch (exception: IOException) { - Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT).show() + _debugMessage.postValue("Couldn't update transmitters") } } } diff --git a/build.gradle b/build.gradle index 3c59fa6d..bbcb13a7 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { } 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" } } @@ -33,7 +33,7 @@ ext { preference = '1.1.0' navigation = '2.1.0' - lifecycle = '2.1.0' + lifecycle = '2.2.0-rc01' work = '2.2.0' room = '2.2.1'