Skip to content

Commit

Permalink
Restored manual location input functionality #40
Browse files Browse the repository at this point in the history
  • Loading branch information
rt-bishop committed Feb 19, 2022
1 parent 9d6dbc8 commit ffd1fc1
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ class LocationHandler @Inject constructor(

override fun setStationPosition(latitude: Double, longitude: Double) {
if (QthConverter.isValidPosition(latitude, longitude)) {
val tempLon = if (longitude > 180.0) longitude - 180 else longitude
val newLat = latitude.round(4)
val newLon = longitude.round(4)
val newLon = tempLon.round(4)
currentPosition = GeoPos(newLat, newLon)
settingsHandler.saveStationPosition(newLat, newLon)
_stationPosition.value = DataState.Success(currentPosition)
} else _stationPosition.value =
DataState.Error(context.getString(R.string.pref_pos_gps_null))
DataState.Error(context.getString(R.string.pref_pos_manual_error))
}

override fun setPositionFromGps() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.rtbishop.look4sat.presentation.settingsScreen

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialogFragment
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.databinding.DialogLocationBinding
import com.rtbishop.look4sat.domain.ILocationHandler
import com.rtbishop.look4sat.presentation.setNavResult
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class LocationDialog : AppCompatDialogFragment() {

@Inject
lateinit var locationHandler: ILocationHandler

override fun onCreateView(inflater: LayoutInflater, group: ViewGroup?, state: Bundle?): View? {
return inflater.inflate(R.layout.dialog_location, group, false)
}

override fun onViewCreated(view: View, state: Bundle?) {
super.onViewCreated(view, state)
DialogLocationBinding.bind(view).run {
dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
dialog?.window?.setLayout(
(resources.displayMetrics.widthPixels * 0.94).toInt(),
WindowManager.LayoutParams.WRAP_CONTENT
)
val location = locationHandler.getStationPosition()
locationLatEdit.setText(location.latitude.toString())
locationLonEdit.setText(location.longitude.toString())
locationPosBtn.setOnClickListener {
val latitude = try {
locationLatEdit.text.toString().toDouble()
} catch (exception: Exception) {
180.0
}
val longitude = try {
locationLonEdit.text.toString().toDouble()
} catch (exception: Exception) {
400.0
}
setNavResult("location", Pair(latitude, longitude))
dismiss()
}
locationNegBtn.setOnClickListener { dismiss() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
binding.settingsLocation.locationBtnGps.setOnClickListener {
locationRequest.launch(arrayOf(locationFine, locationCoarse))
}
binding.settingsLocation.locationBtnManual.setOnClickListener {
val action = SettingsFragmentDirections.actionPrefsToLocation()
findNavController().navigate(action)
}
binding.settingsLocation.locationBtnQth.setOnClickListener {
val action = SettingsFragmentDirections.actionPrefsToLocator()
findNavController().navigate(action)
}
getNavResult<Pair<Double, Double>>(R.id.nav_settings, "location") { location ->
viewModel.setStationPosition(location.first, location.second)
}
getNavResult<String>(R.id.nav_settings, "locator") { locator ->
viewModel.setPositionFromQth(locator)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class SettingsViewModel @Inject constructor(

fun getStationPosition(): GeoPos = locationHandler.getStationPosition()

fun setStationPosition(lat: Double, lon: Double) = locationHandler.setStationPosition(lat, lon)

fun setPositionFromGps() = locationHandler.setPositionFromGps()

fun setPositionFromNet() = locationHandler.setPositionFromNet()
Expand Down
25 changes: 18 additions & 7 deletions app/src/main/res/layout/card_prefs_location.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,41 @@
style="@style/NormalButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:drawableStart="@drawable/ic_gps"
android:text="@string/location_gps"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/location_btn_qth"
app:layout_constraintEnd_toStartOf="@+id/location_btn_manual"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/location_lat" />

<Button
android:id="@+id/location_btn_manual"
style="@style/NormalButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_gps"
android:text="Manual"
app:layout_constraintBaseline_toBaselineOf="@+id/location_btn_gps"
app:layout_constraintEnd_toStartOf="@+id/location_btn_qth"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/location_btn_gps" />

<Button
android:id="@+id/location_btn_qth"
style="@style/NormalButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginEnd="8dp"
android:drawableStart="@drawable/ic_qth"
android:text="@string/location_qth"
app:layout_constraintBaseline_toBaselineOf="@+id/location_btn_gps"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/location_btn_gps" />
app:layout_constraintStart_toEndOf="@+id/location_btn_manual" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
102 changes: 102 additions & 0 deletions app/src/main/res/layout/dialog_location.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/SurfaceCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/location_title"
style="@style/DialogTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:minHeight="48dp"
android:text="@string/dialog_filter_passes"
app:layout_constraintBottom_toTopOf="@+id/location_lat_layout"
app:layout_constraintEnd_toEndOf="@+id/location_pos_btn"
app:layout_constraintStart_toStartOf="@+id/location_neg_btn"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/location_lat_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:hint="@string/pref_hours_ahead_hint"
android:importantForAutofill="no"
android:inputType="number"
android:minHeight="48dp"
app:endIconMode="clear_text"
app:layout_constraintBottom_toTopOf="@+id/location_lon_layout"
app:layout_constraintEnd_toEndOf="@+id/location_title"
app:layout_constraintStart_toStartOf="@+id/location_title"
app:layout_constraintTop_toBottomOf="@+id/location_title">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/location_lat_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textUri" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/location_lon_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/pref_min_el_hint"
android:importantForAutofill="no"
android:inputType="number|numberDecimal"
android:minHeight="48dp"
app:endIconMode="clear_text"
app:layout_constraintBottom_toTopOf="@+id/location_neg_btn"
app:layout_constraintEnd_toEndOf="@+id/location_lat_layout"
app:layout_constraintStart_toStartOf="@+id/location_lat_layout"
app:layout_constraintTop_toBottomOf="@+id/location_lat_layout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/location_lon_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textUri" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/location_neg_btn"
style="@style/NormalButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="8dp"
android:text="@string/btn_cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/location_pos_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/location_lon_layout" />

<Button
android:id="@+id/location_pos_btn"
style="@style/NormalButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/btn_accept"
app:layout_constraintBaseline_toBaselineOf="@+id/location_neg_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/location_neg_btn" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
7 changes: 7 additions & 0 deletions app/src/main/res/navigation/navigation_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<action
android:id="@+id/action_prefs_to_sources"
app:destination="@id/nav_sources" />
<action
android:id="@+id/action_prefs_to_location"
app:destination="@id/nav_location" />
<action
android:id="@+id/action_prefs_to_locator"
app:destination="@id/nav_locator" />
Expand All @@ -65,6 +68,10 @@
android:id="@+id/nav_modes"
android:name="com.rtbishop.look4sat.presentation.entriesScreen.ModesDialog"
tools:layout="@layout/dialog_modes" />
<dialog
android:id="@+id/nav_location"
android:name="com.rtbishop.look4sat.presentation.settingsScreen.LocationDialog"
tools:layout="@layout/dialog_location" />
<dialog
android:id="@+id/nav_locator"
android:name="com.rtbishop.look4sat.presentation.settingsScreen.LocatorDialog"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<string name="pref_gsp_title">Местоположение наземной станции</string>
<string name="pref_pos_gps">Установить позицию по данным GPS</string>
<string name="pref_pos_gps_null">Нет данных геолокации</string>
<string name="pref_pos_manual_error">Введены неверные данные</string>
<string name="pref_pos_gps_error">Нет разрешения использовать геоданные</string>
<string name="pref_pos_qth">Установить позицию по локатору QTH</string>
<string name="pref_pos_qth_error">Неправильный локатор QTH</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="pref_gsp_title">Ground station position</string>
<string name="pref_pos_gps">Set position from GPS data</string>
<string name="pref_pos_gps_null">No geolocation data</string>
<string name="pref_pos_manual_error">Invalid location entered</string>
<string name="pref_pos_gps_error">Check your location permissions</string>
<string name="pref_pos_qth">Set position from QTH locator</string>
<string name="pref_pos_qth_error">Invalid QTH locator</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object QthConverter {

fun positionToQth(lat: Double, lon: Double): String? {
if (!isValidPosition(lat, lon)) return null
val tempLon = if (lon > 180.0) lon - 360 else lon
val tempLon = if (lon > 180.0) lon - 180 else lon
val upper = "ABCDEFGHIJKLMNOPQRSTUVWX"
val lower = "abcdefghijklmnopqrstuvwx"
val longitude = tempLon + 180
Expand Down

0 comments on commit ffd1fc1

Please sign in to comment.