Commit 032b0613 authored by Lucas Braz Cunha's avatar Lucas Braz Cunha

Presenters refactoring and actvities Dependents and DependentsList created

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 916c2032
......@@ -29,14 +29,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity"></activity>
<activity android:name=".HomeActivity"></activity>
<activity android:name=".CepActivity"></activity>
<activity android:name=".CepNotFoundActivity"></activity>
<activity android:name=".SignupActivity"></activity>
<activity android:name=".AccountActivity"></activity>
<activity android:name=".ForgotPasswordActivity"></activity>
<activity android:name=".DependentsActivity"></activity>
<activity android:name=".LoginActivity" />
<activity android:name=".HomeActivity" />
<activity android:name=".CepActivity" />
<activity android:name=".CepNotFoundActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".AccountActivity" />
<activity android:name=".ForgotPasswordActivity" />
<activity android:name=".DependentsListActivity" />
<service
android:name=".services.ImageUpdateService"
......@@ -53,7 +53,8 @@
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait" />
<activity android:name=".CitizenActivity" />
<activity android:name=".ScheduleConfirmationActivity"></activity>
<activity android:name=".ScheduleConfirmationActivity" />
<activity android:name=".DependentsActivity"></activity>
</application>
</manifest>
\ No newline at end of file
package br.ufpr.c3sl.agendador.agendador;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.List;
import android.os.Bundle;
import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionErrorDialog;
import br.ufpr.c3sl.agendador.agendador.helpers.adapters.CitizensArrayAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.RecyclerViewItemListener;
import br.ufpr.c3sl.agendador.agendador.models.UserOutput;
import br.ufpr.c3sl.agendador.agendador.presenters.DependentsPresenter;
import br.ufpr.c3sl.agendador.agendador.presenters.PresenterManager;
import br.ufpr.c3sl.agendador.agendador.views.DependentsView;
/**
* Created by Lucas B. Cunha on 08/08/17.
*/
public class DependentsActivity extends AppCompatActivity implements DependentsView{
public class DependentsActivity extends AppCompatActivity implements DependentsView {
private DependentsPresenter presenter;
private ProgressBar pb_dependents;
private RecyclerView rv_dependents;
private ConnectionFailureDialog dg_connection_failure;
private List<UserOutput> dependents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
presenter = new DependentsPresenter(this);
} else {
presenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
if(presenter == null)
presenter = new DependentsPresenter(this);
}
setContentView(R.layout.activity_dependents);
pb_dependents = (ProgressBar) findViewById(R.id.pb_dependentact);
rv_dependents = (RecyclerView) findViewById(R.id.rv_dependents);
RecyclerView.LayoutManager layout = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
rv_dependents.setLayoutManager(layout);
rv_dependents.setVisibility(View.INVISIBLE);
rv_dependents.addOnItemTouchListener(new RecyclerViewItemListener(getApplicationContext(), rv_dependents,
new RecyclerViewItemListener.RecyclerTouchListener() {
public void onClickItem(View v, int position) {
/*Intent intent = new Intent(DependentsActivity.this, );
Bundle bundle = new Bundle();
bundle.putParcelable(Utils.CITIZEN, citizens.get(position));
intent.putExtra(Utils.CITIZEN_BUNDLE, bundle);
startActivity(intent);
finish();*/
Toast.makeText(getBaseContext(), "Selecionou algum", Toast.LENGTH_SHORT).show();
}
public void onLongClickItem(View v, int position) {
//do nothing.
}
}));
}
/*public void successfulCitizens(List<UserOutput> citizens) {
this.citizens = citizens;
}
*/
public void onRequestFailure(){
Bundle bundle = new Bundle();
if(dg_connection_failure == null){
dg_connection_failure = new ConnectionFailureDialog();
}
bundle.putString(ConnectionErrorDialog.DIALOG_MESSAGE, getString(R.string.dialog_term_error));
dg_connection_failure.setArguments(bundle);
dg_connection_failure.show(getFragmentManager(), ConnectionErrorDialog.DIALOG_CONNECTION_ERROR);
}
@Override
protected void onResume() {
super.onResume();
presenter.bindView(this);
if(dependents == null)
presenter.requestDependents();
}
@Override
protected void onPause() {
super.onPause();
presenter.unbindView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
PresenterManager.getInstance().savePresenter(presenter, outState);
super.onSaveInstanceState(outState);
}
public void showLayout(boolean enabled) {
if(enabled){
rv_dependents.setAdapter(new CitizensArrayAdapter(this, dependents));
rv_dependents.setVisibility(View.VISIBLE);
}
}
public void setProgressBar(boolean enabled) {
if (enabled) {
pb_dependents.setVisibility(View.VISIBLE);
rv_dependents.setVisibility(View.INVISIBLE);
} else {
pb_dependents.setVisibility(View.INVISIBLE);
}
}
public static class ConnectionFailureDialog extends DialogFragment {
public static final String DIALOG_MESSAGE_KEY = "message";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getArguments().getString(DIALOG_MESSAGE_KEY))
.setTitle(getString(R.string.warningDialog_title))
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Activity activity = getActivity();
activity.onBackPressed();
}
});
return builder.create();
}
}
}
package br.ufpr.c3sl.agendador.agendador;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import java.util.List;
import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionErrorDialog;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.helpers.adapters.CitizensArrayAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.RecyclerViewItemListener;
import br.ufpr.c3sl.agendador.agendador.models.UserOutput;
import br.ufpr.c3sl.agendador.agendador.presenters.DependentsListPresenter;
import br.ufpr.c3sl.agendador.agendador.presenters.PresenterManager;
import br.ufpr.c3sl.agendador.agendador.views.DependentsListView;
/**
* Created by Lucas B. Cunha on 08/08/17.
*/
public class DependentsListActivity extends AppCompatActivity implements DependentsListView {
private DependentsListPresenter presenter;
private ProgressBar pb_dependents;
private RecyclerView rv_dependents;
private ConnectionFailureDialog dg_connection_failure;
private List<UserOutput> dependents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
presenter = new DependentsListPresenter(this);
} else {
presenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
if(presenter == null)
presenter = new DependentsListPresenter(this);
}
setContentView(R.layout.activity_dependents_list);
pb_dependents = (ProgressBar) findViewById(R.id.pb_dependentact);
rv_dependents = (RecyclerView) findViewById(R.id.rv_dependents);
RecyclerView.LayoutManager layout = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
rv_dependents.setLayoutManager(layout);
rv_dependents.setVisibility(View.INVISIBLE);
rv_dependents.addOnItemTouchListener(new RecyclerViewItemListener(getApplicationContext(), rv_dependents,
new RecyclerViewItemListener.RecyclerTouchListener() {
public void onClickItem(View v, int position) {
Intent intent = new Intent(DependentsListActivity.this, DependentsActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(Utils.CITIZEN, dependents.get(position));
intent.putExtra(Utils.CITIZEN_BUNDLE, bundle);
startActivity(intent);
}
public void onLongClickItem(View v, int position) {
//do nothing.
}
}));
}
/*public void successfulCitizens(List<UserOutput> citizens) {
this.citizens = citizens;
}
*/
public void onRequestFailure(){
Bundle bundle = new Bundle();
if(dg_connection_failure == null){
dg_connection_failure = new ConnectionFailureDialog();
}
bundle.putString(ConnectionErrorDialog.DIALOG_MESSAGE, getString(R.string.dialog_term_error));
dg_connection_failure.setArguments(bundle);
dg_connection_failure.show(getFragmentManager(), ConnectionErrorDialog.DIALOG_CONNECTION_ERROR);
}
@Override
protected void onResume() {
super.onResume();
presenter.bindView(this);
if(dependents == null)
presenter.requestDependents();
}
@Override
protected void onPause() {
super.onPause();
presenter.unbindView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
PresenterManager.getInstance().savePresenter(presenter, outState);
super.onSaveInstanceState(outState);
}
public void showLayout(boolean enabled) {
if(enabled){
rv_dependents.setAdapter(new CitizensArrayAdapter(this, dependents));
rv_dependents.setVisibility(View.VISIBLE);
}
}
public void setProgressBar(boolean enabled) {
if (enabled) {
pb_dependents.setVisibility(View.VISIBLE);
rv_dependents.setVisibility(View.INVISIBLE);
} else {
pb_dependents.setVisibility(View.INVISIBLE);
}
}
public static class ConnectionFailureDialog extends DialogFragment {
public static final String DIALOG_MESSAGE_KEY = "message";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getArguments().getString(DIALOG_MESSAGE_KEY))
.setTitle(getString(R.string.warningDialog_title))
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Activity activity = getActivity();
activity.onBackPressed();
}
});
return builder.create();
}
}
}
......@@ -227,7 +227,7 @@ public class HomeActivity extends AppCompatActivity implements HomeView,
startActivity(intent);
break;
case R.id.nav_dependants:
intent = new Intent(this, DependentsActivity.class);
intent = new Intent(this, DependentsListActivity.class);
startActivity(intent);
break;
default:
......
......@@ -21,15 +21,15 @@ import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionErrorDialog;
import br.ufpr.c3sl.agendador.agendador.helpers.DateValidator;
import br.ufpr.c3sl.agendador.agendador.helpers.Mask;
import br.ufpr.c3sl.agendador.agendador.presenters.PresenterManager;
import br.ufpr.c3sl.agendador.agendador.presenters.SignupPresenter;
import br.ufpr.c3sl.agendador.agendador.presenters.SignUpPresenter;
import br.ufpr.c3sl.agendador.agendador.views.SignupView;
/**
* Created by Horstmann on 13/02/17.
*/
public class SignupActivity extends AppCompatActivity implements SignupView {
private SignupPresenter presenter;
public class SignUpActivity extends AppCompatActivity implements SignupView {
private SignUpPresenter presenter;
private EditText et_name, et_cpf, et_birthday, et_cep,
et_address, et_neighborhood, et_city, et_state,
......@@ -46,11 +46,11 @@ public class SignupActivity extends AppCompatActivity implements SignupView {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
presenter = new SignupPresenter(this);
presenter = new SignUpPresenter(this);
} else {
presenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
if(presenter == null)
presenter = new SignupPresenter(this);
presenter = new SignUpPresenter(this);
}
setContentView(R.layout.activity_signup);
......
......@@ -33,7 +33,6 @@ import retrofit2.Response;
public class AccountPresenter extends BasePresenter<AccountView> {
private ObscuredSharedPreferences osb;
private Context context;
private AccountPresenter accountPresenter;
private boolean isPicUpdate;
......@@ -41,9 +40,9 @@ public class AccountPresenter extends BasePresenter<AccountView> {
public AccountPresenter(Context context) {
this.context = context;
accountPresenter = this;
isPicUpdate = false;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
this.accountPresenter = this;
this.isPicUpdate = false;
this.osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
private void onSuccessfulCheck(AccountOutput accountOutput, Headers headers){
......@@ -116,18 +115,6 @@ public class AccountPresenter extends BasePresenter<AccountView> {
}
}
private void updateToken(Headers headers) {
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();
String token = headers.get("access-token");
if(token != null)
osb.edit().putString("access-token", token).apply();
if(headers.get("access-token") != null)
osb.edit().putString("access-token", headers.get("access-token")).apply();
if(headers.get("client") != null)
osb.edit().putString("client", headers.get("client")).apply();
}
@Override
protected void updateView() {}
......
......@@ -4,6 +4,9 @@ import android.support.annotation.NonNull;
import java.lang.ref.WeakReference;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import okhttp3.Headers;
/**
* Created by Bruno Freitas Tissei on 2/2/17.
*/
......@@ -11,6 +14,8 @@ import java.lang.ref.WeakReference;
public abstract class BasePresenter<V> {
private WeakReference<V> view;
protected ObscuredSharedPreferences osb;
protected void resetState() {
}
......@@ -38,4 +43,12 @@ public abstract class BasePresenter<V> {
protected boolean setupDone() {
return view() != null;
}
protected void updateToken(Headers headers) {
if(headers.get("access-token") != null)
osb.edit().putString("access-token", headers.get("access-token")).apply();
if(headers.get("client") != null)
osb.edit().putString("client", headers.get("client")).apply();
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ import br.ufpr.c3sl.agendador.agendador.views.CepNotFoundView;
*/
public class CepNotFoundPresenter extends BasePresenter<CepNotFoundView> {
Context context;
public CepNotFoundPresenter(Context context) {
......
......@@ -12,7 +12,7 @@ import java.util.HashMap;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.CepNotFoundActivity;
import br.ufpr.c3sl.agendador.agendador.SignupActivity;
import br.ufpr.c3sl.agendador.agendador.SignUpActivity;
import br.ufpr.c3sl.agendador.agendador.models.Address;
import br.ufpr.c3sl.agendador.agendador.models.CepInput;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
......@@ -27,6 +27,7 @@ import retrofit2.Response;
*/
public class CepPresenter extends BasePresenter<CepView> {
private Context context;
private CepPresenter cepPresenter;
......@@ -63,7 +64,7 @@ public class CepPresenter extends BasePresenter<CepView> {
switch (status) {
case 200:
intent = new Intent(context, SignupActivity.class);
intent = new Intent(context, SignUpActivity.class);
intent.putExtra("zipcode_su", address.getZipcode());
intent.putExtra("address_su", address.getAddress());
intent.putExtra("neighborhood_su", address.getNeighborhood());
......
......@@ -23,17 +23,12 @@ import retrofit2.Response;
public class CitizensPresenter extends BasePresenter<CitizenActivity> {
private Context context;
private CitizensPresenter citizensPresenter;
private ObscuredSharedPreferences osb;
public CitizensPresenter(Context context){
this.context = context;
citizensPresenter = this;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
this.osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
@Override
......@@ -41,15 +36,6 @@ public class CitizensPresenter extends BasePresenter<CitizenActivity> {
}
private void updateToken(Headers headers) {
if(headers.get("access-token") != null)
osb.edit().putString("access-token", headers.get("access-token")).apply();
if(headers.get("client") != null)
osb.edit().putString("client", headers.get("client")).apply();
}
public void requestCitizens(){
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
......
package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import java.util.HashMap;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.DependentsListActivity;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import okhttp3.Headers;
/**
* Created by Lucas B. Cunha on 08/08/17.
*/
public class DependentsListPresenter extends BasePresenter<DependentsListActivity> {
private DependentsListPresenter presenter;
public DependentsListPresenter(Context context){
presenter = this;
this.osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
public void requestDependents(){
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));
final ApiEndpoints service = ApiUtils.request(header);
//Call<List<UserOutput>> listCall = service.requestDependents();
presenter.view().setProgressBar(true);
/*listCall.enqueue(new Callback<List<UserOutput>>() {
@Override
public void onResponse(Call<List<UserOutput>> call, Response<List<UserOutput>> response) {
Headers headers = response.headers();
int status = response.code();
List<UserOutput> citizens = response.body();
switch (status) {
case 200:
Log.d("Server response", getClass().getName() + ": 200 - Sucesso!");
updateToken(headers);
//has to be done before showing layout
presenter.view().successfulCitizens(citizens);
presenter.view().showLayout(true);
presenter.view().setProgressBar(false);
break;
default:
Log.e("Server response", getClass().getName() + ": ERRO:" + status);
presenter.view().onRequestFailure();
break;
}
}
@Override
public void onFailure(Call<List<UserOutput>> call, Throwable t) {
Log.e("Server response", getClass().getName() + ": Requisição falhou!!");
presenter.view().setProgressBar(false);
presenter.view().onRequestFailure();
}
});*/
}
@Override
protected void updateView() {
}
}
package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.util.Log;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.DependentsActivity;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.models.UserOutput;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import okhttp3.Headers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Lucas B. Cunha on 08/08/17.
* Created by Lucas Braz Cunha on 11/08/17.
*/
public class DependentsPresenter extends BasePresenter<DependentsActivity> {
private Context context;
private ObscuredSharedPreferences osb;
public class DependentsPresenter extends BasePresenter<DependentsActivity>{
private DependentsPresenter presenter;
public DependentsPresenter(Context context){
this.context = context;
presenter = this;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
private void updateToken(Headers headers) {
if(headers.get("access-token") != null)
osb.edit().putString("access-token", headers.get("access-token")).apply();
if(headers.get("client") != null)
osb.edit().putString("client", headers.get("client")).apply();
this.osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
}
public void requestDependents(){
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put("access-token", osb.getString("access-token", null));