Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes/navigation #83

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class AddNewFarmFieldFragment : Fragment() {

private fun setUpBinding() {

binding.toolbar2.setNavigationOnClickListener {
findNavController().navigateUp()
}

binding.switchAllowMapOutFarmBoundaries.setOnCheckedChangeListener { _, isChecked ->
saveSwitchState(isChecked)
handleSwitchState(isChecked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class ManualWalkingFarmMappingFragment : Fragment() ,OnMapReadyCallback{
buttonStopMapping.visibility = View.GONE
buttonRedoMapping.visibility = View.VISIBLE
buttonSaveMappedArea.visibility = View.VISIBLE

// Draw the polygon to show the area the farmer has walked around
createPolygon()
}

buttonRedoMapping.setOnClickListener {
Expand All @@ -94,8 +97,12 @@ class ManualWalkingFarmMappingFragment : Fragment() ,OnMapReadyCallback{
}

buttonSaveMappedArea.setOnClickListener {
saveMapppedArea()

// create polygon and calculate the area in the ploygon
createPolygon()

// then navigate
saveMapppedArea()
}
}
}
Expand Down Expand Up @@ -148,6 +155,21 @@ class ManualWalkingFarmMappingFragment : Fragment() ,OnMapReadyCallback{

locationProvider = LocationProvider(this.requireContext())

// Zoom to current location
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
location?.let {
val currentLatLng = LatLng(location.latitude, location.longitude)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 18f)) // Zoom to current location
}
}

// Initialize PolylineOptions to track the walking path
val polylineOptions = PolylineOptions()
.color(0xFF0000FF.toInt()) // Blue color for the path
.width(5f)



if (isMappingActive) {

if (ActivityCompat.checkSelfPermission(
Expand All @@ -168,14 +190,18 @@ class ManualWalkingFarmMappingFragment : Fragment() ,OnMapReadyCallback{
}


// GPS to record user movement , track and provide location updates
// GPS to record user movement , track and provide location updates.
// Start plotting immediately.
fusedLocationClient.requestLocationUpdates(locationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
val latLng = LatLng(location.latitude, location.longitude)
pathPoints.add(latLng)
googleMap.addPolyline(PolylineOptions().addAll(pathPoints))

polylineOptions.add(latLng)
googleMap.clear()
googleMap.addPolyline(polylineOptions)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18f))
}
}
Expand Down Expand Up @@ -228,6 +254,7 @@ class ManualWalkingFarmMappingFragment : Fragment() ,OnMapReadyCallback{
binding.buttonSaveMappedArea.text = "Save Mapped Area: %.2f ha".format(areaInHectares)

} else {
binding.buttonSaveMappedArea.text = "Mapped Area: 0.0 ha"
// Show a message if the user hasn't marked enough points to create a polygon
AlertDialog.Builder(requireContext())
.setTitle("Insufficient Path Points")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import timber.log.Timber
* Map out farm boundaries
* There is automated mapping by placing pins on the map
* and manual mapping by walking on the farm for it to detect farm boundaries
*
* App tracks the user's movement and places a path of pins automatically as they walk around their farm,
* forming a boundary once the mapping is stopped.
* This is achieved by continuously recording GPS points (LatLng) while the user is walking.
*/
@AndroidEntryPoint
class MappingFarmLocationWithPinsFragment : Fragment() ,OnMapReadyCallback {
Expand Down Expand Up @@ -66,11 +70,6 @@ class MappingFarmLocationWithPinsFragment : Fragment() ,OnMapReadyCallback {

}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
// outState.putString()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down Expand Up @@ -126,6 +125,7 @@ class MappingFarmLocationWithPinsFragment : Fragment() ,OnMapReadyCallback {

// Continue to AddNewFarmFieldFragment
findNavController().navigate(R.id.addNewFarmFieldFragment, bundle)
findNavController().popBackStack()

}

Expand Down Expand Up @@ -208,7 +208,7 @@ class MappingFarmLocationWithPinsFragment : Fragment() ,OnMapReadyCallback {
private fun updateFarmPolygon() {
farmPolygon?.remove()

if (boundaryPoints.size > 2) {
if (boundaryPoints.size >= 3) {
val polygonOptions = PolygonOptions()
.addAll(boundaryPoints)
.strokeWidth(2f)
Expand All @@ -224,6 +224,13 @@ class MappingFarmLocationWithPinsFragment : Fragment() ,OnMapReadyCallback {
// Display the area
btnSaveMappedArea.text = "Save Mapped Area: %.2f ha".format(areaInHectares)
Timber.tag("MapFarmWithPins").e("Save Mapped Area: %.2f ha".format(areaInHectares))
} else {
AlertDialog.Builder(requireContext())
.setTitle("Insufficient Path Points")
.setMessage("Please mark at least three points to create a farm boundary.")
.setPositiveButton("OK", null)
.show()
btnSaveMappedArea.text = "Mapped Area: 0.0 ha"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class CropCycleFinancialRecordsAnalyticsFragment : Fragment() {
if (cycles.isNotEmpty()) {

val allCropCycles = cycles.map {
it.cropName
"${it.cropName} - ${it.farmName} - ${it.startDate}"
}

val adapter = ArrayAdapter<String>([email protected](),
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_add_new_farm_field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
android:layout_marginStart="@dimen/_16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="@dimen/_16dp"
android:entries="@array/farm_ownership_type"
android:entries="@array/counties"
android:background="@drawable/rounded_corner"
android:fontFamily="@font/montserrat_medium"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@
<item>Rented</item>
</string-array>

<string-array name="counties">
<item>Mombasa</item>
<item>Nairobi</item>
<item>Kisii</item>
<item>Kiambu</item>
<item>Machakos</item>
<item>Makueni</item>
<item>Bomet</item>
<item>Kitui</item>
<item>Nyeri</item>
<item>Nakuru</item>
<item>Nyandarua</item>
<item>Bungoma</item>
<item>Uasin Gishu</item>
</string-array>

<string-array name="sample_crop_cycle_tasks">
<item>Seedling Acquisition</item>
<item>Potting</item>
Expand Down
Loading