Browse Source

asdnöasdh

old
Efe Aydın 6 years ago
parent
commit
ec973b4a2b
2 changed files with 163 additions and 55 deletions
  1. +76
    -46
      MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java
  2. +87
    -9
      MyCity/app/src/main/res/layout/fragment_utility_water.xml

+ 76
- 46
MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java View File

@ -1,32 +1,33 @@
package gq.yigit.mycity.utility;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.helper.StaticLabelsFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import gq.yigit.mycity.MainActivity;
import gq.yigit.mycity.R;
import gq.yigit.mycity.tools.FileActions;
import gq.yigit.mycity.tools.WebRequest;
import org.json.JSONException;
import org.json.JSONObject;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link UtilityWater.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link UtilityWater#newInstance} factory method to
* create an instance of this fragment.
*/
public class UtilityWater extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
import java.util.HashMap;
public class UtilityWater extends Fragment implements WebRequest.responseListener {
private JSONObject waterUsage;
private GraphView graph;
private OnFragmentInteractionListener mListener;
@ -34,38 +35,46 @@ public class UtilityWater extends Fragment {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment UtilityWater.
*/
// TODO: Rename and change types and number of parameters
public static UtilityWater newInstance(String param1, String param2) {
UtilityWater fragment = new UtilityWater();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_utility_water, container, false);
View rootView = inflater.inflate(R.layout.fragment_utility_water, container, false);;
graph = (GraphView) rootView.findViewById(R.id.utility_graph);
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(new String[] {"0", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24"});
staticLabelsFormatter.setVerticalLabels(new String[] {"0", "5", "10","15","20"});
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
FileActions file_manager = new FileActions();
String url = file_manager.readFromFile(getContext(),"server.config").trim();
HashMap<String,String> request = new HashMap<>();
try {
request.put("user_id", MainActivity.userData.getString("id"));
}catch (JSONException e){
Log.e("[ERROR]","User data not correct");
}
request.put("type","water");
WebRequest login_manager = new WebRequest(url + "/resources/",false,request,0);
login_manager.addListener(this);
login_manager.execute();
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
@ -91,18 +100,39 @@ public class UtilityWater extends Fragment {
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void receivedResponse(boolean success, String result,int reqid){
if(success){
try{
waterUsage = new JSONObject(result);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {});
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(new DataPoint[] {});
for(int i = 0;i < waterUsage.getJSONArray("daily_water_usage").length();i++){
series.appendData(new DataPoint(i,waterUsage.getJSONArray("daily_water_usage").getInt(i)),
true,
100);
}
for(int i = 0;i < waterUsage.getJSONArray("ideal_water_usage").length();i++){
series2.appendData(new DataPoint(i,waterUsage.getJSONArray("ideal_water_usage").getInt(i)),
true,
100);
}
series.setTitle("ideal");
series.setColor(Color.BLUE);
series2.setTitle("usage");
series2.setColor(Color.RED);
graph.addSeries(series);
graph.addSeries(series2);
}catch (JSONException e){
Log.e("[ERROR]","Cannot interpret response from water service");
}
}
}
}

+ 87
- 9
MyCity/app/src/main/res/layout/fragment_utility_water.xml View File

@ -1,14 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".utility.UtilityWater">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".utility.UtilityElectricity"
android:layout_marginTop="10dp"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="254dp"
android:id="@+id/utility_graph"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
android:layout_height="match_parent">
<TextView
android:text="Your Bill:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView1"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:textStyle="bold|italic" android:layout_weight="1" android:textAlignment="textEnd"/>
<TextView
android:text="Content Here"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/bill_utility"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Your Points:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:textStyle="bold|italic" android:layout_weight="1" android:textAlignment="textEnd"/>
<TextView
android:text="Content Here"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/points_utility"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="%Efficiency"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView2"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:textStyle="bold|italic" android:layout_weight="1" android:textAlignment="textEnd"/>
<TextView
android:text="Content Here"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/efficiency_utility"
android:textColor="@android:color/black" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Pay Bill"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/pay_bill" android:layout_weight="1"/>
<Button
android:text="use points"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/use_point" android:layout_weight="1"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>

Loading…
Cancel
Save