Updated TLE file update, save and load logic

This commit is contained in:
Arty Bishop committed 2019-12-09 08:53:57 +00:00
1 parent d8bb1ae796
commit 928ccd4991
2 files changed
+25 -14

No files matched your search

@@ -22,9 +22,12 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@Suppress("UNCHECKED_CAST")
class MainViewModel(application: Application) : AndroidViewModel(application) { class MainViewModel(application: Application) : AndroidViewModel(application) {
private val defValueLoc = application.getString(R.string.def_gsp_loc) private val defValueLoc = application.getString(R.string.def_gsp_loc)
@@ -36,6 +39,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
private val keyHours = application.getString(R.string.key_hours_ahead) private val keyHours = application.getString(R.string.key_hours_ahead)
private val keyMinEl = application.getString(R.string.key_min_el) private val keyMinEl = application.getString(R.string.key_min_el)
private val tleFileName = application.getString(R.string.tle_file_name) private val tleFileName = application.getString(R.string.tle_file_name)
private val app = application
@Inject @Inject
lateinit var locationClient: FusedLocationProviderClient lateinit var locationClient: FusedLocationProviderClient
@@ -45,7 +49,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
lateinit var repository: Repository lateinit var repository: Repository
init { init {
(application as LookingSatApp).appComponent.inject(this) (app as LookingSatApp).appComponent.inject(this)
} }
val debugMessage = MutableLiveData("") val debugMessage = MutableLiveData("")
@@ -66,13 +70,17 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
) )
private fun loadTwoLineElementFile(): List<TLE> { private fun loadTwoLineElementFile(): List<TLE> {
return try { try {
TLE.importSat(getApplication<Application>().openFileInput(tleFileName)) val fileInput = app.openFileInput(tleFileName)
.sortedWith(compareBy { it.name }) val objectInput = ObjectInputStream(fileInput)
} catch (exception: FileNotFoundException) { val tleList = objectInput.readObject()
return tleList as List<TLE>
} catch (e: FileNotFoundException) {
debugMessage.postValue("TLE file wasn't found") debugMessage.postValue("TLE file wasn't found")
emptyList() } catch (e: IOException) {
debugMessage.postValue(e.toString())
} }
return emptyList()
} }
suspend fun getPasses() { suspend fun getPasses() {
@@ -132,15 +140,18 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
} }
} }
fun updateTwoLineElementFile() { fun updateAndSaveTleFile() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val stream = repository.fetchTleStream() val inputStream = repository.fetchTleStream()
getApplication<Application>().openFileOutput(tleFileName, Context.MODE_PRIVATE) val tleList = TLE.importSat(inputStream).apply { sortBy { it.name } }
.use { val fileOutStream = app.openFileOutput(tleFileName, Context.MODE_PRIVATE)
it.write(stream.readBytes()) ObjectOutputStream(fileOutStream).apply {
} writeObject(tleList)
tleMainList = loadTwoLineElementFile() flush()
close()
}
tleMainList = tleList
debugMessage.postValue("TLE file was updated") debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) { } catch (exception: IOException) {
debugMessage.postValue("Couldn't update TLE file") debugMessage.postValue("Couldn't update TLE file")
@@ -115,7 +115,7 @@ class MainActivity : AppCompatActivity() {
}) })
drawerBtnLoc.setOnClickListener { updateLocation() } drawerBtnLoc.setOnClickListener { updateLocation() }
drawerBtnTle.setOnClickListener { viewModel.updateTwoLineElementFile() } drawerBtnTle.setOnClickListener { viewModel.updateAndSaveTleFile() }
drawerBtnTrans.setOnClickListener { viewModel.updateTransmittersDatabase() } drawerBtnTrans.setOnClickListener { viewModel.updateTransmittersDatabase() }
drawerBtnGithub.setOnClickListener { drawerBtnGithub.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl))) startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(githubUrl)))