Further changes to SettingsFragment.kt

This commit is contained in:
Arty Bishop committed 2022-01-09 18:03:21 +00:00
1 parent a9a4aa9737
commit 7d3d746c4d
13 files changed
+164 -99

No files matched your search

@@ -68,29 +68,29 @@ class LocationProvider @Inject constructor(
} else _stationPosition.tryEmit(DataState.Error(context.getString(R.string.pref_pos_gps_null)))
}
override fun setPositionFromLocation() {
override fun setPositionFromGps() {
val result = ContextCompat.checkSelfPermission(context, locationFine)
if (manager.isProviderEnabled(providerDef) && result == PackageManager.PERMISSION_GRANTED) {
if (manager.isProviderEnabled(providerGps) && result == PackageManager.PERMISSION_GRANTED) {
val location = manager.getLastKnownLocation(providerDef)
if (location != null) {
setStationPosition(location.latitude, location.longitude)
} else setPositionFromGps()
} else {
_stationPosition.tryEmit(DataState.Loading)
manager.requestLocationUpdates(providerGps, 0L, 0f, this)
}
} else _stationPosition.tryEmit(DataState.Error(context.getString(R.string.pref_pos_gps_null)))
}
override fun setPositionFromNet() {
val result = ContextCompat.checkSelfPermission(context, locationCoarse)
if (manager.isProviderEnabled(providerNet) && result == PackageManager.PERMISSION_GRANTED) {
_stationPosition.tryEmit(DataState.Loading)
manager.requestLocationUpdates(providerNet, 0L, 0f, this)
} else _stationPosition.tryEmit(DataState.Error(context.getString(R.string.pref_pos_gps_null)))
}
override fun setPositionFromGps() {
val result = ContextCompat.checkSelfPermission(context, locationFine)
if (manager.isProviderEnabled(providerGps) && result == PackageManager.PERMISSION_GRANTED) {
_stationPosition.tryEmit(DataState.Loading)
manager.requestLocationUpdates(providerGps, 0L, 0f, this)
val location = manager.getLastKnownLocation(providerDef)
if (location != null) {
setStationPosition(location.latitude, location.longitude)
} else {
_stationPosition.tryEmit(DataState.Loading)
manager.requestLocationUpdates(providerNet, 0L, 0f, this)
}
} else _stationPosition.tryEmit(DataState.Error(context.getString(R.string.pref_pos_gps_null)))
}
@@ -25,6 +25,7 @@ import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.doOnTextChanged
@@ -32,7 +33,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.asLiveData
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
import com.rtbishop.look4sat.BuildConfig
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.databinding.FragmentSettingsBinding
@@ -65,9 +65,9 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
private val locationContract = ActivityResultContracts.RequestMultiplePermissions()
private val locationRequest = registerForActivityResult(locationContract) { permissions ->
when {
permissions[locationFine] == true -> locationHandler.setPositionFromLocation()
permissions[locationFine] == true -> locationHandler.setPositionFromGps()
permissions[locationCoarse] == true -> locationHandler.setPositionFromNet()
else -> showSnack(getString(R.string.pref_pos_gps_error))
else -> showToast(getString(R.string.pref_pos_gps_error))
}
}
private val contentContract = ActivityResultContracts.GetContent()
@@ -89,7 +89,7 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
setupOtherCard(settingsBinding)
setupWarrantyCard(settingsBinding)
locationHandler.stationPosition.asLiveData().observe(viewLifecycleOwner) { stationPos ->
stationPos?.let { handleStationPosition(it) }
stationPos?.let { handleStationPosition(it, settingsBinding) }
}
}
@@ -107,17 +107,8 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
}
private fun setupDataCard(binding: FragmentSettingsBinding) {
binding.prefsData.updateBtnFile.setOnClickListener { contentRequest.launch("*/*") }
binding.prefsData.updateBtnWeb.setOnClickListener {
findNavController().navigateSafe(R.id.action_prefs_to_sources)
}
getNavResult<List<String>>(R.id.nav_prefs, "sources") { sources ->
lifecycleScope.launchWhenResumed { dataRepository.updateDataFromWeb(sources) }
}
}
private fun setupLocationCard(binding: FragmentSettingsBinding) {
setPositionText(locationHandler.getStationPosition(), binding)
binding.prefsLocation.locationBtnGps.setOnClickListener {
locationRequest.launch(arrayOf(locationFine, locationCoarse))
}
@@ -136,6 +127,16 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
}
private fun setupDataCard(binding: FragmentSettingsBinding) {
binding.prefsData.updateBtnFile.setOnClickListener { contentRequest.launch("*/*") }
binding.prefsData.updateBtnWeb.setOnClickListener {
findNavController().navigateSafe(R.id.action_prefs_to_sources)
}
getNavResult<List<String>>(R.id.nav_prefs, "sources") { sources ->
lifecycleScope.launchWhenResumed { dataRepository.updateDataFromWeb(sources) }
}
}
private fun setupTrackingCard(binding: FragmentSettingsBinding) {
binding.prefsTracking.trackingSwitch.apply {
isChecked = preferences.getRotatorEnabled()
@@ -177,16 +178,32 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
binding.prefsWarranty.warrantyLicense.movementMethod = LinkMovementMethod.getInstance()
}
private fun handleStationPosition(position: DataState<GeoPos>) {
when (position) {
is DataState.Success -> showSnack(getString(R.string.pref_pos_success))
is DataState.Error -> showSnack(position.message.toString())
DataState.Loading -> showSnack("Fetching position")
private fun setPositionText(geoPos: GeoPos, binding: FragmentSettingsBinding) {
val latFormat = getString(R.string.location_lat)
val lonFormat = getString(R.string.location_lon)
binding.prefsLocation.locationLat.text = String.format(latFormat, geoPos.latitude)
binding.prefsLocation.locationLon.text = String.format(lonFormat, geoPos.longitude)
}
private fun handleStationPosition(pos: DataState<GeoPos>, binding: FragmentSettingsBinding) {
when (pos) {
is DataState.Success -> {
setPositionText(pos.data, binding)
binding.prefsLocation.locationProgress.isIndeterminate = false
showToast(getString(R.string.pref_pos_success))
}
is DataState.Error -> {
binding.prefsLocation.locationProgress.isIndeterminate = false
showToast(pos.message.toString())
}
DataState.Loading -> {
binding.prefsLocation.locationProgress.isIndeterminate = true
}
}
}
private fun showSnack(message: String) {
Snackbar.make(requireView(), message, Snackbar.LENGTH_SHORT).show()
private fun showToast(message: String) {
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}
private fun gotoUrl(url: String) {
+3 -3
View File
@@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/themeAccent"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z" />
<path
android:fillColor="@color/themeAccent"
android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z" />
</vector>
@@ -6,15 +6,51 @@
<TextView
android:id="@+id/location_title"
style="@style/SettingsCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/location_title"
android:textColor="@color/themeAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/location_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="6dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/location_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/location_title"
app:layout_constraintTop_toTopOf="@+id/location_title" />
<TextView
android:id="@+id/location_lat"
style="@style/SettingsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/location_lat"
app:layout_constraintEnd_toStartOf="@+id/location_lon"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="@+id/location_title"
app:layout_constraintTop_toBottomOf="@+id/location_title" />
<TextView
android:id="@+id/location_lon"
style="@style/SettingsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_lon"
app:layout_constraintBaseline_toBaselineOf="@+id/location_lat"
app:layout_constraintEnd_toEndOf="@+id/location_progress"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/location_lat" />
<Button
android:id="@+id/location_btn_gps"
style="@style/AppButton"
@@ -30,7 +66,7 @@
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/location_title" />
app:layout_constraintTop_toBottomOf="@+id/location_lat" />
<Button
android:id="@+id/location_btn_qth"
+6 -10
View File
@@ -6,11 +6,11 @@
<TextView
android:id="@+id/other_title"
style="@style/SettingsCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="@string/other_title"
android:textColor="@color/themeAccent"
app:layout_constraintStart_toStartOf="parent"
@@ -18,40 +18,36 @@
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/other_switch_utc"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:minHeight="48dp"
android:textSize="16sp"
android:text="@string/other_switch_utc"
android:textColor="@color/themeLight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toStartOf="@+id/other_title"
app:layout_constraintTop_toBottomOf="@+id/other_title" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/other_switch_sweep"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:textSize="16sp"
android:text="@string/other_switch_sweep"
android:textColor="@color/themeLight"
app:layout_constraintEnd_toEndOf="@+id/other_switch_utc"
app:layout_constraintStart_toStartOf="@+id/other_switch_utc"
app:layout_constraintTop_toBottomOf="@+id/other_switch_utc" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/other_switch_sensors"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:minHeight="48dp"
android:textSize="16sp"
android:text="@string/other_switch_sensors"
android:textColor="@color/themeLight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/other_switch_utc"
app:layout_constraintStart_toStartOf="@+id/other_switch_sweep"
@@ -6,28 +6,25 @@
<TextView
android:id="@+id/tracking_title"
style="@style/SettingsCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="@string/tracking_title"
android:textColor="@color/themeAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/tracking_switch"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="4dp"
android:minHeight="48dp"
android:textSize="16sp"
android:text="@string/tracking_switch"
android:textColor="@color/themeLight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toStartOf="@+id/tracking_title"
app:layout_constraintTop_toBottomOf="@+id/tracking_title" />
<com.google.android.material.textfield.TextInputLayout
+25 -15
View File
@@ -6,44 +6,54 @@
<TextView
android:id="@+id/update_title"
style="@style/SettingsCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="@string/update_title"
android:textColor="@color/themeAccent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/update_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="6dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/update_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/update_title"
app:layout_constraintTop_toTopOf="@+id/update_title" />
<Button
android:id="@+id/update_btn_web"
style="@style/AppButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:drawableStart="@drawable/ic_update_web"
android:text="@string/update_web"
app:layout_constraintBaseline_toBaselineOf="@+id/update_btn_file"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/update_btn_file"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="@+id/update_btn_file" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/update_title" />
<Button
android:id="@+id/update_btn_file"
style="@style/AppButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="4dp"
android:drawableStart="@drawable/ic_update_file"
android:text="@string/update_file"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/update_btn_web"
app:layout_constraintBaseline_toBaselineOf="@+id/update_btn_web"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/update_title" />
app:layout_constraintStart_toEndOf="@+id/update_btn_web" />
</androidx.constraintlayout.widget.ConstraintLayout>
@@ -6,39 +6,35 @@
<TextView
android:id="@+id/warranty_title"
style="@style/SettingsCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/warranty_title"
android:textColor="@color/themeAccent"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/warranty_thanks"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:text="@string/warranty_thanks"
android:textColor="@color/themeLight"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toStartOf="@+id/warranty_title"
app:layout_constraintTop_toBottomOf="@+id/warranty_title" />
<TextView
android:id="@+id/warranty_license"
style="@style/SettingsText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:text="@string/warranty_license"
android:textColor="@color/themeLight"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/warranty_thanks"
app:layout_constraintStart_toStartOf="@+id/warranty_thanks"
+15 -15
View File
@@ -49,25 +49,12 @@
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_prefs_data"
style="@style/SettingsCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_prefs_info">
<include
android:id="@+id/prefs_data"
layout="@layout/card_prefs_update" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_prefs_location"
style="@style/SettingsCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_prefs_data">
app:layout_constraintTop_toBottomOf="@+id/card_prefs_info">
<include
android:id="@+id/prefs_location"
@@ -76,12 +63,25 @@
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_prefs_tracking"
android:id="@+id/card_prefs_data"
style="@style/SettingsCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_prefs_location">
<include
android:id="@+id/prefs_data"
layout="@layout/card_prefs_update" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_prefs_tracking"
style="@style/SettingsCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_prefs_data">
<include
android:id="@+id/prefs_tracking"
layout="@layout/card_prefs_tracking" />
+2
View File
@@ -75,6 +75,8 @@
<string name="about_support">Поддержать</string>
<string name="location_title">Местоположение наземной станции</string>
<string name="location_lat">Широта: %.4f°</string>
<string name="location_lon">Долгота: %.4f°</string>
<string name="location_gps">Данные GPS</string>
<string name="location_gps_null">Нет данных геолокации</string>
<string name="location_gps_error">Нет разрешения использовать геоданные</string>
+2
View File
@@ -94,6 +94,8 @@
<string name="about_fdroid" translatable="false">F-Droid</string>
<string name="location_title">Station position update</string>
<string name="location_lat">Latitude: %.4f°</string>
<string name="location_lon">Longitude: %.4f°</string>
<string name="location_gps">From GPS</string>
<string name="location_gps_null">No geolocation data</string>
<string name="location_gps_error">Check your location permissions</string>
+11
View File
@@ -86,4 +86,15 @@
<item name="cardCornerRadius">8dp</item>
</style>
<style name="SettingsText">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/themeLight</item>
</style>
<style name="SettingsCategory">
<item name="android:includeFontPadding">false</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/themeAccent</item>
</style>
</resources>