Added Event.kt class for exceptions handling and showing

This commit is contained in:
Arty Bishop committed 2020-10-10 22:30:24 +01:00
1 parent 753a3c823f
commit 57eb0a7a38
5 files changed
+44 -12

No files matched your search

@@ -24,10 +24,7 @@ import androidx.hilt.Assisted
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.*
import com.github.amsacode.predict4java.GroundStationPosition
import com.rtbishop.look4sat.data.Result
import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.SatPass
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.*
import com.rtbishop.look4sat.repo.EntriesRepo
import com.rtbishop.look4sat.repo.SourcesRepo
import com.rtbishop.look4sat.repo.TransmittersRepo
@@ -50,6 +47,7 @@ class SharedViewModel @ViewModelInject constructor(
private val _currentTimeMillis = MutableLiveData(System.currentTimeMillis())
private val _passes = MutableLiveData<Result<MutableList<SatPass>>>()
private val _appEvent = MutableLiveData<Event<Any>>()
private var selectedEntries = emptyList<SatEntry>()
private var shouldTriggerCalculation = true
@@ -64,8 +62,13 @@ class SharedViewModel @ViewModelInject constructor(
fun getEntries() = entriesRepo.getEntries()
fun getCurrentTimeMillis(): LiveData<Long> = _currentTimeMillis
fun getPasses(): LiveData<Result<MutableList<SatPass>>> = _passes
fun getAppEvent(): LiveData<Event<Any>> = _appEvent
fun getTransmittersForSat(satId: Int) = transmittersRepo.getTransmittersForSat(satId)
fun postAppEvent(event: Event<Any>) {
_appEvent.value = event
}
fun triggerCalculation() {
if (shouldTriggerCalculation) {
shouldTriggerCalculation = false
@@ -79,15 +82,23 @@ class SharedViewModel @ViewModelInject constructor(
fun updateEntriesFromFile(uri: Uri) {
viewModelScope.launch {
entriesRepo.updateEntriesFromFile(uri)
try {
entriesRepo.updateEntriesFromFile(uri)
} catch (exception: Exception) {
postAppEvent(Event(exception))
}
}
}
fun updateEntriesFromSources(sources: List<TleSource>) {
viewModelScope.launch {
sourcesRepo.updateSources(sources)
entriesRepo.updateEntriesFromSources(sources)
transmittersRepo.updateTransmitters()
try {
sourcesRepo.updateSources(sources)
entriesRepo.updateEntriesFromSources(sources)
transmittersRepo.updateTransmitters()
} catch (exception: Exception) {
postAppEvent(Event(exception))
}
}
}
@@ -0,0 +1,17 @@
package com.rtbishop.look4sat.data
class Event<out T>(private val content: T) {
private var hasBeenHandled = false
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
fun peekContent(): T = content
}
@@ -16,6 +16,7 @@ import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.databinding.FragmentEntriesBinding
import com.rtbishop.look4sat.ui.adapters.EntriesAdapter
import com.rtbishop.look4sat.utility.Utilities.snack
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
@@ -62,6 +63,11 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
viewModel.setSelectedEntries(it)
entriesAdapter.setEntries(it as MutableList<SatEntry>)
})
viewModel.getAppEvent().observe(viewLifecycleOwner, { event ->
event.getContentIfNotHandled()?.let {
getString(R.string.error_updating_data).snack(requireView())
}
})
}
private fun showImportFromWebDialog() {
+1 -2
View File
@@ -51,8 +51,7 @@
<string name="no_downlink_doppler">Тек. нисход: нет</string>
<string name="no_trans">Не найдены трансиверы для этого спутника</string>
<string name="err_update_data">Пожалуйста, обновите данные спутников</string>
<string name="err_enter_value">Пожалуйста, введите значение</string>
<string name="error_updating_data">Ошибка импорта спутников</string>
<string name="pass_aos_az">НПС — %2d°</string>
<string name="pass_los_az">%2d° — КПС</string>
+1 -2
View File
@@ -52,8 +52,7 @@
<string name="no_downlink_doppler">Curr. downlink: null</string>
<string name="no_trans">No transceivers found for this satellite</string>
<string name="err_update_data">Please, update satellite data</string>
<string name="err_enter_value">Please, enter the value</string>
<string name="error_updating_data">Error importing satellite data</string>
<string name="pass_satName" translatable="false">%s</string>
<string name="pass_satId" translatable="false">ID: %d</string>