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

AGILE#256: [WIP] Create notification on schedule

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 58306e25
......@@ -47,6 +47,15 @@
</intent-filter>
</service>
<service
android:name=".services.NotificationCreateService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<activity android:name=".TermActivity" />
<activity android:name=".SchedulingActivity"/>
<activity android:name=".CitizenListActivity" />
......@@ -57,7 +66,7 @@
<activity android:name=".RetrievalFailureActivity" />
<activity android:name=".RetrievalSuccessActivity" />
<activity android:name=".SchedulesHistoryActivity" />
<activity android:name=".ScheduleInfoActivity"></activity>
<activity android:name=".ScheduleInfoActivity" />
</application>
</manifest>
\ No newline at end of file
......@@ -6,6 +6,7 @@ import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
......@@ -19,22 +20,30 @@ import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionErrorDialog;
import br.ufpr.c3sl.agendador.agendador.helpers.DateValidator;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.UserImgHelper;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.models.CheckSchedules;
import br.ufpr.c3sl.agendador.agendador.models.CitizenCompact;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
import br.ufpr.c3sl.agendador.agendador.models.UserOutput;
import br.ufpr.c3sl.agendador.agendador.presenters.PresenterManager;
import br.ufpr.c3sl.agendador.agendador.presenters.ScheduleConfirmationPresenter;
import br.ufpr.c3sl.agendador.agendador.views.ScheduleConfirmationView;
......@@ -58,22 +67,27 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
private ProgressBar pb_scheduleConfirmation;
private EditText et_email;
private TextView tv_email_warning;
private Button bt_schedule, bt_back;
private String[] notificationTime;
private static String SPINNER_POS = "spinner_position";
private Spinner sp_notification;
private static final String EXIT_CONFIRMATION_DIALOG_TAG = "exit_dialog";
private RadioButton rb_yes, rb_no;
private long id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
scheduleConfirmationPresenter = new ScheduleConfirmationPresenter(getBaseContext());
}else{
scheduleConfirmationPresenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
if(scheduleConfirmationPresenter == null)
scheduleConfirmationPresenter = new ScheduleConfirmationPresenter(this);
}
setContentView(R.layout.activity_schedule_confirmation);
Bundle bundle = getIntent().getBundleExtra(Utils.SCHEDULE_BUNDLE);
......@@ -146,6 +160,15 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
navigationView.setNavigationItemSelectedListener(this);
notificationTime = getResources().getStringArray(R.array.notification_time);
sp_notification = (Spinner) findViewById(R.id.spnr_schedule_time);
sp_notification.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, notificationTime));
sp_notification.setSelection(1, true);
pb_scheduleConfirmation = (ProgressBar) findViewById(R.id.pb_schedule_confirmation);
TextView tv_citizen = (TextView) findViewById(R.id.tv_schedule_citizen_content);
......@@ -154,7 +177,8 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
TextView tv_type = (TextView) findViewById(R.id.tv_schedule_type_content);
TextView tv_schedule = (TextView) findViewById(R.id.tv_schedule_date_content);
TextView tv_street = (TextView) findViewById(R.id.tv_schedule_street_content);
et_email = (EditText) findViewById(R.id.et_schedule_email);
tv_email_warning = (TextView) findViewById(R.id.tv_schedule_email_warning);
tv_sector.setText(sectorName);
tv_citizen.setText(citizen.getName());
......@@ -172,9 +196,58 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
bt_schedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scheduleConfirmationPresenter.onScheduleClicked(scheduleConfirmation, citizen);
bt_schedule.setEnabled(false);
bt_back.setEnabled(false);
boolean schedule = true;
if(rb_yes.isChecked()){
if(!DateValidator.isValidEmail(et_email.getText().toString())){
Toast.makeText(getBaseContext(), getResources().getString(R.string.not_valid_email), Toast.LENGTH_SHORT).show();
schedule = false;
tv_email_warning.setVisibility(View.VISIBLE);
}else{
tv_email_warning.setVisibility(View.INVISIBLE);
}
}
if(schedule){
scheduleConfirmationPresenter.onScheduleClicked(scheduleConfirmation, citizen);
bt_schedule.setEnabled(false);
bt_back.setEnabled(false);
}
}
});
final TextView tv_email = (TextView) findViewById(R.id.tv_schedule_email);
rb_yes = (RadioButton) findViewById(R.id.rb_notify_email_yes);
rb_yes.setChecked(true);
rb_no = (RadioButton) findViewById(R.id.rb_notify_email_no);
rb_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rb_no.setChecked(false);
et_email.setEnabled(true);
tv_email.setTextColor(Color.BLACK);
if(sp_notification.getSelectedItemPosition() == 0){
sp_notification.setSelection(1, true);
}
}
});
rb_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rb_yes.setChecked(false);
et_email.setEnabled(false);
et_email.setText("");
tv_email.setTextColor(Color.GRAY);
tv_email_warning.setVisibility(View.INVISIBLE);
}
});
......@@ -184,6 +257,15 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
onBackPressed();
}
});
if(savedInstanceState == null){
scheduleConfirmationPresenter = new ScheduleConfirmationPresenter(getBaseContext());
}else{
sp_notification.setSelection( savedInstanceState.getInt(SPINNER_POS, 1), true);
scheduleConfirmationPresenter = PresenterManager.getInstance().restorePresenter(savedInstanceState);
if(scheduleConfirmationPresenter == null)
scheduleConfirmationPresenter = new ScheduleConfirmationPresenter(this);
}
}
@Override
......@@ -202,6 +284,7 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
@Override
public void onSaveInstanceState(Bundle outState) {
PresenterManager.getInstance().savePresenter(scheduleConfirmationPresenter, outState);
outState.putInt(SPINNER_POS, sp_notification.getSelectedItemPosition());
super.onSaveInstanceState(outState);
}
......@@ -279,7 +362,36 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
}
}
public void onSuccess(){
@Override
public void onSuccess(ScheduleConfirmation schedule){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ", Locale.getDefault());
/*
* Position zero is "don't notify"
*/
int reminderTimeArray[] = getResources().getIntArray(R.array.notification_time_int);
if(sp_notification.getSelectedItemPosition() != 0 ){
Notification notification = new Notification();
notification.scheduleId = schedule.getId();
notification.emailSent = false;
notification.description = "a";
//reminderTime*1000 to transform milliseconds to minutes.
notification.reminderTime = sdf.format(schedule.getStartTime().getTime() - (reminderTimeArray[sp_notification.getSelectedItemPosition()]*1000));
if(rb_yes.isChecked()){
notification.emailAddres = et_email.getText().toString();
}
scheduleConfirmationPresenter.createNotification(notification);
}else{
returnHome();
}
}
@Override
public void returnHome(){
Intent intent = new Intent(ScheduleConfirmationActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
......
......@@ -39,7 +39,7 @@ import br.ufpr.c3sl.agendador.agendador.helpers.UserImgHelper;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.helpers.adapters.HintAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.adapters.SchedulesExpandableListAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.schedulesHistory.FilterListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.SpinnerListener;
import br.ufpr.c3sl.agendador.agendador.models.CheckSchedules;
import br.ufpr.c3sl.agendador.agendador.models.FormFilters;
import br.ufpr.c3sl.agendador.agendador.models.ServicePlace;
......@@ -47,10 +47,10 @@ import br.ufpr.c3sl.agendador.agendador.models.ServiceType;
import br.ufpr.c3sl.agendador.agendador.presenters.PresenterManager;
import br.ufpr.c3sl.agendador.agendador.presenters.SchedulesHistoryPresenter;
import br.ufpr.c3sl.agendador.agendador.views.SchedulesHistoryView;
import br.ufpr.c3sl.agendador.agendador.views.SpinnerActivity;
public class SchedulesHistoryActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SchedulesHistoryView {
public class SchedulesHistoryActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SchedulesHistoryView, SpinnerActivity {
private NavigationView navigationView;
private MenuBuilder menuBuilder;
......@@ -65,7 +65,6 @@ public class SchedulesHistoryActivity extends AppCompatActivity implements Navig
private FormFilters filters;
private long[] sectorsIdArray, serviceTypesIdArray, servicePlacesIdArray, situationIdArray;
private Button bt_search, bt_clear;
private FilterListener filterListener;
private static final String EXIT_CONFIRMATION_DIALOG_TAG = "exit_dialog";
private static final String SPINNER_SECTOR = "spinner_setor";
private static final String SPINNER_TYPE = "spinner_type";
......@@ -242,7 +241,7 @@ public class SchedulesHistoryActivity extends AppCompatActivity implements Navig
}
});
filterListener = new FilterListener(this);
SpinnerListener filterListener = new SpinnerListener(this);
sp_sector.setOnItemSelectedListener(filterListener);
sp_sector.setOnTouchListener(filterListener);
......
......@@ -49,9 +49,9 @@ import br.ufpr.c3sl.agendador.agendador.helpers.adapters.HintAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.adapters.HintEnableAdapter;
import br.ufpr.c3sl.agendador.agendador.helpers.decorators.NotAvailableDecorator;
import br.ufpr.c3sl.agendador.agendador.helpers.decorators.SchedulesDecorator;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.SectorSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.ServicePlaceSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.ServiceTypeSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling.SectorSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling.ServicePlaceSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling.ServiceTypeSpinnerListener;
import br.ufpr.c3sl.agendador.agendador.models.CitizenCompact;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
import br.ufpr.c3sl.agendador.agendador.models.SectorInput;
......
......@@ -108,6 +108,8 @@ public abstract class Utils {
public static final String REQUEST_FILTER_PLACE = "q[service_place_id]";
public static final String REQUEST_FILTER_SITUATION = "q[situation_id]";
public static final String SIZE_LARGE = "large";
public static final String NOTIFICATION = "notification";
public static final String NOTIFICATION_SERVICE = "br.c3sl.ufpr.notification_service";
public static int getPixelValue(int dp, Context context) {
Resources resources = context.getResources();
......@@ -170,9 +172,9 @@ public abstract class Utils {
String s = calendar.get(Calendar.YEAR) + "";
tv_calendarHeaderYear.setText(s);
tv_calendarHeaderDayHour.setText(days_short[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " +
months_short[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DAY_OF_MONTH));
String text = days_short[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " +
months_short[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DAY_OF_MONTH);
tv_calendarHeaderDayHour.setText(text);
}
......
package br.ufpr.c3sl.agendador.agendador.helpers.listeners.schedulesHistory;
package br.ufpr.c3sl.agendador.agendador.helpers.listeners;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import br.ufpr.c3sl.agendador.agendador.views.SchedulesHistoryView;
import br.ufpr.c3sl.agendador.agendador.views.SpinnerActivity;
/**
* Created by Lucas B. Cunha on 30/10/17.
* Created by Lucas Braz Cunha on 22/11/17.
*/
public class FilterListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
public class SpinnerListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
private boolean userSelect;
private SchedulesHistoryView activity;
private SpinnerActivity activity;
public FilterListener(SchedulesHistoryView activity) {
public SpinnerListener(SpinnerActivity activity) {
this.activity = activity;
this.userSelect = false;
}
......@@ -23,6 +23,7 @@ public class FilterListener implements AdapterView.OnItemSelectedListener, View.
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
v.performClick();
return false;
}
......
package br.ufpr.c3sl.agendador.agendador.helpers.listeners;
package br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling;
import android.view.MotionEvent;
import android.view.View;
......@@ -53,6 +53,7 @@ public class SectorSpinnerListener implements AdapterView.OnItemSelectedListener
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
v.performClick();
return false;
}
......
package br.ufpr.c3sl.agendador.agendador.helpers.listeners;
package br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling;
import android.view.MotionEvent;
import android.view.View;
......@@ -48,12 +48,13 @@ public class ServicePlaceSpinnerListener implements AdapterView.OnItemSelectedLi
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
v.performClick();
return false;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect && servicePlaces != null && servicePlaces.size() > 0 ) {
if (userSelect && servicePlaces != null && servicePlaces.size() > 0) {
mcv.setSelectionMode(MaterialCalendarView.SELECTION_MODE_SINGLE);
//if the position is the hint.
......
package br.ufpr.c3sl.agendador.agendador.helpers.listeners;
package br.ufpr.c3sl.agendador.agendador.helpers.listeners.scheduling;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
......@@ -53,12 +52,13 @@ public class ServiceTypeSpinnerListener implements AdapterView.OnItemSelectedLis
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
v.performClick();
return false;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect && serviceTypes != null && serviceTypes.size() > 0 ) {
if (userSelect && serviceTypes != null && serviceTypes.size() > 0) {
presenter.requestAvailableSchedules(serviceTypes.get(pos).getId());
Utils.updateCalendarHeaderDate(mcv, tv_calendarHeaderYear, tv_calendarHeaderDayHour, days_short, months_short, null);
......
package br.ufpr.c3sl.agendador.agendador.models;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
/**
* Created by Lucas Braz Cunha on 24/11/17.
*/
public class Notification implements Parcelable{
@SerializedName("schedule_id")
public long scheduleId;
@SerializedName("reminder_time")
public String reminderTime;
@SerializedName("read")
public boolean hasRead;
@SerializedName("content")
public String description;
@SerializedName("reminder_email")
public String emailAddres;
@SerializedName("email_sent")
public boolean emailSent;
public Notification() {
}
/**reads back fields IN THE ORDER they were written */
private Notification(Parcel pc){
scheduleId = pc.readLong();
reminderTime = pc.readString();
hasRead = pc.readByte() != 0;
description = pc.readString();
emailAddres = pc.readString();
emailSent = pc.readByte() != 0;
}
/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<Notification> CREATOR = new Parcelable.Creator<Notification>() {
public Notification createFromParcel(Parcel pc) {
return new Notification(pc);
}
public Notification[] newArray(int size) {
return new Notification[size];
}
};
/**
* Flatten this object in to a Parcel.
*
* @param dest The Parcel in which the object should be written.
* @param flags Additional flags about how the object should be written.
* May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(scheduleId);
dest.writeString(reminderTime);
dest.writeByte((byte) (hasRead ? 1 : 0));
dest.writeString(description);
dest.writeString(emailAddres);
dest.writeByte((byte) (emailSent ? 1 : 0));
}
@Override
public int describeContents() {
return 0;
}
}
......@@ -14,6 +14,7 @@ import br.ufpr.c3sl.agendador.agendador.models.Dependent;
import br.ufpr.c3sl.agendador.agendador.models.DependentCreation;
import br.ufpr.c3sl.agendador.agendador.models.FormFilters;
import br.ufpr.c3sl.agendador.agendador.models.FullDependent;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleNote;
import br.ufpr.c3sl.agendador.agendador.models.SectorInput;
......@@ -87,9 +88,11 @@ public interface ApiEndpoints {
@GET("schedules?permission=citizen")
Call<CheckSchedules> requestSchedulesHistory(@QueryMap Map<String, String> options);
@GET("forms/schedule_history?permission=citizen")
Call<FormFilters> requestCitizenSectors();
@POST("notifications?permission=2")
Call<ScheduleConfirmation> requestCreateNotification(@Body Notification notification);
}
\ 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/develop/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/develop/v1/";
public static ApiEndpoints request(final Map<String, String> header) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
......
......@@ -2,10 +2,17 @@ package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
......@@ -15,11 +22,14 @@ import br.ufpr.c3sl.agendador.agendador.ScheduleConfirmationActivity;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.models.CitizenCompact;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleNote;
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 br.ufpr.c3sl.agendador.agendador.services.ImageUpdateService;
import br.ufpr.c3sl.agendador.agendador.services.NotificationCreateService;
import okhttp3.Headers;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -33,10 +43,16 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
private ScheduleConfirmationPresenter scheduleConfirmationPresenter;
private FirebaseJobDispatcher dispatcher;
public ScheduleConfirmationPresenter(Context context){
this.context = context;
this.scheduleConfirmationPresenter = this;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
dispatcher = new FirebaseJobDispatcher(
new GooglePlayDriver(context)
);
}
......@@ -60,14 +76,14 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
public void onResponse(Call<ScheduleConfirmation> call, Response<ScheduleConfirmation> response) {
Headers headers = response.headers();
int status = response.code();
//ScheduleConfirmation confirmationResponse = response.body();
ScheduleConfirmation confirmationResponse = response.body();
updateHeaders(headers);
switch (status) {
case 200:
Log.d("Server response", this.getClass().getName() + ": 200 - Sucesso!");
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().onSuccess(confirmationResponse);
scheduleConfirmationPresenter.view().onSuccess();
break;
default:
Log.e("Server response", this.getClass().getName() + ": ERRO:" + status);
......@@ -87,6 +103,86 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
}
public void createNotification(final Notification notification){
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put(Utils.ACCESS_TOKEN, osb.getString(Utils.ACCESS_TOKEN, null));
header.put(Utils.CLIENT, osb.getString(Utils.CLIENT, null));
header.put(Utils.UID, osb.getString(Utils.UID, null));
final ApiEndpoints service = ApiUtils.request(header);
// TODO: 24/11/17 arrumar retorno
Call<ScheduleConfirmation> listCall = service.requestCreateNotification(notification);
scheduleConfirmationPresenter.view().setProgressBar(true);
listCall.enqueue(new Callback<ScheduleConfirmation>() {
@Override
public void onResponse(Call<ScheduleConfirmation> call, Response<ScheduleConfirmation> response) {
Headers headers = response.headers();
int status = response.code();
//ScheduleConfirmation confirmationResponse = response.body();
updateHeaders(headers);
switch (status) {
case 200:
Log.d("Server response", this.getClass().getName() + ": 200 - Sucess!");
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
break;
default:
Log.e("Server response", this.getClass().getName() + ": ERRO:" + status + ". Scheduling job");
scheduleNotificationJob(notification);
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
break;
}
}
@Override
public void onFailure(Call<ScheduleConfirmation> call, Throwable t) {
Log.e("Server response", this.getClass().getName() + ": Request Failed, Scheduling job.");
scheduleNotificationJob(notification);
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
}
});
}
private void scheduleNotificationJob(Notification notification){
Bundle extras = new Bundle();
Gson g = new Gson();
String json = g.toJson(notification);
extras.putString(Utils.NOTIFICATION, json);
Job job = dispatcher.newJobBuilder()
.setService(NotificationCreateService.class)
.setTag(Utils.NOTIFICATION_SERVICE)
//time the function gets is in seconds, 60s = 1 minutes, 1 hour = 3600
.setTrigger(Trigger.executionWindow(0, 3600))
//will be retried if it fails.
.setRecurring(true)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)