Browse Source

Finished Utility Page with tabbing

Water tab is not finished because it is practically the same thing as electricity except the values. 
Also added a type parameter to the flask server.
-Ordular, ilk hedefiniz NAVİGASYON SAYFASIDIR
old
Yiğit Çolakoğlu 6 years ago
parent
commit
e8089f7c95
18 changed files with 403 additions and 161 deletions
  1. +1
    -0
      MyCity/app/build.gradle
  2. +1
    -11
      MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java
  3. +138
    -0
      MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityElectricity.java
  4. +2
    -6
      MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityMain.java
  5. +29
    -42
      MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java
  6. +0
    -7
      MyCity/app/src/main/res/layout/app_bar_main.xml
  7. +5
    -3
      MyCity/app/src/main/res/layout/fragment_utility.xml
  8. +39
    -27
      MyCity/app/src/main/res/layout/fragment_utility_electricity.xml
  9. +14
    -0
      MyCity/app/src/main/res/layout/fragment_utility_water.xml
  10. +2
    -1
      MyCity/app/src/main/res/layout/nav_header_main.xml
  11. +5
    -3
      server_side/api/app.py
  12. BIN
      server_side/api/modules/__pycache__/rating_system.cpython-37.pyc
  13. BIN
      server_side/api/modules/__pycache__/utility.cpython-37.pyc
  14. +4
    -34
      server_side/api/modules/databases/ratings.json
  15. +93
    -21
      server_side/api/modules/databases/users.json
  16. +4
    -2
      server_side/api/modules/databases/votings.json
  17. +31
    -4
      server_side/api/modules/rating_system.py
  18. +35
    -0
      server_side/api/modules/utility.py

+ 1
- 0
MyCity/app/build.gradle View File

@ -9,6 +9,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
android.defaultConfig.vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {


+ 1
- 11
MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java View File

@ -6,9 +6,7 @@ import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
@ -27,7 +25,7 @@ import android.widget.Toast;
import gq.yigit.mycity.navigation.MapsFragment;
import gq.yigit.mycity.tools.*;
import gq.yigit.mycity.tools.WebRequest.responseListener;
import gq.yigit.mycity.utilityFragment.UtilityMain;
import gq.yigit.mycity.utility.UtilityMain;
import gq.yigit.mycity.votesFragment.VoteFragment;
import gq.yigit.mycity.votesFragment.VotesContent;
import gq.yigit.mycity.votesFragment.VotesFragment;
@ -67,14 +65,6 @@ public class MainActivity extends AppCompatActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cntxt = getApplicationContext();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(


+ 138
- 0
MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityElectricity.java View File

@ -0,0 +1,138 @@
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;
import java.util.HashMap;
public class UtilityElectricity extends Fragment implements WebRequest.responseListener {
private JSONObject electricityUsage;
private GraphView graph;
private OnFragmentInteractionListener mListener;
public UtilityElectricity() {
// Required empty public constructor
}
public static UtilityElectricity newInstance(String param1, String param2) {
UtilityElectricity fragment = new UtilityElectricity();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_utility_electricity, 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","electricity");
WebRequest login_manager = new WebRequest(url + "/resources/",false,request);
login_manager.addListener(this);
login_manager.execute();
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void receivedResponse(boolean success, String result){
if(success){
try{
electricityUsage = new JSONObject(result);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {});
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(new DataPoint[] {});
for(int i = 0;i < electricityUsage.getJSONArray("daily_electricity_usage").length();i++){
series.appendData(new DataPoint(i,electricityUsage.getJSONArray("daily_electricity_usage").getInt(i)),
true,
100);
}
for(int i = 0;i < electricityUsage.getJSONArray("ideal_electricity_usage").length();i++){
series2.appendData(new DataPoint(i,electricityUsage.getJSONArray("ideal_electricity_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 electric service");
}
}
}
}

MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityMain.java → MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityMain.java View File


MyCity/app/src/main/java/gq/yigit/mycity/utilityFragment/UtilityElectricity.java → MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java View File


+ 0
- 7
MyCity/app/src/main/res/layout/app_bar_main.xml View File

@ -21,12 +21,5 @@
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>

+ 5
- 3
MyCity/app/src/main/res/layout/fragment_utility.xml View File

@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".utilityFragment.UtilityMain" android:orientation="vertical"
tools:context=".utility.UtilityMain" android:orientation="vertical"
android:layout_marginTop="55dp">
@ -24,12 +24,14 @@
<android.support.design.widget.TabLayout
android:id="@+id/result_tabs"
android:background="#e8e8e8"
app:tabTextColor="#d5d5d5"
app:tabTextColor="#8e8e8e"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabIndicatorColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"


+ 39
- 27
MyCity/app/src/main/res/layout/fragment_utility_electricity.xml View File

@ -3,8 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".utilityFragment.UtilityElectricity" android:orientation="vertical"
android:layout_marginTop="5dp">
tools:context=".utility.UtilityElectricity"
android:layout_marginTop="10dp"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<com.jjoe64.graphview.GraphView
@ -12,55 +13,66 @@ android:layout_marginTop="5dp">
android:layout_height="254dp"
android:id="@+id/utility_graph"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Your Points:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView"
android:textColor="@android:color/holo_orange_dark" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"/>
<TextView
android:text="Your Bill:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView1"
android:textColor="@android:color/holo_orange_dark" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"/>
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="%Efficieny:"
android:text="Content Here"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView2"
android:textColor="@android:color/holo_orange_dark" android:textSize="16sp"
android:layout_marginVertical="4dp" android:layout_marginHorizontal="7dp"/>
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="vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="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_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="Content Here"
android:text="%Efficiency"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/bill_utility"
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: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/efficieny_utility"
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_marginVertical="4dp" android:layout_marginHorizontal="7dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout


+ 14
- 0
MyCity/app/src/main/res/layout/fragment_utility_water.xml View File

@ -0,0 +1,14 @@
<?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">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
</FrameLayout>

+ 2
- 1
MyCity/app/src/main/res/layout/nav_header_main.xml View File

@ -26,6 +26,7 @@
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" android:id="@+id/uname"/>
android:textAppearance="@style/TextAppearance.AppCompat.Body1" android:id="@+id/uname"
android:layout_marginLeft="25dp"/>
</LinearLayout>

+ 5
- 3
server_side/api/app.py View File

@ -1,7 +1,7 @@
from flask import Flask, send_from_directory
from flask_restful import Api
from api.modules import user_info, voting_system, rating_system
from api.modules import user_info, voting_system, rating_system, utility
app = Flask(__name__)
api = Api(app)
@ -12,7 +12,7 @@ def send_img(path):
if __name__ == '__main__':
context = ('encryption/mycity.crt','encryption/mycity-decrypted.key')
context = ('encryption/mycity.crt', 'encryption/mycity-decrypted.key')
api.add_resource( voting_system.Votings, '/votings', '/votings/' )
api.add_resource( voting_system.Voting, '/votings/<int:voting_id>' )
api.add_resource( voting_system.Vote, '/vote', '/vote/' )
@ -24,5 +24,7 @@ if __name__ == '__main__':
api.add_resource( user_info.Users, '/users', '/users/' )
api.add_resource( user_info.User, '/users/<path:user_id>', '/users/<path:user_id>/' )
api.add_resource( user_info.Login, '/login', '/login/' )
api.add_resource(utility.Resources, '/resources', '/resources/')
app.run(host='0.0.0.0', port=5000, ssl_context=context)
app.run(host='0.0.0.0', port=5000, ssl_context=context)

BIN
server_side/api/modules/__pycache__/rating_system.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/utility.cpython-37.pyc View File


+ 4
- 34
server_side/api/modules/databases/ratings.json View File

@ -9,43 +9,13 @@
"longitude": 32.9101,
"diff": 0.3
},
"rates": [
{
"id": 1,
"score": 10,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"note": "IT WAS AWESOME"
},
{
"rates": {
"9vard12ty0ad2yvwp3q53rsf3h43r2vq": {
"id": 2,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 10,
"note": null
},
{
"id": 3,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 5,
"note": null
},
{
"id": 4,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 3,
"note": "Not bad boiii"
},
{
"id": 5,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 3,
"note": "Not bad boiii"
},
{
"id": 6,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 8,
"note": "ujj"
"note": "Fuck you"
}
]
}
}
]

+ 93
- 21
server_side/api/modules/databases/users.json View File

@ -1,23 +1,95 @@
{
"efe": {
"id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"realname": "Efe Aydın",
"avatar": "/img/user1.png",
"password": "827ccb0eea8a706c4c34a16891f84e7b",
"email": "efeaydin@mycity.com",
"stats": {
"bus_usage_week": 1,
"bus_usage_month": 5,
"bus_usage_year": 67
},
"daily_electricity_usage": [
10,
5,
5,
6,
7
],
"points": 50
}
"efe": {
"id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"realname": "Efe Aydın",
"avatar": "/img/user1.png",
"password": "827ccb0eea8a706c4c34a16891f84e7b",
"email": "efeaydin@mycity.com",
"stats": {
"bus_usage_week": 1,
"bus_usage_month": 5,
"bus_usage_year": 67
},
"daily_electricity_usage": [
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
],
"daily_water_usage": [
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
],
"ideal_electricity_usage": [
11,
11,
11,
11,
11,
12,
14,
15,
15,
15,
15,
15,
15,
15,
15,
15,
15,
16,
16,
15,
15,
14,
12
],
"ideal_water_usage": [
1
],
"points": 50
}
}

+ 4
- 2
server_side/api/modules/databases/votings.json View File

@ -25,12 +25,14 @@
"name": "wooting",
"desc": "wooting desc",
"img": "/img/voting.jpg",
"voters": [],
"voters": [
"9vard12ty0ad2yvwp3q53rsf3h43r2vq"
],
"votes": {
"1": {
"name": "woote 1",
"desc": "woote desc",
"votes": 2
"votes": 3
},
"2": {
"id": 1,


+ 31
- 4
server_side/api/modules/rating_system.py View File

@ -18,10 +18,11 @@ with open(db_path, 'r') as f:
with open(user_db, 'r') as f:
users = json.load(f)
class Ratings(Resource):
def post(self):
"""
Example URL Query:
Example POST Data:
latitude=<latitude>&
longitude=<longitude>
"""
@ -45,6 +46,30 @@ class Ratings(Resource):
return ret_data
# def post(self):
# """
# Example POST Data:
# name=<rating_name>&
# desc=<rating_desc>& # OPTIONAL
# img=<rating_img>& # OPTIONAL
# """
# args = request.form
# rating_id = len(ratings) + 1
# rating = {
# 'id': rating_id,
# 'name': args['name'],
# 'desc': args.get('desc'),
# 'img' : args.get('img'),
# 'rates': []
# }
#
# ratings.append(rating)
#
# with open(db_path, 'w') as f:
# json.dump(ratings, f, indent=4)
#
# return rating
class Rating(Resource):
def get(self, rating_id):
@ -55,6 +80,7 @@ class Rating(Resource):
except:
abort(404, error="Rating {} doesn't exist".format(rating_id))
class Rate(Resource):
def post(self):
"""
@ -70,12 +96,12 @@ class Rate(Resource):
if 0 >= score >= 10:
abort(500, 'Score should be between 0 and 10')
note = request.form.get('note')
ratings[rating_id - 1]['rates'].append({
ratings[rating_id - 1]['rates'][rater_id] = {
'id': len(ratings[rating_id - 1]['rates']) + 1,
'rater': request.form['rater_id'],
'score': score,
'note': note
})
}
with open(db_path, 'w') as f:
json.dump(ratings, f, indent=4)
@ -83,9 +109,10 @@ class Rate(Resource):
return {'error': 'User doesn\'t exists'}
if __name__ == '__main__':
api.add_resource(Ratings, '/ratings', '/ratings/')
api.add_resource(Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
api.add_resource(Rate, '/rate', '/rate/')
app.run(host='0.0.0.0', port=5000)
app.run(host='0.0.0.0', port=5000)

+ 35
- 0
server_side/api/modules/utility.py View File

@ -0,0 +1,35 @@
import os
import copy
import json
from api.modules import utils
from flask import Flask, request
from flask_restful import Resource, Api, abort
app = Flask(__name__)
api = Api(app)
db_path = os.path.join(app.root_path, 'databases', 'users.json')
with open(db_path, 'r') as f:
users = json.load(f)
class Resources(Resource):
def post(self):
user = utils.find_by_id(users.values(), request.form['user_id'])
if request.form["type"] == "electricity":
return {
'daily_electricity_usage': user['daily_electricity_usage'],
'ideal_electricity_usage': user['ideal_electricity_usage']
}
elif request.form["type"] == "water":
return {
'daily_water_usage': user['daily_water_usage'],
'ideal_water_usage': user['ideal_water_usage']
}
if __name__ == '__main__':
api.add_resource(Resources, '/resources', '/resources/')
app.run(host='0.0.0.0', port=5000)

Loading…
Cancel
Save