Skip to content

Commit

Permalink
Updates: concepts actualization
Browse files Browse the repository at this point in the history
  • Loading branch information
MuindiStephen committed Aug 22, 2024
1 parent 63a4f1a commit 1e25bd3
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 74 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ dependencies {

// Sweet Alert Dialog
implementation 'cn.pedant.sweetalert:library:1.3'

// MPAndroid chart lib
implementation("ir.mahozad.android:pie-chart:0.7.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ package com.steve_md.smartmkulima.model
data class FarmConditions(
val temperature: Double,
val humidity: Double,
val soilMoisture: Double
val soilMoisture: Double,
val windspeed: Double,
val precipitation: Double,
val lightDensity: Double,
val nbkLevel: Double
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import com.steve_md.smartmkulima.model.mapmodels.responses.toPlace
import java.io.InputStream
import java.io.InputStreamReader

/**
* Place Name, Latitude, Longitude, Address, Rating :(
*/
data class Place(
val name: String,
val latLng: LatLng,
Expand All @@ -28,7 +31,6 @@ data class Place(
override fun getSnippet(): String {
return address
}

}

class PlacesReader(private val context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ class LocateAgriTechCompaniesFragment : Fragment() , OnMapReadyCallback {
override fun onResume() {
super.onResume()
mapView.onResume()
promptUserForLocationPermissions()
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.steve_md.smartmkulima.ui.fragments.main

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -26,14 +29,16 @@ import com.steve_md.smartmkulima.model.FarmConditions
import com.steve_md.smartmkulima.ui.fragments.others.LocationProvider
import com.steve_md.smartmkulima.utils.displaySnackBar
import dagger.hilt.android.AndroidEntryPoint
import ir.mahozad.android.PieChart
import ir.mahozad.android.unit.Dimension
import timber.log.Timber

@AndroidEntryPoint
class MonitorFarmConditionFragment : Fragment(),OnMapReadyCallback {

private lateinit var mapView: MapView
private lateinit var googleMap: GoogleMap
private var LOCATION_PERMISSION_REQUEST_CODE = 1
private var LOCATIONPERMISSIONREQUESTCODE = 1
private lateinit var locationProvider: LocationProvider

override fun onCreateView(
Expand Down Expand Up @@ -77,27 +82,81 @@ class MonitorFarmConditionFragment : Fragment(),OnMapReadyCallback {
displaySnackBar("Could not update your location")
}
}

setUpChart()
}

private fun setUpChart() {
val pieChart = view?.findViewById<PieChart>(R.id.pieChart)

pieChart?.slices = listOf(
PieChart.Slice(0.3f, Color.rgb(120, 181, 0), Color.rgb(149, 224, 0), legend = "Temperature"),
PieChart.Slice(0.2f, Color.rgb(204, 168, 0), Color.rgb(249, 228, 0), legend = "Humidity"),
PieChart.Slice(0.2f, Color.rgb(0, 162, 216), Color.rgb(31, 199, 255), legend = "Soil Moisture"),
PieChart.Slice(0.17f, Color.rgb(255, 4, 4), Color.rgb(255, 72, 86), legend = "Wind speed"),
PieChart.Slice(0.13f, Color.rgb(160, 165, 170), Color.rgb(175, 180, 185), legend = "Precipitation") ,
PieChart.Slice(0.13f, Color.rgb(160, 165, 170), Color.rgb(175, 180, 185), legend = "Light Density"),
PieChart.Slice(0.13f, Color.rgb(160, 165, 170), Color.rgb(175, 180, 185), legend = "NBK level")
)

pieChart?.gradientType = PieChart.GradientType.RADIAL
pieChart?.legendIconsMargin = Dimension.DP(8F)
pieChart?.legendTitleMargin = Dimension.DP(14F)
pieChart?.legendLinesMargin = Dimension.DP(10F)
pieChart?.legendsMargin = Dimension.DP(20F)
pieChart?.legendsPercentageMargin = Dimension.DP(8F)
pieChart?.legendsSize = Dimension.DP(11F)
pieChart?.legendsPercentageSize = Dimension.DP(11F)
pieChart?.legendsIcon = PieChart.DefaultIcons.SQUARE
}

val Float.toSp get() = this * Resources.getSystem().displayMetrics.scaledDensity

fun dpToPx(dp: Int): Int {
return (dp * Resources.getSystem().displayMetrics.density).toInt()
}
// Mock farm conditions with Fake DATA
private fun monitorFarmConditions(latitude: Double, longitude: Double) {
val mockFarmConditions = FarmConditions(
temperature = 22.0,
humidity = 69.7,
soilMoisture = 0.6
temperature = 25.8,
humidity = 67.7,
soilMoisture = 32.7,
windspeed = 9.0,
precipitation = 5.8,
lightDensity = 45000.0,
nbkLevel = 30.5
)

/*
val temperature: Double,
val humidity: Double,
val soilMoisture: Double,
val windspeed: Double,
val precipitation: Double,
val lightDensity: Double,
val nbkLevel: Double
*/
// Process the farm conditions data as needed
displayFarmConditions(mockFarmConditions)
}

@SuppressLint("SetTextI18n")
private fun displayFarmConditions(mockFarmConditions: FarmConditions) {
val temperature = view?.findViewById<TextView>(R.id.textViewTemperature)
val humidity = view?.findViewById<TextView>(R.id.textViewHumidity)
val soilMoisture = view?.findViewById<TextView>(R.id.textViewSoilMoisture)

temperature?.text = mockFarmConditions.temperature.toString()
humidity?.text = mockFarmConditions.humidity.toString()
soilMoisture?.text = mockFarmConditions.soilMoisture.toString()
val windspeed = view?.findViewById<TextView>(R.id.textViewWindSpeed)
val precipitation = view?.findViewById<TextView>(R.id.textViewPrecipitation)
val lightDensity = view?.findViewById<TextView>(R.id.textViewLightDensity)
val nbkLevel = view?.findViewById<TextView>(R.id.textViewNBKLevel)

temperature?.text = "${mockFarmConditions.temperature}&#176;C"
humidity?.text = "${mockFarmConditions.humidity}%"
soilMoisture?.text = "${mockFarmConditions.soilMoisture}%"
windspeed?.text = "${mockFarmConditions.windspeed} m/s"
precipitation?.text = "${mockFarmConditions.precipitation} mm"
lightDensity?.text = "${mockFarmConditions.windspeed} lux"
nbkLevel?.text = "${mockFarmConditions.nbkLevel} %"
}

override fun onMapReady(p0: GoogleMap) {
Expand All @@ -113,7 +172,10 @@ class MonitorFarmConditionFragment : Fragment(),OnMapReadyCallback {
} else {
requestLocationPermission()
}
val location = LatLng(0.5699258, 34.5566803)

// Sample farm Location // Not Real Data
val location = LatLng(-1.2860732, 36.8103714)

googleMap.addMarker(MarkerOptions().position(location).title("Your Farm is Here"))
?.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 12f))
Expand All @@ -123,13 +185,13 @@ class MonitorFarmConditionFragment : Fragment(),OnMapReadyCallback {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE
LOCATIONPERMISSIONREQUESTCODE
)
}

/**
* Handle lifecycle for the Map
* This is implemented in order for it ot work well
* This is implemented in order for it to work pretty well
*/
override fun onPause() {
super.onPause()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,35 @@ class PredictYourCropProductionFragment : Fragment() {
}

binding.buttonPredict.setOnClickListener {
val temperature = binding.inputTemperature.text.toString().toDouble()
val rainfall = binding.inputRainfall.text.toString().toDouble()
val temperature = binding.inputTemperature.text.toString().toDouble().toString()
val rainfall = binding.inputRainfall.text.toString().toDouble().toString()
val soilQualityPh = binding.inputSoilQualityPh.text.toString()
val selectedCrop = binding.spinnerCropPredict.selectedItem.toString()
val selectedSeason = binding.spinnerSowingSeason.selectedItem.toString()


if (temperature.isNotEmpty() && rainfall.isNotEmpty() && soilQualityPh.isNotEmpty()) {

} else {

}
// call prediction function here :)
if (temperature != null && rainfall != null &&
soilQualityPh != null && selectedCrop!= null
&& selectedSeason!=null){
if (temperature != null && rainfall != null && selectedCrop!= null && selectedSeason!=null){
val prediction = predictFarmProduce(
temperature,
rainfall,
temperature.toDouble(),
rainfall.toDouble(),
soilQualityPh,
selectedSeason,
selectedCrop
)
binding.textViewResult.text = "Expected production: $prediction"

Timber.tag(this.tag.toString()).v("Prediction: $prediction")
}
else {
binding.enterTemperature.error = "Empty strings"
binding.enterRainfall.error = "Empty strings"
binding.enterSoilQualityPh.error = "Empty strings"
Timber.tag(this.tag.toString()).v("Em")
}
}
}
Expand Down
Loading

0 comments on commit 1e25bd3

Please sign in to comment.