Commit 84611818 authored by Lucas Braz Cunha's avatar Lucas Braz Cunha

Issue AGILE#174: Update request on user screen[Working]

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent e5103320
......@@ -25,6 +25,7 @@ import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
......@@ -79,7 +80,13 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
tv_phone_warning, tv_password_confirm_warning, tv_email_warning,
tv_account_new_password_warning;
private boolean editable, isChangingPassword, isChangingPhoto, isPhotoDelete;
private MenuItem btn_edit, btn_sync;
private boolean editable;
private boolean isChangingPassword;
private boolean isChangingPhoto;
private boolean isPhotoDelete;
private boolean isUpdated;
private UserPhotoHelper userPhotoHelper;
......@@ -349,13 +356,14 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
isChangingPassword = false;
isChangingPhoto = false;
isPhotoDelete = false;
isUpdated = false;
}
@Override
protected void onResume() {
super.onResume();
presenter.bindView(this);
}
@Override
......@@ -373,6 +381,9 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.account_menu, menu);
btn_edit = menu.findItem(R.id.btn_edit_account);
btn_sync = menu.findItem(R.id.btn_sync_account);
enableButtonEdit(false);
return true;
}
......@@ -419,15 +430,7 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
switch (item.getItemId()) {
case android.R.id.home:
if (editable) {
enableAllFields(false);
setButtonVisible(false);
clearWarnings();
loadData();
scrollUp();
if(userPhotoHelper.existsPhoto())
userPhotoHelper.updateImgView(imgv_profile, userPhotoHelper.getPhotoFileUri());
else
imgv_profile.setImageBitmap(null);
cancelUpdate();
} else {
Intent intent = new Intent(AccountActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
......@@ -435,9 +438,19 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
finish();
}
return true;
case R.id.edit_account:
enableAllFields(true);
setButtonVisible(true);
case R.id.btn_edit_account:
if(editable){
cancelUpdate();
}
else{
enableAllFields(true);
setButtonVisible(true);
}
return true;
case R.id.btn_sync_account:
Log.d("AccountActivity", "CLIQUEIIIIIIIIIIIIIIIII");
enableButtonSync(false);
presenter.updateCheck();
return true;
default:
return super.onOptionsItemSelected(item);
......@@ -472,15 +485,31 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
enableAllFields(false);
} else {
pb_account.setVisibility(View.INVISIBLE);
enableAllFields(true);
btn_update.setEnabled(true);
btn_cancel.setEnabled(true);
enableAllFields(true);
}
}
private void enableAllFields(boolean enable) {
editable = enable;
public void enableButtonEdit(boolean enable){
btn_edit.setEnabled(enable);
if(enable)
btn_edit.getIcon().setAlpha(255);
else
btn_edit.getIcon().setAlpha(130);
}
public void enableButtonSync(boolean enable){
btn_sync.setEnabled(enable);
if(enable)
btn_sync.getIcon().setAlpha(255);
else
btn_sync.getIcon().setAlpha(130);
}
public void enableAllFields(boolean enable) {
editable = enable;
et_name.setEnabled(enable);
et_rg.setEnabled(enable);
et_sus_number.setEnabled(enable);
......@@ -509,26 +538,28 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
}
}
public void successfulUpdate() {
try{
//has to be done before load data.
userPhotoHelper.confirmUpdate(isPhotoDelete);
if(isChangingPhoto)
userPhotoHelper.confirmUpdate(isPhotoDelete);
}catch (Exception e){
// TODO: 04/05/17 user messages & error warning
Toast.makeText(this, "Ocorreu um erro ao salvar a imagem, tente novamente.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
enableAllFields(false);
setButtonVisible(false);
enableButtonEdit(true);
enableAllFields(false);
loadData();
Log.d(this.getClass().getName(),"Atualizou com sucesso");
}
public void cancelUpdate(){
private void cancelUpdate(){
scrollUp();
enableAllFields(false);
setButtonVisible(false);
clearWarnings();
loadData();
if(userPhotoHelper.existsPhoto())
userPhotoHelper.updateImgView(imgv_profile, userPhotoHelper.getPhotoFileUri());
......@@ -1031,5 +1062,13 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
public void setCurrentPassword(String current_password) {
this.current_password = current_password;
}
public void setUpdated(boolean updated) {
isUpdated = updated;
}
}
package br.ufpr.c3sl.agendador.agendador.models;
import android.content.Context;
import com.google.gson.annotations.SerializedName;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
/**
* Created by Bruno Freitas Tissei on 2/3/17.
*/
......
......@@ -39,4 +39,7 @@ public interface ApiEndpoints {
@GET("citizens/{id}/picture")
Call<ResponseBody> requestPhoto(@Path("id") int id, @Query("size") String size);
@GET("accounts/self")
Call<AccountOutput> requestSelf();
}
\ No newline at end of file
......@@ -14,8 +14,8 @@ import retrofit2.converter.gson.GsonConverterFactory;
*/
public abstract class ApiUtils {
// public static final String BASE_URL = "http://10.0.2.2:3000/v1/";
public static final String BASE_URL = "http://newcastle.c3sl.ufpr.br/devapi/v1/";
public static final String BASE_URL = "http://10.0.2.2:3000/v1/";
// public static final String BASE_URL = "http://newcastle.c3sl.ufpr.br/devapi/v1/";
public static ApiEndpoints request(final Map<String, String> header) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
......
......@@ -2,6 +2,7 @@ package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
......@@ -27,8 +28,6 @@ import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static android.util.Log.d;
/**
* Created by Horstmann on 02/03/17.
*/
......@@ -40,12 +39,90 @@ public class AccountPresenter extends BasePresenter<AccountView> {
private AccountPresenter accountPresenter;
private boolean isPicUpdate;
public AccountPresenter(Context context) {
this.context = context;
accountPresenter = this;
isPicUpdate = false;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
private void onSuccessfulCheck(AccountOutput accountOutput, Headers headers){
osb.edit().putString("access-token", headers.get("access-token")).apply();
osb.edit().putString("expiry", headers.get("expiry")).apply();
accountOutput.getCitizen().save(context);
}
// TODO: 18/05/17 Testar a funcao
public void updateCheck(){
//get user data from back-end to keep App updated.
//similar to login
accountPresenter.view().setProgressBar(true);
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put("access-token", osb.getString("access-token", null));
header.put("client", osb.getString("client", null));
header.put("uid", osb.getString("uid", null));
Log.d("SELF Request", "Token enviado:" + osb.getString("access-token", null) + "\n\n");
ApiEndpoints service = ApiUtils.request(header);
Call<AccountOutput> listCall = service.requestSelf();
listCall.enqueue(new Callback<AccountOutput>() {
@Override
public void onResponse(Call<AccountOutput> call, Response<AccountOutput> response) {
Headers headers = response.headers();
int status = response.code();
AccountOutput accountOutput = response.body();
Log.d("SELF Request", "Token recebido:" + headers.get("access-token") + "\n\n");
Log.d("SELF Request", "Client recebido:" + headers.get("client") + "\n\n");
Log.d("SELF Request", headers.names().toString());
Log.d("SELF Request", response.raw().toString());
if(headers.get("access-token") == null || headers.get("access-token").equals(""))
Toast.makeText(context,"Token Null ou Vazio!!",Toast.LENGTH_SHORT).show();
switch (status) {
case 200:
Log.d("SELF Request", "Voltou 200!!!");
//info downloaded
accountPresenter.view().setUpdated(true);
onSuccessfulCheck(accountOutput, headers);
accountPresenter.view().setProgressBar(false);
accountPresenter.view().successfulUpdate();
accountPresenter.view().enableButtonEdit(true);
accountPresenter.view().enableButtonSync(true);
break;
default:
Log.d("SELF Request", "Voltou " + status + "!!!");
//error!! block edit and wait for user to try synchronization again.
//notify user
Toast.makeText(context,"Ocorreu um problema com o servidor, tente novamente mais tarde.",Toast.LENGTH_SHORT).show();
accountPresenter.view().setUpdated(false);
accountPresenter.view().setProgressBar(false);
accountPresenter.view().enableAllFields(false);
accountPresenter.view().enableButtonEdit(false);
accountPresenter.view().enableButtonSync(true);
break;
}
}
@Override
public void onFailure(Call<AccountOutput> call, Throwable t) {
Log.d("SELF Request", "Falhou!!!");
//disable edit on UI & notify user & wait for user to try sync again
accountPresenter.view().setUpdated(false);
accountPresenter.view().setProgressBar(false);
accountPresenter.view().enableAllFields(false);
accountPresenter.view().setNoConnection(true);
accountPresenter.view().enableButtonEdit(false);
accountPresenter.view().enableButtonSync(true);
}
});
}
private void onSuccessfulUpdate(AccountOutput accountOutput, Headers headers) {
updateToken(headers);
accountOutput.getCitizen().save(context);
......@@ -59,8 +136,6 @@ public class AccountPresenter extends BasePresenter<AccountView> {
}
private void updateToken(Headers headers) {
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
osb.edit().putString("access-token", headers.get("access-token")).apply();
osb.edit().putString("client", headers.get("client")).apply();
osb.edit().putString("uid", headers.get("uid")).apply();
......@@ -70,8 +145,9 @@ public class AccountPresenter extends BasePresenter<AccountView> {
protected void updateView() {
}
public void onUpdateClicked(){
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
......@@ -130,7 +206,6 @@ public class AccountPresenter extends BasePresenter<AccountView> {
@Override
public void onFailure(Call<AccountOutput> call, Throwable t) {
Log.d("Server response", "Requisição falhou!!");
accountPresenter.view().setProgressBar(false);
accountPresenter.view().setNoConnection(true);
......@@ -138,8 +213,6 @@ public class AccountPresenter extends BasePresenter<AccountView> {
});
}
public void onCepNotFocused(String cep) {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
......@@ -163,7 +236,6 @@ public class AccountPresenter extends BasePresenter<AccountView> {
values.put("cityname_su", address.getCity_name());
values.put("statename_su", address.getState_name());
accountPresenter.view().setProgressBar(false);
break;
case 404:
......
......@@ -176,11 +176,6 @@ public class LoginPresenter extends BasePresenter<LoginView> {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// TODO: 17/04/17 what to do if request fails to get image?
/*
Job scheduled to check if there is internet connection
and download user image
*/
Bundle extras = new Bundle();
extras.putString("access-token",osb.getString("access-token", null));
......@@ -207,8 +202,6 @@ public class LoginPresenter extends BasePresenter<LoginView> {
//if device is rebooted the job will be restarted
.setLifetime(Lifetime.FOREVER)
.build();
dispatcher.mustSchedule(job);
loginPresenter.view().setProgressBar(false);
......
......@@ -21,8 +21,14 @@ public interface AccountView {
void setNoConnection(boolean enabled);
void cancelUpdate();
void setNoConnectionCep(boolean enabled);
void setUpdated(boolean updated);
void enableButtonEdit(boolean enable);
void enableAllFields(boolean enable);
void enableButtonSync(boolean enable);
}
......@@ -3,10 +3,14 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/edit_account"
android:title="Sair"
<item android:id="@+id/btn_edit_account"
android:title="Editar"
android:icon="@drawable/ic_edit_account"
app:showAsAction="always"/>
app:showAsAction="ifRoom"/>
<item android:id="@+id/btn_sync_account"
android:title="Sincronizar"
android:icon="@android:drawable/stat_notify_sync_noanim"
app:showAsAction="ifRoom"/>
<item android:id="@+id/configuration_actionbtn"
android:title="Configurações"
app:showAsAction="never"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment