Finished (finally!!) TLE custom sources dialog

This commit is contained in:
Arty Bishop committed 2020-05-26 16:53:59 +01:00
1 parent 7f26d2b136
commit 389edcb33a
8 files changed
+68 -71

No files matched your search

@@ -19,6 +19,7 @@
package com.rtbishop.look4sat.network
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.Transmitter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -34,12 +35,14 @@ class RemoteDataSource @Inject constructor(
private val client: OkHttpClient
) : RemoteSource {
override suspend fun fetchTleStream(tleUrl: String): InputStream {
override suspend fun fetchTleStream(urlList: List<TleSource>): InputStream {
val streamTable = Hashtable<String, InputStream>()
withContext(Dispatchers.IO) {
val request = Request.Builder().url(tleUrl).build()
val stream = client.newCall(request).execute().body()?.byteStream()
streamTable[tleUrl] = stream
urlList.forEach {
val request = Request.Builder().url(it.url).build()
val stream = client.newCall(request).execute().body()?.byteStream()
streamTable[it.url] = stream
}
}
return SequenceInputStream(streamTable.elements())
}
@@ -1,11 +1,12 @@
package com.rtbishop.look4sat.network
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.Transmitter
import java.io.InputStream
interface RemoteSource {
suspend fun fetchTleStream(tleUrl: String): InputStream
suspend fun fetchTleStream(urlList: List<TleSource>): InputStream
suspend fun fetchTransmitters(): List<Transmitter>
}
@@ -23,6 +23,7 @@ import android.content.ContentResolver
import android.net.Uri
import com.github.amsacode.predict4java.TLE
import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.Transmitter
import com.rtbishop.look4sat.network.RemoteSource
import com.rtbishop.look4sat.persistence.LocalSource
@@ -46,8 +47,8 @@ class DefaultRepository @Inject constructor(
}
}
override suspend fun updateEntriesFromUrl(tleUrl: String) {
val stream = remoteSource.fetchTleStream(tleUrl)
override suspend fun updateEntriesFromUrl(urlList: List<TleSource>) {
val stream = remoteSource.fetchTleStream(urlList)
withContext(Dispatchers.IO) {
val tleList = TLE.importSat(stream)
val entries = tleList.map { SatEntry(it) }
@@ -2,13 +2,14 @@ package com.rtbishop.look4sat.repo
import android.net.Uri
import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.Transmitter
interface Repository {
suspend fun updateEntriesFromFile(tleUri: Uri)
suspend fun updateEntriesFromUrl(tleUrl: String)
suspend fun updateEntriesFromUrl(urlList: List<TleSource>)
suspend fun getAllEntries(): List<SatEntry>
@@ -28,11 +28,9 @@ import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
@@ -47,7 +45,6 @@ import com.rtbishop.look4sat.dagger.ViewModelFactory
import com.rtbishop.look4sat.data.Result
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.databinding.ActivityMainBinding
import com.rtbishop.look4sat.databinding.DialogTleUrlBinding
import com.rtbishop.look4sat.databinding.DrawerHeaderBinding
import com.rtbishop.look4sat.ui.fragments.TleSourcesDialogFragment
import com.rtbishop.look4sat.utility.GeneralUtils.toast
@@ -183,26 +180,6 @@ class MainActivity : AppCompatActivity(), TleSourcesDialogFragment.SourcesSubmit
drawerBinding.drawerBtnExit.setOnClickListener { finish() }
}
private fun showTleUrlDialog() {
val dialogBinding = DialogTleUrlBinding.inflate(layoutInflater)
val dialog = AlertDialog.Builder(this).setView(dialogBinding.root).create()
dialogBinding.apply {
var tleUrl = String()
dialogTleInput.apply {
// hint = viewModel.getTleUrl()
addTextChangedListener { tleUrl = it.toString() }
}
dialogTleNeg.setOnClickListener {
dialog.dismiss()
}
dialogTlePos.setOnClickListener {
viewModel.updateEntriesFromWeb(tleUrl)
dialog.dismiss()
}
}
dialog.show()
}
private fun showTleSourcesDialog() {
val sources = viewModel.getTleSources().map { TleSource(it) }
TleSourcesDialogFragment(sources).apply {
@@ -212,8 +189,7 @@ class MainActivity : AppCompatActivity(), TleSourcesDialogFragment.SourcesSubmit
}
override fun onSourcesSubmit(list: List<TleSource>) {
val sources = list.map { it.url }.toSet()
viewModel.setTleSource(sources)
viewModel.updateEntriesFromWeb(list)
}
private fun View.lockButton() {
@@ -26,6 +26,7 @@ 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.repo.Repository
import com.rtbishop.look4sat.utility.PassPredictor
import com.rtbishop.look4sat.utility.PrefsManager
@@ -59,6 +60,7 @@ class SharedViewModel @Inject constructor(
fun getRefreshRate() = prefsManager.getRefreshRate()
fun getHoursAhead() = prefsManager.getHoursAhead()
fun getMinElevation() = prefsManager.getMinElevation()
fun getTleSources() = prefsManager.getTleSources()
fun setPositionFromPref() = _gsp.postValue(Result.Success(prefsManager.getPosition()))
suspend fun getAllEntries() = repository.getAllEntries()
@@ -98,18 +100,15 @@ class SharedViewModel @Inject constructor(
}
}
fun updateEntriesFromWeb(tleUrl: String? = null) {
fun updateEntriesFromWeb(list: List<TleSource>) {
val sourcesSet = list.map { it.url }.toSet()
prefsManager.setTleSources(sourcesSet)
viewModelScope.launch(Dispatchers.IO) {
try {
// val url = if (tleUrl.isNullOrEmpty()) {
// prefsManager.getTleUrl()
// } else {
// prefsManager.setTleUrl(tleUrl)
// tleUrl
// }
// val selected = repository.getSelectedEntries().map { it.catNum }
// repository.updateEntriesFromUrl(url)
// repository.updateEntriesSelection(selected)
val selected = repository.getSelectedEntries().map { it.catNum }
repository.updateEntriesFromUrl(list)
repository.updateEntriesSelection(selected)
_updateStatus.postValue(Result.Success(0))
} catch (e: Exception) {
_updateStatus.postValue(Result.Error(e))
@@ -117,6 +116,13 @@ class SharedViewModel @Inject constructor(
}
}
fun updateEntriesSelection(catNumList: MutableList<Int>) {
viewModelScope.launch(Dispatchers.IO) {
repository.updateEntriesSelection(catNumList)
calculatePasses()
}
}
fun updateTransmitters() {
viewModelScope.launch(Dispatchers.IO) {
try {
@@ -128,13 +134,6 @@ class SharedViewModel @Inject constructor(
}
}
fun updateEntriesSelection(catNumList: MutableList<Int>) {
viewModelScope.launch(Dispatchers.IO) {
repository.updateEntriesSelection(catNumList)
calculatePasses()
}
}
fun calculatePasses() {
_satPassList.postValue(Result.InProgress)
calculationJob?.cancel()
@@ -150,14 +149,6 @@ class SharedViewModel @Inject constructor(
}
}
fun getTleSources(): Set<String> {
return prefsManager.getTleSources()
}
fun setTleSource(sources: Set<String>) {
prefsManager.setTleSources(sources)
}
private fun getPassesForEntries(
entry: SatEntry,
dateNow: Date,
@@ -19,10 +19,11 @@
package com.rtbishop.look4sat.ui.adapters
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.databinding.ItemTleSourceBinding
@@ -33,10 +34,11 @@ class TleSourcesAdapter(private var sources: MutableList<TleSource>) :
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TleSourceHolder {
val binding = ItemTleSourceBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return TleSourceHolder(binding.root, binding)
return TleSourceHolder(binding.root, binding, TleSourceWatcher())
}
override fun onBindViewHolder(holder: TleSourceHolder, position: Int) {
holder.textWatcher.updatePosition(position)
holder.bind(sources[position])
}
@@ -44,27 +46,46 @@ class TleSourcesAdapter(private var sources: MutableList<TleSource>) :
return sources.size
}
fun getSources(): MutableList<TleSource> {
return sources
}
fun setSources(list: MutableList<TleSource>) {
sources = list
notifyDataSetChanged()
}
fun getSources(): MutableList<TleSource> {
return sources
}
inner class TleSourceHolder(itemView: View, private val binding: ItemTleSourceBinding) :
inner class TleSourceHolder(
itemView: View,
val binding: ItemTleSourceBinding,
val textWatcher: TleSourceWatcher
) :
RecyclerView.ViewHolder(itemView) {
fun bind(source: TleSource) {
binding.tleSourceUrl.setText(source.url)
binding.tleSourceUrl.doAfterTextChanged {
source.url = it.toString()
}
binding.tleSourceUrl.addTextChangedListener(textWatcher)
binding.tleSourceBtnDel.setOnClickListener {
sources.remove(source)
notifyItemRemoved(adapterPosition)
}
}
}
inner class TleSourceWatcher : TextWatcher {
private var position = 0
fun updatePosition(position: Int) {
this.position = position
}
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
sources[position].url = s.toString()
}
}
}
@@ -32,7 +32,8 @@ class TleSourcesDialogFragment(sources: List<TleSource>) :
AppCompatDialogFragment() {
private lateinit var sourcesListener: SourcesSubmitListener
private var sourcesAdapter = TleSourcesAdapter(sources.toMutableList())
private val emptySource = TleSource("")
private val sourcesAdapter = TleSourcesAdapter(sources.toMutableList())
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogTleSourcesBinding.inflate(requireActivity().layoutInflater)
@@ -51,14 +52,16 @@ class TleSourcesDialogFragment(sources: List<TleSource>) :
binding.tleSourceBtnAdd.setOnClickListener {
val tempSources = sourcesAdapter.getSources()
tempSources.add(TleSource())
tempSources.add(emptySource)
sourcesAdapter.setSources(tempSources)
sourcesAdapter.notifyDataSetChanged()
}
binding.tleSourcesBtnNeg.setOnClickListener { dismiss() }
binding.tleSourcesBtnPos.setOnClickListener {
sourcesListener.onSourcesSubmit(sourcesAdapter.getSources())
val filteredSources = sourcesAdapter.getSources()
.filter { it.url != String() && it.url != " " && it.url.contains("https://") }
sourcesListener.onSourcesSubmit(filteredSources)
dismiss()
}