diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java index 875a314..432b3a0 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java @@ -10,36 +10,31 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.TextView; +import android.widget.*; import gq.yigit.mycity.R; import gq.yigit.mycity.tools.*; import org.json.JSONObject; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link VoteFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link VoteFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class VoteFragment extends Fragment implements responseListener, imageListener { - // 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 String url; private String vote_id; - + private String user_vote = ""; private OnFragmentInteractionListener mListener; private ImageView header_img; private TextView title; private TextView desc; + private Spinner spinner; private Button submit; + private Context cntxt; public VoteFragment() { // Required empty public constructor @@ -66,21 +61,23 @@ public class VoteFragment extends Fragment implements responseListener, imageLis @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - header_img = container.findViewById(R.id.header); - title = container.findViewById(R.id.name); - desc = container.findViewById(R.id.description); - submit = container.findViewById(R.id.submit); + final View rootView = inflater.inflate(R.layout.fragment_vote, container, false); + header_img = rootView.findViewById(R.id.header); + title = rootView.findViewById(R.id.name); + desc = rootView.findViewById(R.id.description); + submit = rootView.findViewById(R.id.submit); + spinner = rootView.findViewById(R.id.vote); FileActions file_manager = new FileActions(); url = file_manager.readFromFile(getContext(),"server.config").trim(); String url_vote = url + "/votings/" + vote_id; - WebRequest request_manager = new WebRequest(url_vote,true,new HashMap()); - request_manager.addListener(this); - request_manager.execute(); - return inflater.inflate(R.layout.fragment_vote, container, false); + WebRequest data_request = new WebRequest(url_vote,true,new HashMap()); + data_request.addListener(this); + data_request.execute(); + cntxt = getContext(); + return rootView; } - // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); @@ -110,6 +107,7 @@ public class VoteFragment extends Fragment implements responseListener, imageLis } public void receivedResponse(boolean success,String response){ + Log.i("[INFO]",response); if(success){ try{ JSONObject vote_data = new JSONObject(response); @@ -118,13 +116,57 @@ public class VoteFragment extends Fragment implements responseListener, imageLis ImageDownload img_manager = new ImageDownload(); img_manager.addListener(this); img_manager.execute(url+(String)vote_data.get("img")); + + ArrayAdapter adapter; + List list; + list = new ArrayList(); + + Iterator keys = vote_data.getJSONObject("votes").keys(); + list.add("Please Select..."); + while (keys.hasNext()) { + Object key = keys.next(); + JSONObject value = vote_data.getJSONObject("votes").getJSONObject((String) key); + String component = value.getString("name"); + list.add(component); + } + + adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_spinner_item, list); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + }catch (Exception e){ Log.e("[ERROR]",e.getMessage()); } + + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parentView, View selectedItemView, int position, long id) { + Log.i("[INFO]",String.valueOf(position)); + user_vote = String.valueOf(position); + } + @Override + public void onNothingSelected(AdapterView parentView) { + } + }); } } public void imageDownloaded(Bitmap img){ header_img.setImageBitmap(img); + submit.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(!user_vote.isEmpty()){ + HashMap params = new HashMap<>(); + params.put("voting_id",vote_id); + params.put("vote_id",user_vote); + WebRequest vote_request = new WebRequest(url+"/vote",true, params); + vote_request.execute(); + }else{ + Toast.makeText(cntxt,"Please selet a vote!",Toast.LENGTH_SHORT); + } + } + }); } } diff --git a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc index b49d79e..50508aa 100644 Binary files a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc and b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc differ diff --git a/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc b/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc index 3105b8a..eda37db 100644 Binary files a/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc and b/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc differ diff --git a/server_side/api/rating_system/rating_system.py b/server_side/api/rating_system/rating_system.py index ec95b75..0380fdf 100644 --- a/server_side/api/rating_system/rating_system.py +++ b/server_side/api/rating_system/rating_system.py @@ -2,15 +2,7 @@ import os import json from flask import Flask, request -<<<<<<< HEAD from flask_restful import Resource, Api, abort -======= -<<<<<<< HEAD -from flask_restful import Resource, Api -======= -from flask_restful import Resource, Api, abort ->>>>>>> efe ->>>>>>> yigit app = Flask(__name__) api = Api(app) @@ -91,4 +83,4 @@ if __name__ == '__main__': api.add_resource(Rating, '/ratings/', '/ratings//') api.add_resource(Rate, '/rate', '/rate/') - app.run(host='0.0.0.0', port=5000) +app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc index d488601..7471bb5 100644 Binary files a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc and b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc differ diff --git a/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc b/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc index 7d63e79..5f144a4 100644 Binary files a/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc and b/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc differ diff --git a/server_side/api/voting_system/voting_system.py~efe b/server_side/api/voting_system/voting_system.py similarity index 100% rename from server_side/api/voting_system/voting_system.py~efe rename to server_side/api/voting_system/voting_system.py diff --git a/server_side/api/voting_system/voting_system.py~HEAD b/server_side/api/voting_system/voting_system.py~HEAD deleted file mode 100644 index 729efda..0000000 --- a/server_side/api/voting_system/voting_system.py~HEAD +++ /dev/null @@ -1,101 +0,0 @@ -import os -import json - -from flask import Flask, request -from flask_restful import Resource, Api, abort - -app = Flask(__name__) -api = Api(app) - -with open(os.path.join(app.root_path, 'votings.json'), 'r') as f: - votings = json.load(f) - -class Votings(Resource): - def get(self): - voting = [ - { - 'id' : v['id'], - 'name': v['name'], - 'desc': v['desc'], - 'img' : v['img'] - } - for v in votings - ] - return voting - - def post(self): - """ - Example POST Data: - name=& - desc=& # OPTIONAL - img=& # OPTIONAL - votes=[ - { - "name": "", - "desc": "" # OPTIONAL - }, - (...) - ] - - """ - args = request.form - voting_id = len(votings) + 1 - voting = { - 'id': voting_id, - 'name': args['name'], - 'desc': args.get('desc'), - 'img' : args.get('img'), - 'votes': [ - { - 'id' : k + 1, - 'name': vote['name'], - 'desc': vote.get('desc'), - 'votes': 0 - } - for k, vote in enumerate(json.loads(args['votes'])) - ] - } - - votings.append(voting) - - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: - json.dump(votings, f, indent=4) - - return voting - - -class Voting(Resource): - def get(self, voting_id): - try: - return votings[voting_id - 1] - except: - abort(404, error="Voting {} doesn't exist".format(voting_id)) - -class Vote(Resource): - def get(self): - """ - Example URL Query: - /vote?voting_id=&vote_id=&voter_id= - """ - voting_id = int(request.args['voting_id']) - vote_id = int(request.args['vote_id']) - voter_id = int(request.args['voter_id']) - error = False - for i in range(votings[voting_id]["votes"]): - if(voter_id in votings[voting_id]["votes"][i]["votes"]): - error = True - if error: - return "An error occured" - - votings[voting_id]["votes"][vote_id]["votes"].append(voter_id) - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: - json.dump(votings, f, indent=4) - - return votings[voting_id - 1] - -if __name__ == '__main__': - api.add_resource(Votings, '/votings', '/votings/') - api.add_resource(Voting, '/votings/', '/votings//') - api.add_resource(Vote, '/vote', '/vote/') - - app.run(host='0.0.0.0', port=5000)