Skip to content

Commit

Permalink
Add spinner for geonav popup
Browse files Browse the repository at this point in the history
* Added spinner in dialog_geonav_collect.xml for picking the popup type
* Removed taking data of firstInfoBar
  • Loading branch information
kamathprasad9 committed Sep 12, 2023
1 parent 1db8082 commit 1182ac8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void handleMessage(Message msg) {
secureBluetooth.initialize();

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
geoNavHelper = new GeoNavHelper(this, infoBarHelper);
geoNavHelper = new GeoNavHelper(this);
ep = getSharedPreferences(GeneralKeys.SHARED_PREF_FILE_NAME, 0);

ttsHelper = new TextToSpeechHelper(this, () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fieldbook.tracker.dialogs

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.Spinner
import androidx.appcompat.app.AlertDialog
Expand All @@ -13,6 +15,8 @@ import com.fieldbook.tracker.utilities.Utils
class GeoNavCollectDialog(private val activity: CollectActivity) :
AlertDialog.Builder(activity, R.style.AppAlertDialog) {

private val TAG: String = "GeoNavCollectDialog"

private val prefs by lazy {
context.getSharedPreferences(GeneralKeys.SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE)
}
Expand All @@ -35,9 +39,16 @@ class GeoNavCollectDialog(private val activity: CollectActivity) :
prefs.edit().putString(GeneralKeys.GEONAV_CONFIG_DEGREE_PRECISION, value).apply()
}

private var geoNavPopupDisplay
get() = prefs.getString(GeneralKeys.GEONAV_POPUP_DISPLAY, "plot_id")
set(value) {
prefs.edit().putString(GeneralKeys.GEONAV_POPUP_DISPLAY, value).apply()
}

private var autoNavigateCb: CheckBox? = null
private var audioOnDropCb: CheckBox? = null
private var degreeOfPrecisionSp: Spinner? = null
private var geoNavPopupDisplaySp: Spinner? = null

private val view by lazy {
LayoutInflater.from(context).inflate(R.layout.dialog_geonav_collect, null, false)
Expand All @@ -54,6 +65,15 @@ class GeoNavCollectDialog(private val activity: CollectActivity) :
autoNavigateCb = view.findViewById(R.id.dialog_geonav_collect_auto_navigate)
audioOnDropCb = view.findViewById(R.id.dialog_geonav_collect_notify_on_precision_loss)
degreeOfPrecisionSp = view.findViewById(R.id.dialog_geonav_collect_precision_threshold)
geoNavPopupDisplaySp = view.findViewById(R.id.dialog_geonav_popup_display)

// fetching spinner items
val geoNavPopupDisplayAdapter = ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item)
geoNavPopupDisplayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
geoNavPopupDisplayAdapter.addAll(getGeoNavPopupSpinnerItems().toList())

// Set the ArrayAdapter to the Spinner
geoNavPopupDisplaySp?.adapter = geoNavPopupDisplayAdapter

loadPreferencesIntoUi()

Expand Down Expand Up @@ -89,11 +109,34 @@ class GeoNavCollectDialog(private val activity: CollectActivity) :
else -> 0
}
)

// set text for geoNavPopupDisplaySp based on preferences
val popupItem = getGeoNavPopupSpinnerItems()
val index = popupItem.indexOf(geoNavPopupDisplay)
val selection = if( index != -1 ) index else 0
geoNavPopupDisplaySp?.setSelection(selection)
}

private fun getGeoNavPopupSpinnerItems(): Array<String> {
//query database for attributes/traits to use
try {
val attributes = activity.getDatabase().getAllObservationUnitAttributeNames(activity.studyId.toInt())
var traits = activity.getDatabase().allTraitObjects.toTypedArray()
val other = traits.filter { !it.visible }.toTypedArray()
traits = traits.filter { it.visible }.toTypedArray()

return attributes + traits.map { it.trait } + other.map { it.trait }
} catch (e: Exception) {
Log.d(TAG, "Error occurred when querying for attributes in GeoNavCollectDialog.")
e.printStackTrace()
}
return arrayOf()
}

private fun saveUiToPreferences() {
auto = autoNavigateCb?.isChecked ?: false
audioOnDrop = audioOnDropCb?.isChecked ?: false
degreeOfPrecision = degreeOfPrecisionSp?.selectedItem.toString()
geoNavPopupDisplay = geoNavPopupDisplaySp?.selectedItem.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class GeneralKeys {
public static final String GEONAV_CONFIG_AUDIO_ON_DROP = GEONAV_PREFIX + "AUDIO_ON_DROP";

public static final String GEONAV_CONFIG_DEGREE_PRECISION = GEONAV_PREFIX + "DEGREE_PRECISION";

public static final String GEONAV_POPUP_DISPLAY = GEONAV_PREFIX + "POPUP_DISPLAY";
// @formatter:on

// GNSS
Expand Down
26 changes: 10 additions & 16 deletions app/src/main/java/com/fieldbook/tracker/utilities/GeoNavHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.location.Location
import android.os.Handler
import android.os.HandlerThread
import android.os.Message
import android.provider.ContactsContract.Data
import android.util.Log
import android.view.View
import android.widget.ImageButton
Expand All @@ -35,14 +34,11 @@ import com.fieldbook.tracker.location.GPSTracker
import com.fieldbook.tracker.location.gnss.ConnectThread
import com.fieldbook.tracker.location.gnss.GNSSResponseReceiver
import com.fieldbook.tracker.location.gnss.NmeaParser
import com.fieldbook.tracker.objects.InfoBarModel
import com.fieldbook.tracker.preferences.GeneralKeys
import com.fieldbook.tracker.utilities.GeodeticUtils.Companion.impactZoneSearch
import com.fieldbook.tracker.utilities.GeodeticUtils.Companion.lowPassFilter
import com.fieldbook.tracker.utilities.GeodeticUtils.Companion.truncateFixQuality
import com.fieldbook.tracker.utilities.GeodeticUtils.Companion.writeGeoNavLog
import com.fieldbook.tracker.utilities.InfoBarHelper
import com.fieldbook.tracker.utilities.Utils
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.SnackbarLayout
import org.phenoapps.utils.BaseDocumentTreeUtil.Companion.getDirectory
Expand All @@ -53,7 +49,7 @@ import javax.inject.Inject
import kotlin.math.pow
import kotlin.math.sqrt

class GeoNavHelper @Inject constructor(private val controller: CollectController, private val infoBarHelper: InfoBarHelper):
class GeoNavHelper @Inject constructor(private val controller: CollectController):
SensorEventListener, GPSTracker.GPSTrackerListener {

/**
Expand All @@ -68,6 +64,10 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController

private var currentFixQuality = false

private val prefs by lazy {
controller.getContext().getSharedPreferences(GeneralKeys.SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE)
}

private val mGnssResponseReceiver: GNSSResponseReceiver = object : GNSSResponseReceiver() {
override fun onGNSSParsed(parser: NmeaParser) {

Expand Down Expand Up @@ -467,14 +467,8 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
val tv =
snackView.findViewById<TextView>(R.id.geonav_snackbar_tv)

// get the first info-bar field displayed on the screen
val models: List<InfoBarModel>? =
infoBarHelper?.getInfoBarData()

val firstInfoBar = models?.get(0)?.prefix

// fallback for firstInfoBar will be plot_id
tv.text = firstInfoBar + ": " + getInfoBarData(id, "${firstInfoBar?: "plot_id"}")
var popupHeader = prefs.getString(GeneralKeys.GEONAV_POPUP_DISPLAY, "plot_id")
tv.text = getPopupInfo(id, "${popupHeader?: "plot_id"}")

// if (tv != null) {
// tv.text = id
Expand All @@ -500,7 +494,7 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
}
}

private fun getInfoBarData(id: String, firstInfoBar: String): String {
private fun getPopupInfo(id: String, popupHeader: String): String {

var database : DataHelper = controller.getDatabase()

Expand All @@ -510,9 +504,9 @@ class GeoNavHelper @Inject constructor(private val controller: CollectController
val attributes: List<String> = ArrayList(Arrays.asList(*database.rangeColumnNames))

//check if the label is an attribute or a trait
val isAttribute = attributes.contains(firstInfoBar)
val isAttribute = attributes.contains(popupHeader)

return controller.queryForLabelValue(id, firstInfoBar, isAttribute)
return controller.queryForLabelValue(id, popupHeader, isAttribute)
}

/**
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/dialog_geonav_collect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@
android:entries="@array/dialog_geonav_collect_degrees_of_precision"/>
</LinearLayout>

<LinearLayout
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_geonav_collect_geonav_popup_type" />

<Spinner
android:id="@+id/dialog_geonav_popup_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

</LinearLayout>
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 @@ -411,6 +411,7 @@
<string name="dialog_geonav_collect_auto_navigate">Auto Navigate</string>
<string name="dialog_geonav_collect_notify_on_precision_loss">Audio on precision drop</string>
<string name="dialog_geonav_collect_precision_threshold_title">Degree of precision</string>
<string name="dialog_geonav_collect_geonav_popup_type">GeoNav Popup Type</string>
<string name="dialog_geonav_collect_reconnect">Reconnect</string>
<string name="dialog_geonav_collect_reset_start_toast_message">Resetting connection…</string>
<string name="dialog_geonav_collect_reset_end_toast_message">Connection reset</string>
Expand Down

0 comments on commit 1182ac8

Please sign in to comment.