From ec03c76fa18208d5da940b76e2f3558327e1ee24 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Mon, 18 May 2020 14:37:35 +0100 Subject: [PATCH] Added a data model and an adapter for TLE custom sources dialog --- .../com/rtbishop/look4sat/data/TleSource.kt | 3 ++ .../rtbishop/look4sat/ui/SharedViewModel.kt | 8 +++ .../look4sat/ui/adapters/TleSourcesAdapter.kt | 50 +++++++++++++++++++ .../rtbishop/look4sat/utility/PrefsManager.kt | 18 ++++--- .../main/res/layout/dialog_tle_sources.xml | 23 +++++++-- 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt create mode 100644 app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt diff --git a/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt b/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt new file mode 100644 index 00000000..a5c1521a --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt @@ -0,0 +1,3 @@ +package com.rtbishop.look4sat.data + +data class TleSource(var url: String) \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt index 9368337e..9757db95 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt @@ -151,6 +151,14 @@ class SharedViewModel @Inject constructor( } } + fun getTleSources(): Set { + return prefsManager.getTleSources() + } + + fun setTleSource(sources: Set) { + prefsManager.setTleSources(sources) + } + private fun getPassesForEntries( entry: SatEntry, dateNow: Date, diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt b/app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt new file mode 100644 index 00000000..380e26e2 --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt @@ -0,0 +1,50 @@ +package com.rtbishop.look4sat.ui.adapters + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.widget.addTextChangedListener +import androidx.recyclerview.widget.RecyclerView +import com.rtbishop.look4sat.data.TleSource +import com.rtbishop.look4sat.databinding.ItemTleSourceBinding + +class TleSourcesAdapter(private var sources: MutableList) : + RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TleSourceHolder { + val binding = ItemTleSourceBinding + .inflate(LayoutInflater.from(parent.context), parent, false) + return TleSourceHolder(binding.root, binding) + } + + override fun onBindViewHolder(holder: TleSourceHolder, position: Int) { + holder.bind(sources[position]) + } + + override fun getItemCount(): Int { + return sources.size + } + + fun setSources(sources: MutableList) { + this.sources = sources + } + + fun getSources(): MutableList { + return sources + } + + inner class TleSourceHolder(itemView: View, private val binding: ItemTleSourceBinding) : + RecyclerView.ViewHolder(itemView) { + + fun bind(source: TleSource) { + binding.tleSourceBtnDel.setOnClickListener { + sources.remove(source) + notifyItemRemoved(adapterPosition) + } + binding.tleSourceUrl.addTextChangedListener { + source.url = it.toString() + notifyItemChanged(adapterPosition) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt b/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt index 5cd802a2..ddaec5da 100644 --- a/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt +++ b/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt @@ -37,6 +37,7 @@ class PrefsManager @Inject constructor( private val keyRefreshRate = "rate" private val keyCompass = "compass" private val keyTleUrl = "tleUrl" + private val keyTleSources = "tleSources" private val defaultHoursAhead = 8 private val defaultMinEl = 16.0 private val defaultRefreshRate = "3000" @@ -84,13 +85,6 @@ class PrefsManager @Inject constructor( } } - fun setRefreshRate(rate: Long) { - preferences.edit { - putLong(keyRefreshRate, rate) - apply() - } - } - fun setPosition(gsp: GroundStationPosition) { preferences.edit { putString(keyLatitude, gsp.latitude.toString()) @@ -106,6 +100,16 @@ class PrefsManager @Inject constructor( } } + fun getTleSources(): Set { + return preferences.getStringSet(keyTleSources, null) ?: emptySet() + } + + fun setTleSources(sources: Set) { + preferences.edit { + putStringSet(keyTleSources, sources) + } + } + private fun SharedPreferences.Editor.putDouble(key: String, double: Double) { putLong(key, double.toRawBits()) } diff --git a/app/src/main/res/layout/dialog_tle_sources.xml b/app/src/main/res/layout/dialog_tle_sources.xml index b70cb6e4..4b4dc31e 100644 --- a/app/src/main/res/layout/dialog_tle_sources.xml +++ b/app/src/main/res/layout/dialog_tle_sources.xml @@ -20,15 +20,16 @@ + app:layout_constraintTop_toBottomOf="@+id/tleSourcesTitle" + app:layout_constraintVertical_chainStyle="packed" /> + + + \ No newline at end of file