Skip to content

Commit

Permalink
Final Commit before Beta-release
Browse files Browse the repository at this point in the history
  • Loading branch information
AmartyaSingh97 committed Mar 8, 2023
1 parent 11495c0 commit 4fd7985
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 68 deletions.
97 changes: 93 additions & 4 deletions app/src/main/java/com/example/krishi/home/Crop.java
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<String, String> getParams() {
Map<String, String> 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);
}
}
94 changes: 47 additions & 47 deletions app/src/main/java/com/example/krishi/home/Weather.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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) {
Expand Down Expand Up @@ -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<JSONObject>() {
@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);
}

Expand All @@ -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);
Expand All @@ -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() {

Expand Down
37 changes: 21 additions & 16 deletions app/src/main/res/layout/fragment_crop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@
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"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

<EditText
android:id="@+id/editTextTextPersonName"
android:id="@+id/nValue"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginBottom="48dp"
android:ems="10"
android:inputType="number"
android:inputType="textPersonName"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="@+id/textView13"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
Expand All @@ -52,24 +53,25 @@
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/editTextNumber"
app:layout_constraintBottom_toTopOf="@+id/pValue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.585"
app:layout_constraintStart_toEndOf="@+id/textView10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

<EditText
android:id="@+id/editTextNumber"
android:id="@+id/pValue"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginBottom="72dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/editTextNumber6"
android:textAlignment="center"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/phValue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.572"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName" />
app:layout_constraintStart_toEndOf="@+id/nValue" />

<TextView
android:id="@+id/textView13"
Expand All @@ -79,18 +81,19 @@
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/editTextNumber4"
app:layout_constraintBottom_toTopOf="@+id/kValue"
app:layout_constraintEnd_toStartOf="@+id/textView14"
app:layout_constraintHorizontal_bias="0.305"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/editTextNumber4"
android:id="@+id/kValue"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginBottom="68dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView14"
app:layout_constraintHorizontal_bias="0.246"
Expand All @@ -104,24 +107,25 @@
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/editTextNumber6"
app:layout_constraintBottom_toTopOf="@+id/phValue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.712"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextNumber"
app:layout_constraintTop_toBottomOf="@+id/pValue"
app:layout_constraintVertical_bias="1.0" />

<EditText
android:id="@+id/editTextNumber6"
android:id="@+id/phValue"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginBottom="72dp"
android:ems="10"
android:inputType="number"
android:inputType="textPersonName"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.585"
app:layout_constraintStart_toEndOf="@+id/editTextNumber4" />
app:layout_constraintStart_toEndOf="@+id/kValue" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 4fd7985

Please sign in to comment.