diff --git a/app/src/main/java/com/example/krishi/home/Crop.java b/app/src/main/java/com/example/krishi/home/Crop.java index 678b7a3..b1768a4 100644 --- a/app/src/main/java/com/example/krishi/home/Crop.java +++ b/app/src/main/java/com/example/krishi/home/Crop.java @@ -1,18 +1,42 @@ package com.example.krishi.home; +import android.app.ProgressDialog; import android.os.Bundle; - -import androidx.fragment.app.Fragment; - import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; +import androidx.fragment.app.Fragment; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; import com.example.krishi.R; +import java.util.HashMap; +import java.util.Map; + public class Crop extends Fragment { + String temp,humidity,rainfall; + + EditText N,P,K,Ph; + + Button submit; + + TextView result; + + RequestQueue requestQueue; + + String api_url = "https://krishiml.azurewebsites.net//predict"; + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -23,6 +47,71 @@ public void onCreate(Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_crop, container, false); + View view = inflater.inflate(R.layout.fragment_crop, container, false); + + N = view.findViewById(R.id.nValue); + P = view.findViewById(R.id.pValue); + K = view.findViewById(R.id.kValue); + Ph = view.findViewById(R.id.phValue); + submit = view.findViewById(R.id.button); + result = view.findViewById(R.id.crop); + + temp = Weather.getTemperatureData(); + humidity = Weather.getHumidityData(); + rainfall = Weather.getRainfallData(); + + if(temp==null || humidity == null || rainfall == null){ + String s = "Open weather page"; + result.setText(s); + Toast.makeText(requireContext(), "Pls open weather page", Toast.LENGTH_SHORT).show(); + } + + requestQueue = Volley.newRequestQueue(requireContext()); + + + submit.setOnClickListener(v -> { + if(N.getText().toString().isEmpty() || P.getText().toString().isEmpty() || + K.getText().toString().isEmpty() || Ph.getText().toString().isEmpty()){ + Toast.makeText(requireContext(), "Please enter all the values", Toast.LENGTH_SHORT).show(); + } + else{ + predictCrop(temp,humidity,rainfall, N.getText().toString(),P.getText().toString(), + K.getText().toString(),Ph.getText().toString()); + } + }); + return view; + } + + private void predictCrop( + String temp, String humidity, String rainfall, String n, String p, String k, String ph + ) { + ProgressDialog pDialog = new ProgressDialog(requireContext()); + pDialog.setMessage("Loading...PLease wait"); + pDialog.show(); + + StringRequest stringRequest = new StringRequest(Request.Method.POST, api_url, + response -> { + pDialog.dismiss(); + result.setText(response.substring(8,response.length()-2)); + }, + error -> { + pDialog.dismiss(); + Toast.makeText(requireContext(), "Error", Toast.LENGTH_SHORT).show(); + }){ + @Override + protected Map getParams() { + Map params = new HashMap<>(); + params.put("N", n); + params.put("P", p); + params.put("K", k); + params.put("temperature", temp); + params.put("humidity", humidity); + params.put("ph", ph); + params.put("rainfall", rainfall); + return params; + } + }; + + requestQueue.add(stringRequest); } } \ No newline at end of file diff --git a/app/src/main/java/com/example/krishi/home/Weather.java b/app/src/main/java/com/example/krishi/home/Weather.java index eefc03a..0f48d99 100644 --- a/app/src/main/java/com/example/krishi/home/Weather.java +++ b/app/src/main/java/com/example/krishi/home/Weather.java @@ -1,7 +1,6 @@ package com.example.krishi.home; import android.Manifest; -import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; @@ -15,29 +14,19 @@ import android.widget.TextView; import android.widget.Toast; -import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.fragment.app.Fragment; import com.android.volley.Request; import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.example.krishi.R; import com.example.krishi.data.responses.WeatherResponse; -import com.google.android.gms.location.FusedLocationProviderClient; -import com.google.android.gms.location.LocationServices; import org.json.JSONException; import org.json.JSONObject; -import java.util.Objects; - -import pub.devrel.easypermissions.AfterPermissionGranted; -import pub.devrel.easypermissions.EasyPermissions; - public class Weather extends Fragment { @@ -59,6 +48,10 @@ public class Weather extends Fragment { Location locationByGPS, locationByNetwork; + static String temperatureData; + static String humidityData; + static String rainfallData; + @Override public void onCreate(Bundle savedInstanceState) { @@ -97,53 +90,45 @@ private void getTemp() { api_url = api_url + lat + "," + lon; // System.out.println(api_url); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( - Request.Method.GET, api_url, null, new Response.Listener() { - @Override - public void onResponse(JSONObject response) { - try { -// System.out.println("recieved response"); - JSONObject location = response.getJSONObject("location"); - JSONObject current = response.getJSONObject("current"); + Request.Method.GET, api_url, null, response -> { + try { + // System.out.println("recieved response"); + JSONObject location = response.getJSONObject("location"); + JSONObject current = response.getJSONObject("current"); - String cityName = location.getString("name"); + String cityName = location.getString("name"); - double temp1 = current.getDouble("temp_c"); - int temp2 = (int) Math.round(temp1); - String temperature = String.valueOf(temp2); + double temp1 = current.getDouble("temp_c"); + int temp2 = (int) Math.round(temp1); + String temperature = String.valueOf(temp2); - double feels_like1 = current.getDouble("feelslike_c"); - int feels_like2 = (int) Math.round(feels_like1); - String feelsLike = String.valueOf(feels_like2); + double feels_like1 = current.getDouble("feelslike_c"); + int feels_like2 = (int) Math.round(feels_like1); + String feelsLike = String.valueOf(feels_like2); - int humidity1 = current.getInt("humidity"); - String humidity2 = String.valueOf(humidity1); + int humidity1 = current.getInt("humidity"); + String humidity2 = String.valueOf(humidity1); - JSONObject condition = current.getJSONObject("condition"); - String condition1 = condition.getString("text"); + JSONObject condition = current.getJSONObject("condition"); + String condition1 = condition.getString("text"); - double rain1 = current.getDouble("precip_mm"); - int rain2 = (int) Math.round(rain1); - String rainfall1 = String.valueOf(rain2); + double rain1 = current.getDouble("precip_mm"); + int rain2 = (int) Math.round(rain1); + String rainfall1 = String.valueOf(rain2); - int id = condition.getInt("code"); + int id = condition.getInt("code"); - WeatherResponse weatherResponse = new WeatherResponse(cityName, temperature, humidity2, - feelsLike, condition1, rainfall1, id); + WeatherResponse weatherResponse = new WeatherResponse(cityName, temperature, humidity2, + feelsLike, condition1, rainfall1, id); - setParameters(weatherResponse); - api_url = copy; + setParameters(weatherResponse); + api_url = copy; - } catch (JSONException e) { - throw new RuntimeException(e); - } + } catch (JSONException e) { + throw new RuntimeException(e); + } - } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show(); - } - }); + }, error -> Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show()); queue.add(jsonObjectRequest); } @@ -156,6 +141,10 @@ private void setParameters(WeatherResponse weatherResponse) { feels_like.setText(weatherResponse.getFeels_like()); condition.setText(weatherResponse.getCondition()); + temperatureData = weatherResponse.getTemp(); + humidityData = weatherResponse.getHumidity(); + rainfallData = weatherResponse.getRainfall(); + int id = weatherResponse.getId(); if (id == 1273 || id == 1276 || id == 1279 || id == 1282 || id == 1087) { icon.setImageResource(R.drawable.thunderstorm); @@ -174,6 +163,17 @@ private void setParameters(WeatherResponse weatherResponse) { icon.setImageResource(R.drawable.clouds); } } + public static String getTemperatureData() { + return temperatureData; + } + + public static String getHumidityData() { + return humidityData; + } + + public static String getRainfallData() { + return rainfallData; + } public void getLastLocation() { diff --git a/app/src/main/res/layout/fragment_crop.xml b/app/src/main/res/layout/fragment_crop.xml index 03a4b85..c6f5007 100644 --- a/app/src/main/res/layout/fragment_crop.xml +++ b/app/src/main/res/layout/fragment_crop.xml @@ -25,7 +25,7 @@ android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" - app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName" + app:layout_constraintBottom_toTopOf="@+id/nValue" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.22" app:layout_constraintStart_toStartOf="parent" @@ -33,12 +33,13 @@ app:layout_constraintVertical_bias="1.0" /> + app:layout_constraintStart_toEndOf="@+id/nValue" /> + app:layout_constraintStart_toEndOf="@+id/kValue" /> @@ -187,6 +191,7 @@ android:text="Crop" android:textColor="@color/black" android:textSize="40sp" + android:textAlignment="center" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" diff --git a/app/src/main/res/layout/fragment_person.xml b/app/src/main/res/layout/fragment_person.xml index 1c5f175..7cf81f0 100644 --- a/app/src/main/res/layout/fragment_person.xml +++ b/app/src/main/res/layout/fragment_person.xml @@ -30,6 +30,8 @@ android:src="@drawable/baseline_person_24" app:layout_constraintBottom_toTopOf="@+id/btn_logout" app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.473" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="1.0" /> \ No newline at end of file