diff --git a/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt b/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt index a5c1521a..9d9aff94 100644 --- a/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/data/TleSource.kt @@ -1,3 +1,3 @@ package com.rtbishop.look4sat.data -data class TleSource(var url: String) \ No newline at end of file +data class TleSource(var url: String = String()) \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt index 39fc116b..8d2b24b2 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt @@ -45,15 +45,17 @@ import com.rtbishop.look4sat.Look4SatApp import com.rtbishop.look4sat.R 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 import kotlinx.coroutines.delay import kotlinx.coroutines.launch import javax.inject.Inject -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), TleSourcesDialogFragment.SourcesSubmitListener { private val pickFileReqCode = 111 private val permLocationCode = 101 @@ -165,7 +167,7 @@ class MainActivity : AppCompatActivity() { it.lockButton() } drawerBinding.drawerBtnTleUrl.setOnClickListener { - showTleUrlDialog() + showTleSourcesDialog() mainBinding.drawerLayout.closeDrawers() it.lockButton() } @@ -187,7 +189,7 @@ class MainActivity : AppCompatActivity() { dialogBinding.apply { var tleUrl = String() dialogTleInput.apply { - hint = viewModel.getTleUrl() +// hint = viewModel.getTleUrl() addTextChangedListener { tleUrl = it.toString() } } dialogTleNeg.setOnClickListener { @@ -201,6 +203,19 @@ class MainActivity : AppCompatActivity() { dialog.show() } + private fun showTleSourcesDialog() { + val sources = viewModel.getTleSources().map { TleSource(it) } + TleSourcesDialogFragment(sources).apply { + setSourcesListener(this@MainActivity) + show(supportFragmentManager, "TleSourcesDialog") + } + } + + override fun onSourcesSubmit(list: List) { + val sources = list.map { it.url }.toSet() + viewModel.setTleSource(sources) + } + private fun View.lockButton() { lifecycleScope.launch { this@lockButton.isEnabled = false 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 9757db95..1f50d76a 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt @@ -59,7 +59,6 @@ class SharedViewModel @Inject constructor( fun getRefreshRate() = prefsManager.getRefreshRate() fun getHoursAhead() = prefsManager.getHoursAhead() fun getMinElevation() = prefsManager.getMinElevation() - fun getTleUrl() = prefsManager.getTleUrl() fun setPositionFromPref() = _gsp.postValue(Result.Success(prefsManager.getPosition())) suspend fun getAllEntries() = repository.getAllEntries() @@ -102,15 +101,15 @@ class SharedViewModel @Inject constructor( fun updateEntriesFromWeb(tleUrl: String? = null) { 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 url = if (tleUrl.isNullOrEmpty()) { +// prefsManager.getTleUrl() +// } else { +// prefsManager.setTleUrl(tleUrl) +// tleUrl +// } +// val selected = repository.getSelectedEntries().map { it.catNum } +// repository.updateEntriesFromUrl(url) +// repository.updateEntriesSelection(selected) _updateStatus.postValue(Result.Success(0)) } catch (e: Exception) { _updateStatus.postValue(Result.Error(e)) 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 index 380e26e2..c4d566e7 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/adapters/TleSourcesAdapter.kt @@ -1,9 +1,28 @@ +/* + * Look4Sat. Amateur radio and weather satellite tracker and passes predictor for Android. + * Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + 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.core.widget.doAfterTextChanged import androidx.recyclerview.widget.RecyclerView import com.rtbishop.look4sat.data.TleSource import com.rtbishop.look4sat.databinding.ItemTleSourceBinding @@ -25,8 +44,9 @@ class TleSourcesAdapter(private var sources: MutableList) : return sources.size } - fun setSources(sources: MutableList) { - this.sources = sources + fun setSources(list: MutableList) { + sources = list + notifyDataSetChanged() } fun getSources(): MutableList { @@ -37,14 +57,14 @@ class TleSourcesAdapter(private var sources: MutableList) : RecyclerView.ViewHolder(itemView) { fun bind(source: TleSource) { + binding.tleSourceUrl.setText(source.url) + binding.tleSourceUrl.doAfterTextChanged { + source.url = it.toString() + } 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/ui/fragments/TleSourcesDialogFragment.kt b/app/src/main/java/com/rtbishop/look4sat/ui/fragments/TleSourcesDialogFragment.kt new file mode 100644 index 00000000..af00514a --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/ui/fragments/TleSourcesDialogFragment.kt @@ -0,0 +1,76 @@ +/* + * Look4Sat. Amateur radio and weather satellite tracker and passes predictor for Android. + * Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package com.rtbishop.look4sat.ui.fragments + +import android.app.Dialog +import android.os.Bundle +import android.view.ViewGroup +import androidx.appcompat.app.AppCompatDialogFragment +import androidx.recyclerview.widget.LinearLayoutManager +import com.rtbishop.look4sat.data.TleSource +import com.rtbishop.look4sat.databinding.DialogTleSourcesBinding +import com.rtbishop.look4sat.ui.adapters.TleSourcesAdapter + +class TleSourcesDialogFragment(sources: List) : + AppCompatDialogFragment() { + + private lateinit var sourcesListener: SourcesSubmitListener + private var sourcesAdapter = TleSourcesAdapter(sources.toMutableList()) + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val binding = DialogTleSourcesBinding.inflate(requireActivity().layoutInflater) + val tleSourcesDialog = Dialog(requireActivity()).apply { + setContentView(binding.root) + window?.setLayout( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + } + + binding.tleSourcesRecycler.apply { + layoutManager = LinearLayoutManager(activity) + adapter = sourcesAdapter + } + + binding.tleSourceBtnAdd.setOnClickListener { + val tempSources = sourcesAdapter.getSources() + tempSources.add(TleSource()) + sourcesAdapter.setSources(tempSources) + sourcesAdapter.notifyDataSetChanged() + } + + binding.tleSourcesBtnNeg.setOnClickListener { dismiss() } + binding.tleSourcesBtnPos.setOnClickListener { + sourcesListener.onSourcesSubmit(sourcesAdapter.getSources()) + dismiss() + } + + return tleSourcesDialog + } + + fun setSourcesListener(listener: SourcesSubmitListener): TleSourcesDialogFragment { + sourcesListener = listener + return this + } + + interface SourcesSubmitListener { + fun onSourcesSubmit(list: List) + } +} \ 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 ddaec5da..511e5fa9 100644 --- a/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt +++ b/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt @@ -36,7 +36,6 @@ class PrefsManager @Inject constructor( private val keyAltitude = "altitude" 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 @@ -67,10 +66,6 @@ class PrefsManager @Inject constructor( return GroundStationPosition(lat, lon, alt) } - fun getTleUrl(): String { - return preferences.getString(keyTleUrl, defaultTleUrl)!! - } - fun setHoursAhead(hours: Int) { preferences.edit { putInt(keyHoursAhead, hours) @@ -94,19 +89,14 @@ class PrefsManager @Inject constructor( } } - fun setTleUrl(tleUrl: String) { - preferences.edit { - putString(keyTleUrl, tleUrl) - } - } - fun getTleSources(): Set { - return preferences.getStringSet(keyTleSources, null) ?: emptySet() + return preferences.getStringSet(keyTleSources, null) ?: setOf(defaultTleUrl) } fun setTleSources(sources: Set) { preferences.edit { putStringSet(keyTleSources, sources) + apply() } } diff --git a/app/src/main/res/layout/dialog_tle_sources.xml b/app/src/main/res/layout/dialog_tle_sources.xml index 4b4dc31e..40791a9c 100644 --- a/app/src/main/res/layout/dialog_tle_sources.xml +++ b/app/src/main/res/layout/dialog_tle_sources.xml @@ -9,34 +9,65 @@ android:id="@+id/tleSourcesTitle" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginStart="8dp" - android:layout_marginTop="8dp" - android:layout_marginEnd="8dp" - android:text="@string/placeholder" - app:layout_constraintEnd_toEndOf="parent" + android:layout_marginStart="12dp" + android:layout_marginTop="12dp" + android:text="@string/manage_tle_sources" + android:textSize="18sp" + app:layout_constraintEnd_toStartOf="@+id/tleSourceBtnAdd" + app:layout_constraintHorizontal_bias="0.51" + app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + + app:layout_constraintTop_toBottomOf="@+id/tleSourceBtnAdd" + app:layout_constraintVertical_chainStyle="packed"> + + + + @@ -47,20 +78,8 @@ android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:text="@string/btn_ok" + android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="@+id/tleSourcesRecycler" /> - - + app:layout_constraintEnd_toEndOf="@+id/tleSourceBtnAdd" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_tle_source.xml b/app/src/main/res/layout/item_tle_source.xml index 2d732f44..4835a8bd 100644 --- a/app/src/main/res/layout/item_tle_source.xml +++ b/app/src/main/res/layout/item_tle_source.xml @@ -6,30 +6,38 @@ android:layout_height="wrap_content" android:padding="4dp"> - + app:layout_constraintTop_toTopOf="parent"> + + + + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 3e313898..05f8630c 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -141,4 +141,6 @@ СТЦ Проверьте ваши настройки GPS + Manage TLE sources + TLE source diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0f3bcd4c..aba03515 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -166,4 +166,7 @@ GSP Placeholder + Manage TLE sources + Please use HTTPS URLs only + TLE source