Commit b4cc0803e9b28b1dbdb9224faaf40922b4b19a9e

Authored by geraldo morais
1 parent 0746eb16
Exists in master

Voto de projetos implementado

app/src/main/java/com/monitorabrasil/monitoralegislativo/adapters/ProjetoAdapter.java
@@ -10,10 +10,12 @@ import android.widget.TextView; @@ -10,10 +10,12 @@ import android.widget.TextView;
10 10
11 import com.monitorabrasil.monitoralegislativo.R; 11 import com.monitorabrasil.monitoralegislativo.R;
12 import com.monitorabrasil.monitoralegislativo.interfaces.RecyclerViewOnClickListenerHack; 12 import com.monitorabrasil.monitoralegislativo.interfaces.RecyclerViewOnClickListenerHack;
  13 +import com.parse.FindCallback;
13 import com.parse.ParseException; 14 import com.parse.ParseException;
14 import com.parse.ParseObject; 15 import com.parse.ParseObject;
15 -  
16 -import org.w3c.dom.Text; 16 +import com.parse.ParseQuery;
  17 +import com.parse.ParseUser;
  18 +import com.parse.SaveCallback;
17 19
18 import java.util.List; 20 import java.util.List;
19 21
@@ -30,6 +32,7 @@ public class ProjetoAdapter extends RecyclerView.Adapter<ProjetoAdapter.ViewHold @@ -30,6 +32,7 @@ public class ProjetoAdapter extends RecyclerView.Adapter<ProjetoAdapter.ViewHold
30 // you provide access to all the views for a data item in a view holder 32 // you provide access to all the views for a data item in a view holder
31 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 33 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
32 // each data item is just a string in this case 34 // each data item is just a string in this case
  35 + public ParseObject projetoVotado;
33 public TextView classificacao; 36 public TextView classificacao;
34 public TextView data; 37 public TextView data;
35 public TextView autor; 38 public TextView autor;
@@ -72,7 +75,27 @@ public class ProjetoAdapter extends RecyclerView.Adapter<ProjetoAdapter.ViewHold @@ -72,7 +75,27 @@ public class ProjetoAdapter extends RecyclerView.Adapter<ProjetoAdapter.ViewHold
72 75
73 @Override 76 @Override
74 public void onBindViewHolder(final ViewHolder viewHolder, int i) { 77 public void onBindViewHolder(final ViewHolder viewHolder, int i) {
75 - ParseObject projeto = mDataset.get(i); 78 + final ParseObject projeto = mDataset.get(i);
  79 + //verifica se ja votou
  80 + ParseQuery<ParseObject> query = ParseQuery.getQuery("Voto");
  81 + query.fromLocalDatastore();
  82 + query.whereEqualTo("projeto", projeto);
  83 + query.findInBackground(new FindCallback<ParseObject>() {
  84 + @Override
  85 + public void done(List<ParseObject> list, ParseException e) {
  86 + if(list.size() > 0){
  87 + String voto = list.get(0).get("voto").toString();
  88 + viewHolder.projetoVotado = list.get(0);
  89 + if(voto.equals("s")){
  90 + viewHolder.btnConcordo.setText("Concordei");
  91 + }else{
  92 + viewHolder.btnDiscordo.setText("Discordei");
  93 + }
  94 + }
  95 + }
  96 + });
  97 +
  98 +
76 viewHolder.classificacao.setText(projeto.get("classificacao").toString()); 99 viewHolder.classificacao.setText(projeto.get("classificacao").toString());
77 viewHolder.data.setText(projeto.get("data").toString()); 100 viewHolder.data.setText(projeto.get("data").toString());
78 viewHolder.descricao.setText(projeto.get("descricao").toString()); 101 viewHolder.descricao.setText(projeto.get("descricao").toString());
@@ -86,21 +109,64 @@ public class ProjetoAdapter extends RecyclerView.Adapter&lt;ProjetoAdapter.ViewHold @@ -86,21 +109,64 @@ public class ProjetoAdapter extends RecyclerView.Adapter&lt;ProjetoAdapter.ViewHold
86 109
87 viewHolder.btnConcordo.setOnClickListener(new View.OnClickListener() { 110 viewHolder.btnConcordo.setOnClickListener(new View.OnClickListener() {
88 @Override 111 @Override
89 - public void onClick(View v) {  
90 - Snackbar.make(v, "Voto registrado para "+viewHolder.classificacao.getText().toString(), Snackbar.LENGTH_LONG)  
91 - .setAction("Action", null).show(); 112 + public void onClick(final View v) {
  113 + registraVoto(v, projeto,"s",viewHolder);
  114 +
92 } 115 }
93 }); 116 });
94 117
95 viewHolder.btnDiscordo.setOnClickListener(new View.OnClickListener() { 118 viewHolder.btnDiscordo.setOnClickListener(new View.OnClickListener() {
96 @Override 119 @Override
97 public void onClick(View v) { 120 public void onClick(View v) {
98 - Snackbar.make(v, "Voto registrado para "+viewHolder.classificacao.getText().toString(), Snackbar.LENGTH_LONG)  
99 - .setAction("Action", null).show(); 121 + registraVoto(v, projeto, "n", viewHolder);
100 } 122 }
101 }); 123 });
102 } 124 }
103 125
  126 + private void registraVoto(final View v, ParseObject projeto, String tipoVoto, ViewHolder viewHolder2) {
  127 +
  128 + //verificar se esta logado
  129 + if(ParseUser.getCurrentUser() == null){
  130 + Snackbar.make(v, "Para votar é necessário estar logado", Snackbar.LENGTH_LONG)
  131 + .setAction("Action", null).show();
  132 + return;
  133 + }
  134 +
  135 + //verificar se ja votou nesse projeto
  136 + if(viewHolder2.projetoVotado != null){
  137 + viewHolder2.projetoVotado.unpinInBackground();
  138 + viewHolder2.projetoVotado.deleteInBackground();
  139 +
  140 + }
  141 +
  142 + ParseObject voto = new ParseObject("Voto");
  143 + voto.put("projeto", projeto);
  144 + voto.put("user", ParseUser.getCurrentUser());
  145 + voto.put("voto", tipoVoto);
  146 + Button btnVoto = (Button) v;
  147 + if(tipoVoto.equals("s")){
  148 +
  149 + btnVoto.setText("Concordei");
  150 + viewHolder2.btnDiscordo.setText("Discordo");
  151 + }else{
  152 + btnVoto.setText("Discordei");
  153 + viewHolder2.btnConcordo.setText("Concordo");
  154 + }
  155 +
  156 + try {
  157 + voto.pin();
  158 + voto.saveInBackground(new SaveCallback() {
  159 + @Override
  160 + public void done(ParseException e) {
  161 + Snackbar.make(v, "Voto registrado", Snackbar.LENGTH_SHORT)
  162 + .setAction("Action", null).show();
  163 + }
  164 + });
  165 + } catch (ParseException e) {
  166 + e.printStackTrace();
  167 + }
  168 + }
  169 +
104 public void setRecyclerViewOnClickListenerHack(RecyclerViewOnClickListenerHack r){ 170 public void setRecyclerViewOnClickListenerHack(RecyclerViewOnClickListenerHack r){
105 mRecyclerViewOnClickListenerHack = r; 171 mRecyclerViewOnClickListenerHack = r;
106 } 172 }
app/src/main/res/layout/activity_login.xml
@@ -30,25 +30,34 @@ @@ -30,25 +30,34 @@
30 30
31 <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent" 31 <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent"
32 android:layout_height="wrap_content" android:orientation="vertical"> 32 android:layout_height="wrap_content" android:orientation="vertical">
33 -  
34 - <EditText 33 + <android.support.design.widget.TextInputLayout
35 android:layout_width="match_parent" 34 android:layout_width="match_parent"
36 - android:layout_height="wrap_content"  
37 - android:hint="@string/nome"  
38 - android:id="@+id/txtNome" />  
39 - 35 + android:layout_height="wrap_content">
  36 + <EditText
  37 + android:layout_width="match_parent"
  38 + android:layout_height="wrap_content"
  39 + android:hint="@string/nome"
  40 + android:id="@+id/txtNome" />
  41 + </android.support.design.widget.TextInputLayout>
  42 +
  43 + <android.support.design.widget.TextInputLayout
  44 + android:layout_width="match_parent"
  45 + android:layout_height="wrap_content">
40 <AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent" 46 <AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent"
41 android:layout_height="wrap_content" android:hint="@string/prompt_email" 47 android:layout_height="wrap_content" android:hint="@string/prompt_email"
42 android:inputType="textEmailAddress" android:maxLines="1" 48 android:inputType="textEmailAddress" android:maxLines="1"
43 android:singleLine="true" /> 49 android:singleLine="true" />
44 - 50 + </android.support.design.widget.TextInputLayout>
  51 + <android.support.design.widget.TextInputLayout
  52 + android:layout_width="match_parent"
  53 + android:layout_height="wrap_content">
45 <EditText android:id="@+id/password" android:layout_width="match_parent" 54 <EditText android:id="@+id/password" android:layout_width="match_parent"
46 android:layout_height="wrap_content" android:hint="@string/prompt_password" 55 android:layout_height="wrap_content" android:hint="@string/prompt_password"
47 android:imeActionId="@+id/login" 56 android:imeActionId="@+id/login"
48 android:imeActionLabel="@string/action_sign_in_short" 57 android:imeActionLabel="@string/action_sign_in_short"
49 android:imeOptions="actionUnspecified" android:inputType="textPassword" 58 android:imeOptions="actionUnspecified" android:inputType="textPassword"
50 android:maxLines="1" android:singleLine="true" /> 59 android:maxLines="1" android:singleLine="true" />
51 - 60 + </android.support.design.widget.TextInputLayout>
52 61
53 <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall" 62 <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall"
54 android:layout_width="match_parent" android:layout_height="wrap_content" 63 android:layout_width="match_parent" android:layout_height="wrap_content"